Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP 6110 fixes #3397

Merged
merged 8 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,24 +657,26 @@ export class Block {
return result
}

async requestsTrieIsValid(): Promise<boolean> {
async requestsTrieIsValid(requestsInput?: CLRequest[]): Promise<boolean> {
if (!this.common.isActivatedEIP(7685)) {
throw new Error('EIP 7685 is not activated')
}

let result
if (this.requests!.length === 0) {
result = equalsBytes(this.header.requestsRoot!, KECCAK256_RLP)
return result
}
const requests = requestsInput ?? this.requests!

if (this.cache.requestsRoot === undefined) {
this.cache.requestsRoot = await Block.genRequestsTrieRoot(this.requests!)
if (requests!.length === 0) {
return equalsBytes(this.header.requestsRoot!, KECCAK256_RLP)
}

result = equalsBytes(this.cache.requestsRoot, this.header.requestsRoot!)

return result
if (requestsInput === undefined) {
if (this.cache.requestsRoot === undefined) {
this.cache.requestsRoot = await Block.genRequestsTrieRoot(this.requests!)
}
return equalsBytes(this.cache.requestsRoot, this.header.requestsRoot!)
} else {
const reportedRoot = await Block.genRequestsTrieRoot(requests)
return equalsBytes(reportedRoot, this.header.requestsRoot!)
}
}
/**
* Validates transaction signatures and minimum gas requirements.
Expand Down
48 changes: 46 additions & 2 deletions packages/vm/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Common } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import {
Address,
BIGINT_0,
CLRequest,
bigIntToBytes,
bytesToBigInt,
bytesToHex,
bytesToInt,
setLengthLeft,
Expand Down Expand Up @@ -125,9 +127,51 @@ const accumulateDeposits = async (
withdrawalCredsIdx + 32,
withdrawalCredsIdx + 32 + withdrawalCredsSize
)
const amount = log[2].slice(amountIdx + 32, amountIdx + 32 + amountSize)
const amountBytes = log[2].slice(amountIdx + 32, amountIdx + 32 + amountSize)
const amountBytesBigEndian = new Uint8Array([
amountBytes[7],
amountBytes[6],
amountBytes[5],
amountBytes[4],
amountBytes[3],
amountBytes[2],
amountBytes[1],
amountBytes[0],
])
const amountBigInt = bytesToBigInt(amountBytesBigEndian)

let amount: Uint8Array
if (amountBigInt === BIGINT_0) {
amount = new Uint8Array()
} else {
amount = bigIntToBytes(amountBigInt)
}

const signature = log[2].slice(sigIdx + 32, sigIdx + 32 + sigSize)
const index = log[2].slice(indexIdx + 32, indexIdx + 32 + indexSize)

const indexBytes = log[2].slice(indexIdx + 32, indexIdx + 32 + indexSize)

// Convert the little-endian array to big-endian array
const indexBytesBigEndian = new Uint8Array([
indexBytes[7],
indexBytes[6],
indexBytes[5],
indexBytes[4],
indexBytes[3],
indexBytes[2],
indexBytes[1],
indexBytes[0],
])
const indexBigInt = bytesToBigInt(indexBytesBigEndian)

let index: Uint8Array

if (indexBigInt === BIGINT_0) {
index = new Uint8Array()
} else {
index = bigIntToBytes(indexBigInt)
}

requests.push(
new CLRequest(0x0, RLP.encode([pubkey, withdrawalCreds, amount, signature, index]))
)
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/src/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ export async function runBlock(this: VM, opts: RunBlockOpts): Promise<RunBlockRe
block = Block.fromBlockData(blockData, { common: this.common })
} else {
if (this.common.isActivatedEIP(7685)) {
const valid = await block.requestsTrieIsValid()
const valid = await block.requestsTrieIsValid(requests)
if (!valid) {
const validRoot = await Block.genRequestsTrieRoot(block.requests!)
const validRoot = await Block.genRequestsTrieRoot(requests!)
if (this.DEBUG)
debug(
`Invalid requestsRoot received=${bytesToHex(
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/test/api/EIPs/eip-7685.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('EIP-7685 runBlock tests', () => {
},
{ common }
)
await expect(async () => vm.runBlock({ block })).rejects.toThrow('invalid block stateRoot')
await expect(async () => vm.runBlock({ block })).rejects.toThrow(/invalid requestsRoot/)
})
it('should error when requestsRoot does not match requests provided', async () => {
const vm = await setupVM({ common })
Expand Down
Loading