Skip to content

Commit

Permalink
chore: versioning state
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Feb 10, 2023
1 parent a6a41ca commit e8c34ef
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 103 deletions.
7 changes: 4 additions & 3 deletions engine-standalone-storage/src/relayer_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub mod error {
mod test {
use super::FallibleIterator;
use crate::sync::types::{TransactionKind, TransactionMessage};
use aurora_engine::state::EngineStateV2;
use aurora_engine::{connector, parameters, state};
use aurora_engine_types::H256;

Expand All @@ -210,12 +211,12 @@ mod test {
fn test_fill_db() {
let mut storage = crate::Storage::open("rocks_tmp/").unwrap();
let mut connection = super::connect_without_tls(&Default::default()).unwrap();
let engine_state = state::EngineState {
let engine_state = state::EngineState::V2(EngineStateV2 {
chain_id: aurora_engine_types::types::u256_to_arr(&1313161555.into()),
owner_id: "aurora".parse().unwrap(),
upgrade_delay_blocks: 0,
default_gas_token: Default::default(),
};
});

// Initialize engine and connector states in storage.
// Use explicit scope so borrows against `storage` are dropped before processing DB rows.
Expand All @@ -234,7 +235,7 @@ mod test {
state::set_state(&mut local_io, engine_state.clone()).unwrap();
connector::EthConnectorContract::create_contract(
io,
engine_state.owner_id.clone(),
engine_state.owner_id(),
parameters::InitCallArgs {
prover_account: "prover.bridge.near".parse().unwrap(),
eth_custodian_address: "6bfad42cfc4efc96f529d786d643ff4a8b89fa52"
Expand Down
1 change: 1 addition & 0 deletions engine-tests/src/tests/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ fn test_state_format() {
};
let state: aurora_engine::state::EngineState = args.into();
let expected_hex: String = [
"00", // V2 - "00", V1 - "01",
"000000000000000000000000000000000000000000000000000000000000029a",
"04000000626f7373",
"0300000000000000",
Expand Down
5 changes: 3 additions & 2 deletions engine-tests/src/tests/standalone/sanity.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use aurora_engine::state::EngineStateV2;
use aurora_engine::{engine, state};
use aurora_engine_sdk::env::DEFAULT_PREPAID_GAS;
use aurora_engine_test_doubles::io::{Storage, StoragePointer};
Expand All @@ -15,12 +16,12 @@ fn test_deploy_code() {
buf
};
let owner_id: AccountId = "aurora".parse().unwrap();
let state = state::EngineState {
let state = state::EngineState::V2(EngineStateV2 {
chain_id,
owner_id: owner_id.clone(),
upgrade_delay_blocks: 0,
default_gas_token: Default::default(),
};
});
let origin = Address::new(H160([0u8; 20]));
let storage = RefCell::new(Storage::default());
let io = StoragePointer(&storage);
Expand Down
4 changes: 0 additions & 4 deletions engine-types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ impl<T> Stack<T> {
}
}

pub fn str_from_slice(inp: &[u8]) -> &str {
str::from_utf8(inp).unwrap()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 2 additions & 6 deletions engine/src/deposit_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,8 @@ mod tests {
let parse_error =
TokenMessageData::parse_event_message_and_prepare_token_message_data(message, fee)
.unwrap_err();
let actual_parse_error = std::str::from_utf8(parse_error.as_ref()).unwrap();
let expected_parse_error = AddressError::IncorrectLength.to_string();

assert_eq!(expected_parse_error, actual_parse_error);
assert_eq!(parse_error.as_ref(), AddressError::IncorrectLength.as_ref());
}

#[test]
Expand All @@ -541,10 +539,8 @@ mod tests {
let parse_error =
TokenMessageData::parse_event_message_and_prepare_token_message_data(message, fee)
.unwrap_err();
let actual_parse_error = std::str::from_utf8(parse_error.as_ref()).unwrap();
let expected_parse_error = AddressError::FailedDecodeHex.to_string();

assert_eq!(expected_parse_error, actual_parse_error);
assert_eq!(parse_error.as_ref(), AddressError::FailedDecodeHex.as_ref());
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ pub fn submit<I: IO + Copy, E: Env, P: PromiseHandler>(

// Validate the chain ID, if provided inside the signature:
if let Some(chain_id) = transaction.chain_id {
if U256::from(chain_id) != U256::from(state.chain_id) {
if U256::from(chain_id) != U256::from(state.chain_id()) {
return Err(EngineErrorKind::InvalidChainId.into());
}
}
Expand Down Expand Up @@ -1535,7 +1535,7 @@ impl<'env, I: IO + Copy, E: Env> Backend for Engine<'env, I, E> {
if idx.saturating_sub(U256::from(256)) <= number && number < idx {
// since `idx` comes from `u64` it is always safe to downcast `number` from `U256`
compute_block_hash(
self.state.chain_id,
self.state.chain_id(),
number.low_u64(),
self.current_account_id.as_bytes(),
)
Expand Down Expand Up @@ -1595,7 +1595,7 @@ impl<'env, I: IO + Copy, E: Env> Backend for Engine<'env, I, E> {

/// Returns the states chain ID.
fn chain_id(&self) -> U256 {
U256::from(self.state.chain_id)
U256::from(self.state.chain_id())
}

/// Checks if an address exists.
Expand Down
30 changes: 13 additions & 17 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ mod contract {
near_account_to_evm_address, SdkExpect, SdkProcess, SdkUnwrap,
};
use crate::prelude::storage::{bytes_to_key, KeyPrefix};
use crate::prelude::{sdk, u256_to_arr, Address, PromiseResult, Yocto, ERR_FAILED_PARSE, H256};
use crate::prelude::{
sdk, u256_to_arr, Address, PromiseResult, ToString, Yocto, ERR_FAILED_PARSE, H256,
};
use crate::{errors, pausables, state};
use aurora_engine_sdk::env::Env;
use aurora_engine_sdk::io::{StorageIntermediate, IO};
Expand Down Expand Up @@ -140,7 +142,7 @@ mod contract {
pub extern "C" fn get_owner() {
let mut io = Runtime;
let state = state::get_state(&io).sdk_unwrap();
io.return_output(state.owner_id.as_bytes());
io.return_output(state.owner_id().as_bytes());
}

/// Get bridge prover id for this contract.
Expand All @@ -155,15 +157,15 @@ mod contract {
#[no_mangle]
pub extern "C" fn get_chain_id() {
let mut io = Runtime;
io.return_output(&state::get_state(&io).sdk_unwrap().chain_id)
io.return_output(&state::get_state(&io).sdk_unwrap().chain_id())
}

#[no_mangle]
pub extern "C" fn get_upgrade_index() {
let mut io = Runtime;
let state = state::get_state(&io).sdk_unwrap();
let index = internal_get_upgrade_index();
io.return_output(&(index + state.upgrade_delay_blocks).to_le_bytes())
io.return_output(&(index + state.upgrade_delay_blocks()).to_le_bytes())
}

/// Stage new code for deployment.
Expand All @@ -187,7 +189,7 @@ mod contract {
let state = state::get_state(&io).sdk_unwrap();
require_owner_only(&state, &io.predecessor_account_id());
let index = internal_get_upgrade_index();
if io.block_height() <= index + state.upgrade_delay_blocks {
if io.block_height() <= index + state.upgrade_delay_blocks() {
sdk::panic_utf8(errors::ERR_NOT_ALLOWED_TOO_EARLY);
}
Runtime::self_deploy(&bytes_to_key(KeyPrefix::Config, CODE_KEY));
Expand All @@ -197,13 +199,7 @@ mod contract {
/// to make any necessary changes to the state such that it aligns with the newly deployed
/// code.
#[no_mangle]
pub extern "C" fn state_migration() {
let mut io = Runtime;
io.assert_private_call().sdk_unwrap();

// TODO: Must be removed after migration.
state::legacy::migrate_state(&mut io).sdk_unwrap();
}
pub extern "C" fn state_migration() {}

/// Resumes previously [`paused`] precompiles.
///
Expand Down Expand Up @@ -479,7 +475,7 @@ mod contract {
let block_height = io.read_input_borsh().sdk_unwrap();
let account_id = io.current_account_id();
let chain_id = state::get_state(&io)
.map(|state| state.chain_id)
.map(|state| state.chain_id())
.sdk_unwrap();
let block_hash =
crate::engine::compute_block_hash(chain_id, block_height, account_id.as_bytes());
Expand Down Expand Up @@ -531,7 +527,7 @@ mod contract {
let mut state = state::get_state(&io).sdk_unwrap();
require_owner_only(&state, &io.predecessor_account_id());
let args: BeginChainArgs = io.read_input_borsh().sdk_unwrap();
state.chain_id = args.chain_id;
state.set_chain_id(args.chain_id);
state::set_state(&mut io, state).sdk_unwrap();
// set genesis block balances
for account_balance in args.genesis_alloc {
Expand All @@ -542,7 +538,7 @@ mod contract {
)
}
// return new chain ID
io.return_output(&state::get_state(&io).sdk_unwrap().chain_id)
io.return_output(&state::get_state(&io).sdk_unwrap().chain_id())
}

#[cfg(feature = "evm_bully")]
Expand Down Expand Up @@ -911,7 +907,7 @@ mod contract {
#[no_mangle]
pub extern "C" fn mint_account() {
use crate::connector::ZERO_ATTACHED_BALANCE;
use crate::prelude::{NEP141Wei, ToString, U256};
use crate::prelude::{NEP141Wei, U256};
use evm::backend::ApplyBackend;
const GAS_FOR_VERIFY: NearGas = NearGas::new(20_000_000_000_000);
const GAS_FOR_FINISH: NearGas = NearGas::new(50_000_000_000_000);
Expand Down Expand Up @@ -987,7 +983,7 @@ mod contract {
}

fn require_owner_only(state: &state::EngineState, predecessor_account_id: &AccountId) {
if &state.owner_id != predecessor_account_id {
if &state.owner_id() != predecessor_account_id {
sdk::panic_utf8(errors::ERR_NOT_ALLOWED);
}
}
Expand Down
Loading

0 comments on commit e8c34ef

Please sign in to comment.