Skip to content

Commit

Permalink
Moved ContractsInfo table to the off-chain database (#1768)
Browse files Browse the repository at this point in the history
Closes #1721

The change moves the `ContractsInfo` table to the off-chain database.
The `ContractConfig` type doesn't have a `salt` field anymore because
this information can be retrieved from the transactions.
  • Loading branch information
xgreenx committed Mar 19, 2024
1 parent 837ca27 commit 8018aa0
Show file tree
Hide file tree
Showing 39 changed files with 239 additions and 199 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Description of the upcoming release here.
### Changed

#### Breaking
- [#1768](https://github.com/FuelLabs/fuel-core/pull/1768): Moved `ContractsInfo` table to the off-chain database. Removed `salt` field from the `ContractConfig`.
- [#1761](https://github.com/FuelLabs/fuel-core/pull/1761): Adjustments to the upcoming testnet configs:
- Decreased the max size of the contract/predicate/script to be 100KB.
- Decreased the max size of the transaction to be 110KB.
Expand Down
3 changes: 2 additions & 1 deletion bin/e2e-test-client/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use fuel_core_types::{
canonical::Serialize,
Address,
AssetId,
Salt,
},
fuel_vm::SecretKey,
};
Expand Down Expand Up @@ -300,6 +301,7 @@ impl Wallet {
pub async fn deploy_contract(
&self,
config: ContractConfig,
salt: Salt,
slots: Vec<StorageSlot>,
) -> anyhow::Result<()> {
let asset_id = AssetId::BASE;
Expand All @@ -313,7 +315,6 @@ impl Wallet {
let ContractConfig {
contract_id,
code: bytes,
salt,
..
} = config;
let state_root = Contract::initial_state_root(slots.iter());
Expand Down
14 changes: 5 additions & 9 deletions bin/e2e-test-client/src/tests/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use crate::test_context::TestContext;
use fuel_core_chain_config::ContractConfig;
use fuel_core_types::fuel_crypto::rand::{
rngs::StdRng,
Rng,
SeedableRng,
};
use libtest_mimic::Failed;
use std::time::Duration;
use tokio::time::timeout;
Expand All @@ -19,17 +14,18 @@ pub async fn deploy_large_contract(ctx: &TestContext) -> Result<(), Failed> {
} else {
vec![0u8; 1024 * 1024]
};
let mut rng = StdRng::seed_from_u64(2222);
let mut contract_config = ContractConfig {
contract_id: Default::default(),
code: bytecode,
salt: rng.gen(),
..Default::default()
};
let salt = Default::default();
let storage_slots = vec![];
contract_config.update_contract_id(&storage_slots);
contract_config.update_contract_id(salt, &storage_slots);

let deployment_request = ctx.bob.deploy_contract(contract_config, storage_slots);
let deployment_request =
ctx.bob
.deploy_contract(contract_config, salt, storage_slots);

// wait for contract to deploy in 5 minutes, because 16mb takes a lot of time.
timeout(Duration::from_secs(300), deployment_request).await??;
Expand Down
20 changes: 13 additions & 7 deletions bin/e2e-test-client/src/tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use fuel_core_types::{
Transaction,
UniqueIdentifier,
},
fuel_types::canonical::Deserialize,
fuel_types::{
canonical::Deserialize,
Salt,
},
services::executor::TransactionExecutionResult,
};
use itertools::Itertools;
Expand Down Expand Up @@ -101,6 +104,7 @@ pub async fn dry_run_multiple_txs(ctx: &TestContext) -> Result<(), Failed> {
}

fn load_contract(
salt: Salt,
path: impl AsRef<Path>,
) -> Result<(ContractConfig, Vec<StorageSlot>), Failed> {
let snapshot = SnapshotMetadata::read(path)?;
Expand Down Expand Up @@ -129,7 +133,7 @@ fn load_contract(
}
let mut contract_config = contracts[0].clone();

contract_config.update_contract_id(&state);
contract_config.update_contract_id(salt, &state);

contract_config
};
Expand All @@ -139,7 +143,11 @@ fn load_contract(

// Maybe deploy a contract with large state and execute the script
pub async fn run_contract_large_state(ctx: &TestContext) -> Result<(), Failed> {
let (contract_config, state) = load_contract("./src/tests/test_data/large_state")?;
let salt: Salt = "0x3b91bab936e4f3db9453046b34c142514e78b64374bf61a04ab45afbd6bca83e"
.parse()
.expect("Should be able to parse the salt");
let (contract_config, state) =
load_contract(salt, "./src/tests/test_data/large_state")?;
let dry_run = include_bytes!("test_data/large_state/tx.json");
let dry_run: Transaction = serde_json::from_slice(dry_run.as_ref())
.expect("Should be able do decode the Transaction");
Expand All @@ -154,11 +162,9 @@ pub async fn run_contract_large_state(ctx: &TestContext) -> Result<(), Failed> {
// if the contract is not deployed yet, let's deploy it
let result = ctx.bob.client.contract(&contract_id).await;
if result?.is_none() {
let deployment_request = ctx.bob.deploy_contract(contract_config, state);
let deployment_request = ctx.bob.deploy_contract(contract_config, salt, state);

// wait for contract to deploy in 300 seconds because `state_root` calculation is too long.
// https://github.com/FuelLabs/fuel-core/issues/1143
timeout(Duration::from_secs(300), deployment_request).await??;
timeout(Duration::from_secs(20), deployment_request).await??;
}

_dry_runs(ctx, &[dry_run], 1000, DryRunResult::MayFail).await
Expand Down

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions bin/fuel-core/src/cli/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ mod tests {
tables::{
Coins,
ContractsAssets,
ContractsInfo,
ContractsLatestUtxo,
ContractsRawCode,
ContractsState,
Expand All @@ -288,10 +287,7 @@ mod tests {
CompressedCoin,
CompressedCoinV1,
},
contract::{
ContractUtxoInfo,
ContractsInfoType,
},
contract::ContractUtxoInfo,
message::{
Message,
MessageV1,
Expand All @@ -301,7 +297,6 @@ mod tests {
TxPointer,
UtxoId,
},
fuel_vm::Salt,
};
use rand::{
rngs::StdRng,
Expand Down Expand Up @@ -449,12 +444,6 @@ mod tests {
.insert(&contract_id, code.as_ref())
.unwrap();

let salt: Salt = self.rng.gen();
self.db
.storage_as_mut::<ContractsInfo>()
.insert(&contract_id, &ContractsInfoType::V1(salt.into()))
.unwrap();

let utxo_id = UtxoId::new(self.rng.gen(), self.rng.gen());
let tx_pointer = TxPointer::new(self.rng.gen(), self.rng.gen());

Expand All @@ -469,7 +458,6 @@ mod tests {
ContractConfig {
contract_id,
code,
salt,
tx_id: *utxo_id.tx_id(),
output_index: utxo_id.output_index(),
tx_pointer_block_height: tx_pointer.block_height(),
Expand Down
5 changes: 2 additions & 3 deletions crates/chain-config/src/config/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct ContractConfig {
pub contract_id: ContractId,
#[serde_as(as = "HexIfHumanReadable")]
pub code: Vec<u8>,
pub salt: Salt,
pub tx_id: Bytes32,
pub output_index: u8,
/// TxPointer: auto-generated if None
Expand Down Expand Up @@ -54,7 +53,6 @@ impl crate::Randomize for ContractConfig {
Self {
contract_id: ContractId::new(super::random_bytes_32(&mut rng)),
code: (super::random_bytes_32(&mut rng)).to_vec(),
salt: Salt::new(super::random_bytes_32(&mut rng)),
tx_id: super::random_bytes_32(&mut rng).into(),
output_index: rng.gen(),
tx_pointer_block_height: rng.gen(),
Expand All @@ -66,13 +64,14 @@ impl crate::Randomize for ContractConfig {
impl ContractConfig {
pub fn update_contract_id<'a>(
&mut self,
salt: Salt,
storage_slots: impl IntoIterator<Item = &'a StorageSlot>,
) {
let state_root = Contract::initial_state_root(storage_slots.into_iter());

let contract = Contract::from(self.code.clone());
let root = contract.root();
let contract_id = contract.id(&self.salt, &root, &state_root);
let contract_id = contract.id(&salt, &root, &state_root);
self.contract_id = contract_id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"balances": [
[
"0x61644a25da5ad31754e23bc1eca8c766a61ceaa8c60ddc65f158a30209256e35",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"state": [
[
"0x61644a25da5ad31754e23bc1eca8c766a61ceaa8c60ddc65f158a30209256e35",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"tx_pointer_block_height": "0xf9681a64",
"tx_pointer_tx_idx": "0xd125"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"tx_id": "0x644a25da5ad31754e23bc1eca8c766a61ceaa8c60ddc65f158a30209256e35c9",
"output_index": "0xa4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
],
"contract_state": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expression: encoded_json
{
"contract_id": "0101010101010101010101010101010101010101010101010101010101010101",
"code": "0202020202020202020202020202020202020202020202020202020202020202",
"salt": "0303030303030303030303030303030303030303030303030303030303030303",
"tx_id": "0404040404040404040404040404040404040404040404040404040404040404",
"output_index": 5,
"tx_pointer_block_height": 6,
Expand Down
1 change: 0 additions & 1 deletion crates/chain-config/src/config/state/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,6 @@ mod tests {
let contract = ContractConfig {
contract_id: [1u8; 32].into(),
code: [2u8; 32].into(),
salt: [3u8; 32].into(),
tx_id: [4u8; 32].into(),
output_index: 5,
tx_pointer_block_height: BlockHeight::new(6),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"balances": [
[
"0x61644a25da5ad31754e23bc1eca8c766a61ceaa8c60ddc65f158a30209256e35",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"state": [
[
"0x61644a25da5ad31754e23bc1eca8c766a61ceaa8c60ddc65f158a30209256e35",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"tx_pointer_block_height": "0xd3301861",
"tx_pointer_tx_idx": "0x1a64"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000",
"tx_id": "0x61644a25da5ad31754e23bc1eca8c766a61ceaa8c60ddc65f158a30209256e35",
"output_index": "0xc9"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ expression: json
{
"contract_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x24400000",
"salt": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
]
},
Expand Down
9 changes: 0 additions & 9 deletions crates/fuel-core/src/database/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use fuel_core_storage::{
not_found,
tables::{
ContractsAssets,
ContractsInfo,
ContractsLatestUtxo,
ContractsRawCode,
ContractsState,
Expand Down Expand Up @@ -77,13 +76,6 @@ impl Database {
})
.ok_or_else(|| not_found!("ContractsRawCode"))?;

let salt = *self
.storage::<ContractsInfo>()
.get(&contract_id)
.unwrap()
.expect("Contract does not exist")
.salt();

let latest_utxo = self
.storage::<ContractsLatestUtxo>()
.get(&contract_id)
Expand All @@ -96,7 +88,6 @@ impl Database {
Ok(ContractConfig {
contract_id,
code,
salt,
tx_id: *utxo_id.tx_id(),
output_index: utxo_id.output_index(),
tx_pointer_block_height: tx_pointer.block_height(),
Expand Down
5 changes: 5 additions & 0 deletions crates/fuel-core/src/graphql_api/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use fuel_core_types::{
fuel_tx::{
Address,
AssetId,
Salt,
TxPointer,
UtxoId,
},
Expand Down Expand Up @@ -221,4 +222,8 @@ impl OffChainDatabase for ReadView {
self.off_chain
.owned_transactions_ids(owner, start, direction)
}

fn contract_salt(&self, contract_id: &ContractId) -> StorageResult<Salt> {
self.off_chain.contract_salt(contract_id)
}
}
7 changes: 5 additions & 2 deletions crates/fuel-core/src/graphql_api/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use fuel_core_storage::{
tables::{
Coins,
ContractsAssets,
ContractsInfo,
ContractsRawCode,
FuelBlocks,
Messages,
Expand All @@ -33,6 +32,7 @@ use fuel_core_types::{
Message,
},
fuel_tx::{
Salt,
Transaction,
TxId,
TxPointer,
Expand Down Expand Up @@ -83,6 +83,8 @@ pub trait OffChainDatabase: Send + Sync {
start: Option<TxPointer>,
direction: IterDirection,
) -> BoxedIter<StorageResult<(TxPointer, TxId)>>;

fn contract_salt(&self, contract_id: &ContractId) -> StorageResult<Salt>;
}

/// The on chain database port expected by GraphQL API service.
Expand Down Expand Up @@ -129,7 +131,6 @@ pub trait DatabaseMessages: StorageInspect<Messages, Error = StorageError> {
/// Trait that specifies all the getters required for contract.
pub trait DatabaseContracts:
StorageInspect<ContractsRawCode, Error = StorageError>
+ StorageInspect<ContractsInfo, Error = StorageError>
+ StorageInspect<ContractsAssets, Error = StorageError>
{
fn contract_balances(
Expand Down Expand Up @@ -208,6 +209,7 @@ pub mod worker {
use super::super::storage::blocks::FuelBlockIdsToHeights;
use crate::fuel_core_graphql_api::storage::{
coins::OwnedCoins,
contracts::ContractsInfo,
messages::OwnedMessageIds,
};
use fuel_core_services::stream::BoxStream;
Expand Down Expand Up @@ -241,6 +243,7 @@ pub mod worker {
StorageMutate<OwnedMessageIds, Error = StorageError>
+ StorageMutate<OwnedCoins, Error = StorageError>
+ StorageMutate<FuelBlockIdsToHeights, Error = StorageError>
+ StorageMutate<ContractsInfo, Error = StorageError>
{
fn record_tx_id_owner(
&mut self,
Expand Down
Loading

0 comments on commit 8018aa0

Please sign in to comment.