-
Notifications
You must be signed in to change notification settings - Fork 995
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
Custom Block HTR #5219
Custom Block HTR #5219
Changes from 7 commits
1e54ee2
a7f934b
4aeafcd
b9ed31c
2e3dfcd
96b6f11
3f3c2c8
5b53ff4
8575b31
bf06c6d
686c0f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,64 @@ func marshalAttestationData(data *ethpb.AttestationData) []byte { | |
return enc | ||
} | ||
|
||
func attestationRoot(att *ethpb.Attestation) ([32]byte, error) { | ||
fieldRoots := make([][32]byte, 3) | ||
|
||
// Bitfield. | ||
aggregationRoot, err := bitlistRoot(att.AggregationBits, 2048) | ||
if err != nil { | ||
return [32]byte{}, err | ||
} | ||
fieldRoots[0] = aggregationRoot | ||
|
||
dataRoot, err := attestationDataRoot(att.Data) | ||
if err != nil { | ||
return [32]byte{}, err | ||
} | ||
fieldRoots[1] = dataRoot | ||
|
||
signatureBuf := bytesutil.ToBytes96(att.Signature) | ||
packedSig, err := pack([][]byte{signatureBuf[:]}) | ||
if err != nil { | ||
return [32]byte{}, err | ||
} | ||
sigRoot, err := bitwiseMerkleize(packedSig, uint64(len(packedSig)), uint64(len(packedSig))) | ||
if err != nil { | ||
return [32]byte{}, err | ||
} | ||
fieldRoots[2] = sigRoot | ||
return bitwiseMerkleizeArrays(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots))) | ||
} | ||
|
||
func blockAttestationRoot(atts []*ethpb.Attestation) ([32]byte, error) { | ||
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. Prefer naming it 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. we use the max block attestation capacity here, so this will only apply for beacon block attestations. If we use attestations which are larger than 128 in length, this root will be incorrect. 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. makes sense 👍 |
||
roots := make([][]byte, len(atts)) | ||
for i := 0; i < len(atts); i++ { | ||
pendingRoot, err := attestationRoot(atts[i]) | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization") | ||
} | ||
roots[i] = pendingRoot[:] | ||
} | ||
|
||
attsRootsRoot, err := bitwiseMerkleize( | ||
roots, | ||
uint64(len(roots)), | ||
params.BeaconConfig().MaxAttestations, | ||
) | ||
if err != nil { | ||
return [32]byte{}, errors.Wrap(err, "could not compute block attestations merkleization") | ||
} | ||
attsLenBuf := new(bytes.Buffer) | ||
if err := binary.Write(attsLenBuf, binary.LittleEndian, uint64(len(atts))); err != nil { | ||
return [32]byte{}, errors.Wrap(err, "could not marshal epoch attestations length") | ||
} | ||
// We need to mix in the length of the slice. | ||
attsLenRoot := make([]byte, 32) | ||
copy(attsLenRoot, attsLenBuf.Bytes()) | ||
res := mixInLength(attsRootsRoot, attsLenRoot) | ||
return res, nil | ||
} | ||
|
||
func attestationDataRoot(data *ethpb.AttestationData) ([32]byte, error) { | ||
fieldRoots := make([][]byte, 5) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package stateutil_benchmark | ||
|
||
import ( | ||
"testing" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/go-ssz" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" | ||
"github.com/prysmaticlabs/prysm/shared/testutil" | ||
) | ||
|
||
func BenchmarkBlockHTR(b *testing.B) { | ||
genState, keys := testutil.DeterministicGenesisState(b, 200) | ||
conf := testutil.DefaultBlockGenConfig() | ||
blk, err := testutil.GenerateFullBlock(genState, keys, conf, 10) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
atts := make([]*ethpb.Attestation, 0, 128) | ||
for i := 0; i < 128; i++ { | ||
atts = append(atts, blk.Block.Body.Attestations[0]) | ||
} | ||
deposits, _, err := testutil.DeterministicDepositsAndKeys(16) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
blk.Block.Body.Attestations = atts | ||
blk.Block.Body.Deposits = deposits | ||
|
||
b.Run("SSZ_HTR", func(b *testing.B) { | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
if _, err := ssz.HashTreeRoot(blk.Block); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
|
||
b.Run("Custom_SSZ_HTR", func(b *testing.B) { | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
if _, err := stateutil.BlockRoot(blk.Block); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} |
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.
Instead of hard coding 2048, better to use
params.BeaconConfig.MaxValidatorsPerCommittee
It changes to 1024 and it'll take next person a while to discover this is the issue :)
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.
ah nice, catch. changing it now