Skip to content

Commit

Permalink
VM/SM/Blockchain/Trie/EVM: Copy() -> shallowCopy() function renaming (#…
Browse files Browse the repository at this point in the history
…2826)

* Rename vm copy function to shallowCopy

* Rename StateManager copy function to shallowCopy

* Rename evm copy function to shallowCopy

* Rename Blockchain copy function to shallowCopy

* Fix interface copy function naming

* Rename Trie copy function to shallowCopy

* Fix tests

* Fix tests

* Fix test

* Update mocked objects in tests to reflect naming change

* Fix test

* Update examples and recipes to reflect naming changes

* Fix tests

* Update documentation to reflect naming changes

* Update benchmarks and tests

---------

Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
scorbajio and holgerd77 committed Jul 3, 2023
1 parent 49cf084 commit 629c80f
Show file tree
Hide file tree
Showing 60 changed files with 125 additions and 125 deletions.
4 changes: 2 additions & 2 deletions packages/blockchain/docs/classes/Blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ The genesis Block for the blockchain.

___

### copy
### shallowCopy

**copy**(): [`Blockchain`](Blockchain.md)
**shallowCopy**(): [`Blockchain`](Blockchain.md)

Returns a deep copy of this [Blockchain](Blockchain.md) instance.

Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/docs/interfaces/blockchaininterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@

## Methods

### copy
### shallowCopy

**copy**(): [`BlockchainInterface`](BlockchainInterface.md)
**shallowCopy**(): [`BlockchainInterface`](BlockchainInterface.md)

Returns a copy of the blockchain

Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class Blockchain implements BlockchainInterface {
* set the {@link db} of this returned instance to a copy of
* the original.
*/
copy(): Blockchain {
shallowCopy(): Blockchain {
const copiedBlockchain = Object.create(
Object.getPrototypeOf(this),
Object.getOwnPropertyDescriptors(this)
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface BlockchainInterface {
/**
* Returns a copy of the blockchain
*/
copy(): BlockchainInterface
shallowCopy(): BlockchainInterface

/**
* Validates a block header, throwing if invalid. It is being validated against the reported `parentHash`.
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export class VMExecution extends Execution {
*/
async executeBlocks(first: number, last: number, txHashes: string[]) {
this.config.logger.info('Preparing for block execution (debug mode, no services started)...')
const vm = await this.vm.copy()
const vm = await this.vm.shallowCopy()

for (let blockNumber = first; blockNumber <= last; blockNumber++) {
const block = await vm.blockchain.getBlock(blockNumber)
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/miner/miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class Miner {
// Use a copy of the vm to not modify the existing state.
// The state will be updated when the newly assembled block
// is inserted into the canonical chain.
const vmCopy = await this.execution.vm.copy()
const vmCopy = await this.execution.vm.shallowCopy()

// Set the state root to ensure the resulting state
// is based on the parent block's state
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/rpc/modules/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Debug {
const tx = block.transactions[txIndex]

// Copy VM so as to not modify state when running transactions being traced
const vmCopy = await this.service.execution.vm.copy()
const vmCopy = await this.service.execution.vm.shallowCopy()
await vmCopy.stateManager.setStateRoot(parentBlock.header.stateRoot)
for (let x = 0; x < txIndex; x++) {
// Run all txns in the block prior to the traced transaction
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/rpc/modules/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ export class Engine {
}

const payloadId = await this.pendingBlock.start(
await this.vm.copy(),
await this.vm.shallowCopy(),
headBlock,
{
timestamp,
Expand Down
16 changes: 8 additions & 8 deletions packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()
await vm.stateManager.setStateRoot(block.header.stateRoot)

const { from, to, gas: gasLimit, gasPrice, value, data } = transaction
Expand Down Expand Up @@ -467,7 +467,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()
await vm.stateManager.setStateRoot(block.header.stateRoot)

if (transaction.gas === undefined) {
Expand Down Expand Up @@ -531,7 +531,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()
await vm.stateManager.setStateRoot(block.header.stateRoot)
const account = await vm.stateManager.getAccount(address)
if (account === undefined) {
Expand Down Expand Up @@ -603,7 +603,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()
await vm.stateManager.setStateRoot(block.header.stateRoot)

const address = Address.fromString(addressHex)
Expand Down Expand Up @@ -631,7 +631,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()
// TODO: this needs more thought, keep on latest for now
const block = await getBlockByOption(blockOpt, this._chain)
await vm.stateManager.setStateRoot(block.header.stateRoot)
Expand Down Expand Up @@ -711,7 +711,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()
await vm.stateManager.setStateRoot(block.header.stateRoot)

const address = Address.fromString(addressHex)
Expand Down Expand Up @@ -781,7 +781,7 @@ export class Eth {
block.header.baseFeePerGas!
: (tx as LegacyTransaction).gasPrice

const vmCopy = await this._vm!.copy()
const vmCopy = await this._vm!.shallowCopy()
vmCopy._common.setHardfork(tx.common.hardfork())
// Run tx through copied vm to get tx gasUsed and createdAddress
const runBlockResult = await vmCopy.runBlock({
Expand Down Expand Up @@ -1014,7 +1014,7 @@ export class Eth {
throw new Error('missing vm')
}

const vm = await this._vm.copy()
const vm = await this._vm.shallowCopy()

if (!('getProof' in vm.stateManager)) {
throw new Error('getProof RPC method not supported with the StateManager provided')
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/service/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class TxPool {
}

// Copy VM in order to not overwrite the state root of the VMExecution module which may be concurrently running blocks
const vmCopy = await this.vm.copy()
const vmCopy = await this.vm.shallowCopy()
// Set state root to latest block so that account balance is correct when doing balance check
await vmCopy.stateManager.setStateRoot(block.stateRoot)
let account = await vmCopy.stateManager.getAccount(senderAddress)
Expand Down
6 changes: 3 additions & 3 deletions packages/client/test/integration/fullethereumservice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ tape('[Integration:FullEthereumService]', async (t) => {
// Stub out setStateRoot since correct state root doesn't exist in mock state.
const ogSetStateRoot = DefaultStateManager.prototype.setStateRoot
DefaultStateManager.prototype.setStateRoot = (): any => {}
const originalStateManagerCopy = DefaultStateManager.prototype.copy
DefaultStateManager.prototype.copy = function () {
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
async function setup(): Promise<[MockServer, FullEthereumService]> {
Expand Down Expand Up @@ -116,6 +116,6 @@ tape('[Integration:FullEthereumService]', async (t) => {

// unstub setStateRoot
DefaultStateManager.prototype.setStateRoot = ogSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})
})
2 changes: 1 addition & 1 deletion packages/client/test/miner/miner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tape('[Miner]', async (t) => {
},
validateHeader: () => {},
// eslint-disable-next-line no-invalid-this
copy: () => this.blockchain,
shallowCopy: () => this.blockchain,
_init: async () => undefined,
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/miner/pendingBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const setup = () => {
execution: {
vm: {
stateManager,
copy: () => service.execution.vm,
shallowCopy: () => service.execution.vm,
setStateRoot: () => {},
blockchain: mockBlockchain({}),
},
Expand Down
12 changes: 6 additions & 6 deletions packages/client/test/rpc/engine/getPayloadBodiesByHashV1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ tape(`${method}: call with too many hashes`, async (t) => {
tape(`${method}: call with valid parameters`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.copy
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.copy = function () {
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(genesisJSON, 'post-merge', {
Expand Down Expand Up @@ -107,15 +107,15 @@ tape(`${method}: call with valid parameters`, async (t) => {
await baseRequest(t, server, req, 200, expectRes)
// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})

tape(`${method}: call with valid parameters on pre-Shanghai block`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.copy
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.copy = function () {
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(
Expand Down Expand Up @@ -191,5 +191,5 @@ tape(`${method}: call with valid parameters on pre-Shanghai block`, async (t) =>
await baseRequest(t, server, req, 200, expectRes)
// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ tape(`${method}: call with invalid parameters`, async (t) => {
tape(`${method}: call with valid parameters`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.copy
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.copy = function () {
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(genesisJSON, 'post-merge', {
Expand Down Expand Up @@ -127,15 +127,15 @@ tape(`${method}: call with valid parameters`, async (t) => {
await baseRequest(t, server, req2, 200, expectRes2)
// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})

tape(`${method}: call with valid parameters on pre-Shanghai hardfork`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.copy
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.copy = function () {
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const { chain, service, server, common } = await setupChain(preShanghaiGenesisJSON, 'london', {
Expand Down Expand Up @@ -206,5 +206,5 @@ tape(`${method}: call with valid parameters on pre-Shanghai hardfork`, async (t)
await baseRequest(t, server, req, 200, expectRes)
// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})
6 changes: 3 additions & 3 deletions packages/client/test/rpc/engine/getPayloadV3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ tape(`${method}: call with unknown payloadId`, async (t) => {
tape(`${method}: call with known payload`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.copy
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.copy = function () {
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const { service, server, common } = await setupChain(genesisJSON, 'post-merge', {
Expand Down Expand Up @@ -134,5 +134,5 @@ tape(`${method}: call with known payload`, async (t) => {

await baseRequest(t, server, req, 200, expectRes, false)
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})
2 changes: 1 addition & 1 deletion packages/client/test/rpc/eth/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ tape(`${method}: call with valid arguments`, async (t) => {
return address
}
const { execResult } = await (
await vm.copy()
await vm.shallowCopy()
).runTx({
tx: estimateTx,
skipNonce: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/rpc/eth/estimateGas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tape(`${method}: call with valid arguments`, async (t) => {
return address
}
const { totalGasSpent } = await (
await vm.copy()
await vm.shallowCopy()
).runTx({
tx: estimateTx,
skipNonce: true,
Expand Down
18 changes: 9 additions & 9 deletions packages/client/test/rpc/eth/sendRawTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const method = 'eth_sendRawTransaction'
tape(`${method}: call with valid arguments`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
const originalStateManagerCopy = DefaultStateManager.prototype.copy
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.setStateRoot = function (): any {}
DefaultStateManager.prototype.copy = function () {
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const common = new Common({ chain: Chain.Mainnet })
Expand Down Expand Up @@ -69,7 +69,7 @@ tape(`${method}: call with valid arguments`, async (t) => {
await baseRequest(t, server, req, 200, expectRes)
// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})

tape(`${method}: send local tx with gasprice lower than minimum`, async (t) => {
Expand Down Expand Up @@ -176,8 +176,8 @@ tape(`${method}: call with no peers`, async (t) => {
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
DefaultStateManager.prototype.setStateRoot = (): any => {}
const originalStateManagerCopy = DefaultStateManager.prototype.copy
DefaultStateManager.prototype.copy = function () {
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
Expand Down Expand Up @@ -209,16 +209,16 @@ tape(`${method}: call with no peers`, async (t) => {

// Restore setStateRoot
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
})

tape('blob EIP 4844 transaction', async (t) => {
t.plan(2)
// Disable stateroot validation in TxPool since valid state root isn't available
const originalSetStateRoot = DefaultStateManager.prototype.setStateRoot
DefaultStateManager.prototype.setStateRoot = (): any => {}
const originalStateManagerCopy = DefaultStateManager.prototype.copy
DefaultStateManager.prototype.copy = function () {
const originalStateManagerCopy = DefaultStateManager.prototype.shallowCopy
DefaultStateManager.prototype.shallowCopy = function () {
return this
}
// Disable block header consensus format validation
Expand Down Expand Up @@ -292,6 +292,6 @@ tape('blob EIP 4844 transaction', async (t) => {

// Restore stubbed out functionality
DefaultStateManager.prototype.setStateRoot = originalSetStateRoot
DefaultStateManager.prototype.copy = originalStateManagerCopy
DefaultStateManager.prototype.shallowCopy = originalStateManagerCopy
BlockHeader.prototype._consensusFormatValidation = consensusFormatValidation
})
2 changes: 1 addition & 1 deletion packages/client/test/rpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export async function runBlockWithTxs(
const { vm } = execution
// build block with tx
const parentBlock = await chain.getCanonicalHeadBlock()
const vmCopy = await vm.copy()
const vmCopy = await vm.shallowCopy()
const blockBuilder = await vmCopy.buildBlock({
parentBlock,
headerData: {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/rpc/mockBlockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function mockBlockchain(options: any = {}) {
return Block.fromBlockData().header
},
genesisBlock: block,
copy() {
shallowCopy() {
return this
},
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/sync/txpool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const setup = () => {
getAccount: () => new Account(BigInt(0), BigInt('50000000000000000000')),
setStateRoot: async (_root: Uint8Array) => {},
},
copy: () => service.execution.vm,
shallowCopy: () => service.execution.vm,
},
},
}
Expand Down
Loading

1 comment on commit 629c80f

@Paulorenatomiguel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preciso de ajuda com os blockvhain

Please sign in to comment.