-
Notifications
You must be signed in to change notification settings - Fork 753
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: Add ssz roots capability for withdrawals hash tree root #2488
Changes from all commits
aa35820
1ae21a5
595d761
7986dce
0836d56
f868bc3
91d2740
8bbb708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
ByteVectorType, | ||
ContainerType, | ||
ListCompositeType, | ||
UintBigintType, | ||
UintNumberType, | ||
} from '@chainsafe/ssz' | ||
|
||
import { MAX_WITHDRAWALS_PER_PAYLOAD } from './constants' | ||
|
||
export const UintNum64 = new UintNumberType(8) | ||
export const UintBigInt64 = new UintBigintType(8) | ||
export const Bytes20 = new ByteVectorType(20) | ||
|
||
export const Withdrawal = new ContainerType( | ||
{ | ||
index: UintBigInt64, | ||
validatorIndex: UintBigInt64, | ||
address: Bytes20, | ||
amount: UintBigInt64, | ||
}, | ||
{ typeName: 'Withdrawal', jsonCase: 'eth2' } | ||
) | ||
export const Withdrawals = new ListCompositeType(Withdrawal, MAX_WITHDRAWALS_PER_PAYLOAD) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as tape from 'tape' | ||
|
||
import { Withdrawal, ssz } from '../src' | ||
const withdrawalsData = [ | ||
{ | ||
index: BigInt(0), | ||
validatorIndex: BigInt(65535), | ||
address: Buffer.from('0000000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('0'), | ||
}, | ||
{ | ||
index: BigInt(1), | ||
validatorIndex: BigInt(65536), | ||
address: Buffer.from('0100000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('04523128485832663883'), | ||
}, | ||
{ | ||
index: BigInt(2), | ||
validatorIndex: BigInt(65537), | ||
address: Buffer.from('0200000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('09046256971665327767'), | ||
}, | ||
{ | ||
index: BigInt(4), | ||
validatorIndex: BigInt(65538), | ||
address: Buffer.from('0300000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('13569385457497991651'), | ||
}, | ||
{ | ||
index: BigInt(4), | ||
validatorIndex: BigInt(65539), | ||
address: Buffer.from('0400000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('18446744073709551615'), | ||
}, | ||
{ | ||
index: BigInt(5), | ||
validatorIndex: BigInt(65540), | ||
address: Buffer.from('0500000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('02261564242916331941'), | ||
}, | ||
{ | ||
index: BigInt(6), | ||
validatorIndex: BigInt(65541), | ||
address: Buffer.from('0600000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('02713877091499598330'), | ||
}, | ||
{ | ||
index: BigInt(7), | ||
validatorIndex: BigInt(65542), | ||
address: Buffer.from('0700000000000000000000000000000000000000', 'hex'), | ||
amount: BigInt('03166189940082864718'), | ||
}, | ||
] | ||
|
||
tape('ssz', (t) => { | ||
t.test('withdrawals', (st) => { | ||
const withdrawals = withdrawalsData.map((wt) => Withdrawal.fromWithdrawalData(wt)) | ||
const withdrawalsValue = withdrawals.map((wt) => wt.toValue()) | ||
const sszValues = ssz.Withdrawals.toViewDU(withdrawalsData) | ||
.toValue() | ||
.map((wt) => { | ||
wt.address = Buffer.from(wt.address) | ||
return wt | ||
}) | ||
st.deepEqual(sszValues, withdrawalsValue, 'sszValues should be same as withdrawalsValue') | ||
const withdrawalsRoot = ssz.Withdrawals.hashTreeRoot(withdrawalsValue) | ||
st.equal( | ||
Buffer.from(withdrawalsRoot).toString('hex'), | ||
'bd97f65e513f870484e85927510acb291fcfb3e593c05ab7f21f206921264946', | ||
'ssz root should match' | ||
) | ||
st.end() | ||
}) | ||
|
||
const specWithdrawals = [ | ||
// https://github.com/ethereum/consensus-spec-tests/tree/v1.3.0-rc.1/tests/mainnet/capella/ssz_static/Withdrawal/ssz_random/case_0 | ||
{ | ||
index: BigInt('17107150653359250726'), | ||
validatorIndex: BigInt('1906681273455760070'), | ||
address: Buffer.from('02ab1379b6334b58df82c85d50ff1214663cba20', 'hex'), | ||
amount: BigInt('5055030296454530815'), | ||
}, | ||
] | ||
|
||
t.test('match spec v1.3.0-rc.1', (st) => { | ||
const withdrawalsRoot = ssz.Withdrawal.hashTreeRoot(specWithdrawals[0]) | ||
st.equal( | ||
Buffer.from(withdrawalsRoot).toString('hex'), | ||
'ed9cec6fb8ee22b146059d02c38940cca1dd22a00d0132b000999b983fceff95', | ||
'ssz root should match' | ||
) | ||
st.end() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,12 +83,18 @@ tape('Withdrawal', (t) => { | |
st.end() | ||
}) | ||
|
||
t.test('fromValuesArray and toJSON', (st) => { | ||
t.test('fromValuesArray, toJSON and toValue', (st) => { | ||
const withdrawals = (gethWithdrawalsBuffer as WithdrawalBuffer[]).map( | ||
Withdrawal.fromValuesArray | ||
) | ||
const withdrawalsJson = withdrawals.map((wt) => wt.toJSON()) | ||
st.deepEqual(withdrawalsGethVector, withdrawalsJson, 'Withdrawals json should match') | ||
|
||
const withdrawalsValue = withdrawals.map((wt) => wt.toValue()) | ||
st.deepEqual( | ||
withdrawalsValue.map((wt) => `0x${wt.address.toString('hex')}`), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0x3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4 |
||
withdrawalsJson.map((wt) => wt.address) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0x990De5Cc9Cc644F0703f6A09e084470a949eC370 |
||
) | ||
st.end() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0x990De5Cc9Cc644F0703f6A09e084470a949eC370 |
||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice! I wonder if we should add something like this for blob txns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes its good 👍 picked idea from ssz code structure