-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initialization for the genesis base asset contract (#2304)
This match initializes storage slots for the genesis base asset contract(because we forgot to add it). In the next release we will remove it form our codebase=) Side change: - Moves `ChangesIterator` to the `fuel-core-storage` crate to allow usage in the tests. ## Checklist - [x] New behavior is reflected in tests ### Before requesting review - [x] I have reviewed the code myself
- Loading branch information
Showing
9 changed files
with
261 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use fuel_core_storage::{ | ||
column::Column, | ||
kv_store::KeyValueInspect, | ||
tables::ContractsState, | ||
transactional::StorageTransaction, | ||
ContractsStateKey, | ||
StorageAsMut, | ||
}; | ||
use fuel_core_types::{ | ||
fuel_tx::Input, | ||
fuel_types::{ | ||
Bytes32, | ||
ContractId, | ||
}, | ||
services::executor::Result as ExecutorResult, | ||
}; | ||
|
||
struct Slot { | ||
key: &'static str, | ||
value: &'static str, | ||
} | ||
|
||
const CONTRACT_ID_WITH_BAD_STATE: &str = | ||
"0x7e2becd64cd598da59b4d1064b711661898656c6b1f4918a787156b8965dc83c"; | ||
|
||
const STATE: [Slot; 4] = [ | ||
Slot { | ||
key: "35fa5b7532d53cf687e13e3db014eaf208c5b8c534ab693dd7090d5e02675f3e", | ||
value: "0000000000000000000000000000000000000000000000000000000000000000", | ||
}, | ||
Slot { | ||
key: "35fa5b7532d53cf687e13e3db014eaf208c5b8c534ab693dd7090d5e02675f3f", | ||
value: "0000000000000000000000000000000000000000000000000000000000000000", | ||
}, | ||
Slot { | ||
key: "7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd55", | ||
value: "0000000000000000000000000000000000000000000000000000000000000000", | ||
}, | ||
Slot { | ||
key: "7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd56", | ||
value: "0000000000000000000000000000000000000000000000000000000000000000", | ||
}, | ||
]; | ||
|
||
pub fn maybe_fix_storage<S>( | ||
inputs: &[Input], | ||
storage: &mut StorageTransaction<S>, | ||
) -> ExecutorResult<()> | ||
where | ||
S: KeyValueInspect<Column = Column>, | ||
{ | ||
for input in inputs { | ||
if let Input::Contract(contract) = input { | ||
maybe_fix_contract(&contract.contract_id, storage)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn maybe_fix_contract<S>( | ||
contract_id: &ContractId, | ||
storage: &mut StorageTransaction<S>, | ||
) -> ExecutorResult<()> | ||
where | ||
S: KeyValueInspect<Column = Column>, | ||
{ | ||
let bad_contract_id: ContractId = CONTRACT_ID_WITH_BAD_STATE.parse().unwrap(); | ||
|
||
if contract_id == &bad_contract_id { | ||
for slot in STATE.iter() { | ||
let storage_key: Bytes32 = slot.key.parse().unwrap(); | ||
|
||
let key = ContractsStateKey::new(contract_id, &storage_key); | ||
let contains = storage | ||
.storage_as_mut::<ContractsState>() | ||
.contains_key(&key)?; | ||
|
||
if contains { | ||
continue; | ||
} | ||
|
||
let value: Bytes32 = slot.value.parse().unwrap(); | ||
|
||
storage | ||
.storage_as_mut::<ContractsState>() | ||
.insert(&key, value.as_ref())?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use fuel_core_storage::{ | ||
iter::{ | ||
changes_iterator::ChangesIterator, | ||
IteratorOverTable, | ||
}, | ||
structured_storage::test::InMemoryStorage, | ||
transactional::IntoTransaction, | ||
}; | ||
|
||
#[test] | ||
fn dummy() { | ||
// Given | ||
let contract_id: ContractId = | ||
"0x7e2becd64cd598da59b4d1064b711661898656c6b1f4918a787156b8965dc83c" | ||
.parse() | ||
.unwrap(); | ||
let input = Input::contract( | ||
Default::default(), | ||
Default::default(), | ||
Default::default(), | ||
Default::default(), | ||
contract_id, | ||
); | ||
let mut storage = InMemoryStorage::default().into_transaction(); | ||
|
||
// When | ||
maybe_fix_storage(&[input], &mut storage).unwrap(); | ||
|
||
// Then | ||
let changes = storage.into_changes(); | ||
let view = ChangesIterator::new(&changes); | ||
let entries = view | ||
.iter_all::<ContractsState>(None) | ||
.map(|r| r.unwrap()) | ||
.collect::<Vec<_>>(); | ||
assert_eq!(entries.len(), STATE.len()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
extern crate alloc; | ||
|
||
pub mod executor; | ||
mod hacks; | ||
pub mod ports; | ||
pub mod refs; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.