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

Merged
merged 6 commits into from
Nov 1, 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
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
5 changes: 2 additions & 3 deletions packages/tx/test/eip7702.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
BIGINT_1,
MAX_INTEGER,
MAX_UINT64,
SECP256K1_ORDER_DIV_2,
bigIntToHex,
createAddressFromPrivateKey,
createZeroAddress,
Expand Down Expand Up @@ -86,9 +85,9 @@ describe('[EOACode7702Transaction]', () => {
{ nonce: bigIntToHex(MAX_UINT64 + BIGINT_1) },
'Invalid EIP-7702 transaction: nonce exceeds 2^64 - 1',
],
[{ yParity: '0x2' }, 'yParity should be 0 or 1'],
[{ yParity: '0x0100' }, 'yParity should be < 2^8'],
[{ r: bigIntToHex(MAX_INTEGER + BIGINT_1) }, 'r exceeds 2^256 - 1'],
[{ s: bigIntToHex(SECP256K1_ORDER_DIV_2 + BIGINT_1) }, 's > secp256k1n/2'],
[{ s: bigIntToHex(MAX_INTEGER + BIGINT_1) }, 's exceeds 2^256 - 1'],
[{ yParity: '0x0002' }, 'yParity cannot have leading zeros'],
[{ r: '0x0001' }, 'r cannot have leading zeros'],
[{ s: '0x0001' }, 's cannot have leading zeros'],
Expand Down
30 changes: 27 additions & 3 deletions packages/vm/src/runTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
BIGINT_0,
BIGINT_1,
KECCAK256_NULL,
MAX_UINT64,
SECP256K1_ORDER_DIV_2,
bytesToBigInt,
bytesToHex,
bytesToUnprefixedHex,
Expand Down Expand Up @@ -456,9 +458,25 @@
// 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

Check warning on line 464 in packages/vm/src/runTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runTx.ts#L464

Added line #L464 was not covered by tests
}
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

Check warning on line 471 in packages/vm/src/runTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runTx.ts#L471

Added line #L471 was not covered by tests
}
const yParity = bytesToBigInt(data[3])

if (yParity > BIGINT_1) {
continue

Check warning on line 476 in packages/vm/src/runTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runTx.ts#L476

Added line #L476 was not covered by tests
}

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 @@ -506,8 +524,14 @@
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())

Check warning on line 530 in packages/vm/src/runTx.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/runTx.ts#L530

Added line #L530 was not covered by tests
} else {
const addressCode = concatBytes(DELEGATION_7702_FLAG, address)
await vm.stateManager.putCode(authority, addressCode)
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions packages/vm/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,17 @@ export function makeTx(
if (signature.v !== undefined) {
signature.yParity = bytesToHex(unpadBytes(hexToBytes(signature.v)))
}
if (signature.nonce !== undefined && signature.nonce[0] === '0x00') {
signature.nonce[0] = '0x'
if (signature.r !== undefined) {
signature.r = bytesToHex(unpadBytes(hexToBytes(signature.r)))
}
if (signature.s !== undefined) {
signature.s = bytesToHex(unpadBytes(hexToBytes(signature.s)))
}
if (signature.chainId !== undefined) {
signature.chainId = bytesToHex(unpadBytes(hexToBytes(signature.chainId)))
}
if (signature.nonce !== undefined && signature.nonce === '0x00') {
signature.nonce = '0x'
}
}
tx = createEOACode7702Tx(txData, opts)
Expand Down
Loading