Skip to content

Commit

Permalink
Merge branch 'ethereumjs:master' into common/arbitrum-one-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Cussone authored Nov 16, 2022
2 parents e21d8a1 + 5274b49 commit 4fc8ab4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/client-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- name: Build from source
run: >
docker buildx build .
-f Dockerfile.fromSouce
-f Dockerfile.fromSource
--tag ethereumjs:local-source
--platform linux/amd64
--build-arg COMMIT=$(git rev-parse HEAD)
Expand Down
9 changes: 6 additions & 3 deletions Dockerfile.fromSouce → Dockerfile.fromSource
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FROM node:16-alpine as build
WORKDIR /usr/app
RUN apk update && apk add --no-cache bash git g++ make python3 && rm -rf /var/cache/apk/*

COPY . /usr/app
WORKDIR /usr/app

COPY package.json .
COPY package-lock.json .
RUN npm i --ignore-scripts

COPY . .
RUN npm i

FROM node:16-alpine
Expand Down
41 changes: 21 additions & 20 deletions packages/client/lib/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { RLP } from '@ethereumjs/rlp'
import { TransactionFactory } from '@ethereumjs/tx'
import {
arrToBufArr,
bigIntToBuffer,
bigIntToUnpaddedBuffer,
bufArrToArr,
bufferToBigInt,
bufferToInt,
intToBuffer,
intToUnpaddedBuffer,
} from '@ethereumjs/util'
import { encodeReceipt } from '@ethereumjs/vm/dist/runBlock'

Expand Down Expand Up @@ -86,7 +86,7 @@ export class EthProtocol extends Protocol {
{
name: 'NewBlockHashes',
code: 0x01,
encode: (hashes: any[]) => hashes.map((hn) => [hn[0], bigIntToBuffer(hn[1])]),
encode: (hashes: any[]) => hashes.map((hn) => [hn[0], bigIntToUnpaddedBuffer(hn[1])]),
decode: (hashes: any[]) => hashes.map((hn) => [hn[0], bufferToBigInt(hn[1])]),
},
{
Expand Down Expand Up @@ -116,12 +116,12 @@ export class EthProtocol extends Protocol {
code: 0x03,
response: 0x04,
encode: ({ reqId, block, max, skip = 0, reverse = false }: GetBlockHeadersOpts) => [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
[
typeof block === 'bigint' ? bigIntToBuffer(block) : block,
max === 0 ? Buffer.from([]) : intToBuffer(max),
skip === 0 ? Buffer.from([]) : intToBuffer(skip),
!reverse ? Buffer.from([]) : Buffer.from([1]),
typeof block === 'bigint' ? bigIntToUnpaddedBuffer(block) : block,
intToUnpaddedBuffer(max),
intToUnpaddedBuffer(skip),
intToUnpaddedBuffer(!reverse ? 0 : 1),
],
],
decode: ([reqId, [block, max, skip, reverse]]: any) => ({
Expand All @@ -136,7 +136,7 @@ export class EthProtocol extends Protocol {
name: 'BlockHeaders',
code: 0x04,
encode: ({ reqId, headers }: { reqId: bigint; headers: BlockHeader[] }) => [
bigIntToBuffer(reqId),
bigIntToUnpaddedBuffer(reqId),
headers.map((h) => h.raw()),
],
decode: ([reqId, headers]: [Buffer, BlockHeaderBuffer[]]) => [
Expand All @@ -161,7 +161,7 @@ export class EthProtocol extends Protocol {
code: 0x05,
response: 0x06,
encode: ({ reqId, hashes }: GetBlockBodiesOpts) => [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
hashes,
],
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({
Expand All @@ -173,15 +173,15 @@ export class EthProtocol extends Protocol {
name: 'BlockBodies',
code: 0x06,
encode: ({ reqId, bodies }: { reqId: bigint; bodies: BlockBodyBuffer[] }) => [
bigIntToBuffer(reqId),
bigIntToUnpaddedBuffer(reqId),
bodies,
],
decode: ([reqId, bodies]: [Buffer, BlockBodyBuffer[]]) => [bufferToBigInt(reqId), bodies],
},
{
name: 'NewBlock',
code: 0x07,
encode: ([block, td]: [Block, bigint]) => [block.raw(), bigIntToBuffer(td)],
encode: ([block, td]: [Block, bigint]) => [block.raw(), bigIntToUnpaddedBuffer(td)],
decode: ([block, td]: [BlockBuffer, Buffer]) => [
Block.fromValuesArray(block, {
common: this.config.chainCommon,
Expand All @@ -193,13 +193,15 @@ export class EthProtocol extends Protocol {
{
name: 'NewPooledTransactionHashes',
code: 0x08,
encode: (hashes: Buffer[]) => hashes,
decode: (hashes: Buffer[]) => hashes,
},
{
name: 'GetPooledTransactions',
code: 0x09,
response: 0x0a,
encode: ({ reqId, hashes }: GetPooledTransactionsOpts) => [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
hashes,
],
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({
Expand All @@ -219,7 +221,7 @@ export class EthProtocol extends Protocol {
serializedTxs.push(tx.serialize())
}
}
return [bigIntToBuffer(reqId), serializedTxs]
return [bigIntToUnpaddedBuffer(reqId), serializedTxs]
},
decode: ([reqId, txs]: [Buffer, any[]]) => [
bufferToBigInt(reqId),
Expand All @@ -234,7 +236,7 @@ export class EthProtocol extends Protocol {
code: 0x0f,
response: 0x10,
encode: ({ reqId, hashes }: { reqId: bigint; hashes: Buffer[] }) => [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
hashes,
],
decode: ([reqId, hashes]: [Buffer, Buffer[]]) => ({
Expand All @@ -251,7 +253,7 @@ export class EthProtocol extends Protocol {
const encodedReceipt = encodeReceipt(receipt, receipt.txType)
serializedReceipts.push(encodedReceipt)
}
return [bigIntToBuffer(reqId), serializedReceipts]
return [bigIntToUnpaddedBuffer(reqId), serializedReceipts]
},
decode: ([reqId, receipts]: [Buffer, Buffer[]]) => [
bufferToBigInt(reqId),
Expand Down Expand Up @@ -325,12 +327,11 @@ export class EthProtocol extends Protocol {
*/
encodeStatus(): any {
return {
networkId: bigIntToBuffer(this.chain.networkId),
td:
this.chain.blocks.td === BigInt(0) ? Buffer.from([]) : bigIntToBuffer(this.chain.blocks.td),
networkId: bigIntToUnpaddedBuffer(this.chain.networkId),
td: bigIntToUnpaddedBuffer(this.chain.blocks.td),
bestHash: this.chain.blocks.latest!.hash(),
genesisHash: this.chain.genesis.hash(),
latestBlock: bigIntToBuffer(this.chain.blocks.latest!.header.number),
latestBlock: bigIntToUnpaddedBuffer(this.chain.blocks.latest!.header.number),
}
}

Expand Down
32 changes: 16 additions & 16 deletions packages/client/lib/net/protocol/lesprotocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlockHeader } from '@ethereumjs/block'
import { bigIntToBuffer, bufferToBigInt, bufferToInt, intToBuffer } from '@ethereumjs/util'
import { bigIntToUnpaddedBuffer, bufferToBigInt, bufferToInt, intToBuffer } from '@ethereumjs/util'

import { Protocol } from './protocol'

Expand Down Expand Up @@ -56,8 +56,8 @@ export class LesProtocol extends Protocol {
encode: ({ headHash, headNumber, headTd, reorgDepth }: any) => [
// TO DO: handle state changes
headHash,
bigIntToBuffer(headNumber),
bigIntToBuffer(headTd),
bigIntToUnpaddedBuffer(headNumber),
bigIntToUnpaddedBuffer(headTd),
intToBuffer(reorgDepth),
],
decode: ([headHash, headNumber, headTd, reorgDepth]: any) => ({
Expand All @@ -73,8 +73,13 @@ export class LesProtocol extends Protocol {
code: 0x02,
response: 0x03,
encode: ({ reqId, block, max, skip = 0, reverse = false }: GetBlockHeadersOpts) => [
bigIntToBuffer(reqId ?? ++this.nextReqId),
[typeof block === 'bigint' ? bigIntToBuffer(block) : block, max, skip, !reverse ? 0 : 1],
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
[
typeof block === 'bigint' ? bigIntToUnpaddedBuffer(block) : block,
max,
skip,
!reverse ? 0 : 1,
],
],
decode: ([reqId, [block, max, skip, reverse]]: any) => ({
reqId: bufferToBigInt(reqId),
Expand All @@ -88,8 +93,8 @@ export class LesProtocol extends Protocol {
name: 'BlockHeaders',
code: 0x03,
encode: ({ reqId, bv, headers }: any) => [
bigIntToBuffer(reqId),
bigIntToBuffer(bv),
bigIntToUnpaddedBuffer(reqId),
bigIntToUnpaddedBuffer(bv),
headers.map((h: BlockHeader) => h.raw()),
],
decode: ([reqId, bv, headers]: any) => ({
Expand Down Expand Up @@ -176,18 +181,13 @@ export class LesProtocol extends Protocol {
this.chain.genesis.hash()
)
const nextFork = this.config.chainCommon.nextHardforkBlock(this.config.chainCommon.hardfork())
const forkID = [
Buffer.from(forkHash.slice(2), 'hex'),
typeof nextFork === 'bigint' && nextFork !== BigInt(0)
? bigIntToBuffer(nextFork)
: Buffer.from([]),
]
const forkID = [Buffer.from(forkHash.slice(2), 'hex'), bigIntToUnpaddedBuffer(nextFork ?? 0n)]

return {
networkId: bigIntToBuffer(this.chain.networkId),
headTd: bigIntToBuffer(this.chain.headers.td),
networkId: bigIntToUnpaddedBuffer(this.chain.networkId),
headTd: bigIntToUnpaddedBuffer(this.chain.headers.td),
headHash: this.chain.headers.latest?.hash(),
headNum: bigIntToBuffer(this.chain.headers.height),
headNum: bigIntToUnpaddedBuffer(this.chain.headers.height),
genesisHash: this.chain.genesis.hash(),
forkID,
recentTxLookup: intToBuffer(1),
Expand Down
26 changes: 13 additions & 13 deletions packages/client/lib/net/protocol/snapprotocol.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
accountBodyFromSlim,
accountBodyToSlim,
bigIntToBuffer,
bigIntToUnpaddedBuffer,
bufferToBigInt,
setLengthLeft,
} from '@ethereumjs/util'
Expand Down Expand Up @@ -97,11 +97,11 @@ export class SnapProtocol extends Protocol {
// [reqID: P, rootHash: B_32, startingHash: B_32, limitHash: B_32, responseBytes: P]
encode: ({ reqId, root, origin, limit, bytes }: GetAccountRangeOpts) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
setLengthLeft(root, 32),
setLengthLeft(origin, 32),
setLengthLeft(limit, 32),
bigIntToBuffer(bytes),
bigIntToUnpaddedBuffer(bytes),
]
},
decode: ([reqId, root, origin, limit, bytes]: any) => {
Expand All @@ -128,7 +128,7 @@ export class SnapProtocol extends Protocol {
proof: Buffer[]
}) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
accounts.map((account) => [
setLengthLeft(account.hash, 32),
accountBodyToSlim(account.body),
Expand Down Expand Up @@ -157,12 +157,12 @@ export class SnapProtocol extends Protocol {
// [reqID: P, rootHash: B_32, accountHashes: [B_32], startingHash: B, limitHash: B, responseBytes: P]
encode: ({ reqId, root, accounts, origin, limit, bytes }: GetStorageRangesOpts) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
setLengthLeft(root, 32),
accounts.map((acc) => setLengthLeft(acc, 32)),
origin,
limit,
bigIntToBuffer(bytes),
bigIntToUnpaddedBuffer(bytes),
]
},
decode: ([reqId, root, accounts, origin, limit, bytes]: any) => {
Expand Down Expand Up @@ -190,7 +190,7 @@ export class SnapProtocol extends Protocol {
proof: Buffer[]
}) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
slots.map((accSlots) =>
accSlots.map((slotData) => [setLengthLeft(slotData.hash, 32), slotData.body])
),
Expand All @@ -214,9 +214,9 @@ export class SnapProtocol extends Protocol {
// [reqID: P, hashes: [hash1: B_32, hash2: B_32, ...], bytes: P]
encode: ({ reqId, hashes, bytes }: GetByteCodesOpts) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
hashes.map((hash) => setLengthLeft(hash, 32)),
bigIntToBuffer(bytes),
bigIntToUnpaddedBuffer(bytes),
]
},
decode: ([reqId, hashes, bytes]: any) => {
Expand All @@ -232,7 +232,7 @@ export class SnapProtocol extends Protocol {
code: 0x05,
// [reqID: P, codes: [code1: B, code2: B, ...]]
encode: ({ reqId, codes }: { reqId: bigint; codes: Buffer[] }) => {
return [bigIntToBuffer(reqId ?? ++this.nextReqId), codes]
return [bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId), codes]
},
decode: ([reqId, codes]: any) => {
return {
Expand All @@ -248,10 +248,10 @@ export class SnapProtocol extends Protocol {
// [reqID: P, rootHash: B_32, paths: [[accPath: B, slotPath1: B, slotPath2: B, ...]...], bytes: P]
encode: ({ reqId, root, paths, bytes }: GetTrieNodesOpts) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId),
setLengthLeft(root, 32),
paths,
bigIntToBuffer(bytes),
bigIntToUnpaddedBuffer(bytes),
]
},
decode: ([reqId, root, paths, bytes]: any) => {
Expand All @@ -268,7 +268,7 @@ export class SnapProtocol extends Protocol {
code: 0x07,
// [reqID: P, nodes: [node1: B, node2: B, ...]]
encode: ({ reqId, nodes }: { reqId: bigint; nodes: Buffer[] }) => {
return [bigIntToBuffer(reqId ?? ++this.nextReqId), nodes]
return [bigIntToUnpaddedBuffer(reqId ?? ++this.nextReqId), nodes]
},
decode: ([reqId, nodes]: any) => {
return {
Expand Down
4 changes: 4 additions & 0 deletions packages/util/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,7 @@ export const bigIntToHex = (num: bigint) => {
export function bigIntToUnpaddedBuffer(value: bigint): Buffer {
return unpadBuffer(bigIntToBuffer(value))
}

export function intToUnpaddedBuffer(value: number): Buffer {
return unpadBuffer(intToBuffer(value))
}
9 changes: 9 additions & 0 deletions packages/util/test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
fromSigned,
intToBuffer,
intToHex,
intToUnpaddedBuffer,
isZeroAddress,
setLengthLeft,
setLengthRight,
Expand Down Expand Up @@ -487,6 +488,14 @@ tape('bigIntToUnpaddedBuffer', function (t) {
})
})

tape('intToUnpaddedBuffer', function (t) {
t.test('should equal unpadded buffer value', function (st) {
st.ok(intToUnpaddedBuffer(0).equals(Buffer.from([])))
st.ok(intToUnpaddedBuffer(100).equals(Buffer.from('64', 'hex')))
st.end()
})
})

tape('bigIntToHex', (st) => {
st.equal(bigIntToHex(BigInt(1)), '0x1')
st.end()
Expand Down

0 comments on commit 4fc8ab4

Please sign in to comment.