Skip to content
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

Serialize block/header with jam codec #47

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions internal/block/block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package block

import (
"crypto/rand"
"github.com/eigerco/strawberry/internal/crypto"
"github.com/eigerco/strawberry/internal/jamtime"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/require"

"github.com/eigerco/strawberry/pkg/serialization"
"github.com/eigerco/strawberry/pkg/serialization/codec"
)

func Test_BlockEncodeDecode(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: 112,
},
{
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),
}

ticketProofs := []TicketProof{
{
EntryIndex: uint8(0),
Proof: randomTicketProof(t),
},
}
ticketExtrinsic := &TicketExtrinsic{
TicketProofs: ticketProofs,
}

preimageExtrinsic := &PreimageExtrinsic{
{
ServiceIndex: uint32(1),
Data: []byte("preimage data"),
},
}

verdicts := []Verdict{
{
ReportHash: randomHash(t),
EpochIndex: uint32(1),
Judgments: []Judgment{
{
IsValid: true,
ValidatorIndex: uint16(2),
Signature: randomEd25519Signature(t),
},
},
},
}
disputeExtrinsic := &DisputeExtrinsic{
Verdicts: verdicts,
Culprits: []Culprit{
{
ReportHash: randomHash(t),
ValidatorEd25519PublicKey: randomED25519PublicKey(t),
Signature: randomEd25519Signature(t),
},
},
Faults: []Fault{
{
ReportHash: randomHash(t),
IsValid: true,
ValidatorEd25519PublicKey: randomED25519PublicKey(t),
Signature: randomEd25519Signature(t),
},
},
}

assurancesExtrinsic := &AssurancesExtrinsic{
{
Anchor: randomHash(t),
Flag: true,
ValidatorIndex: uint16(1),
Signature: randomEd25519Signature(t),
},
}

guaranteesExtrinsic := &GuaranteesExtrinsic{
Guarantees: []Guarantee{
{
WorkReport: WorkReport{
Specification: WorkPackageSpecification{
Hash: randomHash(t),
Length: uint32(100),
ErasureRoot: randomHash(t),
SegmentRoot: randomHash(t),
},
Context: RefinementContext{
AnchorHeaderHash: randomHash(t),
AnchorPosteriorStateRoot: randomHash(t),
AnchorPosteriorBeefyRoot: randomHash(t),
LookupAnchorHeaderHash: randomHash(t),
LookupAnchorTimeslot: 125,
PrerequisiteHash: nil,
},
CoreIndex: uint16(1),
AuthorizerHash: randomHash(t),
Output: []byte("output data"),
Results: []WorkResult{
{
ServiceIndex: uint32(1),
CodeHash: randomHash(t),
PayloadHash: randomHash(t),
GasRatio: uint64(10),
Output: WorkResultOutput{
Data: []byte("work result data"),
Error: NoError,
},
},
},
},
Credentials: []CredentialSignature{
{
ValidatorIndex: uint32(1),
Signature: randomEd25519Signature(t),
},
},
Timeslot: 200,
},
},
}

e := Extrinsic{
ET: ticketExtrinsic,
EP: preimageExtrinsic,
ED: disputeExtrinsic,
EA: assurancesExtrinsic,
EG: guaranteesExtrinsic,
}

originalBlock := Block{
Header: &h,
Extrinsic: &e,
}

serializer := serialization.NewSerializer(&codec.JAMCodec{})
serialized, err := serializer.Encode(originalBlock)
require.NoError(t, err)

var deserializedBlock Block
err = serializer.Decode(serialized, &deserializedBlock)
require.NoError(t, err)

assert.Equal(t, originalBlock, deserializedBlock)
}

func randomTicketProof(t *testing.T) [ticketProofSize]byte {
var hash [ticketProofSize]byte
_, err := rand.Read(hash[:])
require.NoError(t, err)

return hash
}

func randomEd25519Signature(t *testing.T) [crypto.Ed25519SignatureSize]byte {
var hash [crypto.Ed25519SignatureSize]byte
_, err := rand.Read(hash[:])
require.NoError(t, err)

return hash
}
14 changes: 6 additions & 8 deletions internal/block/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package block
import (
"crypto/ed25519"
"crypto/rand"
"github.com/eigerco/strawberry/internal/jamtime"
"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"
)
Expand Down Expand Up @@ -45,17 +45,13 @@ func Test_HeaderEncodeDecode(t *testing.T) {
VRFSignature: randomSignature(t),
BlockSealSignature: randomSignature(t),
}
serializer := serialization.NewSerializer[uint64](&codec.SCALECodec[uint64]{})
serializer := serialization.NewSerializer(&codec.JAMCodec{})
bb, err := serializer.Encode(h)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

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

assert.Equal(t, h, h2)
}
Expand All @@ -66,6 +62,7 @@ func randomHash(t *testing.T) crypto.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)
Expand All @@ -78,6 +75,7 @@ func randomPublicKey(t *testing.T) crypto.BandersnatchPublicKey {
require.NoError(t, err)
return crypto.BandersnatchPublicKey(hash)
}

func randomSignature(t *testing.T) crypto.BandersnatchSignature {
hash := make([]byte, 96)
_, err := rand.Read(hash)
Expand Down
6 changes: 1 addition & 5 deletions pkg/serialization/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ type Uint interface {
uint8 | uint16 | uint32 | uint64
}

type Codec[T Uint] interface {
type Codec interface {
Marshal(v interface{}) ([]byte, error)
MarshalGeneral(x uint64) ([]byte, error)
MarshalTrivialUint(x T, l uint8) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
UnmarshalGeneral(data []byte, v *uint64) error
UnmarshalTrivialUint(data []byte, v *T) error
}
Loading