Skip to content

Commit

Permalink
fix: use common serializer with scale codec
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Aug 14, 2024
1 parent 97d2c1d commit de84089
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions internal/block/header_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package block

import (
"crypto/ed25519"
"crypto/rand"
"testing"

"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/eigerco/strawberry/internal/time"
"github.com/stretchr/testify/assert"

"github.com/eigerco/strawberry/internal/crypto"
"github.com/eigerco/strawberry/internal/jamtime"
"github.com/eigerco/strawberry/pkg/serialization"
"github.com/eigerco/strawberry/pkg/serialization/codec"
)

func Test_HeaderMarshalUnmarshal(t *testing.T) {
func Test_HeaderEncodeDecode(t *testing.T) {
h := Header{
ParentHash: randomHash(),
PriorStateRoot: randomHash(),
Expand All @@ -24,28 +26,32 @@ func Test_HeaderMarshalUnmarshal(t *testing.T) {
},
Entropy: randomHash(),
},
WinningTicketsMarker: [time.TimeslotsPerEpoch]*Ticket{{
WinningTicketsMarker: [jamtime.TimeslotsPerEpoch]*Ticket{{
Identifier: randomHash(),
EntryIndex: 111,
}, {
Identifier: randomHash(),
EntryIndex: 222,
}},
JudgementsMarkers: []crypto.Hash{
Verdicts: []crypto.Hash{
randomHash(),
randomHash(),
},
PublicKeyIndex: 1,
OffendersMarkers: []crypto.Ed25519PublicKey{
randomED25519PublicKey(),
},
BlockAuthorIndex: 1,
VRFSignature: randomSignature(),
BlockSealSignature: randomSignature(),
}
bb, err := scale.Marshal(h)
serializer := serialization.NewSerializer(&codec.SCALECodec{})
bb, err := serializer.Encode(h)
if err != nil {
t.Fatal(err)
}

h2 := Header{}
err = scale.Unmarshal(bb, &h2)
err = serializer.Decode(bb, &h2)
if err != nil {
t.Fatal(err)
}
Expand All @@ -61,6 +67,14 @@ func randomHash() crypto.Hash {
}
return crypto.Hash(hash)
}
func randomED25519PublicKey() crypto.Ed25519PublicKey {
hash := make([]byte, ed25519.PublicKeySize)
_, err := rand.Read(hash)
if err != nil {
panic(err)
}
return hash
}
func randomPublicKey() crypto.BandersnatchPublicKey {
hash := make([]byte, crypto.BandersnatchSize)
_, err := rand.Read(hash)
Expand Down

0 comments on commit de84089

Please sign in to comment.