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

vm/tx: update 7702 to devnet-4 #3737

Draft
wants to merge 12 commits into
base: devnet4-contracts
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/tx/src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const paramsTx: ParamsDict = {
7702: {
// TODO: Set correct minimum hardfork
// gasPrices
perAuthBaseGas: 2500, // Gas cost of each authority item, provided the authority exists in the trie
perAuthBaseGas: 12500, // Gas cost of each authority item, provided the authority exists in the trie
perEmptyAccountCost: 25000, // Gas cost of each authority item, in case the authority does not exist in the trie
},
}
17 changes: 8 additions & 9 deletions packages/tx/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
BIGINT_0,
BIGINT_1,
MAX_INTEGER,
MAX_UINT64,
type PrefixedHexString,
SECP256K1_ORDER_DIV_2,
TypeOutput,
bytesToBigInt,
bytesToHex,
Expand Down Expand Up @@ -213,21 +210,23 @@ export class AuthorizationLists {
if (address.length !== 20) {
throw new Error('Invalid EIP-7702 transaction: address length should be 20 bytes')
}
if (bytesToBigInt(chainId) > MAX_INTEGER) {
throw new Error('Invalid EIP-7702 transaction: chainId exceeds 2^256 - 1')
if (bytesToBigInt(chainId) > MAX_UINT64) {
throw new Error('Invalid EIP-7702 transaction: chainId exceeds 2^64 - 1')
}
if (bytesToBigInt(nonce) > MAX_UINT64) {
throw new Error('Invalid EIP-7702 transaction: nonce exceeds 2^64 - 1')
}
const yParityBigInt = bytesToBigInt(yParity)
if (yParityBigInt !== BIGINT_0 && yParityBigInt !== BIGINT_1) {
throw new Error('Invalid EIP-7702 transaction: yParity should be 0 or 1')
if (yParityBigInt >= BigInt(2 ** 8)) {
throw new Error(
'Invalid EIP-7702 transaction: yParity should be fit within 1 byte (0 - 255)',
)
}
if (bytesToBigInt(r) > MAX_INTEGER) {
throw new Error('Invalid EIP-7702 transaction: r exceeds 2^256 - 1')
}
if (bytesToBigInt(s) > SECP256K1_ORDER_DIV_2) {
throw new Error('Invalid EIP-7702 transaction: s > secp256k1n/2')
if (bytesToBigInt(s) > MAX_INTEGER) {
throw new Error('Invalid EIP-7702 transaction: s exceeds 2^256 - 1')
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions packages/vm/src/runTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
BIGINT_0,
BIGINT_1,
KECCAK256_NULL,
MAX_UINT64,
SECP256K1_ORDER_DIV_2,
bytesToBigInt,
bytesToHex,
bytesToUnprefixedHex,
Expand Down Expand Up @@ -447,9 +449,20 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise<RunTxResult> {
// Address to take code from
const address = data[1]
const nonce = data[2]
if (bytesToBigInt(nonce) >= MAX_UINT64) {
// authority nonce >= 2^64 - 1. Bumping this nonce by one will not make this fit in an uint64.
// EIPs PR: https://github.com/ethereum/EIPs/pull/8938
continue
}
const s = data[5]
if (bytesToBigInt(s) > SECP256K1_ORDER_DIV_2) {
// Malleability protection to avoid "flipping" a valid signature to get
// another valid signature (which yields the same account on `ecrecover`)
// This is invalid, so skip this auth tuple
continue
}
const yParity = bytesToBigInt(data[3])
const r = data[4]
const s = data[5]

const rlpdSignedMessage = RLP.encode([chainId, address, nonce])
const toSign = keccak256(concatBytes(MAGIC, rlpdSignedMessage))
Expand Down Expand Up @@ -497,8 +510,14 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise<RunTxResult> {
account.nonce++
await vm.evm.journal.putAccount(authority, account)

const addressCode = concatBytes(DELEGATION_7702_FLAG, address)
await vm.stateManager.putCode(authority, addressCode)
if (equalsBytes(address, new Uint8Array(20))) {
// Special case (see EIP PR: https://github.com/ethereum/EIPs/pull/8929)
// If delegated to the zero address, clear the delegation of authority
await vm.stateManager.putCode(authority, new Uint8Array())
} else {
const addressCode = concatBytes(DELEGATION_7702_FLAG, address)
await vm.stateManager.putCode(authority, addressCode)
}
}
}

Expand Down
Loading