diff --git a/packages/client/src/rpc/modules/eth.ts b/packages/client/src/rpc/modules/eth.ts index 5384b9fee4..10d8bce834 100644 --- a/packages/client/src/rpc/modules/eth.ts +++ b/packages/client/src/rpc/modules/eth.ts @@ -738,7 +738,7 @@ export class Eth { await vm.stateManager.setStateRoot(block.header.stateRoot) const address = Address.fromString(addressHex) - const code = await vm.stateManager.getContractCode(address) + const code = await vm.stateManager.getCode(address) return bytesToHex(code) } @@ -780,7 +780,7 @@ export class Eth { const address = Address.fromString(addressHex) const key = setLengthLeft(hexToBytes(keyHex), 32) - const storage = await vm.stateManager.getContractStorage(address, key) + const storage = await vm.stateManager.getStorage(address, key) return storage !== null && storage !== undefined ? bytesToHex(setLengthLeft(Uint8Array.from(storage) as Uint8Array, 32)) : EMPTY_SLOT diff --git a/packages/common/src/interfaces.ts b/packages/common/src/interfaces.ts index 113b5b47e9..e67fe0da76 100644 --- a/packages/common/src/interfaces.ts +++ b/packages/common/src/interfaces.ts @@ -148,31 +148,38 @@ export interface AccessWitnessInterface { * */ export interface StateManagerInterface { + // Account methods getAccount(address: Address): Promise putAccount(address: Address, account?: Account): Promise deleteAccount(address: Address): Promise modifyAccountFields(address: Address, accountFields: AccountFields): Promise - putContractCode(address: Address, value: Uint8Array): Promise - getContractCode(address: Address): Promise - getContractCodeSize?(address: Address): Promise - getContractStorage(address: Address, key: Uint8Array): Promise - putContractStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise - clearContractStorage(address: Address): Promise + + // Code methods + putCode(address: Address, value: Uint8Array): Promise + getCode(address: Address): Promise + getCodeSize(address: Address): Promise + + // Storage methods + getStorage(address: Address, key: Uint8Array): Promise + putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise + clearStorage(address: Address): Promise + + // Checkpointing methods checkpoint(): Promise commit(): Promise revert(): Promise + + // State root methods getStateRoot(): Promise setStateRoot(stateRoot: Uint8Array, clearCache?: boolean): Promise - getProof?(address: Address, storageSlots: Uint8Array[]): Promise hasStateRoot(root: Uint8Array): Promise // only used in client + + // Other + getProof?(address: Address, storageSlots: Uint8Array[]): Promise shallowCopy(downlevelCaches?: boolean): StateManagerInterface getAppliedKey?(address: Uint8Array): Uint8Array - /* - * The following optional methods are Verkle related - * - * Experimental (do not implement) - */ + // Verkle (experimental) checkChunkWitnessPresent?(contract: Address, programCounter: number): Promise } diff --git a/packages/evm/src/evm.ts b/packages/evm/src/evm.ts index 1aed094c00..fc40cf017b 100644 --- a/packages/evm/src/evm.ts +++ b/packages/evm/src/evm.ts @@ -160,6 +160,17 @@ export class EVM implements EVMInterface { this.blockchain = opts.blockchain! this.stateManager = opts.stateManager! + if (this.common.isActivatedEIP(6800)) { + const mandatory = ['checkChunkWitnessPresent'] + for (const m of mandatory) { + if (!(m in this.stateManager)) { + throw new Error( + `State manager used must implement ${m} if Verkle (EIP-6800) is activated`, + ) + } + } + } + this._bn128 = bn128 this.events = new AsyncEventEmitter() this._optsCached = opts @@ -470,7 +481,7 @@ export class EVM implements EVMInterface { } await this.journal.putAccount(message.to, toAccount) - await this.stateManager.clearContractStorage(message.to) + await this.stateManager.clearStorage(message.to) const newContractEvent = { address: message.to, @@ -676,7 +687,7 @@ export class EVM implements EVMInterface { } } - await this.stateManager.putContractCode(message.to, result.returnValue) + await this.stateManager.putCode(message.to, result.returnValue) if (this.DEBUG) { debug(`Code saved on new contract creation`) } @@ -999,7 +1010,7 @@ export class EVM implements EVMInterface { message.code = precompile message.isCompiled = true } else { - message.code = await this.stateManager.getContractCode(message.codeAddress) + message.code = await this.stateManager.getCode(message.codeAddress) message.isCompiled = false message.chargeCodeAccesses = true } diff --git a/packages/evm/src/interpreter.ts b/packages/evm/src/interpreter.ts index 8e2516b124..55e776f271 100644 --- a/packages/evm/src/interpreter.ts +++ b/packages/evm/src/interpreter.ts @@ -601,7 +601,7 @@ export class Interpreter { * Store 256-bit a value in memory to persistent storage. */ async storageStore(key: Uint8Array, value: Uint8Array): Promise { - await this._stateManager.putContractStorage(this._env.address, key, value) + await this._stateManager.putStorage(this._env.address, key, value) const account = await this._stateManager.getAccount(this._env.address) if (!account) { throw new Error('could not read account while persisting memory') @@ -618,7 +618,7 @@ export class Interpreter { if (original) { return this._stateManager.originalStorageCache.get(this._env.address, key) } else { - return this._stateManager.getContractStorage(this._env.address, key) + return this._stateManager.getStorage(this._env.address, key) } } diff --git a/packages/evm/src/opcodes/functions.ts b/packages/evm/src/opcodes/functions.ts index 5976f9381e..8b8fc1fde0 100644 --- a/packages/evm/src/opcodes/functions.ts +++ b/packages/evm/src/opcodes/functions.ts @@ -524,26 +524,16 @@ export const handlers: Map = new Map([ const addressBigInt = runState.stack.pop() const address = new Address(addresstoBytes(addressBigInt)) // EOF check - const code = await runState.stateManager.getContractCode(address) + const code = await runState.stateManager.getCode(address) if (isEOF(code)) { // In legacy code, the target code is treated as to be "EOFBYTES" code runState.stack.push(BigInt(EOFBYTES.length)) return } - let size - if (typeof runState.stateManager.getContractCodeSize === 'function') { - size = BigInt( - await runState.stateManager.getContractCodeSize( - new Address(addresstoBytes(addressBigInt)), - ), - ) - } else { - size = BigInt( - (await runState.stateManager.getContractCode(new Address(addresstoBytes(addressBigInt)))) - .length, - ) - } + const size = BigInt( + await runState.stateManager.getCodeSize(new Address(addresstoBytes(addressBigInt))), + ) runState.stack.push(size) }, @@ -555,9 +545,7 @@ export const handlers: Map = new Map([ const [addressBigInt, memOffset, codeOffset, dataLength] = runState.stack.popN(4) if (dataLength !== BIGINT_0) { - let code = await runState.stateManager.getContractCode( - new Address(addresstoBytes(addressBigInt)), - ) + let code = await runState.stateManager.getCode(new Address(addresstoBytes(addressBigInt))) if (isEOF(code)) { // In legacy code, the target code is treated as to be "EOFBYTES" code @@ -579,7 +567,7 @@ export const handlers: Map = new Map([ const address = new Address(addresstoBytes(addressBigInt)) // EOF check - const code = await runState.stateManager.getContractCode(address) + const code = await runState.stateManager.getCode(address) if (isEOF(code)) { // In legacy code, the target code is treated as to be "EOFBYTES" code // Therefore, push the hash of EOFBYTES to the stack @@ -665,7 +653,7 @@ export const handlers: Map = new Map([ ) runState.interpreter.useGas(statelessGas, `BLOCKHASH`) } - const storage = await runState.stateManager.getContractStorage(historyAddress, key) + const storage = await runState.stateManager.getStorage(historyAddress, key) runState.stack.push(bytesToBigInt(storage)) } else { @@ -1674,7 +1662,7 @@ export const handlers: Map = new Map([ const toAddress = new Address(addresstoBytes(toAddr)) - const code = await runState.stateManager.getContractCode(toAddress) + const code = await runState.stateManager.getCode(toAddress) if (!isEOF(code)) { // EXTDELEGATECALL cannot call legacy contracts diff --git a/packages/evm/src/opcodes/gas.ts b/packages/evm/src/opcodes/gas.ts index dc0ad6e5bf..81cfae362c 100644 --- a/packages/evm/src/opcodes/gas.ts +++ b/packages/evm/src/opcodes/gas.ts @@ -230,7 +230,7 @@ export const dynamicGasHandlers: Map codeSize) { codeEnd = codeSize } diff --git a/packages/evm/test/blobVersionedHashes.spec.ts b/packages/evm/test/blobVersionedHashes.spec.ts index f8676be1aa..ef97c4bddf 100644 --- a/packages/evm/test/blobVersionedHashes.spec.ts +++ b/packages/evm/test/blobVersionedHashes.spec.ts @@ -51,7 +51,7 @@ describe(`BLOBHASH: access blobVersionedHashes within contract calls`, () => { const getBlobHasIndexCode = '0x60004960005260206000F3' const contractAddress = new Address(hexToBytes('0x00000000000000000000000000000000000000ff')) // contract address - await evm.stateManager.putContractCode(contractAddress, hexToBytes(getBlobHasIndexCode)) // setup the contract code + await evm.stateManager.putCode(contractAddress, hexToBytes(getBlobHasIndexCode)) // setup the contract code const caller = new Address(hexToBytes('0x00000000000000000000000000000000000000ee')) // caller address await evm.stateManager.putAccount(caller, new Account(BigInt(0), BigInt(0x11111111))) // give the calling account a big balance so we don't run out of funds @@ -132,7 +132,7 @@ describe(`BLOBHASH: access blobVersionedHashes in a CREATE/CREATE2 frame`, () => const res = await evm.runCall(runCallArgs) const address = Address.fromString(bytesToHex(res.execResult.returnValue.slice(12))) - const code = await evm.stateManager.getContractCode(address) + const code = await evm.stateManager.getCode(address) assert.equal( bytesToHex(code), diff --git a/packages/evm/test/eips/eip-3860.spec.ts b/packages/evm/test/eips/eip-3860.spec.ts index 394643bb90..09992f364a 100644 --- a/packages/evm/test/eips/eip-3860.spec.ts +++ b/packages/evm/test/eips/eip-3860.spec.ts @@ -68,8 +68,8 @@ describe('EIP 3860 tests', () => { '0x7f600a80600080396000f3000000000000000000000000000000000000000000006000526000355a8160006000f05a8203600a55806000556001600155505050', ) - await evm.stateManager.putContractCode(contractFactory, factoryCode) - await evmWithout3860.stateManager.putContractCode(contractFactory, factoryCode) + await evm.stateManager.putCode(contractFactory, factoryCode) + await evmWithout3860.stateManager.putCode(contractFactory, factoryCode) const data = hexToBytes('0x000000000000000000000000000000000000000000000000000000000000c000') const runCallArgs = { from: caller, @@ -112,8 +112,8 @@ describe('EIP 3860 tests', () => { '0x7f600a80600080396000f3000000000000000000000000000000000000000000006000526000355a60008260006000f55a8203600a55806000556001600155505050', ) - await evm.stateManager.putContractCode(contractFactory, factoryCode) - await evmWithout3860.stateManager.putContractCode(contractFactory, factoryCode) + await evm.stateManager.putCode(contractFactory, factoryCode) + await evmWithout3860.stateManager.putCode(contractFactory, factoryCode) const data = hexToBytes('0x000000000000000000000000000000000000000000000000000000000000c000') const runCallArgs = { from: caller, @@ -190,8 +190,8 @@ describe('EIP 3860 tests', () => { // This is either the contract address if it was succesful, or 0 in case of error const factoryCode = hexToBytes(`0x600060003560006000${code}600055`) - await evm.stateManager.putContractCode(contractFactory, factoryCode) - await evmDisabled.stateManager.putContractCode(contractFactory, factoryCode) + await evm.stateManager.putCode(contractFactory, factoryCode) + await evmDisabled.stateManager.putCode(contractFactory, factoryCode) const runCallArgs = { from: caller, @@ -204,11 +204,8 @@ describe('EIP 3860 tests', () => { await evmDisabled.runCall(runCallArgs) const key0 = hexToBytes(`0x${'00'.repeat(32)}`) - const storageActive = await evm.stateManager.getContractStorage(contractFactory, key0) - const storageInactive = await evmDisabled.stateManager.getContractStorage( - contractFactory, - key0, - ) + const storageActive = await evm.stateManager.getStorage(contractFactory, key0) + const storageInactive = await evmDisabled.stateManager.getStorage(contractFactory, key0) assert.ok( !equalsBytes(storageActive, new Uint8Array()), diff --git a/packages/evm/test/eips/eof-runner.spec.ts b/packages/evm/test/eips/eof-runner.spec.ts index 574a1bf1bf..6cfa643585 100644 --- a/packages/evm/test/eips/eof-runner.spec.ts +++ b/packages/evm/test/eips/eof-runner.spec.ts @@ -23,7 +23,7 @@ describe('EOF: should run a simple contract', async () => { const caller = new Address(hexToBytes('0x00000000000000000000000000000000000000ee')) // caller address const contractAddress = new Address(hexToBytes('0x00000000000000000000000000000000000000ff')) // contract address - await evm.stateManager.putContractCode(contractAddress, code) + await evm.stateManager.putCode(contractAddress, code) await evm.stateManager.putAccount(caller, new Account(BigInt(0), BigInt(0x11111111))) const runCallArgs = { diff --git a/packages/evm/test/precompiles/09-blake2f.spec.ts b/packages/evm/test/precompiles/09-blake2f.spec.ts index a502e1fa91..21a1390896 100644 --- a/packages/evm/test/precompiles/09-blake2f.spec.ts +++ b/packages/evm/test/precompiles/09-blake2f.spec.ts @@ -136,7 +136,7 @@ describe('Precompiles: BLAKE2F', () => { // -> Calls Blake2F with this data (so, with the calldata) // -> Returns the data from Blake2F const code = `0x366000602037600080366020600060095AF1593D6000593E3D90F3` - await evm.stateManager.putContractCode(addr, hexToBytes(code)) + await evm.stateManager.putCode(addr, hexToBytes(code)) const res = await evm.runCall({ data: hexToBytes(calldata), diff --git a/packages/evm/test/runCall.spec.ts b/packages/evm/test/runCall.spec.ts index bdd2a3f899..0e59c6633c 100644 --- a/packages/evm/test/runCall.spec.ts +++ b/packages/evm/test/runCall.spec.ts @@ -70,7 +70,7 @@ describe('RunCall tests', () => { RETURN [0x00, 0x20] */ - await evm.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code + await evm.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code await evm.stateManager.putAccount(caller, new Account(BigInt(0), BigInt(0x11111111))) // give the calling account a big balance so we don't run out of funds const codeHash = keccak256(new Uint8Array()) for (let value = 0; value <= 1000; value += 20) { @@ -122,8 +122,8 @@ describe('RunCall tests', () => { STOP */ - await evmByzantium.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code - await evmConstantinople.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code + await evmByzantium.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code + await evmConstantinople.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code const runCallArgs = { caller, // call address @@ -174,8 +174,8 @@ describe('RunCall tests', () => { */ - await evm.stateManager.putContractCode(address, hexToBytes(code)) - await evm.stateManager.putContractStorage( + await evm.stateManager.putCode(address, hexToBytes(code)) + await evm.stateManager.putStorage( address, new Uint8Array(32), hexToBytes(`0x${'00'.repeat(31)}01`), @@ -204,7 +204,7 @@ describe('RunCall tests', () => { // push 1 push 0 sstore stop const code = '0x600160015500' - await evm.stateManager.putContractCode(address, hexToBytes(code)) + await evm.stateManager.putCode(address, hexToBytes(code)) // setup the call arguments const runCallArgs = { @@ -229,7 +229,7 @@ describe('RunCall tests', () => { // code to call 0x00..00dd, which does not exist const code = '0x6000600060006000600060DD61FFFF5A03F100' - await evm.stateManager.putContractCode(address, hexToBytes(code)) + await evm.stateManager.putCode(address, hexToBytes(code)) // setup the call arguments const runCallArgs = { @@ -257,7 +257,7 @@ describe('RunCall tests', () => { // but using too much memory const code = '0x61FFFF60FF60006000600060EE6000F200' - await evm.stateManager.putContractCode(address, hexToBytes(code)) + await evm.stateManager.putCode(address, hexToBytes(code)) // setup the call arguments const runCallArgs = { @@ -285,7 +285,7 @@ describe('RunCall tests', () => { // this should thus go OOG const code = '0x60FEFF' - await evm.stateManager.putContractCode(address, hexToBytes(code)) + await evm.stateManager.putCode(address, hexToBytes(code)) // setup the call arguments const runCallArgs = { @@ -314,7 +314,7 @@ describe('RunCall tests', () => { const code = '0x3460005500' await evm.stateManager.putAccount(caller, new Account()) - await evm.stateManager.putContractCode(address, hexToBytes(code)) + await evm.stateManager.putCode(address, hexToBytes(code)) const account = await evm.stateManager.getAccount(caller) account!.balance = BigInt(100) @@ -388,7 +388,7 @@ describe('RunCall tests', () => { STOP */ - await evm.stateManager.putContractCode(address, hexToBytes(code)) + await evm.stateManager.putCode(address, hexToBytes(code)) const account = await evm.stateManager.getAccount(address) account!.nonce = MAX_UINT64 - BigInt(1) @@ -402,7 +402,7 @@ describe('RunCall tests', () => { } await evm.runCall(runCallArgs) - let storage = await evm.stateManager.getContractStorage(address, slot) + let storage = await evm.stateManager.getStorage(address, slot) // The nonce is MAX_UINT64 - 1, so we are allowed to create a contract (nonce of creating contract is now MAX_UINT64) assert.notDeepEqual(storage, emptyBytes, 'successfully created contract') @@ -410,7 +410,7 @@ describe('RunCall tests', () => { await evm.runCall(runCallArgs) // The nonce is MAX_UINT64, so we are NOT allowed to create a contract (nonce of creating contract is now MAX_UINT64) - storage = await evm.stateManager.getContractStorage(address, slot) + storage = await evm.stateManager.getStorage(address, slot) assert.deepEqual( storage, emptyBytes, @@ -448,7 +448,7 @@ describe('RunCall tests', () => { '0x00000000000000000000000028373a29d17af317e669579d97e7dddc9da6e3e2e7dddc9da6e3e200000000000000000000000000000000000000000000000000' assert.equal(result.createdAddress?.toString(), expectedAddress, 'created address correct') - const deployedCode = await evm.stateManager.getContractCode(result.createdAddress!) + const deployedCode = await evm.stateManager.getCode(result.createdAddress!) assert.equal(bytesToHex(deployedCode), expectedCode, 'deployed code correct') }) @@ -481,7 +481,7 @@ describe('RunCall tests', () => { // runCall against a contract to reach `_reduceSenderBalance` const contractCode = hexToBytes('0x00') // 00: STOP const contractAddress = Address.fromString('0x000000000000000000000000636F6E7472616374') - await evm.stateManager.putContractCode(contractAddress, contractCode) + await evm.stateManager.putCode(contractAddress, contractCode) const senderKey = hexToBytes( '0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', ) @@ -616,7 +616,7 @@ describe('RunCall tests', () => { const contractCode = hexToBytes('0x600060405200') // PUSH 0 PUSH 40 MSTORE STOP const contractAddress = Address.fromString('0x000000000000000000000000636F6E7472616374') - await evm.stateManager.putContractCode(contractAddress, contractCode) + await evm.stateManager.putCode(contractAddress, contractCode) const runCallArgs = { gasLimit: BigInt(21000), @@ -730,10 +730,10 @@ describe('RunCall tests', () => { const callerCode = hexToBytes(`0x60008080808061AAAA61${gasLimit}f1600055`) await evm.stateManager.putAccount(callCodeAddress, new Account()) - await evm.stateManager.putContractCode(callCodeAddress, callCode) + await evm.stateManager.putCode(callCodeAddress, callCode) await evm.stateManager.putAccount(callerAddress, new Account(undefined, BigInt(1))) - await evm.stateManager.putContractCode(callerAddress, callerCode) + await evm.stateManager.putCode(callerAddress, callerCode) const runCallArgs = { to: callerAddress, @@ -741,9 +741,7 @@ describe('RunCall tests', () => { } await evm.runCall(runCallArgs) - const callResult = bytesToHex( - await evm.stateManager.getContractStorage(callerAddress, zeros(32)), - ) + const callResult = bytesToHex(await evm.stateManager.getStorage(callerAddress, zeros(32))) // Expect slot to have value of either: 0 since CALLCODE and CODE did not have enough gas to execute // Or 1, if CALL(CODE) has enough gas to enter the new call frame assert.equal(callResult, expectedOutput, `should have result ${expectedOutput}`) diff --git a/packages/evm/test/runCode.spec.ts b/packages/evm/test/runCode.spec.ts index c63560fa9f..00eafbb573 100644 --- a/packages/evm/test/runCode.spec.ts +++ b/packages/evm/test/runCode.spec.ts @@ -77,14 +77,14 @@ describe('VM.runCode: interpreter', () => { it('should throw on non-EvmError', async () => { const evm = await createEVM() - // NOTE: due to now throwing on `getContractStorage` if account does not exist + // NOTE: due to now throwing on `getStorage` if account does not exist // this now means that if `runCode` is called and the address it runs on (default: zero address) // does not exist, then if SSTORE/SLOAD is used, the runCode will immediately fail because StateManager now throws // TODO: is this behavior which we should fix? (Either in StateManager OR in runCode where we load the account first, // then re-put the account after (if account === undefined put empty account, such that the account exists)) const address = Address.fromString(`0x${'00'.repeat(20)}`) await evm.stateManager.putAccount(address, new Account()) - evm.stateManager.putContractStorage = (..._args) => { + evm.stateManager.putStorage = (..._args) => { throw new Error('Test') } diff --git a/packages/evm/test/stack.spec.ts b/packages/evm/test/stack.spec.ts index 4e0fe7e6f0..ed75419f04 100644 --- a/packages/evm/test/stack.spec.ts +++ b/packages/evm/test/stack.spec.ts @@ -122,7 +122,7 @@ describe('Stack', () => { RETURN stack: [0, 0x20] (we thus return the stack item which was originally pushed as 0, and then DUPed) */ await evm.stateManager.putAccount(addr, account) - await evm.stateManager.putContractCode(addr, hexToBytes(code)) + await evm.stateManager.putCode(addr, hexToBytes(code)) await evm.stateManager.putAccount(caller, new Account(BigInt(0), BigInt(0x11))) const runCallArgs = { caller, diff --git a/packages/statemanager/examples/fromProofInstantiation.ts b/packages/statemanager/examples/fromProofInstantiation.ts index 97df90f29c..2e1117f9c8 100644 --- a/packages/statemanager/examples/fromProofInstantiation.ts +++ b/packages/statemanager/examples/fromProofInstantiation.ts @@ -15,9 +15,9 @@ const main = async () => { const storageValue1 = hexToBytes('0x01') const storageValue2 = hexToBytes('0x02') - await stateManager.putContractCode(contractAddress, byteCode) - await stateManager.putContractStorage(contractAddress, storageKey1, storageValue1) - await stateManager.putContractStorage(contractAddress, storageKey2, storageValue2) + await stateManager.putCode(contractAddress, byteCode) + await stateManager.putStorage(contractAddress, storageKey1, storageValue1) + await stateManager.putStorage(contractAddress, storageKey2, storageValue2) const proof = await stateManager.getProof(contractAddress) const proofWithStorage = await stateManager.getProof(contractAddress, [storageKey1, storageKey2]) @@ -25,22 +25,16 @@ const main = async () => { // To add more proof data, use `addProofData` await partialStateManager.addProofData(proofWithStorage) - console.log(await partialStateManager.getContractCode(contractAddress)) // contract bytecode is not included in proof - console.log( - await partialStateManager.getContractStorage(contractAddress, storageKey1), - storageValue1, - ) // should match - console.log( - await partialStateManager.getContractStorage(contractAddress, storageKey2), - storageValue2, - ) // should match + console.log(await partialStateManager.getCode(contractAddress)) // contract bytecode is not included in proof + console.log(await partialStateManager.getStorage(contractAddress, storageKey1), storageValue1) // should match + console.log(await partialStateManager.getStorage(contractAddress, storageKey2), storageValue2) // should match const accountFromNewSM = await partialStateManager.getAccount(contractAddress) const accountFromOldSM = await stateManager.getAccount(contractAddress) console.log(accountFromNewSM, accountFromOldSM) // should match - const slot1FromNewSM = await stateManager.getContractStorage(contractAddress, storageKey1) - const slot2FromNewSM = await stateManager.getContractStorage(contractAddress, storageKey2) + const slot1FromNewSM = await stateManager.getStorage(contractAddress, storageKey1) + const slot2FromNewSM = await stateManager.getStorage(contractAddress, storageKey2) console.log(slot1FromNewSM, storageValue1) // should match console.log(slot2FromNewSM, storageValue2) // should match } diff --git a/packages/statemanager/src/cache/originalStorageCache.ts b/packages/statemanager/src/cache/originalStorageCache.ts index dd837be4e3..b7093ac9bd 100644 --- a/packages/statemanager/src/cache/originalStorageCache.ts +++ b/packages/statemanager/src/cache/originalStorageCache.ts @@ -2,23 +2,23 @@ import { bytesToUnprefixedHex } from '@ethereumjs/util' import type { Address } from '@ethereumjs/util' -type getContractStorage = (address: Address, key: Uint8Array) => Promise +type getStorage = (address: Address, key: Uint8Array) => Promise /** * Helper class to cache original storage values (so values already being present in * the pre-state of a call), mainly for correct gas cost calculation in EVM/VM. * - * TODO: Usage of this class is very implicit through the injected `getContractStorage()` + * TODO: Usage of this class is very implicit through the injected `getStorage()` * method bound to the calling state manager. It should be examined if there are alternative * designs being more transparent and direct along the next breaking release round. * */ export class OriginalStorageCache { private map: Map> - private getContractStorage: getContractStorage - constructor(getContractStorage: getContractStorage) { + private getStorage: getStorage + constructor(getStorage: getStorage) { this.map = new Map() - this.getContractStorage = getContractStorage + this.getStorage = getStorage } async get(address: Address, key: Uint8Array): Promise { @@ -31,7 +31,7 @@ export class OriginalStorageCache { return value } } - const value = await this.getContractStorage(address, key) + const value = await this.getStorage(address, key) this.put(address, key, value) return value } diff --git a/packages/statemanager/src/cache/storage.ts b/packages/statemanager/src/cache/storage.ts index cf24607219..345e74ad3b 100644 --- a/packages/statemanager/src/cache/storage.ts +++ b/packages/statemanager/src/cache/storage.ts @@ -172,7 +172,7 @@ export class StorageCache extends Cache { * Deletes all storage slots for address from the cache * @param address */ - clearContractStorage(address: Address): void { + clearStorage(address: Address): void { const addressHex = bytesToUnprefixedHex(address.bytes) if (this._lruCache) { this._lruCache!.set(addressHex, new Map()) diff --git a/packages/statemanager/src/index.ts b/packages/statemanager/src/index.ts index d6d3d9c56a..b0059ad440 100644 --- a/packages/statemanager/src/index.ts +++ b/packages/statemanager/src/index.ts @@ -4,3 +4,4 @@ export * from './rpcStateManager.js' export * from './simpleStateManager.js' export * from './statelessVerkleStateManager.js' export * from './stateManager.js' +export * from './types.js' diff --git a/packages/statemanager/src/rpcStateManager.ts b/packages/statemanager/src/rpcStateManager.ts index 3c4f9570d5..71ef9d06ef 100644 --- a/packages/statemanager/src/rpcStateManager.ts +++ b/packages/statemanager/src/rpcStateManager.ts @@ -18,7 +18,7 @@ import { keccak256 } from 'ethereum-cryptography/keccak.js' import { AccountCache, CacheType, OriginalStorageCache, StorageCache } from './cache/index.js' -import type { Proof } from './index.js' +import type { Proof, RPCStateManagerOpts } from './index.js' import type { AccountFields, EVMStateManagerInterface, @@ -28,16 +28,6 @@ import type { import type { Address, PrefixedHexString } from '@ethereumjs/util' import type { Debugger } from 'debug' -export interface RPCStateManagerOpts { - provider: string - blockTag: bigint | 'earliest' - - /** - * The common to use - */ - common?: Common -} - const KECCAK256_RLP_EMPTY_ACCOUNT = RLP.encode(new Account().serialize()).slice(2) export class RPCStateManager implements EVMStateManagerInterface { @@ -71,7 +61,7 @@ export class RPCStateManager implements EVMStateManagerInterface { this._storageCache = new StorageCache({ size: 100000, type: CacheType.ORDERED_MAP }) this._accountCache = new AccountCache({ size: 100000, type: CacheType.ORDERED_MAP }) - this.originalStorageCache = new OriginalStorageCache(this.getContractStorage.bind(this)) + this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this)) this.common = opts.common ?? new Common({ chain: Chain.Mainnet }) this.keccakFunction = opts.common?.customCrypto.keccak256 ?? keccak256 } @@ -125,7 +115,7 @@ export class RPCStateManager implements EVMStateManagerInterface { * @returns {Promise} - Resolves with the code corresponding to the provided address. * Returns an empty `Uint8Array` if the account has no associated code. */ - async getContractCode(address: Address): Promise { + async getCode(address: Address): Promise { let codeBytes = this._contractCache.get(address.toString()) if (codeBytes !== undefined) return codeBytes const code = await fetchFromProvider(this._provider, { @@ -137,8 +127,8 @@ export class RPCStateManager implements EVMStateManagerInterface { return codeBytes } - async getContractCodeSize(address: Address): Promise { - const contractCode = await this.getContractCode(address) + async getCodeSize(address: Address): Promise { + const contractCode = await this.getCode(address) return contractCode.length } @@ -148,7 +138,7 @@ export class RPCStateManager implements EVMStateManagerInterface { * @param address - Address of the `account` to add the `code` for * @param value - The value of the `code` */ - async putContractCode(address: Address, value: Uint8Array): Promise { + async putCode(address: Address, value: Uint8Array): Promise { // Store contract code in the cache this._contractCache.set(address.toString(), value) } @@ -162,7 +152,7 @@ export class RPCStateManager implements EVMStateManagerInterface { * corresponding to the provided address at the provided key. * If this does not exist an empty `Uint8Array` is returned. */ - async getContractStorage(address: Address, key: Uint8Array): Promise { + async getStorage(address: Address, key: Uint8Array): Promise { // Check storage slot in cache if (key.length !== 32) { throw new Error('Storage key must be 32 bytes long') @@ -180,7 +170,7 @@ export class RPCStateManager implements EVMStateManagerInterface { }) value = toBytes(storage) - await this.putContractStorage(address, key, value) + await this.putStorage(address, key, value) return value } @@ -193,7 +183,7 @@ export class RPCStateManager implements EVMStateManagerInterface { * Cannot be more than 32 bytes. Leading zeros are stripped. * If it is empty or filled with zeros, deletes the value. */ - async putContractStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { + async putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { this._storageCache.put(address, key, value) } @@ -201,8 +191,8 @@ export class RPCStateManager implements EVMStateManagerInterface { * Clears all storage entries for the account corresponding to `address`. * @param address - Address to clear the storage of */ - async clearContractStorage(address: Address): Promise { - this._storageCache.clearContractStorage(address) + async clearStorage(address: Address): Promise { + this._storageCache.clearStorage(address) } /** diff --git a/packages/statemanager/src/simpleStateManager.ts b/packages/statemanager/src/simpleStateManager.ts index f9976f3f71..8169bbb1b5 100644 --- a/packages/statemanager/src/simpleStateManager.ts +++ b/packages/statemanager/src/simpleStateManager.ts @@ -3,6 +3,7 @@ import { keccak256 } from 'ethereum-cryptography/keccak.js' import { OriginalStorageCache } from './cache/originalStorageCache.js' +import type { SimpleStateManagerOpts } from './index.js' import type { AccountFields, Common, @@ -13,16 +14,6 @@ import type { } from '@ethereumjs/common' import type { Address, PrefixedHexString } from '@ethereumjs/util' -/** - * Options for constructing a {@link SimpleStateManager}. - */ -export interface SimpleStateManagerOpts { - /** - * The common to use - */ - common?: Common -} - /** * Simple and dependency-free state manager for basic state access use cases * where a merkle-patricia or verkle tree backed state manager is too heavy-weight. @@ -52,7 +43,7 @@ export class SimpleStateManager implements EVMStateManagerInterface { constructor(opts: SimpleStateManagerOpts = {}) { this.checkpointSync() - this.originalStorageCache = new OriginalStorageCache(this.getContractStorage.bind(this)) + this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this)) this.common = opts.common } @@ -105,11 +96,11 @@ export class SimpleStateManager implements EVMStateManagerInterface { await this.putAccount(address, account) } - async getContractCode(address: Address): Promise { + async getCode(address: Address): Promise { return this.topCodeStack().get(address.toString()) ?? new Uint8Array(0) } - async putContractCode(address: Address, value: Uint8Array): Promise { + async putCode(address: Address, value: Uint8Array): Promise { this.topCodeStack().set(address.toString(), value) if ((await this.getAccount(address)) === undefined) { await this.putAccount(address, new Account()) @@ -119,18 +110,18 @@ export class SimpleStateManager implements EVMStateManagerInterface { }) } - async getContractCodeSize(address: Address): Promise { - const contractCode = await this.getContractCode(address) + async getCodeSize(address: Address): Promise { + const contractCode = await this.getCode(address) return contractCode.length } - async getContractStorage(address: Address, key: Uint8Array): Promise { + async getStorage(address: Address, key: Uint8Array): Promise { return ( this.topStorageStack().get(`${address.toString()}_${bytesToHex(key)}`) ?? new Uint8Array(0) ) } - async putContractStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { + async putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { this.topStorageStack().set(`${address.toString()}_${bytesToHex(key)}`, value) } @@ -174,12 +165,9 @@ export class SimpleStateManager implements EVMStateManagerInterface { } // Only goes for long term create situations, skip - async clearContractStorage(): Promise {} + async clearStorage(): Promise {} // Only "core" methods implemented - checkChunkWitnessPresent?(): Promise { - throw new Error('Method not implemented.') - } dumpStorage(): Promise { throw new Error('Method not implemented.') } diff --git a/packages/statemanager/src/stateManager.ts b/packages/statemanager/src/stateManager.ts index df1a53484d..df452b4558 100644 --- a/packages/statemanager/src/stateManager.ts +++ b/packages/statemanager/src/stateManager.ts @@ -21,7 +21,6 @@ import { toBytes, unpadBytes, unprefixedHexToBytes, - utf8ToBytes, } from '@ethereumjs/util' import debugDefault from 'debug' import { keccak256 } from 'ethereum-cryptography/keccak.js' @@ -34,124 +33,19 @@ import { StorageCache, } from './cache/index.js' +import { CODEHASH_PREFIX, type CacheSettings, type DefaultStateManagerOpts } from './index.js' + +import type { StorageProof } from './index.js' import type { AccountFields, EVMStateManagerInterface, + Proof, StorageDump, StorageRange, } from '@ethereumjs/common' import type { DB, PrefixedHexString } from '@ethereumjs/util' import type { Debugger } from 'debug' -export type StorageProof = { - key: PrefixedHexString - proof: PrefixedHexString[] - value: PrefixedHexString -} - -export type Proof = { - address: PrefixedHexString - balance: PrefixedHexString - codeHash: PrefixedHexString - nonce: PrefixedHexString - storageHash: PrefixedHexString - accountProof: PrefixedHexString[] - storageProof: StorageProof[] -} - -type CacheOptions = { - /** - * Allows for cache deactivation - * - * Depending on the use case and underlying datastore (and eventual concurrent cache - * mechanisms there), usage with or without cache can be faster - * - * Default: false - */ - deactivate?: boolean - - /** - * Cache type to use. - * - * Available options: - * - * ORDERED_MAP: Cache with no fixed upper bound and dynamic allocation, - * use for dynamic setups like testing or similar. - * - * LRU: LRU cache with pre-allocation of memory and a fixed size. - * Use for larger and more persistent caches. - */ - type?: CacheType - - /** - * Size of the cache (only for LRU cache) - * - * Default: 100000 (account cache) / 20000 (storage cache) / 20000 (code cache) - * - * Note: the cache/trie interplay mechanism is designed in a way that - * the theoretical number of max modified accounts between two flush operations - * should be smaller than the cache size, otherwise the cache will "forget" the - * old modifications resulting in an incomplete set of trie-flushed accounts. - */ - size?: number -} - -type CacheSettings = { - deactivate: boolean - type: CacheType - size: number -} - -/** - * Prefix to distinguish between a contract deployed with code `0x80` - * and `RLP([])` (also having the value `0x80`). - * - * Otherwise the creation of the code hash for the `0x80` contract - * will be the same as the hash of the empty trie which leads to - * misbehaviour in the underlying trie library. - */ -export const CODEHASH_PREFIX = utf8ToBytes('c') - -/** - * Options for constructing a {@link StateManager}. - */ -export interface DefaultStateManagerOpts { - /** - * A {@link Trie} instance - */ - trie?: Trie - /** - * Option to prefix codehashes in the database. This defaults to `true`. - * If this is disabled, note that it is possible to corrupt the trie, by deploying code - * which code is equal to the preimage of a trie-node. - * E.g. by putting the code `0x80` into the empty trie, will lead to a corrupted trie. - */ - prefixCodeHashes?: boolean - - /** - * Option to prefix the keys for the storage tries with the first 7 bytes from the - * associated account address. Activating this option gives a noticeable performance - * boost for storage DB reads when operating on larger tries. - * - * Note: Activating/deactivating this option causes continued state reads to be - * incompatible with existing databases. - * - * Default: false (for backwards compatibility reasons) - */ - prefixStorageTrieKeys?: boolean - - accountCacheOpts?: CacheOptions - - storageCacheOpts?: CacheOptions - - codeCacheOpts?: CacheOptions - - /** - * The common to use - */ - common?: Common -} - /** * Default StateManager implementation for the VM. * @@ -220,7 +114,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { this.keccakFunction = opts.common?.customCrypto.keccak256 ?? keccak256 - this.originalStorageCache = new OriginalStorageCache(this.getContractStorage.bind(this)) + this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this)) this._prefixCodeHashes = opts.prefixCodeHashes ?? true this._prefixStorageTrieKeys = opts.prefixStorageTrieKeys ?? false @@ -352,7 +246,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { this._accountCache!.del(address) } if (!this._storageCacheSettings.deactivate) { - this._storageCache?.clearContractStorage(address) + this._storageCache?.clearStorage(address) } } @@ -362,7 +256,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { * @param address - Address of the `account` to add the `code` for * @param value - The value of the `code` */ - async putContractCode(address: Address, value: Uint8Array): Promise { + async putCode(address: Address, value: Uint8Array): Promise { this._codeCache?.put(address, value) const codeHash = this.keccakFunction(value) @@ -382,7 +276,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { * @returns {Promise} - Resolves with the code corresponding to the provided address. * Returns an empty `Uint8Array` if the account has no associated code. */ - async getContractCode(address: Address): Promise { + async getCode(address: Address): Promise { if (!this._codeCacheSettings.deactivate) { const elem = this._codeCache?.get(address) if (elem !== undefined) { @@ -407,8 +301,8 @@ export class DefaultStateManager implements EVMStateManagerInterface { return code } - async getContractCodeSize(address: Address): Promise { - const contractCode = await this.getContractCode(address) + async getCodeSize(address: Address): Promise { + const contractCode = await this.getCode(address) return contractCode.length } @@ -479,7 +373,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { * corresponding to the provided address at the provided key. * If this does not exist an empty `Uint8Array` is returned. */ - async getContractStorage(address: Address, key: Uint8Array): Promise { + async getStorage(address: Address, key: Uint8Array): Promise { if (key.length !== 32) { throw new Error('Storage key must be 32 bytes long') } @@ -566,7 +460,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { * Cannot be more than 32 bytes. Leading zeros are stripped. * If it is a empty or filled with zeros, deletes the value. */ - async putContractStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { + async putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { if (key.length !== 32) { throw new Error('Storage key must be 32 bytes long') } @@ -577,7 +471,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { const account = await this.getAccount(address) if (!account) { - throw new Error('putContractStorage() called on non-existing account') + throw new Error('putStorage() called on non-existing account') } value = unpadBytes(value) @@ -593,12 +487,12 @@ export class DefaultStateManager implements EVMStateManagerInterface { * Clears all storage entries for the account corresponding to `address`. * @param address - Address to clear the storage of */ - async clearContractStorage(address: Address): Promise { + async clearStorage(address: Address): Promise { let account = await this.getAccount(address) if (!account) { account = new Account() } - this._storageCache?.clearContractStorage(address) + this._storageCache?.clearStorage(address) await this._modifyContractStorage(address, account, (storageTrie, done) => { storageTrie.root(storageTrie.EMPTY_TRIE_ROOT) done() @@ -747,7 +641,7 @@ export class DefaultStateManager implements EVMStateManagerInterface { for (const storageKey of storageSlots) { const proof = (await storageTrie.createProof(storageKey)).map((p) => bytesToHex(p)) - const value = bytesToHex(await this.getContractStorage(address, storageKey)) + const value = bytesToHex(await this.getStorage(address, storageKey)) const proofItem: StorageProof = { key: bytesToHex(storageKey), value: value === '0x' ? '0x0' : value, // Return '0x' values as '0x0' since this is a JSON RPC response @@ -1057,11 +951,11 @@ export class DefaultStateManager implements EVMStateManagerInterface { const account = createAccount({ balance, nonce }) await this.putAccount(addr, account) if (code !== undefined) { - await this.putContractCode(addr, toBytes(code)) + await this.putCode(addr, toBytes(code)) } if (storage !== undefined) { for (const [key, value] of storage) { - await this.putContractStorage(addr, toBytes(key), toBytes(value)) + await this.putStorage(addr, toBytes(key), toBytes(value)) } } } diff --git a/packages/statemanager/src/statelessVerkleStateManager.ts b/packages/statemanager/src/statelessVerkleStateManager.ts index 498842f074..589d868505 100644 --- a/packages/statemanager/src/statelessVerkleStateManager.ts +++ b/packages/statemanager/src/statelessVerkleStateManager.ts @@ -34,10 +34,10 @@ import { } from './cache/index.js' import type { AccessedStateWithAddress } from './accessWitness.js' +import type { CacheSettings, StatelessVerkleStateManagerOpts, VerkleState } from './index.js' import type { DefaultStateManager } from './stateManager.js' import type { AccountFields, - Common, EVMStateManagerInterface, Proof, StorageDump, @@ -53,73 +53,6 @@ import type { const debug = debugDefault('statemanager:verkle') -export interface VerkleState { - [key: PrefixedHexString]: PrefixedHexString | null -} - -export interface EncodedVerkleProof { - [key: PrefixedHexString]: PrefixedHexString -} - -type CacheOptions = { - /** - * Allows for cache deactivation - * - * Depending on the use case and underlying datastore (and eventual concurrent cache - * mechanisms there), usage with or without cache can be faster - * - * Default: false - */ - deactivate?: boolean - - /** - * Cache type to use. - * - * Available options: - * - * ORDERED_MAP: Cache with no fixed upper bound and dynamic allocation, - * use for dynamic setups like testing or similar. - * - * LRU: LRU cache with pre-allocation of memory and a fixed size. - * Use for larger and more persistent caches. - */ - type?: CacheType - - /** - * Size of the cache (only for LRU cache) - * - * Default: 100000 (account cache) / 20000 (storage cache) - * - * Note: the cache/trie interplay mechanism is designed in a way that - * the theoretical number of max modified accounts between two flush operations - * should be smaller than the cache size, otherwise the cache will "forget" the - * old modifications resulting in an incomplete set of trie-flushed accounts. - */ - size?: number -} - -type CacheSettings = { - deactivate: boolean - type: CacheType - size: number -} - -/** - * Options dictionary. - */ -export interface StatelessVerkleStateManagerOpts { - /** - * The common to use - */ - common?: Common - accountCacheOpts?: CacheOptions - storageCacheOpts?: CacheOptions - codeCacheOpts?: CacheOptions - accesses?: AccessWitness - verkleCrypto: VerkleCrypto - initialStateRoot?: Uint8Array -} - const PUSH_OFFSET = 95 // eslint-disable-next-line @typescript-eslint/no-unused-vars const PUSH1 = PUSH_OFFSET + 1 @@ -185,7 +118,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { * Instantiate the StateManager interface. */ constructor(opts: StatelessVerkleStateManagerOpts) { - this.originalStorageCache = new OriginalStorageCache(this.getContractStorage.bind(this)) + this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this)) this._accountCacheSettings = { deactivate: opts.accountCacheOpts?.deactivate ?? false, @@ -337,9 +270,9 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { * @param address - Address of the `account` to add the `code` for * @param value - The value of the `code` */ - async putContractCode(address: Address, value: Uint8Array): Promise { + async putCode(address: Address, value: Uint8Array): Promise { if (this.DEBUG) { - debug(`putContractCode address=${address.toString()} value=${short(value)}`) + debug(`putCode address=${address.toString()} value=${short(value)}`) } this._codeCache?.put(address, value) @@ -361,9 +294,9 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { * @returns {Promise} - Resolves with the code corresponding to the provided address. * Returns an empty `Uint8Array` if the account has no associated code. */ - async getContractCode(address: Address): Promise { + async getCode(address: Address): Promise { if (this.DEBUG) { - debug(`getContractCode address=${address.toString()}`) + debug(`getCode address=${address.toString()}`) } if (!this._codeCacheSettings.deactivate) { @@ -420,7 +353,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { return contactCode } - async getContractCodeSize(address: Address): Promise { + async getCodeSize(address: Address): Promise { if (!this._accountCacheSettings.deactivate) { const elem = this._accountCache!.get(address) if (elem !== undefined) { @@ -452,7 +385,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { * corresponding to the provided address at the provided key. * If this does not exist an empty `Uint8Array` is returned. */ - async getContractStorage(address: Address, key: Uint8Array): Promise { + async getStorage(address: Address, key: Uint8Array): Promise { if (!this._storageCacheSettings.deactivate) { const value = this._storageCache!.get(address, key) if (value !== undefined) { @@ -481,7 +414,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { * @param key - Key to set the value at. Must be 32 bytes long. * @param value - Value to set at `key` for account corresponding to `address`. Cannot be more than 32 bytes. Leading zeros are stripped. If it is a empty or filled with zeros, deletes the value. */ - async putContractStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { + async putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise { if (!this._storageCacheSettings.deactivate) { this._storageCache!.put(address, key, value) } else { @@ -501,10 +434,10 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { * Clears all storage entries for the account corresponding to `address`. * @param address - Address to clear the storage of */ - async clearContractStorage(address: Address): Promise { + async clearStorage(address: Address): Promise { const stem = getVerkleStem(this.verkleCrypto, address, 0) const codeHashKey = getVerkleKey(stem, VerkleLeafType.CodeHash) - this._storageCache?.clearContractStorage(address) + this._storageCache?.clearStorage(address) // Update codeHash to `c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470` this._state[bytesToHex(codeHashKey)] = KECCAK256_NULL_S } @@ -639,7 +572,7 @@ export class StatelessVerkleStateManager implements EVMStateManagerInterface { this._accountCache!.del(address) if (!this._storageCacheSettings.deactivate) { - this._storageCache?.clearContractStorage(address) + this._storageCache?.clearStorage(address) } } diff --git a/packages/statemanager/src/types.ts b/packages/statemanager/src/types.ts new file mode 100644 index 0000000000..da35fe4a85 --- /dev/null +++ b/packages/statemanager/src/types.ts @@ -0,0 +1,155 @@ +import { type PrefixedHexString, utf8ToBytes } from '@ethereumjs/util' + +import type { CacheType } from './cache/index.js' +import type { AccessWitness } from './index.js' +import type { Common } from '@ethereumjs/common' +import type { Trie } from '@ethereumjs/trie' +import type { VerkleCrypto } from '@ethereumjs/util' + +type CacheOptions = { + /** + * Allows for cache deactivation + * + * Depending on the use case and underlying datastore (and eventual concurrent cache + * mechanisms there), usage with or without cache can be faster + * + * Default: false + */ + deactivate?: boolean + + /** + * Cache type to use. + * + * Available options: + * + * ORDERED_MAP: Cache with no fixed upper bound and dynamic allocation, + * use for dynamic setups like testing or similar. + * + * LRU: LRU cache with pre-allocation of memory and a fixed size. + * Use for larger and more persistent caches. + */ + type?: CacheType + + /** + * Size of the cache (only for LRU cache) + * + * Default: 100000 (account cache) / 20000 (storage cache) / 20000 (code cache) + * + * Note: the cache/trie interplay mechanism is designed in a way that + * the theoretical number of max modified accounts between two flush operations + * should be smaller than the cache size, otherwise the cache will "forget" the + * old modifications resulting in an incomplete set of trie-flushed accounts. + */ + size?: number +} + +export type CacheSettings = { + deactivate: boolean + type: CacheType + size: number +} + +/** + * Basic state manager options (not to be used directly) + */ +interface BaseStateManagerOpts { + /** + * The common to use + */ + common?: Common +} + +/** + * Cache state manager options (not to be used directly) + */ +interface CacheStateManagerOpts { + accountCacheOpts?: CacheOptions + storageCacheOpts?: CacheOptions + codeCacheOpts?: CacheOptions +} + +/** + * Options for constructing a {@link SimpleStateManager}. + */ +export interface SimpleStateManagerOpts extends BaseStateManagerOpts { + // Keep this as an alias so that it might be able to extend in the future +} + +export interface RPCStateManagerOpts extends BaseStateManagerOpts { + provider: string + blockTag: bigint | 'earliest' +} + +/** + * Options for constructing a {@link StateManager}. + */ +export interface DefaultStateManagerOpts extends BaseStateManagerOpts, CacheStateManagerOpts { + /** + * A {@link Trie} instance + */ + trie?: Trie + /** + * Option to prefix codehashes in the database. This defaults to `true`. + * If this is disabled, note that it is possible to corrupt the trie, by deploying code + * which code is equal to the preimage of a trie-node. + * E.g. by putting the code `0x80` into the empty trie, will lead to a corrupted trie. + */ + prefixCodeHashes?: boolean + + /** + * Option to prefix the keys for the storage tries with the first 7 bytes from the + * associated account address. Activating this option gives a noticeable performance + * boost for storage DB reads when operating on larger tries. + * + * Note: Activating/deactivating this option causes continued state reads to be + * incompatible with existing databases. + * + * Default: false (for backwards compatibility reasons) + */ + prefixStorageTrieKeys?: boolean +} + +/** + * Options dictionary. + */ +export interface StatelessVerkleStateManagerOpts + extends BaseStateManagerOpts, + CacheStateManagerOpts { + accesses?: AccessWitness + verkleCrypto: VerkleCrypto + initialStateRoot?: Uint8Array +} + +export interface VerkleState { + [key: PrefixedHexString]: PrefixedHexString | null +} + +export interface EncodedVerkleProof { + [key: PrefixedHexString]: PrefixedHexString +} + +/** + * Prefix to distinguish between a contract deployed with code `0x80` + * and `RLP([])` (also having the value `0x80`). + * + * Otherwise the creation of the code hash for the `0x80` contract + * will be the same as the hash of the empty trie which leads to + * misbehaviour in the underlying trie library. + */ +export const CODEHASH_PREFIX = utf8ToBytes('c') + +export type StorageProof = { + key: PrefixedHexString + proof: PrefixedHexString[] + value: PrefixedHexString +} + +export type Proof = { + address: PrefixedHexString + balance: PrefixedHexString + codeHash: PrefixedHexString + nonce: PrefixedHexString + storageHash: PrefixedHexString + accountProof: PrefixedHexString[] + storageProof: StorageProof[] +} diff --git a/packages/statemanager/test/checkpointing.code.spec.ts b/packages/statemanager/test/checkpointing.code.spec.ts index ddda37eb50..12e8815dd4 100644 --- a/packages/statemanager/test/checkpointing.code.spec.ts +++ b/packages/statemanager/test/checkpointing.code.spec.ts @@ -10,7 +10,7 @@ const codeEval = async ( value: Uint8Array, root: Uint8Array, ) => { - assert.deepEqual(await sm.getContractCode(address), value, 'contract code value should be equal') + assert.deepEqual(await sm.getCode(address), value, 'contract code value should be equal') const accountCMP = await sm.getAccount(address) assert.deepEqual(accountCMP!.codeHash, root, 'account code root should be equal') } @@ -96,13 +96,13 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.flush() await codeEval(sm, address, c.c1.value, c.c1.root) sm.clearCaches() - assert.deepEqual(await sm.getContractCode(address), c.c1.value) + assert.deepEqual(await sm.getCode(address), c.c1.value) await codeEval(sm, address, c.c1.value, c.c1.root) }) @@ -111,7 +111,7 @@ describe('StateManager -> Code Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.commit() await sm.flush() await codeEval(sm, address, c.c1.value, c.c1.root) @@ -125,7 +125,7 @@ describe('StateManager -> Code Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.revert() await sm.flush() @@ -140,7 +140,7 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() await sm.commit() await sm.flush() @@ -154,7 +154,7 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() await sm.revert() await sm.flush() @@ -168,9 +168,9 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.commit() await sm.flush() await codeEval(sm, address, c.c2.value, c.c2.root) @@ -183,11 +183,11 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.commit() - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c3.value) await sm.flush() await codeEval(sm, address, c.c3.value, c.c3.root) @@ -199,10 +199,10 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c2.value) + await sm.putCode(address, c.c3.value) await sm.commit() await sm.flush() await codeEval(sm, address, c.c3.value, c.c3.root) @@ -216,8 +216,8 @@ describe('StateManager -> Code Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractCode(address, c.c1.value) - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c1.value) + await sm.putCode(address, c.c2.value) await sm.commit() await sm.flush() await codeEval(sm, address, c.c2.value, c.c2.root) @@ -231,9 +231,9 @@ describe('StateManager -> Code Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.revert() await sm.flush() await codeEval(sm, address, valueEmpty, rootEmpty) @@ -246,9 +246,9 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.revert() await sm.flush() await codeEval(sm, address, c.c1.value, c.c1.root) @@ -261,11 +261,11 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.checkpoint() - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c3.value) await sm.commit() await sm.commit() await sm.flush() @@ -279,11 +279,11 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.checkpoint() - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c3.value) await sm.commit() await sm.revert() await sm.flush() @@ -297,11 +297,11 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.checkpoint() - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c3.value) await sm.revert() await sm.commit() await sm.flush() @@ -315,13 +315,13 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.checkpoint() - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c3.value) await sm.revert() - await sm.putContractCode(address, c.c4.value) + await sm.putCode(address, c.c4.value) await sm.commit() await sm.flush() await codeEval(sm, address, c.c4.value, c.c4.root) @@ -334,15 +334,15 @@ describe('StateManager -> Code Checkpointing', () => { const sm = new SM() await sm.putAccount(address, new Account()) - await sm.putContractCode(address, c.c1.value) + await sm.putCode(address, c.c1.value) await sm.checkpoint() - await sm.putContractCode(address, c.c2.value) + await sm.putCode(address, c.c2.value) await sm.checkpoint() - await sm.putContractCode(address, c.c3.value) + await sm.putCode(address, c.c3.value) await sm.revert() - await sm.putContractCode(address, c.c4.value) + await sm.putCode(address, c.c4.value) await sm.checkpoint() - await sm.putContractCode(address, c.c5.value) + await sm.putCode(address, c.c5.value) await sm.commit() await sm.commit() await sm.flush() diff --git a/packages/statemanager/test/checkpointing.storage.spec.ts b/packages/statemanager/test/checkpointing.storage.spec.ts index 21a8613746..cd8fbb999c 100644 --- a/packages/statemanager/test/checkpointing.storage.spec.ts +++ b/packages/statemanager/test/checkpointing.storage.spec.ts @@ -13,11 +13,7 @@ const storageEval = async ( root: Uint8Array, rootCheck = true, ) => { - assert.deepEqual( - await sm.getContractStorage(address, key), - value, - 'storage value should be equal', - ) + assert.deepEqual(await sm.getStorage(address, key), value, 'storage value should be equal') if (rootCheck) { const accountCMP = await sm.getAccount(address) assert.deepEqual(accountCMP!.storageRoot, root, 'account storage root should be equal') @@ -116,12 +112,12 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.flush() await storageEval(sm, address, key, s.s1.value, s.s1.root, SMDict.rootCheck) sm.clearCaches() - assert.deepEqual(await sm.getContractStorage(address, key), s.s1.value) + assert.deepEqual(await sm.getStorage(address, key), s.s1.value) await storageEval(sm, address, key, s.s1.value, s.s1.root, SMDict.rootCheck) }) @@ -130,7 +126,7 @@ describe('StateManager -> Storage Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.commit() await sm.flush() await storageEval(sm, address, key, s.s1.value, s.s1.root, SMDict.rootCheck) @@ -144,7 +140,7 @@ describe('StateManager -> Storage Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.revert() await sm.flush() @@ -159,7 +155,7 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() await sm.commit() await sm.flush() @@ -173,7 +169,7 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() await sm.revert() await sm.flush() @@ -187,9 +183,9 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.commit() await sm.flush() await storageEval(sm, address, key, s.s2.value, s.s2.root, SMDict.rootCheck) @@ -202,11 +198,11 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.commit() - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s3.value) await sm.flush() await storageEval(sm, address, key, s.s3.value, s.s3.root, SMDict.rootCheck) @@ -218,10 +214,10 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s3.value) await sm.commit() await sm.flush() await storageEval(sm, address, key, s.s3.value, s.s3.root, SMDict.rootCheck) @@ -235,8 +231,8 @@ describe('StateManager -> Storage Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s1.value) - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s2.value) await sm.commit() await sm.flush() await storageEval(sm, address, key, s.s2.value, s.s2.root, SMDict.rootCheck) @@ -250,9 +246,9 @@ describe('StateManager -> Storage Checkpointing', () => { await sm.putAccount(address, new Account()) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.revert() await sm.flush() await storageEval(sm, address, key, valueEmpty, rootEmpty) @@ -265,9 +261,9 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.revert() await sm.flush() await storageEval(sm, address, key, s.s1.value, s.s1.root, SMDict.rootCheck) @@ -280,11 +276,11 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s3.value) await sm.commit() await sm.commit() await sm.flush() @@ -298,11 +294,11 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s3.value) await sm.commit() await sm.revert() await sm.flush() @@ -316,11 +312,11 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s3.value) await sm.revert() await sm.commit() await sm.flush() @@ -334,13 +330,13 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s3.value) await sm.revert() - await sm.putContractStorage(address, key, s.s4.value) + await sm.putStorage(address, key, s.s4.value) await sm.commit() await sm.flush() await storageEval(sm, address, key, s.s4.value, s.s4.root, SMDict.rootCheck) @@ -353,15 +349,15 @@ describe('StateManager -> Storage Checkpointing', () => { const sm = new SMDict.SM() await sm.putAccount(address, new Account()) - await sm.putContractStorage(address, key, s.s1.value) + await sm.putStorage(address, key, s.s1.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s2.value) + await sm.putStorage(address, key, s.s2.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s3.value) + await sm.putStorage(address, key, s.s3.value) await sm.revert() - await sm.putContractStorage(address, key, s.s4.value) + await sm.putStorage(address, key, s.s4.value) await sm.checkpoint() - await sm.putContractStorage(address, key, s.s5.value) + await sm.putStorage(address, key, s.s5.value) await sm.commit() await sm.commit() await sm.flush() diff --git a/packages/statemanager/test/proofStateManager.spec.ts b/packages/statemanager/test/proofStateManager.spec.ts index 9bc74fdd86..41c02a7333 100644 --- a/packages/statemanager/test/proofStateManager.spec.ts +++ b/packages/statemanager/test/proofStateManager.spec.ts @@ -40,7 +40,7 @@ describe('ProofStateManager', () => { await stateManager.putAccount(address, new Account(BigInt(100), BigInt(200))) const storageRoot = (await stateManager.getAccount(address))!.storageRoot - await stateManager.putContractStorage(address, key, new Uint8Array([10])) + await stateManager.putStorage(address, key, new Uint8Array([10])) const proof = await stateManager.getProof(address, [key]) assert.ok(!equalsBytes(hexToBytes(proof.storageHash), storageRoot)) @@ -82,8 +82,8 @@ describe('ProofStateManager', () => { const stateManager = new DefaultStateManager() await stateManager.checkpoint() await stateManager.putAccount(address, new Account()) - await stateManager.putContractStorage(address, key, value) - await stateManager.putContractCode(address, code) + await stateManager.putStorage(address, key, value) + await stateManager.putCode(address, code) const account = await stateManager.getAccount(address) account!.balance = BigInt(1) account!.nonce = BigInt(2) diff --git a/packages/statemanager/test/rpcStateManager.spec.ts b/packages/statemanager/test/rpcStateManager.spec.ts index 1e425abbe8..d4a4f1f03f 100644 --- a/packages/statemanager/test/rpcStateManager.spec.ts +++ b/packages/statemanager/test/rpcStateManager.spec.ts @@ -98,31 +98,31 @@ describe('RPC State Manager API tests', () => { assert.ok(state.getAccount(vitalikDotEth) !== undefined, 'vitalik.eth does exist') const UNIerc20ContractAddress = Address.fromString('0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984') - const UNIContractCode = await state.getContractCode(UNIerc20ContractAddress) + const UNIContractCode = await state.getCode(UNIerc20ContractAddress) assert.ok(UNIContractCode.length > 0, 'was able to retrieve UNI contract code') - await state.putContractCode(UNIerc20ContractAddress, UNIContractCode) + await state.putCode(UNIerc20ContractAddress, UNIContractCode) assert.ok( typeof (state as any)._contractCache.get(UNIerc20ContractAddress.toString()) !== 'undefined', 'UNI ERC20 contract code was found in cache', ) - const storageSlot = await state.getContractStorage( + const storageSlot = await state.getStorage( UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(1n), 32), ) assert.ok(storageSlot.length > 0, 'was able to retrieve storage slot 1 for the UNI contract') await expect(async () => { - await state.getContractStorage(UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(1n), 31)) + await state.getStorage(UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(1n), 31)) }).rejects.toThrowError('Storage key must be 32 bytes long') - await state.putContractStorage( + await state.putStorage( UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(2n), 32), utf8ToBytes('abcd'), ) - const slotValue = await state.getContractStorage( + const slotValue = await state.getStorage( UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(2n), 32), ) @@ -141,7 +141,7 @@ describe('RPC State Manager API tests', () => { await state.checkpoint() - await state.putContractStorage( + await state.putStorage( UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(2n), 32), new Uint8Array(0), @@ -165,7 +165,7 @@ describe('RPC State Manager API tests', () => { assert.ok(true, 'calls getAccountFromProvider for non-cached account') } - const deletedSlot = await state.getContractStorage( + const deletedSlot = await state.getStorage( UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(2n), 32), ) @@ -184,7 +184,7 @@ describe('RPC State Manager API tests', () => { 'account deleted since last checkpoint should exist after revert called', ) - const deletedSlotAfterRevert = await state.getContractStorage( + const deletedSlotAfterRevert = await state.getStorage( UNIerc20ContractAddress, setLengthLeft(bigIntToBytes(2n), 32), ) @@ -201,7 +201,7 @@ describe('RPC State Manager API tests', () => { Object.keys(cacheStorage).length, 'should have 2 storage slots in cache before clear', ) - await state.clearContractStorage(UNIerc20ContractAddress) + await state.clearStorage(UNIerc20ContractAddress) const clearedStorage = await state.dumpStorage(UNIerc20ContractAddress) assert.deepEqual({}, clearedStorage, 'storage cache should be empty after clear') @@ -328,7 +328,7 @@ describe('blockchain', () => hexToBytes('0xf8506f559699a58a4724df4fcf2ad4fd242d20324db541823f128f5974feb6c7'), ) const block = await createBlockFromJsonRpcProvider(provider, 500000n, { setHardfork: true }) - await evm.stateManager.putContractCode(contractAddress, hexToBytes(code)) + await evm.stateManager.putCode(contractAddress, hexToBytes(code)) const runCallArgs: Partial = { caller, gasLimit: BigInt(0xffffffffff), diff --git a/packages/statemanager/test/stateManager.account.spec.ts b/packages/statemanager/test/stateManager.account.spec.ts index 9b4b01989a..ef97c11ce7 100644 --- a/packages/statemanager/test/stateManager.account.spec.ts +++ b/packages/statemanager/test/stateManager.account.spec.ts @@ -57,9 +57,9 @@ describe('StateManager -> General/Account', () => { const key = hexToBytes('0x1234567890123456789012345678901234567890123456789012345678901234') const value = hexToBytes('0x1234') await stateManager.putAccount(address, account) - await stateManager.putContractStorage(address, key, value) + await stateManager.putStorage(address, key, value) - const contract0 = await stateManager.getContractStorage(address, key) + const contract0 = await stateManager.getStorage(address, key) assert.ok( equalsBytes(contract0, value), "contract key's value is set in the _storageTries cache", @@ -68,9 +68,9 @@ describe('StateManager -> General/Account', () => { await stateManager.commit() await stateManager.setStateRoot(initialStateRoot) try { - await stateManager.getContractStorage(address, key) + await stateManager.getStorage(address, key) } catch (e) { - assert.ok(true, 'should throw if getContractStorage() is called on non existing address') + assert.ok(true, 'should throw if getStorage() is called on non existing address') } }) diff --git a/packages/statemanager/test/stateManager.code.spec.ts b/packages/statemanager/test/stateManager.code.spec.ts index 98e713b23c..db47c19a03 100644 --- a/packages/statemanager/test/stateManager.code.spec.ts +++ b/packages/statemanager/test/stateManager.code.spec.ts @@ -33,29 +33,29 @@ describe('StateManager -> Code', () => { const key2 = hexToBytes(`0x${'00'.repeat(31)}01`) await stateManager.putAccount(address1, account) - await stateManager.putContractStorage(address1, key1, key2) - await stateManager.putContractStorage(address1, key2, key2) + await stateManager.putStorage(address1, key1, key2) + await stateManager.putStorage(address1, key2, key2) const root = await stateManager.getStateRoot() const rawNode = await stateManager['_trie']['_db'].get(root) - await codeStateManager.putContractCode(address1, rawNode!) + await codeStateManager.putCode(address1, rawNode!) - let codeSlot1 = await codeStateManager.getContractStorage(address1, key1) - let codeSlot2 = await codeStateManager.getContractStorage(address1, key2) + let codeSlot1 = await codeStateManager.getStorage(address1, key1) + let codeSlot2 = await codeStateManager.getStorage(address1, key2) assert.ok(codeSlot1.length === 0, 'slot 0 is empty') assert.ok(codeSlot2.length === 0, 'slot 1 is empty') - const code = await codeStateManager.getContractCode(address1) + const code = await codeStateManager.getCode(address1) assert.ok(code.length > 0, 'code deposited correctly') - const slot1 = await stateManager.getContractStorage(address1, key1) - const slot2 = await stateManager.getContractStorage(address1, key2) + const slot1 = await stateManager.getStorage(address1, key1) + const slot2 = await stateManager.getStorage(address1, key2) assert.ok(slot1.length > 0, 'storage key0 deposited correctly') assert.ok(slot2.length > 0, 'storage key1 deposited correctly') - let slotCode = await stateManager.getContractCode(address1) + let slotCode = await stateManager.getCode(address1) assert.ok(slotCode.length === 0, 'code cannot be loaded') // Checks by either setting state root to codeHash, or codeHash to stateRoot @@ -65,7 +65,7 @@ describe('StateManager -> Code', () => { await stateManager.putAccount(address1, account1!) - slotCode = await stateManager.getContractCode(address1) + slotCode = await stateManager.getCode(address1) assert.ok(slotCode.length === 0, 'code cannot be loaded') // This test fails if no code prefix is used account1 = await codeStateManager.getAccount(address1) @@ -73,8 +73,8 @@ describe('StateManager -> Code', () => { await codeStateManager.putAccount(address1, account1!) - codeSlot1 = await codeStateManager.getContractStorage(address1, key1) - codeSlot2 = await codeStateManager.getContractStorage(address1, key2) + codeSlot1 = await codeStateManager.getStorage(address1, key1) + codeSlot2 = await codeStateManager.getStorage(address1, key2) assert.ok(codeSlot1.length === 0, 'slot 0 is empty') assert.ok(codeSlot2.length === 0, 'slot 1 is empty') @@ -93,8 +93,8 @@ describe('StateManager -> Code', () => { } const account = createAccount(raw) await stateManager.putAccount(address, account) - await stateManager.putContractCode(address, code) - const codeRetrieved = await stateManager.getContractCode(address) + await stateManager.putCode(address, code) + const codeRetrieved = await stateManager.getCode(address) assert.ok(equalsBytes(code, codeRetrieved)) }) @@ -107,7 +107,7 @@ describe('StateManager -> Code', () => { } const account = createAccount(raw) await stateManager.putAccount(address, account) - const code = await stateManager.getContractCode(address) + const code = await stateManager.getCode(address) assert.ok(equalsBytes(code, new Uint8Array(0))) }) @@ -121,8 +121,8 @@ describe('StateManager -> Code', () => { const account = createAccount(raw) const code = new Uint8Array(0) await stateManager.putAccount(address, account) - await stateManager.putContractCode(address, code) - const codeRetrieved = await stateManager.getContractCode(address) + await stateManager.putCode(address, code) + const codeRetrieved = await stateManager.getCode(address) assert.ok(equalsBytes(codeRetrieved, new Uint8Array(0))) }) @@ -130,8 +130,8 @@ describe('StateManager -> Code', () => { const stateManager = new DefaultStateManager({ accountCacheOpts }) const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b')) const code = hexToBytes('0x80') - await stateManager.putContractCode(address, code) - const codeRetrieved = await stateManager.getContractCode(address) + await stateManager.putCode(address, code) + const codeRetrieved = await stateManager.getCode(address) assert.ok(equalsBytes(codeRetrieved, code)) }) @@ -142,18 +142,18 @@ describe('StateManager -> Code', () => { const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b')) const code = hexToBytes('0x80') try { - await stateManager.putContractCode(address, code) + await stateManager.putCode(address, code) assert.fail('should throw') } catch (e) { assert.ok(true, 'successfully threw') } }) - it('putContractCode with empty code on existing address should correctly propagate', async () => { + it('putCode with empty code on existing address should correctly propagate', async () => { const stateManager = new DefaultStateManager() const address = Address.zero() - await stateManager.putContractCode(address, new Uint8Array([1])) - await stateManager.putContractCode(address, new Uint8Array()) + await stateManager.putCode(address, new Uint8Array([1])) + await stateManager.putCode(address, new Uint8Array()) const account = await stateManager.getAccount(address) assert.ok(account !== undefined) assert.ok(account?.isEmpty()) diff --git a/packages/statemanager/test/stateManager.spec.ts b/packages/statemanager/test/stateManager.spec.ts index 02d09e04ea..c0a842cb1e 100644 --- a/packages/statemanager/test/stateManager.spec.ts +++ b/packages/statemanager/test/stateManager.spec.ts @@ -61,14 +61,14 @@ describe('StateManager -> General', () => { const storageKey = setLengthLeft(bigIntToBytes(2n), 32) const storedData = utf8ToBytes('abcd') - await sm.putContractCode(contractAddress, contractCode) - await sm.putContractStorage(contractAddress, storageKey, storedData) + await sm.putCode(contractAddress, contractCode) + await sm.putStorage(contractAddress, storageKey, storedData) - let storage = await sm.getContractStorage(contractAddress, storageKey) + let storage = await sm.getStorage(contractAddress, storageKey) assert.equal(JSON.stringify(storage), JSON.stringify(storedData), 'contract storage updated') - await sm.clearContractStorage(contractAddress) - storage = await sm.getContractStorage(contractAddress, storageKey) + await sm.clearStorage(contractAddress) + storage = await sm.getStorage(contractAddress, storageKey) assert.equal( JSON.stringify(storage), JSON.stringify(new Uint8Array()), @@ -199,11 +199,11 @@ describe('StateManager -> General', () => { const address = Address.fromString(addressStr) const account = new Account(entry.nonce, entry.balance) await stateManager.putAccount(address, account) - await stateManager.putContractCode(address, entry.code) + await stateManager.putCode(address, entry.code) for (let i = 0; i < entry.keys.length; i++) { const key = entry.keys[i] const value = entry.values[i] - await stateManager.putContractStorage(address, key, value) + await stateManager.putStorage(address, key, value) } await stateManager.flush() stateSetup[addressStr].codeHash = (await stateManager.getAccount(address)!)?.codeHash @@ -230,13 +230,13 @@ describe('StateManager -> General', () => { const stProof = await stateManager.getProof(address1, [state1.keys[0], state1.keys[1]]) await partialStateManager.addProofData(stProof) - let stSlot1_0 = await partialStateManager.getContractStorage(address1, state1.keys[0]) + let stSlot1_0 = await partialStateManager.getStorage(address1, state1.keys[0]) assert.ok(equalsBytes(stSlot1_0, state1.values[0])) - let stSlot1_1 = await partialStateManager.getContractStorage(address1, state1.keys[1]) + let stSlot1_1 = await partialStateManager.getStorage(address1, state1.keys[1]) assert.ok(equalsBytes(stSlot1_1, state1.values[1])) - let stSlot1_2 = await partialStateManager.getContractStorage(address1, state1.keys[2]) + let stSlot1_2 = await partialStateManager.getStorage(address1, state1.keys[2]) assert.ok(equalsBytes(stSlot1_2, new Uint8Array())) // Check Array support as input @@ -252,13 +252,13 @@ describe('StateManager -> General', () => { account3 = await sm.getAccount(address3) assert.ok(account3 === undefined) - stSlot1_0 = await sm.getContractStorage(address1, state1.keys[0]) + stSlot1_0 = await sm.getStorage(address1, state1.keys[0]) assert.ok(equalsBytes(stSlot1_0, state1.values[0])) - stSlot1_1 = await sm.getContractStorage(address1, state1.keys[1]) + stSlot1_1 = await sm.getStorage(address1, state1.keys[1]) assert.ok(equalsBytes(stSlot1_1, state1.values[1])) - stSlot1_2 = await sm.getContractStorage(address1, state1.keys[2]) + stSlot1_2 = await sm.getStorage(address1, state1.keys[2]) assert.ok(equalsBytes(stSlot1_2, new Uint8Array())) } @@ -311,7 +311,7 @@ describe('StateManager -> General', () => { const account2 = new Account(undefined, 100n) await sm.putAccount(address, account) await sm.putAccount(address2, account2) - await sm.putContractStorage(address, setLengthLeft(intToBytes(0), 32), intToBytes(32)) + await sm.putStorage(address, setLengthLeft(intToBytes(0), 32), intToBytes(32)) const storage = await sm.dumpStorage(address) const keys = Object.keys(storage) as PrefixedHexString[] const proof = await sm.getProof( @@ -331,10 +331,7 @@ describe('StateManager -> General', () => { false, 'trie opts are preserved in new sm', ) - assert.deepEqual( - intToBytes(32), - await partialSM.getContractStorage(address, hexToBytes(keys[0])), - ) + assert.deepEqual(intToBytes(32), await partialSM.getStorage(address, hexToBytes(keys[0]))) assert.equal((await partialSM.getAccount(address2))?.balance, 100n) const partialSM2 = await DefaultStateManager.fromProof(proof, true, { trie: newTrie, @@ -345,10 +342,7 @@ describe('StateManager -> General', () => { false, 'trie opts are preserved in new sm', ) - assert.deepEqual( - intToBytes(32), - await partialSM2.getContractStorage(address, hexToBytes(keys[0])), - ) + assert.deepEqual(intToBytes(32), await partialSM2.getStorage(address, hexToBytes(keys[0]))) assert.equal((await partialSM2.getAccount(address2))?.balance, 100n) }, ) diff --git a/packages/statemanager/test/stateManager.storage.spec.ts b/packages/statemanager/test/stateManager.storage.spec.ts index d3829a1028..40d8113348 100644 --- a/packages/statemanager/test/stateManager.storage.spec.ts +++ b/packages/statemanager/test/stateManager.storage.spec.ts @@ -31,7 +31,7 @@ describe('StateManager -> Storage', () => { const key = hexToBytes('0x1234567890123456789012345678901234567890123456789012345678901234') const value = hexToBytes('0x0a') // We used this value as its RLP encoding is also 0a - await stateManager.putContractStorage(address, key, value) + await stateManager.putStorage(address, key, value) const data = await stateManager.dumpStorage(address) const expect = { [bytesToHex(keccak256(key))]: '0x0a' } @@ -45,7 +45,7 @@ describe('StateManager -> Storage', () => { await stateManager.putAccount(address, account) try { - await stateManager.putContractStorage(address, new Uint8Array(12), hexToBytes('0x1231')) + await stateManager.putStorage(address, new Uint8Array(12), hexToBytes('0x1231')) } catch (e: any) { assert.equal(e.message, 'Storage key must be 32 bytes long') return @@ -61,7 +61,7 @@ describe('StateManager -> Storage', () => { await stateManager.putAccount(address, account) try { - await stateManager.getContractStorage(address, new Uint8Array(12)) + await stateManager.getStorage(address, new Uint8Array(12)) } catch (e: any) { assert.equal(e.message, 'Storage key must be 32 bytes long') return @@ -79,7 +79,7 @@ describe('StateManager -> Storage', () => { const key = zeros(32) const value = hexToBytes(`0x${'aa'.repeat(33)}`) try { - await stateManager.putContractStorage(address, key, value) + await stateManager.putStorage(address, key, value) assert.fail('did not throw') } catch (e: any) { assert.ok(true, 'threw on trying to set storage values larger than 32 bytes') @@ -95,15 +95,15 @@ describe('StateManager -> Storage', () => { const key0 = zeros(32) const value0 = hexToBytes(`0x00${'aa'.repeat(30)}`) // put a value of 31-bytes length with a leading zero byte const expect0 = unpadBytes(value0) - await stateManager.putContractStorage(address, key0, value0) - const slot0 = await stateManager.getContractStorage(address, key0) + await stateManager.putStorage(address, key0, value0) + const slot0 = await stateManager.getStorage(address, key0) assert.ok(equalsBytes(slot0, expect0), 'value of 31 bytes padded correctly') const key1 = concatBytes(zeros(31), hexToBytes('0x01')) const value1 = hexToBytes(`0x0000${'aa'.repeat(1)}`) // put a value of 1-byte length with two leading zero bytes const expect1 = unpadBytes(value1) - await stateManager.putContractStorage(address, key1, value1) - const slot1 = await stateManager.getContractStorage(address, key1) + await stateManager.putStorage(address, key1, value1) + const slot1 = await stateManager.getStorage(address, key1) assert.ok(equalsBytes(slot1, expect1), 'value of 1 byte padded correctly') }) @@ -122,15 +122,15 @@ describe('StateManager -> Storage', () => { await stateManager.putAccount(address, account) const value = zeros(length) - await stateManager.putContractStorage(address, key, startValue) - const currentValue = await stateManager.getContractStorage(address, key) + await stateManager.putStorage(address, key, startValue) + const currentValue = await stateManager.getStorage(address, key) if (!equalsBytes(currentValue, startValue)) { // sanity check assert.fail('contract value not set correctly') } else { // delete the value - await stateManager.putContractStorage(address, key, value) - const deleted = await stateManager.getContractStorage(address, key) + await stateManager.putStorage(address, key, value) + const deleted = await stateManager.getStorage(address, key) assert.ok(equalsBytes(deleted, zeros(0)), 'the storage key should be deleted') } } @@ -146,8 +146,8 @@ describe('StateManager -> Storage', () => { const value = hexToBytes('0x0000aabb00') const expect = hexToBytes('0xaabb00') - await stateManager.putContractStorage(address, key, value) - const contractValue = await stateManager.getContractStorage(address, key) + await stateManager.putStorage(address, key, value) + const contractValue = await stateManager.getStorage(address, key) assert.ok(equalsBytes(contractValue, expect), 'trailing zeros are not stripped') }) } diff --git a/packages/statemanager/test/statelessVerkleStateManager.spec.ts b/packages/statemanager/test/statelessVerkleStateManager.spec.ts index 76c36e0dfd..da1588b4e1 100644 --- a/packages/statemanager/test/statelessVerkleStateManager.spec.ts +++ b/packages/statemanager/test/statelessVerkleStateManager.spec.ts @@ -178,20 +178,13 @@ describe('StatelessVerkleStateManager: Kaustinen Verkle Block', () => { const contractAddress = Address.fromString('0x4242424242424242424242424242424242424242') const storageKey = '0x0000000000000000000000000000000000000000000000000000000000000022' const storageValue = '0xf5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b' - await stateManager.putContractStorage( - contractAddress, - hexToBytes(storageKey), - hexToBytes(storageValue), - ) - let contractStorage = await stateManager.getContractStorage( - contractAddress, - hexToBytes(storageKey), - ) + await stateManager.putStorage(contractAddress, hexToBytes(storageKey), hexToBytes(storageValue)) + let contractStorage = await stateManager.getStorage(contractAddress, hexToBytes(storageKey)) assert.equal(bytesToHex(contractStorage), storageValue) - await stateManager.clearContractStorage(contractAddress) - contractStorage = await stateManager.getContractStorage(contractAddress, hexToBytes(storageKey)) + await stateManager.clearStorage(contractAddress) + contractStorage = await stateManager.getStorage(contractAddress, hexToBytes(storageKey)) assert.equal(bytesToHex(contractStorage), bytesToHex(new Uint8Array())) }) diff --git a/packages/statemanager/test/vmState.spec.ts b/packages/statemanager/test/vmState.spec.ts index d486f191ca..698a3196cc 100644 --- a/packages/statemanager/test/vmState.spec.ts +++ b/packages/statemanager/test/vmState.spec.ts @@ -73,7 +73,7 @@ describe('Original storage cache', async () => { it(`should initially have empty storage value`, async () => { await stateManager.checkpoint() - const res = await stateManager.getContractStorage(address, key) + const res = await stateManager.getStorage(address, key) assert.deepEqual(res, new Uint8Array(0)) const origRes = await stateManager.originalStorageCache.get(address, key) @@ -83,8 +83,8 @@ describe('Original storage cache', async () => { }) it(`should set original storage value`, async () => { - await stateManager.putContractStorage(address, key, value) - const res = await stateManager.getContractStorage(address, key) + await stateManager.putStorage(address, key, value) + const res = await stateManager.getStorage(address, key) assert.deepEqual(res, value) }) @@ -95,8 +95,8 @@ describe('Original storage cache', async () => { it(`should return correct original value after modification`, async () => { const newValue = hexToBytes('0x1235') - await stateManager.putContractStorage(address, key, newValue) - const res = await stateManager.getContractStorage(address, key) + await stateManager.putStorage(address, key, newValue) + const res = await stateManager.getStorage(address, key) assert.deepEqual(res, newValue) const origRes = await stateManager.originalStorageCache.get(address, key) @@ -107,22 +107,22 @@ describe('Original storage cache', async () => { const key2 = hexToBytes('0x0000000000000000000000000000000000000000000000000000000000000012') const value2 = utf8ToBytes('12') const value3 = utf8ToBytes('123') - await stateManager.putContractStorage(address, key2, value2) + await stateManager.putStorage(address, key2, value2) - let res = await stateManager.getContractStorage(address, key2) + let res = await stateManager.getStorage(address, key2) assert.deepEqual(res, value2) let origRes = await stateManager.originalStorageCache.get(address, key2) assert.deepEqual(origRes, value2) - await stateManager.putContractStorage(address, key2, value3) + await stateManager.putStorage(address, key2, value3) - res = await stateManager.getContractStorage(address, key2) + res = await stateManager.getStorage(address, key2) assert.deepEqual(res, value3) origRes = await stateManager.originalStorageCache.get(address, key2) assert.deepEqual(origRes, value2) // Check previous key - res = await stateManager.getContractStorage(address, key) + res = await stateManager.getStorage(address, key) assert.deepEqual(res, hexToBytes('0x1235')) origRes = await stateManager.originalStorageCache.get(address, key) assert.deepEqual(origRes, value) diff --git a/packages/vm/benchmarks/util.ts b/packages/vm/benchmarks/util.ts index 460f29ce30..f346134417 100644 --- a/packages/vm/benchmarks/util.ts +++ b/packages/vm/benchmarks/util.ts @@ -33,7 +33,7 @@ export async function getPreState( pre: { [k: string]: StateTestPreAccount }, - common: Common + common: Common, ): Promise { const state = new DefaultStateManager() await state.checkpoint() @@ -42,19 +42,19 @@ export async function getPreState( const { nonce, balance, code, storage } = pre[k] const account = new Account(BigInt(nonce), BigInt(balance)) await state.putAccount(address, account) - await state.putContractCode(address, toBytes(code)) + await state.putCode(address, toBytes(code)) for (const storageKey in storage) { const storageValue = storage[storageKey] const storageValueBytes = hexToBytes( - isHexString(storageValue) ? storageValue : `0x${storageValue}` + isHexString(storageValue) ? storageValue : `0x${storageValue}`, ) // verify if this value buffer is not a zero buffer. if so, we should not write it... const zeroBytesEquivalent = new Uint8Array(storageValueBytes.length) if (!equalsBytes(zeroBytesEquivalent, storageValueBytes)) { - await state.putContractStorage( + await state.putStorage( address, hexToBytes(isHexString(storageKey) ? storageKey : `0x${storageKey}`), - storageValueBytes + storageValueBytes, ) } } diff --git a/packages/vm/examples/run-blockchain.ts b/packages/vm/examples/run-blockchain.ts index 92aa4b6f69..b5115de95d 100644 --- a/packages/vm/examples/run-blockchain.ts +++ b/packages/vm/examples/run-blockchain.ts @@ -75,11 +75,11 @@ async function setupPreConditions(vm: VM, data: any) { for (const [key, val] of Object.entries(storage)) { const storageKey = setLengthLeft(hexToBytes(key), 32) const storageVal = hexToBytes(val as string) - await vm.stateManager.putContractStorage(address, storageKey, storageVal) + await vm.stateManager.putStorage(address, storageKey, storageVal) } const codeBuf = hexToBytes('0x' + code) - await vm.stateManager.putContractCode(address, codeBuf) + await vm.stateManager.putCode(address, codeBuf) } await vm.stateManager.commit() diff --git a/packages/vm/src/requests.ts b/packages/vm/src/requests.ts index 10100698c0..1d5955058a 100644 --- a/packages/vm/src/requests.ts +++ b/packages/vm/src/requests.ts @@ -67,7 +67,7 @@ const accumulateEIP7002Requests = async ( ) const withdrawalsAddress = Address.fromString(bytesToHex(addressBytes)) - const code = await vm.stateManager.getContractCode(withdrawalsAddress) + const code = await vm.stateManager.getCode(withdrawalsAddress) if (code.length === 0) { throw new Error( @@ -117,7 +117,7 @@ const accumulateEIP7251Requests = async ( ) const consolidationsAddress = Address.fromString(bytesToHex(addressBytes)) - const code = await vm.stateManager.getContractCode(consolidationsAddress) + const code = await vm.stateManager.getCode(consolidationsAddress) if (code.length === 0) { throw new Error( diff --git a/packages/vm/src/runBlock.ts b/packages/vm/src/runBlock.ts index 3715a65d78..49534c088a 100644 --- a/packages/vm/src/runBlock.ts +++ b/packages/vm/src/runBlock.ts @@ -527,7 +527,7 @@ export async function accumulateParentBlockHash( ).accessWitness!.touchAddressOnWriteAndComputeGas(historyAddress, treeIndex, subIndex) } const key = setLengthLeft(bigIntToBytes(ringKey), 32) - await vm.stateManager.putContractStorage(historyAddress, key, hash) + await vm.stateManager.putStorage(historyAddress, key, hash) } await putBlockHash(vm, parentHash, currentBlockNumber - BIGINT_1) @@ -564,12 +564,12 @@ export async function accumulateParentBeaconBlockRoot(vm: VM, root: Uint8Array, await vm.evm.journal.putAccount(parentBeaconBlockRootAddress, new Account()) } - await vm.stateManager.putContractStorage( + await vm.stateManager.putStorage( parentBeaconBlockRootAddress, setLengthLeft(bigIntToBytes(timestampIndex), 32), bigIntToBytes(timestamp), ) - await vm.stateManager.putContractStorage( + await vm.stateManager.putStorage( parentBeaconBlockRootAddress, setLengthLeft(bigIntToBytes(timestampExtended), 32), root, diff --git a/packages/vm/src/runTx.ts b/packages/vm/src/runTx.ts index 0ccba5a4f2..a51be5d7b8 100644 --- a/packages/vm/src/runTx.ts +++ b/packages/vm/src/runTx.ts @@ -494,8 +494,8 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { } const addressConverted = new Address(address) - const addressCode = await vm.stateManager.getContractCode(addressConverted) - await vm.stateManager.putContractCode(authority, addressCode) + const addressCode = await vm.stateManager.getCode(addressConverted) + await vm.stateManager.putCode(authority, addressCode) writtenAddresses.add(authority.toString()) vm.evm.journal.addAlwaysWarmAddress(authority.toString()) @@ -687,7 +687,7 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { for (const str of writtenAddresses) { const address = Address.fromString(str) - await vm.stateManager.putContractCode(address, new Uint8Array()) + await vm.stateManager.putCode(address, new Uint8Array()) } if (enableProfiler) { diff --git a/packages/vm/test/api/EIPs/eip-1153.spec.ts b/packages/vm/test/api/EIPs/eip-1153.spec.ts index 894cfc57b0..23e76af7d4 100644 --- a/packages/vm/test/api/EIPs/eip-1153.spec.ts +++ b/packages/vm/test/api/EIPs/eip-1153.spec.ts @@ -54,7 +54,7 @@ describe('EIP 1153: transient storage', () => { }) for (const { code, address } of test.contracts) { - await vm.stateManager.putContractCode(address, hexToBytes(code as PrefixedHexString)) + await vm.stateManager.putCode(address, hexToBytes(code as PrefixedHexString)) } const fromAddress = new Address(privateToAddress(senderKey)) diff --git a/packages/vm/test/api/EIPs/eip-1283-net-gas-metering.spec.ts b/packages/vm/test/api/EIPs/eip-1283-net-gas-metering.spec.ts index f772db6d6f..37ba98da19 100644 --- a/packages/vm/test/api/EIPs/eip-1283-net-gas-metering.spec.ts +++ b/packages/vm/test/api/EIPs/eip-1283-net-gas-metering.spec.ts @@ -42,9 +42,9 @@ describe('Constantinople: EIP-1283', () => { const account = createAccountWithDefaults(BigInt(0), BigInt(0)) await vm.stateManager.putAccount(addr, account) - await vm.stateManager.putContractCode(addr, hexToBytes(testCase.code as PrefixedHexString)) + await vm.stateManager.putCode(addr, hexToBytes(testCase.code as PrefixedHexString)) if (testCase.original !== BigInt(0)) { - await vm.stateManager.putContractStorage(addr, key, bigIntToBytes(testCase.original)) + await vm.stateManager.putStorage(addr, key, bigIntToBytes(testCase.original)) } const runCallArgs = { diff --git a/packages/vm/test/api/EIPs/eip-1559-FeeMarket.spec.ts b/packages/vm/test/api/EIPs/eip-1559-FeeMarket.spec.ts index 6a4d50708e..1a16a18a67 100644 --- a/packages/vm/test/api/EIPs/eip-1559-FeeMarket.spec.ts +++ b/packages/vm/test/api/EIPs/eip-1559-FeeMarket.spec.ts @@ -196,7 +196,7 @@ describe('EIP1559 tests', () => { // (This code returns the reported GASPRICE) const code = hexToBytes('0x3A60005260206000F3') - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const result = await runTx(vm, { tx: block.transactions[0], block }) const returnValue = result.execResult.returnValue diff --git a/packages/vm/test/api/EIPs/eip-2929.spec.ts b/packages/vm/test/api/EIPs/eip-2929.spec.ts index e1c0f4967d..3083489663 100644 --- a/packages/vm/test/api/EIPs/eip-2929.spec.ts +++ b/packages/vm/test/api/EIPs/eip-2929.spec.ts @@ -47,7 +47,7 @@ describe('EIP 2929: gas cost tests', () => { i++ }) - await vm.stateManager.putContractCode(address, hexToBytes(test.code)) + await vm.stateManager.putCode(address, hexToBytes(test.code)) const unsignedTx = createLegacyTx({ gasLimit: initialGas, // ensure we pass a lot of gas, so we do not run out of gas @@ -73,7 +73,7 @@ describe('EIP 2929: gas cost tests', () => { const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Berlin, eips: [2929] }) const vm = await VM.create({ common }) - await vm.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code + await vm.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code // setup the call arguments const unsignedTx = createLegacyTx({ diff --git a/packages/vm/test/api/EIPs/eip-2930-accesslists.spec.ts b/packages/vm/test/api/EIPs/eip-2930-accesslists.spec.ts index 042377e11b..01d0bc4b06 100644 --- a/packages/vm/test/api/EIPs/eip-2930-accesslists.spec.ts +++ b/packages/vm/test/api/EIPs/eip-2930-accesslists.spec.ts @@ -48,7 +48,7 @@ describe('EIP-2930 Optional Access Lists tests', () => { const vm = await VM.create({ common }) // contract code PUSH1 0x00 SLOAD STOP - await vm.stateManager.putContractCode(contractAddress, hexToBytes('0x60005400')) + await vm.stateManager.putCode(contractAddress, hexToBytes('0x60005400')) const address = Address.fromPrivateKey(privateKey) const initialBalance = BigInt(10) ** BigInt(18) diff --git a/packages/vm/test/api/EIPs/eip-2935-historical-block-hashes.spec.ts b/packages/vm/test/api/EIPs/eip-2935-historical-block-hashes.spec.ts index 1eff605d0d..f38c999b80 100644 --- a/packages/vm/test/api/EIPs/eip-2935-historical-block-hashes.spec.ts +++ b/packages/vm/test/api/EIPs/eip-2935-historical-block-hashes.spec.ts @@ -131,7 +131,7 @@ describe('EIP 2935: historical block hashes', () => { const account = await vm.stateManager.getAccount(callerAddress) account!.balance = PREBALANCE await vm.stateManager.putAccount(callerAddress, account!) - await vm.stateManager.putContractCode(historyAddress, contract2935Code) + await vm.stateManager.putCode(historyAddress, contract2935Code) const result = await runTx(vm, { tx, block, skipHardForkValidation: true }) const blockHashi = result.execResult.returnValue @@ -190,7 +190,7 @@ describe('EIP 2935: historical block hashes', () => { await vm.blockchain.putBlock(block) await runBlock(vm, { block, generate: true }) - const storage = await vm.stateManager.getContractStorage( + const storage = await vm.stateManager.getStorage( historyAddress, setLengthLeft(bigIntToBytes(BigInt(0)), 32), ) @@ -247,7 +247,7 @@ describe('EIP 2935: historical block hashes', () => { for (let i = 1; i <= blocksToBuild; i++) { const block = await blockchain.getBlock(i) - const storage = await vm.stateManager.getContractStorage( + const storage = await vm.stateManager.getStorage( historyAddress, setLengthLeft(bigIntToBytes(BigInt(i) % historyServeWindow), 32), ) diff --git a/packages/vm/test/api/EIPs/eip-3074-authcall.spec.ts b/packages/vm/test/api/EIPs/eip-3074-authcall.spec.ts index e912290e6d..1e9d5d23db 100644 --- a/packages/vm/test/api/EIPs/eip-3074-authcall.spec.ts +++ b/packages/vm/test/api/EIPs/eip-3074-authcall.spec.ts @@ -239,7 +239,7 @@ describe('EIP-3074 AUTH', () => { const signature = signMessage(message, contractAddress, privateKey) const code = concatBytes(getAuthCode(message, signature, authAddress), RETURNTOP) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -263,7 +263,7 @@ describe('EIP-3074 AUTH', () => { signature.r = signature.s const code = concatBytes(getAuthCode(message, signature, authAddress), RETURNTOP) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -288,7 +288,7 @@ describe('EIP-3074 AUTH', () => { // use the contractAddress instead of authAddress for the expected address (this should fail) const code = concatBytes(getAuthCode(message, signature, contractAddress), RETURNTOP) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -311,7 +311,7 @@ describe('EIP-3074 AUTH', () => { const signature = flipSignature(signMessage(message, contractAddress, privateKey)) const code = concatBytes(getAuthCode(message, signature, authAddress), RETURNTOP) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -335,7 +335,7 @@ describe('EIP-3074 AUTH', () => { signature.v = 2 const code = concatBytes(getAuthCode(message, signature, authAddress), RETURNTOP) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -364,7 +364,7 @@ describe('EIP-3074 AUTH', () => { RETURNTOP, ) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -390,7 +390,7 @@ describe('EIP-3074 AUTH', () => { RETURNTOP, ) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -413,7 +413,7 @@ describe('EIP-3074 AUTH', () => { const signature = signMessage(message, contractAddress, privateKey) const code = concatBytes(getAuthCode(message, signature, authAddress), RETURNMEMSIZE) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) const tx = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -439,7 +439,7 @@ describe('EIP-3074 AUTH', () => { RETURNMEMSIZE, ) - await vm.stateManager.putContractCode(contractAddress, code2) + await vm.stateManager.putCode(contractAddress, code2) const tx2 = createLegacyTx({ to: contractAddress, gasLimit: 1000000, @@ -463,8 +463,8 @@ describe('EIP-3074 AUTH', () => { // Setups the environment for the VM, puts `code` at contractAddress and also puts the STORECALLER bytecode at the contractStorageAddress async function setupVM(code: Uint8Array) { const vm = await VM.create({ common: common.copy() }) - await vm.stateManager.putContractCode(contractAddress, code) - await vm.stateManager.putContractCode(contractStorageAddress, STORECALLER) + await vm.stateManager.putCode(contractAddress, code) + await vm.stateManager.putCode(contractStorageAddress, STORECALLER) await vm.stateManager.putAccount(callerAddress, new Account()) const account = await vm.stateManager.getAccount(callerAddress) account!.balance = PREBALANCE @@ -496,7 +496,7 @@ describe('EIP-3074 AUTHCALL', () => { const buf = result.execResult.returnValue.slice(31) assert.deepEqual(buf, hexToBytes('0x01'), 'authcall success') - const storage = await vm.stateManager.getContractStorage(contractStorageAddress, zeros(32)) + const storage = await vm.stateManager.getStorage(contractStorageAddress, zeros(32)) assert.deepEqual(storage, address.bytes, 'caller set correctly') }) @@ -527,7 +527,7 @@ describe('EIP-3074 AUTHCALL', () => { await runTx(vm, { tx, block, skipHardForkValidation: true }) - const gasUsed = await vm.stateManager.getContractStorage( + const gasUsed = await vm.stateManager.getStorage( contractStorageAddress, hexToBytes(`0x${'00'.repeat(31)}01`), ) @@ -568,7 +568,7 @@ describe('EIP-3074 AUTHCALL', () => { await runTx(vm, { tx, block, skipHardForkValidation: true }) - const gasUsed = await vm.stateManager.getContractStorage( + const gasUsed = await vm.stateManager.getStorage( contractStorageAddress, hexToBytes(`0x${'00'.repeat(31)}01`), ) @@ -657,7 +657,7 @@ describe('EIP-3074 AUTHCALL', () => { const result = await runTx(vm, { tx, block, skipHardForkValidation: true }) - const gasUsed = await vm.stateManager.getContractStorage( + const gasUsed = await vm.stateManager.getStorage( contractStorageAddress, hexToBytes(`0x${'00'.repeat(31)}01`), ) @@ -818,7 +818,7 @@ describe('EIP-3074 AUTHCALL', () => { }).sign(callerPrivateKey) await runTx(vm, { tx, block, skipHardForkValidation: true }) - const gas = await vm.stateManager.getContractStorage( + const gas = await vm.stateManager.getStorage( contractStorageAddress, hexToBytes(`0x${'00'.repeat(31)}01`), ) @@ -851,7 +851,7 @@ describe('EIP-3074 AUTHCALL', () => { }).sign(callerPrivateKey) const result = await runTx(vm, { tx, block, skipHardForkValidation: true }) - const callInput = await vm.stateManager.getContractStorage( + const callInput = await vm.stateManager.getStorage( contractStorageAddress, hexToBytes(`0x${'00'.repeat(31)}02`), ) diff --git a/packages/vm/test/api/EIPs/eip-3529.spec.ts b/packages/vm/test/api/EIPs/eip-3529.spec.ts index 1a80cd793f..0ce7155b93 100644 --- a/packages/vm/test/api/EIPs/eip-3529.spec.ts +++ b/packages/vm/test/api/EIPs/eip-3529.spec.ts @@ -132,13 +132,13 @@ describe('EIP-3529 tests', () => { const code = hexToBytes(`${testCase.code as PrefixedHexString}00`) // add a STOP opcode (0 gas) so we can find the gas used / effective gas await vm.stateManager.putAccount(address, new Account()) - await vm.stateManager.putContractStorage( + await vm.stateManager.putStorage( address, key, hexToBytes(`0x${testCase.original.toString().padStart(64, '0')}`), ) - await vm.stateManager.getContractStorage(address, key) + await vm.stateManager.getStorage(address, key) vm.evm.journal.addAlwaysWarmSlot(bytesToHex(address.bytes), bytesToHex(key)) await vm.evm.runCode!({ @@ -202,7 +202,7 @@ describe('EIP-3529 tests', () => { for (let i = 0; i < 100; i++) { const key = hexToBytes(`0x${i.toString(16).padStart(64, '0')}`) await vm.stateManager.putAccount(address, new Account()) - await vm.stateManager.putContractStorage(address, key, value) + await vm.stateManager.putStorage(address, key, value) const hex = i.toString(16).padStart(2, '0') // push 0 push sstore code = `${code}600060${hex}55` @@ -210,7 +210,7 @@ describe('EIP-3529 tests', () => { code = `${code}00` - await vm.stateManager.putContractCode(address, hexToBytes(code)) + await vm.stateManager.putCode(address, hexToBytes(code)) const tx = createLegacyTx({ to: address, diff --git a/packages/vm/test/api/EIPs/eip-3541.spec.ts b/packages/vm/test/api/EIPs/eip-3541.spec.ts index d30c7fd17e..7edee010b9 100644 --- a/packages/vm/test/api/EIPs/eip-3541.spec.ts +++ b/packages/vm/test/api/EIPs/eip-3541.spec.ts @@ -26,7 +26,7 @@ describe('EIP 3541 tests', () => { let result = await runTx(vm, { tx, skipHardForkValidation: true }) let created = result.createdAddress - let code = await vm.stateManager.getContractCode(created!) + let code = await vm.stateManager.getCode(created!) assert.equal(code.length, 0, 'did not deposit code') @@ -42,7 +42,7 @@ describe('EIP 3541 tests', () => { result = await runTx(vm, { tx: tx1, skipHardForkValidation: true }) created = result.createdAddress - code = await vm.stateManager.getContractCode(created!) + code = await vm.stateManager.getCode(created!) assert.ok(code.length > 0, 'did deposit code') @@ -57,7 +57,7 @@ describe('EIP 3541 tests', () => { result = await runTx(vm, { tx: tx2, skipHardForkValidation: true }) created = result.createdAddress - code = await vm.stateManager.getContractCode(created!) + code = await vm.stateManager.getCode(created!) assert.ok(code.length > 0, 'did deposit code') }) @@ -79,7 +79,7 @@ describe('EIP 3541 tests', () => { await runTx(vm, { tx, skipHardForkValidation: true }) - let code = await vm.stateManager.getContractCode(address!) + let code = await vm.stateManager.getCode(address!) assert.equal(code.length, 0, 'did not deposit code') @@ -92,7 +92,7 @@ describe('EIP 3541 tests', () => { await runTx(vm, { tx: tx1, skipHardForkValidation: true }) - code = await vm.stateManager.getContractCode(address!) + code = await vm.stateManager.getCode(address!) assert.ok(code.length > 0, 'did deposit code') }) @@ -114,7 +114,7 @@ describe('EIP 3541 tests', () => { await runTx(vm, { tx, skipHardForkValidation: true }) - let code = await vm.stateManager.getContractCode(address!) + let code = await vm.stateManager.getCode(address!) assert.equal(code.length, 0, 'did not deposit code') @@ -127,7 +127,7 @@ describe('EIP 3541 tests', () => { await runTx(vm, { tx: tx1, skipHardForkValidation: true }) - code = await vm.stateManager.getContractCode(address!) + code = await vm.stateManager.getCode(address!) assert.ok(code.length > 0, 'did deposit code') }) diff --git a/packages/vm/test/api/EIPs/eip-3607.spec.ts b/packages/vm/test/api/EIPs/eip-3607.spec.ts index 4a8b12b977..a091769b45 100644 --- a/packages/vm/test/api/EIPs/eip-3607.spec.ts +++ b/packages/vm/test/api/EIPs/eip-3607.spec.ts @@ -12,7 +12,7 @@ describe('EIP-3607 tests', () => { it('should reject txs from senders with deployed code when EIP is enabled', async () => { const vm = await VM.create({ common }) - await vm.stateManager.putContractCode(precompileAddr, new Uint8Array(32).fill(1)) + await vm.stateManager.putCode(precompileAddr, new Uint8Array(32).fill(1)) const tx = createLegacyTx({ gasLimit: 100000 }, { freeze: false }) tx.getSenderAddress = () => precompileAddr try { @@ -29,7 +29,7 @@ describe('EIP-3607 tests', () => { it('should not reject txs from senders with deployed code when EIP is not enabled', async () => { const vm = await VM.create({ common: commonNoEIP3607 }) - await vm.stateManager.putContractCode(precompileAddr, new Uint8Array(32).fill(1)) + await vm.stateManager.putCode(precompileAddr, new Uint8Array(32).fill(1)) const tx = createLegacyTx({ gasLimit: 100000 }, { freeze: false }) tx.getSenderAddress = () => precompileAddr try { diff --git a/packages/vm/test/api/EIPs/eip-3651-warm-coinbase.spec.ts b/packages/vm/test/api/EIPs/eip-3651-warm-coinbase.spec.ts index 8a43198746..a3367ba614 100644 --- a/packages/vm/test/api/EIPs/eip-3651-warm-coinbase.spec.ts +++ b/packages/vm/test/api/EIPs/eip-3651-warm-coinbase.spec.ts @@ -38,7 +38,7 @@ async function getVM(common: Common) { account!.balance = balance await vm.stateManager.putAccount(sender, account!) - await vm.stateManager.putContractCode(contractAddress, code) + await vm.stateManager.putCode(contractAddress, code) return vm } diff --git a/packages/vm/test/api/EIPs/eip-4788-beaconroot.spec.ts b/packages/vm/test/api/EIPs/eip-4788-beaconroot.spec.ts index 1e00713d3c..fd962e1951 100644 --- a/packages/vm/test/api/EIPs/eip-4788-beaconroot.spec.ts +++ b/packages/vm/test/api/EIPs/eip-4788-beaconroot.spec.ts @@ -105,8 +105,8 @@ async function runBlock(block: Block) { common, }) - await vm.stateManager.putContractCode(contractAddress, hexToBytes(CODE)) - await vm.stateManager.putContractCode(BROOT_Address, hexToBytes(BROOT_CODE)) + await vm.stateManager.putCode(contractAddress, hexToBytes(CODE)) + await vm.stateManager.putCode(BROOT_Address, hexToBytes(BROOT_CODE)) return { vmResult: await runBlockVM(vm, { block, @@ -122,7 +122,7 @@ async function runBlock(block: Block) { * Get call status saved in the contract */ async function getCallStatus(vm: VM) { - const stat = await vm.stateManager.getContractStorage(contractAddress, zeros(32)) + const stat = await vm.stateManager.getStorage(contractAddress, zeros(32)) return bytesToBigInt(stat) } diff --git a/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts index fb313c63bd..097d4561ca 100644 --- a/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts +++ b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts @@ -49,7 +49,7 @@ describe('EIP4895 tests', () => { const withdrawalCheckAddress = new Address(hexToBytes(`0x${'fe'.repeat(20)}`)) const withdrawalCode = hexToBytes('0x6002600055') - await vm.stateManager.putContractCode(withdrawalCheckAddress, withdrawalCode) + await vm.stateManager.putCode(withdrawalCheckAddress, withdrawalCode) const contractAddress = new Address(hexToBytes(`0x${'ff'.repeat(20)}`)) @@ -62,7 +62,7 @@ describe('EIP4895 tests', () => { PUSH 0 RETURN // Return the balance */ - await vm.stateManager.putContractCode( + await vm.stateManager.putCode( contractAddress, hexToBytes(`0x73${addresses[0]}3160005260206000F3`), ) @@ -123,7 +123,7 @@ describe('EIP4895 tests', () => { assert.deepEqual(zeros(32), result!, 'withdrawals happen after transactions') - const slotValue = await vm.stateManager.getContractStorage(withdrawalCheckAddress, zeros(32)) + const slotValue = await vm.stateManager.getStorage(withdrawalCheckAddress, zeros(32)) assert.deepEqual(zeros(0), slotValue, 'withdrawals do not invoke code') }) diff --git a/packages/vm/test/api/EIPs/eip-6110.spec.ts b/packages/vm/test/api/EIPs/eip-6110.spec.ts index d7d1425bd0..2f6341f3f3 100644 --- a/packages/vm/test/api/EIPs/eip-6110.spec.ts +++ b/packages/vm/test/api/EIPs/eip-6110.spec.ts @@ -47,7 +47,7 @@ describe('EIP-6110 runBlock tests', () => { }) const beaconContractAddress = Address.fromString(DEPOSIT_CONTRACT_ADDRESS) await vm.stateManager.putAccount(beaconContractAddress, beaconContractAccount) - await vm.stateManager.putContractCode(beaconContractAddress, depositContractByteCode) + await vm.stateManager.putCode(beaconContractAddress, depositContractByteCode) await vm.stateManager.putAccount(sender, createAccount({ balance: 540000000030064771065n })) const block = createBlockFromBlockData( { @@ -80,7 +80,7 @@ describe('EIP-7685 buildBlock tests', () => { }) const beaconContractAddress = Address.fromString(DEPOSIT_CONTRACT_ADDRESS) await vm.stateManager.putAccount(beaconContractAddress, beaconContractAccount) - await vm.stateManager.putContractCode(beaconContractAddress, depositContractByteCode) + await vm.stateManager.putCode(beaconContractAddress, depositContractByteCode) await vm.stateManager.putAccount(sender, createAccount({ balance: 540000000030064771065n })) const block = createBlockFromBlockData({}, { common }) ;(vm.blockchain as any)['dbManager']['getHeader'] = () => block.header diff --git a/packages/vm/test/api/EIPs/eip-6780-selfdestruct-same-tx.spec.ts b/packages/vm/test/api/EIPs/eip-6780-selfdestruct-same-tx.spec.ts index 236c3cb238..ffe1936b26 100644 --- a/packages/vm/test/api/EIPs/eip-6780-selfdestruct-same-tx.spec.ts +++ b/packages/vm/test/api/EIPs/eip-6780-selfdestruct-same-tx.spec.ts @@ -72,7 +72,7 @@ describe('EIP 6780 tests', () => { const target = Address.fromString('0x' + 'ff'.repeat(20)) - await vm.stateManager.putContractCode(target, payload) + await vm.stateManager.putCode(target, payload) const targetContract = await vm.stateManager.getAccount(target) targetContract!.nonce = BigInt(1) await vm.stateManager.putAccount(target, targetContract) @@ -92,7 +92,7 @@ describe('EIP 6780 tests', () => { assert.equal(contract.nonce, BigInt(1), 'nonce 1') const key = hexToBytes(`0x${'00'.repeat(31)}01`) - const storage = await vm.stateManager.getContractStorage(target, key) + const storage = await vm.stateManager.getStorage(target, key) assert.ok(equalsBytes(storage, hexToBytes('0x01')), 'storage not cleared') assert.equal( diff --git a/packages/vm/test/api/EIPs/eip-7702.spec.ts b/packages/vm/test/api/EIPs/eip-7702.spec.ts index bb94ba5226..89f0d4ad87 100644 --- a/packages/vm/test/api/EIPs/eip-7702.spec.ts +++ b/packages/vm/test/api/EIPs/eip-7702.spec.ts @@ -78,10 +78,10 @@ async function runTest( ).sign(defaultSenderPkey) const code1 = hexToBytes('0x600160015500') - await vm.stateManager.putContractCode(code1Addr, code1) + await vm.stateManager.putCode(code1Addr, code1) const code2 = hexToBytes('0x600260015500') - await vm.stateManager.putContractCode(code2Addr, code2) + await vm.stateManager.putCode(code2Addr, code2) const acc = (await vm.stateManager.getAccount(defaultSenderAddr)) ?? new Account() acc.balance = BigInt(1_000_000_000) @@ -90,7 +90,7 @@ async function runTest( await runTx(vm, { tx }) const slot = hexToBytes('0x' + '00'.repeat(31) + '01') - const value = await vm.stateManager.getContractStorage(defaultAuthAddr, slot) + const value = await vm.stateManager.getStorage(defaultAuthAddr, slot) assert.ok(equalsBytes(unpadBytes(expect), value)) if (skipEmptyCode === undefined) { @@ -172,7 +172,7 @@ describe('EIP 7702: set code to EOA accounts', () => { it('Code is already present in account', async () => { const vm = await VM.create({ common }) - await vm.stateManager.putContractCode(defaultAuthAddr, new Uint8Array([1])) + await vm.stateManager.putCode(defaultAuthAddr, new Uint8Array([1])) await runTest( [ { @@ -205,7 +205,7 @@ describe('EIP 7702: set code to EOA accounts', () => { ) const checkAddressWarm = Address.fromString(`0x${'FA'.repeat(20)}`) - await vm.stateManager.putContractCode(checkAddressWarm, checkAddressWarmCode) + await vm.stateManager.putCode(checkAddressWarm, checkAddressWarmCode) const tx = create7702EOACodeTx( { @@ -219,7 +219,7 @@ describe('EIP 7702: set code to EOA accounts', () => { ).sign(defaultSenderPkey) const code1 = hexToBytes('0x') - await vm.stateManager.putContractCode(code1Addr, code1) + await vm.stateManager.putCode(code1Addr, code1) const acc = (await vm.stateManager.getAccount(defaultSenderAddr)) ?? new Account() acc.balance = BigInt(1_000_000_000) @@ -254,7 +254,7 @@ describe('EIP 7702: set code to EOA accounts', () => { // Store value 1 in storage slot 1 // PUSH1 PUSH1 SSTORE STOP const code = hexToBytes('0x600160015500') - await vm.stateManager.putContractCode(code1Addr, code) + await vm.stateManager.putCode(code1Addr, code) const acc = (await vm.stateManager.getAccount(defaultSenderAddr)) ?? new Account() acc.balance = BigInt(1_000_000_000) diff --git a/packages/vm/test/api/index.spec.ts b/packages/vm/test/api/index.spec.ts index c5323450ca..61a187cd0e 100644 --- a/packages/vm/test/api/index.spec.ts +++ b/packages/vm/test/api/index.spec.ts @@ -328,9 +328,9 @@ describe('VM -> setHardfork, state (deprecated), blockchain', () => { STOP */ - await vmNotActivated.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code + await vmNotActivated.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code await vmNotActivated.stateManager.putAccount(caller, new Account(BigInt(0), BigInt(0x111))) // give calling account a positive balance - await vmActivated.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code + await vmActivated.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code await vmActivated.stateManager.putAccount(caller, new Account(BigInt(0), BigInt(0x111))) // give calling account a positive balance // setup the call arguments const runCallArgs = { diff --git a/packages/vm/test/api/istanbul/eip-2200.spec.ts b/packages/vm/test/api/istanbul/eip-2200.spec.ts index dd3b2948c5..d12a8766b7 100644 --- a/packages/vm/test/api/istanbul/eip-2200.spec.ts +++ b/packages/vm/test/api/istanbul/eip-2200.spec.ts @@ -54,9 +54,9 @@ describe('Istanbul: EIP-2200', () => { const account = createAccountWithDefaults(BigInt(0), BigInt(0)) await vm.stateManager.putAccount(addr, account) - await vm.stateManager.putContractCode(addr, hexToBytes(`0x${testCase.code}`)) + await vm.stateManager.putCode(addr, hexToBytes(`0x${testCase.code}`)) if (testCase.original !== BigInt(0)) { - await vm.stateManager.putContractStorage( + await vm.stateManager.putStorage( addr, key, hexToBytes(`0x${testCase.original.toString(16)}`), diff --git a/packages/vm/test/api/runBlock.spec.ts b/packages/vm/test/api/runBlock.spec.ts index 0d554e61f5..c9be021617 100644 --- a/packages/vm/test/api/runBlock.spec.ts +++ b/packages/vm/test/api/runBlock.spec.ts @@ -619,10 +619,10 @@ describe('runBlock() -> tx types', async () => { await setBalance(vm, defaultSenderAddr, 0xfffffffffffffn) const code1 = hexToBytes('0x600160005500') - await vm.stateManager.putContractCode(code1Addr, code1) + await vm.stateManager.putCode(code1Addr, code1) const code2 = hexToBytes('0x600260005500') - await vm.stateManager.putContractCode(code2Addr, code2) + await vm.stateManager.putCode(code2Addr, code2) const authorizationListOpts = [ { address: code1Addr, @@ -667,7 +667,7 @@ describe('runBlock() -> tx types', async () => { ) await runBlock(vm, { block, skipBlockValidation: true, generate: true }) - const storage = await vm.stateManager.getContractStorage(defaultAuthAddr, zeros(32)) + const storage = await vm.stateManager.getStorage(defaultAuthAddr, zeros(32)) assert.ok(equalsBytes(storage, new Uint8Array([2]))) }) }) diff --git a/packages/vm/test/api/runTx.spec.ts b/packages/vm/test/api/runTx.spec.ts index f3b6920f11..7378d5c4f7 100644 --- a/packages/vm/test/api/runTx.spec.ts +++ b/packages/vm/test/api/runTx.spec.ts @@ -468,8 +468,8 @@ describe('runTx() -> runtime behavior', () => { */ const code = hexToBytes('0x6001600055FE') const address = new Address(hexToBytes('0x00000000000000000000000000000000000000ff')) - await vm.stateManager.putContractCode(address, code) - await vm.stateManager.putContractStorage( + await vm.stateManager.putCode(address, code) + await vm.stateManager.putStorage( address, hexToBytes(`0x${'00'.repeat(32)}`), hexToBytes(`0x${'00'.repeat(31)}01`), @@ -797,7 +797,7 @@ it('Validate EXTCODEHASH puts KECCAK256_NULL on stack if calling account has no // Puts EXTCODEHASH of CALLER into slot 0 const code = hexToBytes('0x333F60005500') const codeAddr = Address.fromString('0x' + '20'.repeat(20)) - await vm.stateManager.putContractCode(codeAddr, code) + await vm.stateManager.putCode(codeAddr, code) const tx = createLegacyTx({ gasLimit: 100000, @@ -812,7 +812,7 @@ it('Validate EXTCODEHASH puts KECCAK256_NULL on stack if calling account has no await vm.stateManager.putAccount(addr, acc!) await runTx(vm, { tx, skipHardForkValidation: true }) - const hash = await vm.stateManager.getContractStorage(codeAddr, zeros(32)) + const hash = await vm.stateManager.getStorage(codeAddr, zeros(32)) assert.deepEqual(hash, KECCAK256_NULL, 'hash ok') }) @@ -830,7 +830,7 @@ it('Validate CALL does not charge new account gas when calling CALLER and caller // Calls CALLER and sends back the ETH just sent with the transaction const code = hexToBytes('0x600080808034335AF100') const codeAddr = Address.fromString('0x' + '20'.repeat(20)) - await vm.stateManager.putContractCode(codeAddr, code) + await vm.stateManager.putCode(codeAddr, code) const tx = createLegacyTx({ gasLimit: 100000, @@ -861,7 +861,7 @@ it('Validate SELFDESTRUCT does not charge new account gas when calling CALLER an // Puts EXTCODEHASH of CALLER into slot 0 const code = hexToBytes('0x33FF') const codeAddr = Address.fromString('0x' + '20'.repeat(20)) - await vm.stateManager.putContractCode(codeAddr, code) + await vm.stateManager.putCode(codeAddr, code) const tx = createLegacyTx({ gasLimit: 100000, diff --git a/packages/vm/test/api/state/accountExists.spec.ts b/packages/vm/test/api/state/accountExists.spec.ts index 71ddc7c518..13cb994ef6 100644 --- a/packages/vm/test/api/state/accountExists.spec.ts +++ b/packages/vm/test/api/state/accountExists.spec.ts @@ -26,8 +26,8 @@ describe('correctly apply new account gas fee on pre-Spurious Dragon hardforks', const existingAccount = await vm.stateManager.getAccount(existingAddress) existingAccount!.balance = BigInt(1) await vm.stateManager.putAccount(existingAddress, existingAccount!) - await vm.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code - await vm.stateManager.putContractStorage( + await vm.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code + await vm.stateManager.putStorage( contractAddress, hexToBytes('0xd08f588b94e47566eea77acec87441cecca23f61aea9ed8eb086c062d3837605'), hexToBytes('0x0000000000000000000000000000000000000000000000000000000000000001'), @@ -75,8 +75,8 @@ describe('do not apply new account gas fee for empty account in DB on pre-Spurio toBytes(emptyAddress), emptyAccount.serialize(), ) - await vm.stateManager.putContractCode(contractAddress, hexToBytes(code)) // setup the contract code - await vm.stateManager.putContractStorage( + await vm.stateManager.putCode(contractAddress, hexToBytes(code)) // setup the contract code + await vm.stateManager.putStorage( contractAddress, hexToBytes('0xd08f588b94e47566eea77acec87441cecca23f61aea9ed8eb086c062d3837605'), hexToBytes('0x0000000000000000000000000000000000000000000000000000000000000001'), diff --git a/packages/vm/test/util.ts b/packages/vm/test/util.ts index 96ed73a1ba..98d877ec09 100644 --- a/packages/vm/test/util.ts +++ b/packages/vm/test/util.ts @@ -382,11 +382,11 @@ export async function setupPreConditions(state: EVMStateManagerInterface, testDa continue } const key = setLengthLeft(format(storageKey), 32) - await state.putContractStorage(address, key, val) + await state.putStorage(address, key, val) } // Put contract code - await state.putContractCode(address, codeBuf) + await state.putCode(address, codeBuf) const storageRoot = (await state.getAccount(address))!.storageRoot