Skip to content

Commit

Permalink
feat: add scale codec tests to header (#28)
Browse files Browse the repository at this point in the history
* feat: add codec tests
  • Loading branch information
danielvladco authored Aug 19, 2024
1 parent 1c8284e commit 9f97fd4
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions internal/block/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package block

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"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_HeaderEncodeDecode(t *testing.T) {
h := Header{
ParentHash: randomHash(t),
PriorStateRoot: randomHash(t),
ExtrinsicHash: randomHash(t),
TimeSlotIndex: 123,
EpochMarker: &EpochMarker{
Keys: [NumberOfValidators]crypto.BandersnatchPublicKey{
randomPublicKey(t),
randomPublicKey(t),
},
Entropy: randomHash(t),
},
WinningTicketsMarker: [jamtime.TimeslotsPerEpoch]*Ticket{{
Identifier: randomHash(t),
EntryIndex: 111,
}, {
Identifier: randomHash(t),
EntryIndex: 222,
}},
Verdicts: []crypto.Hash{
randomHash(t),
randomHash(t),
},
OffendersMarkers: []crypto.Ed25519PublicKey{
randomED25519PublicKey(t),
},
BlockAuthorIndex: 1,
VRFSignature: randomSignature(t),
BlockSealSignature: randomSignature(t),
}
serializer := serialization.NewSerializer(&codec.SCALECodec{})
bb, err := serializer.Encode(h)
if err != nil {
t.Fatal(err)
}

h2 := Header{}
err = serializer.Decode(bb, &h2)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, h, h2)
}

func randomHash(t *testing.T) crypto.Hash {
hash := make([]byte, crypto.HashSize)
_, err := rand.Read(hash)
require.NoError(t, err)
return crypto.Hash(hash)
}
func randomED25519PublicKey(t *testing.T) crypto.Ed25519PublicKey {
hash := make([]byte, ed25519.PublicKeySize)
_, err := rand.Read(hash)
require.NoError(t, err)
return hash
}
func randomPublicKey(t *testing.T) crypto.BandersnatchPublicKey {
hash := make([]byte, crypto.BandersnatchSize)
_, err := rand.Read(hash)
require.NoError(t, err)
return crypto.BandersnatchPublicKey(hash)
}
func randomSignature(t *testing.T) crypto.BandersnatchSignature {
hash := make([]byte, 96)
_, err := rand.Read(hash)
require.NoError(t, err)
return crypto.BandersnatchSignature(hash)
}

0 comments on commit 9f97fd4

Please sign in to comment.