diff --git a/packages/blockchain/src/genesisStates/index.ts b/packages/blockchain/src/genesisStates/index.ts index be72e2a5be..eceffaa753 100644 --- a/packages/blockchain/src/genesisStates/index.ts +++ b/packages/blockchain/src/genesisStates/index.ts @@ -48,7 +48,7 @@ export async function genesisStateRoot(genesisState: GenesisState) { ) await storageTrie.put(storageKey, storageVal) } - account.stateRoot = storageTrie.root + account.storageRoot = storageTrie.root } } await trie.put(address, account.serialize()) diff --git a/packages/evm/src/types.ts b/packages/evm/src/types.ts index d1db65c375..9cd9b6527f 100644 --- a/packages/evm/src/types.ts +++ b/packages/evm/src/types.ts @@ -244,7 +244,7 @@ declare type Proof = { storageProof: StorageProof[] } -type AccountFields = Partial> +type AccountFields = Partial> interface StateAccess { accountExists(address: Address): Promise diff --git a/packages/statemanager/src/baseStateManager.ts b/packages/statemanager/src/baseStateManager.ts index 06cc4bd65d..544756cbea 100644 --- a/packages/statemanager/src/baseStateManager.ts +++ b/packages/statemanager/src/baseStateManager.ts @@ -79,7 +79,7 @@ export abstract class BaseStateManager { /** * Gets the account associated with `address`, modifies the given account * fields, then saves the account into state. Account fields can include - * `nonce`, `balance`, `stateRoot`, and `codeHash`. + * `nonce`, `balance`, `storageRoot`, and `codeHash`. * @param address - Address of the account to modify * @param accountFields - Object containing account fields and values to modify */ @@ -87,7 +87,7 @@ export abstract class BaseStateManager { const account = await this.getAccount(address) account.nonce = accountFields.nonce ?? account.nonce account.balance = accountFields.balance ?? account.balance - account.stateRoot = accountFields.stateRoot ?? account.stateRoot + account.storageRoot = accountFields.storageRoot ?? account.storageRoot account.codeHash = accountFields.codeHash ?? account.codeHash await this.putAccount(address, account) } diff --git a/packages/statemanager/src/interface.ts b/packages/statemanager/src/interface.ts index 410de5379e..28614d1431 100644 --- a/packages/statemanager/src/interface.ts +++ b/packages/statemanager/src/interface.ts @@ -8,7 +8,7 @@ export interface StorageDump { [key: string]: string } -export type AccountFields = Partial> +export type AccountFields = Partial> export interface StateAccess { accountExists(address: Address): Promise diff --git a/packages/statemanager/src/stateManager.ts b/packages/statemanager/src/stateManager.ts index a9920bf40d..c32f47e0e4 100644 --- a/packages/statemanager/src/stateManager.ts +++ b/packages/statemanager/src/stateManager.ts @@ -166,7 +166,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage // from state trie const account = await this.getAccount(address) const storageTrie = this._trie.copy(false) - storageTrie.root = account.stateRoot + storageTrie.root = account.storageRoot storageTrie.db.checkpoints = [] return storageTrie } @@ -226,9 +226,9 @@ export class DefaultStateManager extends BaseStateManager implements StateManage const addressHex = address.buf.toString('hex') this._storageTries[addressHex] = storageTrie - // update contract stateRoot + // update contract storageRoot const contract = this._cache.get(address) - contract.stateRoot = storageTrie.root + contract.storageRoot = storageTrie.root await this.putAccount(address, contract) resolve() @@ -347,7 +347,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage balance: bigIntToHex(account.balance), codeHash: bufferToHex(account.codeHash), nonce: bigIntToHex(account.nonce), - storageHash: bufferToHex(account.stateRoot), + storageHash: bufferToHex(account.storageRoot), accountProof, storageProof, } @@ -391,7 +391,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage } } else { const account = Account.fromRlpSerializedAccount(value) - const { nonce, balance, stateRoot, codeHash } = account + const { nonce, balance, storageRoot, codeHash } = account const invalidErrorMsg = 'Invalid proof provided:' if (nonce !== BigInt(proof.nonce)) { throw new Error(`${invalidErrorMsg} nonce does not match`) @@ -399,7 +399,7 @@ export class DefaultStateManager extends BaseStateManager implements StateManage if (balance !== BigInt(proof.balance)) { throw new Error(`${invalidErrorMsg} balance does not match`) } - if (!stateRoot.equals(toBuffer(proof.storageHash))) { + if (!storageRoot.equals(toBuffer(proof.storageHash))) { throw new Error(`${invalidErrorMsg} storageHash does not match`) } if (!codeHash.equals(toBuffer(proof.codeHash))) { diff --git a/packages/statemanager/tests/stateManager.spec.ts b/packages/statemanager/tests/stateManager.spec.ts index 8feb0dc390..cc90b9f2f2 100644 --- a/packages/statemanager/tests/stateManager.spec.ts +++ b/packages/statemanager/tests/stateManager.spec.ts @@ -194,7 +194,7 @@ tape('StateManager', (t) => { 'd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b', 'hex' ), - stateRoot: Buffer.from( + storageRoot: Buffer.from( 'cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7', 'hex' ), @@ -207,7 +207,7 @@ tape('StateManager', (t) => { 'd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b' ) st.equal( - res3.stateRoot.toString('hex'), + res3.storageRoot.toString('hex'), 'cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7' ) @@ -232,18 +232,18 @@ tape('StateManager', (t) => { 'd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b', 'hex' ) - const newStateRoot = Buffer.from( + const newStorageRoot = Buffer.from( 'cafd881ab193703b83816c49ff6c2bf6ba6f464a1be560c42106128c8dbc35e7', 'hex' ) await stateManager.modifyAccountFields(address, { codeHash: newCodeHash, - stateRoot: newStateRoot, + storageRoot: newStorageRoot, }) const res3 = await stateManager.getAccount(address) st.ok(res3.codeHash.equals(newCodeHash)) - st.ok(res3.stateRoot.equals(newStateRoot)) + st.ok(res3.storageRoot.equals(newStorageRoot)) st.end() } ) @@ -365,7 +365,7 @@ tape('StateManager', (t) => { st.ok(slotCode.length === 0, 'code cannot be loaded') // This test fails if no code prefix is used account = await codeStateManager.getAccount(address1) - account.stateRoot = root + account.storageRoot = root await codeStateManager.putAccount(address1, account) diff --git a/packages/trie/test/index.spec.ts b/packages/trie/test/index.spec.ts index 64e6c483b8..0bdd353572 100644 --- a/packages/trie/test/index.spec.ts +++ b/packages/trie/test/index.spec.ts @@ -274,14 +274,14 @@ tape('it should create the genesis state root from ethereum', function (tester) const v = Buffer.from('1e12515ce3e0f817a4ddef9ca55788a1d66bd2df', 'hex') const a = Buffer.from('1a26338f0d905e295fccb71fa9ea849ffa12aaf4', 'hex') - const stateRoot = Buffer.alloc(32) - stateRoot.fill(0) + const storageRoot = Buffer.alloc(32) + storageRoot.fill(0) const startAmount = Buffer.alloc(26) startAmount.fill(0) startAmount[0] = 1 - const account = [startAmount, 0, stateRoot, KECCAK256_NULL] + const account = [startAmount, 0, storageRoot, KECCAK256_NULL] const rlpAccount = Buffer.from(RLP.encode(bufArrToArr(account as Buffer[]))) const cppRlp = 'f85e9a010000000000000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' diff --git a/packages/util/src/account.ts b/packages/util/src/account.ts index defe9849bf..889f94afe3 100644 --- a/packages/util/src/account.ts +++ b/packages/util/src/account.ts @@ -24,23 +24,23 @@ const _0n = BigInt(0) export interface AccountData { nonce?: BigIntLike balance?: BigIntLike - stateRoot?: BufferLike + storageRoot?: BufferLike codeHash?: BufferLike } export class Account { nonce: bigint balance: bigint - stateRoot: Buffer + storageRoot: Buffer codeHash: Buffer static fromAccountData(accountData: AccountData) { - const { nonce, balance, stateRoot, codeHash } = accountData + const { nonce, balance, storageRoot, codeHash } = accountData return new Account( isTruthy(nonce) ? bufferToBigInt(toBuffer(nonce)) : undefined, isTruthy(balance) ? bufferToBigInt(toBuffer(balance)) : undefined, - isTruthy(stateRoot) ? toBuffer(stateRoot) : undefined, + isTruthy(storageRoot) ? toBuffer(storageRoot) : undefined, isTruthy(codeHash) ? toBuffer(codeHash) : undefined ) } @@ -56,19 +56,19 @@ export class Account { } public static fromValuesArray(values: Buffer[]) { - const [nonce, balance, stateRoot, codeHash] = values + const [nonce, balance, storageRoot, codeHash] = values - return new Account(bufferToBigInt(nonce), bufferToBigInt(balance), stateRoot, codeHash) + return new Account(bufferToBigInt(nonce), bufferToBigInt(balance), storageRoot, codeHash) } /** * This constructor assigns and validates the values. * Use the static factory methods to assist in creating an Account from varying data types. */ - constructor(nonce = _0n, balance = _0n, stateRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) { + constructor(nonce = _0n, balance = _0n, storageRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) { this.nonce = nonce this.balance = balance - this.stateRoot = stateRoot + this.storageRoot = storageRoot this.codeHash = codeHash this._validate() @@ -81,8 +81,8 @@ export class Account { if (this.balance < _0n) { throw new Error('balance must be greater than zero') } - if (this.stateRoot.length !== 32) { - throw new Error('stateRoot must have a length of 32') + if (this.storageRoot.length !== 32) { + throw new Error('storageRoot must have a length of 32') } if (this.codeHash.length !== 32) { throw new Error('codeHash must have a length of 32') @@ -96,7 +96,7 @@ export class Account { return [ bigIntToUnpaddedBuffer(this.nonce), bigIntToUnpaddedBuffer(this.balance), - this.stateRoot, + this.storageRoot, this.codeHash, ] } diff --git a/packages/util/test/account.spec.ts b/packages/util/test/account.spec.ts index eea2051020..a2f3b7f342 100644 --- a/packages/util/test/account.spec.ts +++ b/packages/util/test/account.spec.ts @@ -28,9 +28,9 @@ tape('Account', function (t) { st.equal(account.nonce, _0n, 'should have zero nonce') st.equal(account.balance, _0n, 'should have zero balance') st.equal( - account.stateRoot.toString('hex'), + account.storageRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', - 'should have stateRoot equal to KECCAK256_RLP' + 'should have storageRoot equal to KECCAK256_RLP' ) st.equal( account.codeHash.toString('hex'), @@ -44,16 +44,16 @@ tape('Account', function (t) { const raw = [ '0x02', // nonce '0x0384', // balance - '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // stateRoot + '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // storageRoot '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', // codeHash ] const account = Account.fromValuesArray(raw.map(toBuffer)) st.equal(account.nonce, BigInt(2), 'should have correct nonce') st.equal(account.balance, BigInt(900), 'should have correct balance') st.equal( - account.stateRoot.toString('hex'), + account.storageRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', - 'should have correct stateRoot' + 'should have correct storageRoot' ) st.equal( account.codeHash.toString('hex'), @@ -67,16 +67,16 @@ tape('Account', function (t) { const raw = { nonce: '0x02', balance: '0x0384', - stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', } const account = Account.fromAccountData(raw) st.equal(account.nonce, BigInt(2), 'should have correct nonce') st.equal(account.balance, BigInt(900), 'should have correct balance') st.equal( - account.stateRoot.toString('hex'), + account.storageRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', - 'should have correct stateRoot' + 'should have correct storageRoot' ) st.equal( account.codeHash.toString('hex'), @@ -95,9 +95,9 @@ tape('Account', function (t) { st.equal(account.nonce, BigInt(2), 'should have correct nonce') st.equal(account.balance, BigInt(900), 'should have correct balance') st.equal( - account.stateRoot.toString('hex'), + account.storageRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', - 'should have correct stateRoot' + 'should have correct storageRoot' ) st.equal( account.codeHash.toString('hex'), @@ -111,12 +111,12 @@ tape('Account', function (t) { const raw = { nonce: '0x01', balance: '0x42', - stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', } const account = Account.fromAccountData(raw) const accountRlp = Buffer.from( - RLP.encode([raw.nonce, raw.balance, raw.stateRoot, raw.codeHash]) + RLP.encode([raw.nonce, raw.balance, raw.storageRoot, raw.codeHash]) ) st.ok(account.serialize().equals(accountRlp), 'should serialize correctly') st.end() @@ -133,7 +133,7 @@ tape('Account', function (t) { const raw = { nonce: '0x01', balance: '0x0042', - stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', } account = Account.fromAccountData(raw) @@ -148,7 +148,7 @@ tape('Account', function (t) { const raw = { nonce: '0x01', balance: '0x0042', - stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', codeHash: '0xd748bf26ab37599c944babfdbeecf6690801bd61bf2670efb0a34adfc6dca10b', } account = Account.fromAccountData(raw) @@ -159,7 +159,7 @@ tape('Account', function (t) { t.test('validation', function (st) { st.throws(() => { new Account(undefined, undefined, Buffer.from('hey'), undefined) - }, 'should only accept length 32 buffer for stateRoot') + }, 'should only accept length 32 buffer for storageRoot') st.throws(() => { new Account(undefined, undefined, undefined, Buffer.from('hey')) diff --git a/packages/vm/examples/run-solidity-contract.ts b/packages/vm/examples/run-solidity-contract.ts index a81571bdb6..b33409dbbd 100644 --- a/packages/vm/examples/run-solidity-contract.ts +++ b/packages/vm/examples/run-solidity-contract.ts @@ -215,7 +215,7 @@ async function main() { console.log('-------results-------') console.log('nonce: ' + createdAccount.nonce.toString()) console.log('balance in wei: ', createdAccount.balance.toString()) - console.log('stateRoot: 0x' + createdAccount.stateRoot.toString('hex')) + console.log('storageRoot: 0x' + createdAccount.storageRoot.toString('hex')) console.log('codeHash: 0x' + createdAccount.codeHash.toString('hex')) console.log('---------------------') diff --git a/packages/vm/src/vm.ts b/packages/vm/src/vm.ts index fc63748643..257d9bf289 100644 --- a/packages/vm/src/vm.ts +++ b/packages/vm/src/vm.ts @@ -184,7 +184,7 @@ export class VM extends AsyncEventEmitter { // Only do this if it is not overridden in genesis // Note: in the case that custom genesis has storage fields, this is preserved if (account.isEmpty()) { - const newAccount = Account.fromAccountData({ balance: 1, stateRoot: account.stateRoot }) + const newAccount = Account.fromAccountData({ balance: 1, storageRoot: account.storageRoot }) await this.eei.putAccount(address, newAccount) } } diff --git a/packages/vm/tests/util.ts b/packages/vm/tests/util.ts index 44a3e76072..3e4cff1be1 100644 --- a/packages/vm/tests/util.ts +++ b/packages/vm/tests/util.ts @@ -47,7 +47,7 @@ export function dumpState(state: any, cb: Function) { return new Promise((resolve) => { const storage: any = {} const storageTrie = state.copy(false) - storageTrie.root = account.stateRoot + storageTrie.root = account.storageRoot const storageRS = storageTrie.createReadStream() storageRS.on('data', function (data: any) { @@ -68,7 +68,7 @@ export function dumpState(state: any, cb: Function) { } for (let i = 0; i < results.length; i++) { console.log("SHA3'd address: " + bufferToHex(results[i].address)) - console.log('\tstate root: ' + bufferToHex(results[i].stateRoot)) + console.log('\tstorage root: ' + bufferToHex(results[i].storageRoot)) console.log('\tstorage: ') for (const storageKey in results[i].storage) { console.log('\t\t' + storageKey + ': ' + results[i].storage[storageKey]) @@ -215,7 +215,7 @@ export function verifyAccountPostConditions( acctData.storage[key] } - state.root = account.stateRoot + state.root = account.storageRoot const rs = state.createReadStream() rs.on('data', function (data: any) { let key = data.key.toString('hex') @@ -344,14 +344,14 @@ export async function setupPreConditions(state: VmState, testData: any) { // Put contract code await state.putContractCode(address, codeBuf) - const stateRoot = (await state.getAccount(address)).stateRoot + const storageRoot = (await state.getAccount(address)).storageRoot if (testData.exec?.address === addressStr) { - testData.root = stateRoot + testData.root = storageRoot } // Put account data - const account = Account.fromAccountData({ nonce, balance, codeHash, stateRoot }) + const account = Account.fromAccountData({ nonce, balance, codeHash, storageRoot }) await state.putAccount(address, account) } await state.commit()