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

util,vm,evm: update the 2935 latest bytecode in test and fix address conversion issues #3447

Merged
merged 3 commits into from
Jun 12, 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
9 changes: 5 additions & 4 deletions packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
MAX_INTEGER_BIGINT,
SECP256K1_ORDER_DIV_2,
TWO_POW256,
bigIntToAddressBytes,
bigIntToBytes,
bigIntToHex,
bytesToBigInt,
bytesToHex,
concatBytes,
Expand Down Expand Up @@ -622,8 +622,8 @@ export const handlers: Map<number, OpHandler> = new Map([
return
}

const historyAddress = Address.fromString(
bigIntToHex(common.param('vm', 'historyStorageAddress'))
const historyAddress = new Address(
bigIntToAddressBytes(common.param('vm', 'historyStorageAddress'))
)
const key = setLengthLeft(bigIntToBytes(number % historyServeWindow), 32)

Expand Down Expand Up @@ -1156,7 +1156,8 @@ export const handlers: Map<number, OpHandler> = new Map([
return
}

const expectedAddress = new Address(setLengthLeft(bigIntToBytes(authority), 20).slice(-20))
// we don't want strick check here on authority being in address range just last 20 bytes
const expectedAddress = new Address(bigIntToAddressBytes(authority, false))
const account = (await runState.stateManager.getAccount(expectedAddress)) ?? new Account()

if (account.isContract()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Address {
*/
static fromString(str: string): Address {
if (!isValidAddress(str)) {
throw new Error('Invalid address')
throw new Error(`Invalid address input=${str}`)
}
return new Address(hexToBytes(str))
}
Expand Down
10 changes: 10 additions & 0 deletions packages/util/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ export const bigIntToUnpaddedBytes = (value: bigint): Uint8Array => {
return unpadBytes(bigIntToBytes(value))
}

export const bigIntToAddressBytes = (value: bigint, strict: boolean = true): Uint8Array => {
const addressBytes = bigIntToBytes(value)
if (strict && addressBytes.length > 20) {
throw Error(`Invalid address bytes length=${addressBytes.length} strict=${strict}`)
}

// setLength already slices if more than requisite length
return setLengthLeft(addressBytes, 20)
}
g11tech marked this conversation as resolved.
Show resolved Hide resolved

/**
* Convert value from number to an unpadded Uint8Array
* (useful for RLP transport)
Expand Down
31 changes: 31 additions & 0 deletions packages/util/test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { assert, describe, it } from 'vitest'
import {
Address,
addHexPrefix,
bigIntToAddressBytes,
bigIntToBytes,
bigIntToHex,
bigIntToUnpaddedBytes,
Expand Down Expand Up @@ -401,6 +402,36 @@ describe('bigIntToUnpaddedBytes', () => {
})
})

describe('bigIntToAddressBytes', () => {
const testCases = [
[
'0x0aae40965e6800cd9b1f4b05ff21581047e3f91e',
BigInt('0x0aae40965e6800cd9b1f4b05ff21581047e3f91e'),
true,
],
[
'0xe473f7e92ba2490e9fcbbe8bb9c3be3adbb74efc',
BigInt('0xe473f7e92ba2490e9fcbbe8bb9c3be3adbb74efc'),
true,
],
[
'0xae40965e6800cd9b1f4b05ff21581047e3f91e00',
BigInt('0x0aae40965e6800cd9b1f4b05ff21581047e3f91e00'),
false,
],
]

for (const [addressHex, addressBigInt, isSafe] of testCases) {
it('should correctly convert', () => {
const addressHexFromBigInt = bytesToHex(bigIntToAddressBytes(addressBigInt, false))
assert.equal(addressHex, addressHexFromBigInt, `should correctly convert ${addressBigInt}`)
if (isSafe === false) {
assert.throw(() => bigIntToAddressBytes(addressBigInt))
}
})
}
})

describe('intToUnpaddedBytes', () => {
it('should equal unpadded buffer value', () => {
assert.deepEqual(intToUnpaddedBytes(0), Uint8Array.from([]))
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/src/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
BIGINT_8,
GWEI_TO_WEI,
KECCAK256_RLP,
bigIntToAddressBytes,
bigIntToBytes,
bigIntToHex,
bytesToHex,
concatBytes,
equalsBytes,
Expand Down Expand Up @@ -488,8 +488,8 @@ export async function accumulateParentBlockHash(
if (!this.common.isActivatedEIP(2935)) {
throw new Error('Cannot call `accumulateParentBlockHash`: EIP 2935 is not active')
}
const historyAddress = Address.fromString(
bigIntToHex(this.common.param('vm', 'historyStorageAddress'))
const historyAddress = new Address(
bigIntToAddressBytes(this.common.param('vm', 'historyStorageAddress'))
)
const historyServeWindow = this.common.param('vm', 'historyServeWindow')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ const deploymentConfigs = [
// may 25 configuration with set on the lines of 4788
[
// contract code
'0x3373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35600143035500',
'0x3373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35611fff60014303165500',
// deployment tx input
'0x60608060095f395ff33373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35600143035500',
'0x60648060095f395ff33373fffffffffffffffffffffffffffffffffffffffe1460575767ffffffffffffffff5f3511605357600143035f3511604b575f35612000014311604b57611fff5f3516545f5260205ff35b5f5f5260205ff35b5f5ffd5b5f35611fff60014303165500',
// v r s
['0x1b', '0x539', '0x1b9b6eb1f0'],
// sender, hash, deployed address
[
'0x72eed28860ac985f1ec32306564b5926ea7c0b70',
'0xe43ec833884324f31c2e8314534d5b15233d84f32f05a05ea2a45649b587a9df',
'0xcc766763fcc59487cdab9f21487174417b1fa282',
'0xe473f7e92ba2490e9fcbbe8bb9c3be3adbb74efc',
'0x3c769a03d6e2212f1d26ab59ba797dce0900df29ffd23c1dd391fd6b217973ad',
'0x0aae40965e6800cd9b1f4b05ff21581047e3f91e',
],
],
]
Expand Down
Loading