Skip to content

Commit

Permalink
Standalone engine storage saves the AccountId of the associated engin…
Browse files Browse the repository at this point in the history
…e deployed on chain (#510)
  • Loading branch information
birchmd authored May 24, 2022
1 parent f5157b7 commit 4cec5de
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions engine-standalone-storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Error {
TransactionNotFound(TransactionIncluded),
TransactionHashNotFound(H256),
Rocksdb(rocksdb::Error),
EngineAccountIdNotSet,
EngineAccountIdCorrupted,
}

impl From<rocksdb::Error> for Error {
Expand Down
21 changes: 20 additions & 1 deletion engine-standalone-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use aurora_engine_sdk::env::Timestamp;
use aurora_engine_types::H256;
use aurora_engine_types::{account_id::AccountId, H256};
use rocksdb::DB;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
Expand Down Expand Up @@ -34,8 +34,11 @@ pub enum StoragePrefix {
Diff = 0x04,
Engine = 0x05,
BlockMetadata = 0x06,
EngineAccountId = 0x07,
}

const ACCOUNT_ID_KEY: &[u8] = b"engine_account_id";

pub struct Storage {
db: DB,
}
Expand All @@ -46,6 +49,22 @@ impl Storage {
Ok(Self { db })
}

pub fn set_engine_account_id(&mut self, id: &AccountId) -> Result<(), rocksdb::Error> {
let key = construct_storage_key(StoragePrefix::EngineAccountId, ACCOUNT_ID_KEY);
self.db.put(key, id.as_bytes())
}

pub fn get_engine_account_id(&self) -> Result<AccountId, error::Error> {
let key = construct_storage_key(StoragePrefix::EngineAccountId, ACCOUNT_ID_KEY);
let slice = self
.db
.get_pinned(key)?
.ok_or(Error::EngineAccountIdNotSet)?;
let account_id =
AccountId::try_from(slice.as_ref()).map_err(|_| Error::EngineAccountIdCorrupted)?;
Ok(account_id)
}

pub fn get_latest_block(&self) -> Result<(H256, u64), error::Error> {
self.block_read(rocksdb::IteratorMode::End)
}
Expand Down
21 changes: 14 additions & 7 deletions engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use aurora_engine::{connector, engine, parameters::SubmitResult};
use aurora_engine_sdk::env::{self, Env, DEFAULT_PREPAID_GAS};
use aurora_engine_types::{
account_id::AccountId,
parameters::PromiseWithCallbackArgs,
types::{Address, Yocto},
H256,
Expand All @@ -10,11 +11,7 @@ pub mod types;

use crate::engine_state::EngineStateAccess;
use crate::{BlockMetadata, Diff, Storage};
use types::{Message, TransactionKind};

use self::types::TransactionMessage;

const AURORA_ACCOUNT_ID: &str = "aurora";
use types::{Message, TransactionKind, TransactionMessage};

pub fn consume_message(
storage: &mut Storage,
Expand All @@ -41,13 +38,15 @@ pub fn consume_message(
let block_hash = transaction_message.block_hash;
let block_height = storage.get_block_height_by_hash(block_hash)?;
let block_metadata = storage.get_block_metadata(block_hash)?;
let engine_account_id = storage.get_engine_account_id()?;

let (tx_hash, diff, result) = storage
.with_engine_access(block_height, transaction_position, &[], |io| {
execute_transaction(
transaction_message.as_ref(),
block_height,
&block_metadata,
engine_account_id,
io,
)
})
Expand Down Expand Up @@ -77,8 +76,15 @@ pub fn execute_transaction_message(
let block_hash = transaction_message.block_hash;
let block_height = storage.get_block_height_by_hash(block_hash)?;
let block_metadata = storage.get_block_metadata(block_hash)?;
let engine_account_id = storage.get_engine_account_id()?;
let result = storage.with_engine_access(block_height, transaction_position, &[], |io| {
execute_transaction(&transaction_message, block_height, &block_metadata, io)
execute_transaction(
&transaction_message,
block_height,
&block_metadata,
engine_account_id,
io,
)
});
let (tx_hash, diff, maybe_result) = result.result;
let outcome = TransactionIncludedOutcome {
Expand All @@ -94,6 +100,7 @@ fn execute_transaction<'db>(
transaction_message: &TransactionMessage,
block_height: u64,
block_metadata: &BlockMetadata,
engine_account_id: AccountId,
io: EngineStateAccess<'db, 'db, 'db>,
) -> (
H256,
Expand All @@ -105,7 +112,7 @@ fn execute_transaction<'db>(
let relayer_address =
aurora_engine_sdk::types::near_account_to_evm_address(predecessor_account_id.as_bytes());
let near_receipt_id = transaction_message.near_receipt_id;
let current_account_id = AURORA_ACCOUNT_ID.parse().unwrap();
let current_account_id = engine_account_id;
let env = env::Fixed {
signer_account_id,
current_account_id,
Expand Down
3 changes: 3 additions & 0 deletions engine-tests/src/test_utils/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ impl StandaloneRunner {
self.chain_id = chain_id;
let storage = &mut self.storage;
let env = &mut self.env;
storage
.set_engine_account_id(&env.current_account_id)
.unwrap();
env.block_height += 1;
let transaction_hash = H256::zero();
let tx_msg = Self::template_tx_msg(storage, &env, 0, transaction_hash);
Expand Down
4 changes: 4 additions & 0 deletions engine-tests/src/tests/repro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ fn repro_common<'a>(context: ReproContext<'a>) {

// Also validate the SubmitResult in the standalone engine
let mut standalone = standalone::StandaloneRunner::default();
standalone
.storage
.set_engine_account_id(&"aurora".parse().unwrap())
.unwrap();
json_snapshot::initialize_engine_state(&mut standalone.storage, snapshot).unwrap();
let standalone_result = standalone.submit_raw("submit", &runner.context).unwrap();
assert_eq!(
Expand Down
4 changes: 4 additions & 0 deletions engine-tests/src/tests/standalone/json_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ fn test_produce_snapshot() {
.unwrap();
let mut runner = standalone::StandaloneRunner::default();
runner.chain_id = 1313161554;
runner
.storage
.set_engine_account_id(&"aurora".parse().unwrap())
.unwrap();
json_snapshot::initialize_engine_state(&mut runner.storage, snapshot.clone()).unwrap();

// add a couple more transactions that write some extra keys
Expand Down

0 comments on commit 4cec5de

Please sign in to comment.