Skip to content

Commit

Permalink
fix tests spec
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed May 19, 2023
1 parent dd9da28 commit 2bc3f2d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
15 changes: 12 additions & 3 deletions packages/tx/src/eip4844Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
bigIntToHex,
bigIntToUnpaddedBytes,
bytesToBigInt,
bytesToHex,
bytesToPrefixedHexString,
computeVersionedHash,
concatBytes,
Expand Down Expand Up @@ -309,10 +310,12 @@ export class BlobEIP4844Transaction extends BaseTransaction<BlobEIP4844Transacti
if (networkTxValues.length !== 4) {
throw Error(`Expected 4 values in the deserialized network transaction`)
}
const [txValues, kzgCommitments, blobs, kzgProofs] =
const [txValues, blobs, kzgCommitments, kzgProofs] =
networkTxValues as BlobEIP4844NetworkValuesArray
const decodedTx = BlobEIP4844Transaction.fromValuesArray(txValues)
if (decodedTx.to !== undefined) {

// Construct the tx but don't freeze yet, we will assign blobs etc once validated
const decodedTx = BlobEIP4844Transaction.fromValuesArray(txValues, { ...opts, freeze: false })
if (decodedTx.to === undefined) {
throw Error('BlobEIP4844Transaction can not be send without a valid `to`')
}

Expand All @@ -330,6 +333,12 @@ export class BlobEIP4844Transaction extends BaseTransaction<BlobEIP4844Transacti
decodedTx.kzgCommitments = kzgCommitments
decodedTx.kzgProofs = kzgProofs

// freeze the tx
const freeze = opts?.freeze ?? true
if (freeze) {
Object.freeze(decodedTx)
}

return decodedTx
}

Expand Down
50 changes: 28 additions & 22 deletions packages/tx/test/eip4844.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tape('fromTxData using from a json', (t) => {
maxFeePerGas: '0x12a05f200',
gasLimit: '0x33450',
value: '0xbc614e',
input: '0x',
data: '0x',
v: '0x0',
r: '0x8a83833ec07806485a4ded33f24f5cea4b8d4d24dc8f357e6d446bcdae5e58a7',
s: '0x68a2ba422a50cf84c0b5fcbda32ee142196910c97198ffd99035d920c2b557f8',
Expand All @@ -82,12 +82,13 @@ tape('fromTxData using from a json', (t) => {
accessList: null,
maxFeePerDataGas: '0xb2d05e00',
versionedHashes: ['0x01b0a4cdd5f55589f5c5b4d46c76704bb6ce95c0a8c09f77f197a57808dded28'],
kzgAggregatedProof:
'0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
hash: '35cfcdb43774134e8a8b05e936222c35bc5c68b9aa672453eedf5897213b4a6b',
}
const txMeta = {
hash: 'e5e02be0667b6d31895d1b5a8b916a6761cbc9865225c6144a3e2c50936d173e',
serialized:
'034500000000a7585eaecd6b446d7e358fdc244d8d4bea5c4ff233ed4d5a480678c03e83838af857b5c220d93590d9ff9871c910691942e12ea3bdfcb5c084cf502a42baa268b357870200000000000000000000000000000000000000000000000000000000000000000000000000f2052a0100000000000000000000000000000000000000000000000000000000f2052a010000000000000000000000000000000000000000000000000000005034030000000000c00000004e61bc0000000000000000000000000000000000000000000000000000000000d5000000d5000000005ed0b200000000000000000000000000000000000000000000000000000000d500000001ffb38a7a99e3e2335be83fc74b7faa19d553124301b0a4cdd5f55589f5c5b4d46c76704bb6ce95c0a8c09f77f197a57808dded28',
'03f89b84028757b38085012a05f20085012a05f2008303345094ffb38a7a99e3e2335be83fc74b7faa19d553124383bc614e80c084b2d05e00e1a001b0a4cdd5f55589f5c5b4d46c76704bb6ce95c0a8c09f77f197a57808dded2880a08a83833ec07806485a4ded33f24f5cea4b8d4d24dc8f357e6d446bcdae5e58a7a068a2ba422a50cf84c0b5fcbda32ee142196910c97198ffd99035d920c2b557f8',
}

const c = common.copy()
c['_chainParams'] = Object.assign({}, common['_chainParams'], {
chainId: Number(txData.chainId),
Expand All @@ -97,9 +98,27 @@ tape('fromTxData using from a json', (t) => {
t.pass('Should be able to parse a json data and hash it')

t.equal(typeof tx.maxFeePerDataGas, 'bigint', 'should be able to parse correctly')
t.equal(bytesToHex(tx.serialize()), txData.serialized, 'serialization should match')
t.equal(bytesToHex(tx.serialize()), txMeta.serialized, 'serialization should match')
// TODO: fix the hash
t.equal(bytesToHex(tx.hash()), txData.hash, 'hash should match')
t.equal(bytesToHex(tx.hash()), txMeta.hash, 'hash should match')

const jsonData = tx.toJSON()
// override few fields with equivalent values to have a match
t.deepEqual(
{ ...txData, accessList: [] },
{ gasPrice: null, ...jsonData },
'toJSON should give correct json'
)

const fromSerializedTx = BlobEIP4844Transaction.fromSerializedTx(
hexToBytes(txMeta.serialized),
{ common: c }
)
t.equal(
bytesToHex(fromSerializedTx.hash()),
txMeta.hash,
'fromSerializedTx hash should match'
)
} catch (e) {
t.fail('failed to parse json data')
}
Expand All @@ -108,19 +127,6 @@ tape('fromTxData using from a json', (t) => {
}
})

tape('fromSerializedTx - from bytes', (t) => {
const serializedBlobTx = hexToBytes(
'034500000001a34a3d6d997350dfa6c9645624b0a02b1c79591fe90d574f2ee5599103fbcff03e2156483cc73cac5648fa0348b487c90cc2713a7d636df7335333ca1b18c650010000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000ff0000000000000000000000000000000000000000000000000000000000000040420f0000000000c00000000000000000000000000000000000000000000000000000000000000000000000d5000000d5000000e803000000000000000000000000000000000000000000000000000000000000d5000000013da33b9a0894b908ddbb00d96399e506515a1009016ebc7b0ffa71dc019db13caaf539032134295cc5e652fa5b82c8e67f0fd9e1'
)
try {
BlobEIP4844Transaction.fromSerializedTx(serializedBlobTx, { common })
t.pass('Should correctly deserialize blob tx from bytes')
} catch (e) {
t.fail(`Could not deserialize blob tx from bytes, Error: ${(e as Error).message}`)
}
t.end()
})

tape('EIP4844 constructor tests - invalid scenarios', (t) => {
if (isBrowser() === true) {
t.end()
Expand Down Expand Up @@ -344,8 +350,8 @@ tape('hash() and signature verification', async (t) => {
{ common }
)
t.equal(
bytesToHex(unsignedTx.unsignedHash()),
'a99daca5e246f242df985eca984d17ce1a510a780fdd5221d5635f96a5a1bebc',
bytesToHex(unsignedTx.getMessageToSign(true)),
'8ce8c3544ca173c0e8dd0e86319d4ebfe649e15a730137a6659ba3a721a9ff8b',
'produced the correct transaction hash'
)
const signedTx = unsignedTx.sign(
Expand Down

0 comments on commit 2bc3f2d

Please sign in to comment.