-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EIP4788: Beacon block root in EVM (#2810)
* common: add 4788 * evm: add beaconroot * block/header: add 4788 * vm: add 4788 * common/evm/vm: 4788 spec updates * evm: fix build [no ci] * block: add 4788 tests * vm: update 4788 + tests * Update packages/block/src/header.ts [no ci] Co-authored-by: g11tech <[email protected]> * block/vm: update beaconRoot property * block: fix test * add validation in from values array --------- Co-authored-by: g11tech <[email protected]>
- Loading branch information
1 parent
0747b4c
commit b3cb348
Showing
13 changed files
with
452 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Chain, Common, Hardfork } from '@ethereumjs/common' | ||
import { KECCAK256_RLP, bytesToHex, zeros } from '@ethereumjs/util' | ||
import { assert, describe, it } from 'vitest' | ||
|
||
import { BlockHeader } from '../src/header.js' | ||
import { Block } from '../src/index.js' | ||
|
||
describe('EIP4788 header tests', () => { | ||
it('should work', () => { | ||
const earlyCommon = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul }) | ||
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Cancun, eips: [4788] }) | ||
|
||
assert.throws( | ||
() => { | ||
BlockHeader.fromHeaderData( | ||
{ | ||
parentBeaconBlockRoot: zeros(32), | ||
}, | ||
{ | ||
common: earlyCommon, | ||
} | ||
) | ||
}, | ||
'A parentBeaconBlockRoot for a header can only be provided with EIP4788 being activated', | ||
undefined, | ||
'should throw when setting parentBeaconBlockRoot with EIP4788 not being activated' | ||
) | ||
|
||
assert.throws( | ||
() => { | ||
BlockHeader.fromHeaderData( | ||
{ | ||
dataGasUsed: 1n, | ||
}, | ||
{ | ||
common: earlyCommon, | ||
} | ||
) | ||
}, | ||
'data gas used can only be provided with EIP4844 activated', | ||
undefined, | ||
'should throw when setting dataGasUsed with EIP4844 not being activated' | ||
) | ||
assert.doesNotThrow(() => { | ||
BlockHeader.fromHeaderData( | ||
{ | ||
excessDataGas: 0n, | ||
dataGasUsed: 0n, | ||
parentBeaconBlockRoot: zeros(32), | ||
}, | ||
{ | ||
common, | ||
skipConsensusFormatValidation: true, | ||
} | ||
) | ||
}, 'correctly instantiates an EIP4788 block header') | ||
|
||
const block = Block.fromBlockData( | ||
{ | ||
header: BlockHeader.fromHeaderData({}, { common }), | ||
}, | ||
{ common, skipConsensusFormatValidation: true } | ||
) | ||
assert.equal( | ||
block.toJSON().header?.parentBeaconBlockRoot, | ||
bytesToHex(KECCAK256_RLP), | ||
'JSON output includes excessDataGas' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "EIP-4788", | ||
"number": 4788, | ||
"comment": "Beacon block root in the EVM", | ||
"url": "https://eips.ethereum.org/EIPS/eip-4788", | ||
"status": "Draft", | ||
"minimumHardfork": "cancun", | ||
"requiredEIPs": [], | ||
"gasConfig": {}, | ||
"gasPrices": { | ||
"beaconrootCost": { | ||
"v": 4200, | ||
"d": "Gas cost when calling the beaconroot stateful precompile" | ||
} | ||
}, | ||
"vm": { | ||
"historicalRootsLength": { | ||
"v": 98304, | ||
"d": "The modulo parameter of the beaconroot ring buffer in the beaconroot statefull precompile" | ||
} | ||
}, | ||
"pow": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
Address, | ||
bigIntToBytes, | ||
bytesToBigInt, | ||
setLengthLeft, | ||
short, | ||
zeros, | ||
} from '@ethereumjs/util' | ||
|
||
import { type ExecResult, OOGResult } from '../evm.js' | ||
import { ERROR, EvmError } from '../exceptions.js' | ||
|
||
import type { PrecompileInput } from './types.js' | ||
|
||
const address = Address.fromString('0x000000000000000000000000000000000000000b') | ||
|
||
export async function precompile0b(opts: PrecompileInput): Promise<ExecResult> { | ||
const data = opts.data | ||
|
||
const gasUsed = opts.common.param('gasPrices', 'beaconrootCost') | ||
if (opts._debug !== undefined) { | ||
opts._debug( | ||
`Run BEACONROOT (0x0B) precompile data=${short(opts.data)} length=${ | ||
opts.data.length | ||
} gasLimit=${opts.gasLimit} gasUsed=${gasUsed}` | ||
) | ||
} | ||
|
||
if (opts.gasLimit < gasUsed) { | ||
if (opts._debug !== undefined) { | ||
opts._debug(`BEACONROOT (0x0B) failed: OOG`) | ||
} | ||
return OOGResult(opts.gasLimit) | ||
} | ||
|
||
if (data.length < 32) { | ||
return { | ||
returnValue: new Uint8Array(0), | ||
executionGasUsed: gasUsed, | ||
exceptionError: new EvmError(ERROR.INVALID_INPUT_LENGTH), | ||
} | ||
} | ||
|
||
const timestampInput = bytesToBigInt(data.slice(0, 32)) | ||
const historicalRootsLength = BigInt(opts.common.param('vm', 'historicalRootsLength')) | ||
|
||
const timestampIndex = timestampInput % historicalRootsLength | ||
const recordedTimestamp = await opts.stateManager.getContractStorage( | ||
address, | ||
setLengthLeft(bigIntToBytes(timestampIndex), 32) | ||
) | ||
|
||
if (bytesToBigInt(recordedTimestamp) !== timestampInput) { | ||
return { | ||
executionGasUsed: gasUsed, | ||
returnValue: zeros(32), | ||
} | ||
} | ||
const timestampExtended = timestampIndex + historicalRootsLength | ||
const returnData = setLengthLeft( | ||
await opts.stateManager.getContractStorage( | ||
address, | ||
setLengthLeft(bigIntToBytes(timestampExtended), 32) | ||
), | ||
32 | ||
) | ||
|
||
if (opts._debug !== undefined) { | ||
opts._debug(`BEACONROOT (0x0B) return data=${short(returnData)}`) | ||
} | ||
|
||
return { | ||
executionGasUsed: gasUsed, | ||
returnValue: returnData, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.