Skip to content

Commit

Permalink
feat: account storage override
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Oct 10, 2023
1 parent d8d1e73 commit 7ac1201
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
58 changes: 26 additions & 32 deletions crates/rethnet_evm_napi/src/state/overrides.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,10 @@
use napi::{
bindgen_prelude::{BigInt, Buffer},
Either,
};
use napi::bindgen_prelude::{BigInt, Buffer};
use napi_derive::napi;
use rethnet_eth::{Address, Bytes};
use rethnet_evm::HashMap;

use crate::cast::TryCast;

/// Type representing either a diff or full set of overrides for storage information.
pub type StorageOverride = Either<Vec<StorageSlotChange>, Vec<StorageSlotChange>>;

impl TryCast<rethnet_evm::state::StorageOverride> for StorageOverride {
type Error = napi::Error;

fn try_cast(self) -> napi::Result<rethnet_evm::state::StorageOverride> {
match self {
Either::A(diff) => Ok(rethnet_evm::state::StorageOverride::Diff(
diff.into_iter()
.map(|StorageSlotChange { index, value }| {
Ok((BigInt::try_cast(index)?, BigInt::try_cast(value)?))
})
.collect::<napi::Result<_>>()?,
)),
Either::B(full) => Ok(rethnet_evm::state::StorageOverride::Full(
full.into_iter()
.map(|StorageSlotChange { index, value }| {
Ok((BigInt::try_cast(index)?, BigInt::try_cast(value)?))
})
.collect::<napi::Result<_>>()?,
)),
}
}
}

#[napi(object)]
pub struct StorageSlotChange {
pub index: BigInt,
Expand All @@ -46,7 +17,8 @@ pub struct AccountOverride {
pub balance: Option<BigInt>,
pub nonce: Option<BigInt>,
pub code: Option<Buffer>,
pub storage: Option<StorageOverride>,
pub storage: Option<Vec<StorageSlotChange>>,
pub storage_diff: Option<Vec<StorageSlotChange>>,
}

impl TryCast<rethnet_evm::state::AccountOverride> for AccountOverride {
Expand All @@ -58,7 +30,29 @@ impl TryCast<rethnet_evm::state::AccountOverride> for AccountOverride {
let code = self
.code
.map(|code| rethnet_evm::Bytecode::new_raw(Bytes::copy_from_slice(code.as_ref())));
let storage = self.storage.map(StorageOverride::try_cast).transpose()?;

let storage =
match (self.storage, self.storage_diff) {
(None, None) => None,
(None, Some(diff)) => Some(rethnet_evm::state::StorageOverride::Diff(
diff.into_iter()
.map(|StorageSlotChange { index, value }| {
Ok((BigInt::try_cast(index)?, BigInt::try_cast(value)?))
})
.collect::<napi::Result<_>>()?,
)),
(Some(full), None) => Some(rethnet_evm::state::StorageOverride::Full(
full.into_iter()
.map(|StorageSlotChange { index, value }| {
Ok((BigInt::try_cast(index)?, BigInt::try_cast(value)?))
})
.collect::<napi::Result<_>>()?,
)),
(Some(_), Some(_)) => return Err(napi::Error::new(
napi::Status::InvalidArg,
"Both `storage` and `storageDiff` were specified. Only one can be provided.",
)),
};

Ok(rethnet_evm::state::AccountOverride {
balance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Common } from "@nomicfoundation/ethereumjs-common";
import {
Account,
Address,
bufferToBigInt,
KECCAK256_NULL,
toBuffer,
} from "@nomicfoundation/ethereumjs-util";
import { TypedTransaction } from "@nomicfoundation/ethereumjs-tx";
import {
Expand Down Expand Up @@ -141,13 +143,41 @@ export class RethnetAdapter implements VMAdapter {
this._state.asInner(),
new StateOverrides(
Object.entries(stateOverrideSet).map(([address, account]) => {
const storage =
account.state !== undefined
? Object.entries(account.state).map(([key, value]) => {
const index = bufferToBigInt(toBuffer(key));
const number = bufferToBigInt(toBuffer(value));

return {
index,
value: number,
};
})
: undefined;

const storageDiff =
account.stateDiff !== undefined
? Object.entries(account.stateDiff).map(([key, value]) => {
const index = bufferToBigInt(toBuffer(key));
const number = bufferToBigInt(toBuffer(value));

return {
index,
value: number,
};
})
: undefined;

const accountOverride: AccountOverride = {
balance: account.balance,
nonce: account.nonce,
code: account.code,
storage,
storageDiff,
};

return [Address.fromString(address).buf, accountOverride];
return [toBuffer(address), accountOverride];
})
),
config,
Expand Down

0 comments on commit 7ac1201

Please sign in to comment.