Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
domiwei committed Nov 22, 2024
1 parent 7816f7a commit c1392fc
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 30 deletions.
15 changes: 9 additions & 6 deletions cl/cltypes/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func (s *SignedBeaconBlock) Clone() clonable.Clonable {
}

func (i *IndexedAttestation) Clone() clonable.Clonable {
var attestingIndices *solid.RawUint64List
if i.AttestingIndices != nil {
attestingIndices = solid.NewRawUint64List(i.AttestingIndices.Cap(), []uint64{})
}
/*
var attestingIndices *solid.RawUint64List
if i.AttestingIndices != nil {
attestingIndices = solid.NewRawUint64List(i.AttestingIndices.Cap(), []uint64{})
}
*/
return &IndexedAttestation{
AttestingIndices: attestingIndices,
Data: &solid.AttestationData{},
//AttestingIndices: attestingIndices,
Data: &solid.AttestationData{},
}
}

Expand Down
5 changes: 4 additions & 1 deletion cl/cltypes/indexed_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,17 @@ func (i *IndexedAttestation) UnmarshalJSON(buf []byte) error {
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
if i.AttestingIndices == nil {
i.AttestingIndices = solid.NewRawUint64List(attestingIndicesLimit, nil)
}
for _, index := range tmp.AttestingIndices {
v, err := strconv.ParseUint(index, 10, 64)
if err != nil {
return err
}
i.AttestingIndices.Append(v)
}

//slot := tmp.Data.Slot
i.Data = tmp.Data
i.Signature = tmp.Signature
return nil
Expand Down
1 change: 1 addition & 0 deletions cl/cltypes/solid/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (a *Attestation) UnmarshalJSON(data []byte) error {
a.Data = temp.Data
a.Signature = temp.Signature
a.CommitteeBits = temp.CommitteeBits
return nil
}

// Deneb case
Expand Down
24 changes: 9 additions & 15 deletions cl/cltypes/solid/bitvector.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,33 +117,26 @@ func (b *BitVector) EncodingSizeSSZ() int {
}

func (b *BitVector) DecodeSSZ(buf []byte, _ int) error {
b.bitLen = len(buf) * 8
b.container = make([]byte, len(buf))
b.bitLen = b.bitCap // bitCap must be set before decoding by NewBitVector
b.container = make([]byte, b.EncodingSizeSSZ())
copy(b.container, buf)
return nil
}

func (b *BitVector) EncodeSSZ(dst []byte) ([]byte, error) {
// allocate enough space
if cap(dst) < b.EncodingSizeSSZ() {
dst = make([]byte, 0, b.EncodingSizeSSZ())
for i := 0; i < b.EncodingSizeSSZ(); i++ {
dst[i] = byte(0)
}
if len(dst) < b.EncodingSizeSSZ() {
dst = make([]byte, b.EncodingSizeSSZ())
}
copy(dst[:], b.container[:])

Check failure on line 131 in cl/cltypes/solid/bitvector.go

View workflow job for this annotation

GitHub Actions / lint

unslice: could simplify b.container[:] to b.container (gocritic)
/*
dst = append(dst, b.container...)
// pad with zeros
for i := len(b.container); i < (b.bitCap+7)/8; i++ {
dst = append(dst, 0)
}
*/
return dst, nil
}

func (b *BitVector) HashSSZ() ([32]byte, error) {
return merkle_tree.BitvectorRootWithLimit(b.container, uint64(b.bitCap))
// zero padding
buf := make([]byte, b.EncodingSizeSSZ())
copy(buf[:], b.container[:])

Check failure on line 138 in cl/cltypes/solid/bitvector.go

View workflow job for this annotation

GitHub Actions / lint

unslice: could simplify b.container[:] to b.container (gocritic)
return merkle_tree.BitvectorRootWithLimit(buf, uint64(b.bitCap))
}

func (b *BitVector) MarshalJSON() ([]byte, error) {
Expand All @@ -157,6 +150,7 @@ func (b *BitVector) UnmarshalJSON(data []byte) error {
}
b.container = hex
b.bitLen = len(hex) * 8
b.bitCap = b.bitLen
return nil
}

Expand Down
30 changes: 24 additions & 6 deletions cl/spectest/consensus_tests/appendix.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ func init() {
func addSszTests() {
TestFormats.Add("ssz_static").
With("AggregateAndProof", getSSZStaticConsensusTest(&cltypes.AggregateAndProof{})).
With("Attestation", getSSZStaticConsensusTest(&solid.Attestation{})).
//With("Attestation", getSSZStaticConsensusTest(&solid.Attestation{})).
With("AttestationData", getSSZStaticConsensusTest(&solid.AttestationData{})).
With("AttesterSlashing", getSSZStaticConsensusTest(&cltypes.AttesterSlashing{})).
With("BeaconBlock", getSSZStaticConsensusTest(cltypes.NewBeaconBlock(&clparams.MainnetBeaconConfig, clparams.DenebVersion))).
With("BeaconBlockBody", getSSZStaticConsensusTest(cltypes.NewBeaconBody(&clparams.MainnetBeaconConfig, clparams.DenebVersion))).
//With("BeaconBlockBody", getSSZStaticConsensusTest(cltypes.NewBeaconBody(&clparams.MainnetBeaconConfig, clparams.DenebVersion))).
With("BeaconBlockHeader", getSSZStaticConsensusTest(&cltypes.BeaconBlockHeader{})).
With("BeaconState", getSSZStaticConsensusTest(state.New(&clparams.MainnetBeaconConfig))).
With("BlobIdentifier", getSSZStaticConsensusTest(&cltypes.BlobIdentifier{})).
Expand All @@ -124,7 +122,6 @@ func addSszTests() {
//With("ForkData", getSSZStaticConsensusTest(&cltypes.ForkData{})).
//With("HistoricalBatch", getSSZStaticConsensusTest(&cltypes.HistoricalBatch{})).
With("HistoricalSummary", getSSZStaticConsensusTest(&cltypes.HistoricalSummary{})).
With("IndexedAttestation", getSSZStaticConsensusTest(&cltypes.IndexedAttestation{})).
With("LightClientBootstrap", getSSZStaticConsensusTest(&cltypes.LightClientBootstrap{})).
With("LightClientFinalityUpdate", getSSZStaticConsensusTest(&cltypes.LightClientFinalityUpdate{})).
With("LightClientHeader", getSSZStaticConsensusTest(&cltypes.LightClientHeader{})).
Expand All @@ -149,7 +146,28 @@ func addSszTests() {
With("Validator", getSSZStaticConsensusTest(solid.NewValidator())).
With("PendingPartialWithdrawal", getSSZStaticConsensusTest(&solid.PendingPartialWithdrawal{})).
With("WithdrawalRequest", getSSZStaticConsensusTest(&solid.WithdrawalRequest{})).
With("ExecutionRequests", getSSZStaticConsensusTest(cltypes.NewExecutionRequests(&clparams.MainnetBeaconConfig)))
With("ExecutionRequests", getSSZStaticConsensusTest(cltypes.NewExecutionRequests(&clparams.MainnetBeaconConfig))).
With("IndexedAttestation", sszStaticTestNewObjectByVersion(
func(v clparams.StateVersion) *cltypes.IndexedAttestation {
return cltypes.NewIndexedAttestation(v)
}, withTestJson())).
With("BeaconBlock", sszStaticTestNewObjectByVersion(
func(v clparams.StateVersion) *cltypes.BeaconBlock {
return cltypes.NewBeaconBlock(&clparams.MainnetBeaconConfig, v)
}, withTestJson())).
With("AttesterSlashing", sszStaticTestNewObjectByVersion(
func(v clparams.StateVersion) *cltypes.AttesterSlashing {
return cltypes.NewAttesterSlashing(v)
}, withTestJson())).
With("BeaconBlockBody", sszStaticTestNewObjectByVersion(
func(v clparams.StateVersion) *cltypes.BeaconBody {
return cltypes.NewBeaconBody(&clparams.MainnetBeaconConfig, v)
})).
With("Attestation", sszStaticTestNewObjectByVersion(
func(v clparams.StateVersion) *solid.Attestation {
return &solid.Attestation{}
}, withTestJson()))
//With("withdrawal_request", getSSZStaticConsensusTest(&solid.WithdrawalRequest{}))
// With("VoluntaryExit", getSSZStaticConsensusTest(&cltypes.VoluntaryExit{})) TODO
// With("Withdrawal", getSSZStaticConsensusTest(&types.Withdrawal{})) TODO
}
66 changes: 65 additions & 1 deletion cl/spectest/consensus_tests/ssz_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func getSSZStaticConsensusTest[T unmarshalerMarshalerHashable](ref T) spectest.H
haveRoot, err := object.HashSSZ()
require.NoError(t, err)
require.EqualValues(t, expectedRoot, haveRoot)
// Cannot test it without a config.
// Cannot test it without a config. ?????
if isBeaconState {
return nil
}
Expand Down Expand Up @@ -131,3 +131,67 @@ func getSSZStaticConsensusTest[T unmarshalerMarshalerHashable](ref T) spectest.H
return nil
})
}

func sszStaticTestNewObjectByVersion[T unmarshalerMarshalerHashable](
newObjFunc func(v clparams.StateVersion) T, opts ...func(*sszStaticTestOption),
) spectest.Handler {
return spectest.HandlerFunc(func(t *testing.T, fsroot fs.FS, c spectest.TestCase) (err error) {
testOptions := sszStaticTestOption{}
for _, opt := range opts {
opt(&testOptions)
}

// expected root
rootBytes, err := fs.ReadFile(fsroot, rootsFile)
require.NoError(t, err)
root := Root{}
require.NoError(t, yaml.Unmarshal(rootBytes, &root))
expectedRoot := libcommon.HexToHash(root.Root)

// new container
object := newObjFunc(c.Version())

// read ssz bytes and decode
snappyEncoded, err := fs.ReadFile(fsroot, serializedFile)
require.NoError(t, err)
encoded, err := utils.DecompressSnappy(snappyEncoded)
require.NoError(t, err)
if err := object.DecodeSSZ(encoded, int(c.Version())); err != nil {
return err
}

// 1. check hash root
hashRoot, err := object.HashSSZ()
require.NoError(t, err)
require.EqualValues(t, expectedRoot, hashRoot, "hash root not equal")

// 2. check ssz bytes
sszBytes, err := object.EncodeSSZ(nil)
require.NoError(t, err)
require.EqualValues(t, encoded, sszBytes, "ssz bytes not equal")

if testOptions.testJson {
jsonObject := newObjFunc(c.Version())
// make sure object data stay the same after marshal and unmarshal
jsonBytes, err := json.Marshal(object)
require.NoError(t, err, "json.Marshal failed")
require.NoError(t, json.Unmarshal(jsonBytes, jsonObject), "json.Unmarshal failed")

// check hash root again
hashRoot, err := jsonObject.HashSSZ()
require.NoError(t, err, "failed in HashSSZ")
require.Equal(t, expectedRoot, libcommon.Hash(hashRoot), "json not equal")
}
return nil
})
}

type sszStaticTestOption struct {
testJson bool
}

func withTestJson() func(*sszStaticTestOption) {
return func(opt *sszStaticTestOption) {
opt.testJson = true
}
}
4 changes: 3 additions & 1 deletion cl/ssz/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ func MarshalSSZ(buf []byte, schema ...any) (dst []byte, err error) {
startSize := len(dst)
if obj.Static() {
// If the object is static (fixed size), encode it using SSZ and update the dst
if dst, err = obj.EncodeSSZ(dst); err != nil {
encodedBytes, err := obj.EncodeSSZ(nil)
if err != nil {
return nil, err
}
dst = append(dst, encodedBytes...)
} else {
// If the object is dynamic (variable size), store the start offset and the object in separate slices
offsetsStarts = append(offsetsStarts, startSize)
Expand Down

0 comments on commit c1392fc

Please sign in to comment.