Skip to content

Commit

Permalink
<< main
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvladco committed Aug 12, 2024
2 parents 9a9ce8d + 2b46397 commit 5616cc1
Show file tree
Hide file tree
Showing 23 changed files with 1,394 additions and 35 deletions.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/eigerco/strawberry

go 1.22.5

require github.com/ChainSafe/gossamer v0.9.0
require (
github.com/ChainSafe/gossamer v0.9.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 2 additions & 1 deletion internal/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ type Block struct {

// Extrinsic represents the block extrinsic data
type Extrinsic struct {
ET []*TicketProof
ET *TicketExtrinsic
EP *PreimageExtrinsic
ED *DisputeExtrinsic
EA *AssurancesExtrinsic
EG *GuaranteesExtrinsic
}
79 changes: 79 additions & 0 deletions internal/block/guarantee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package block

import (
"github.com/eigerco/strawberry/internal/crypto"
"github.com/eigerco/strawberry/internal/jamtime"
)

// WorkResultError represents the type of error that occurred during work execution
type WorkResultError int

// WorkResultError represents the possible errors for a work result
const (
NoError WorkResultError = iota
OutOfGas // ∞: Out of gas error
Panic // ☇: Panic error
BadCode // BAD: Service's code not available
CodeTooLarge // BIG: Code available but exceeds maximum size
)

// GuaranteesExtrinsic represents the E_G extrinsic
type GuaranteesExtrinsic struct {
Guarantees []Guarantee
}

// Guarantee represents a single guarantee within the E_G extrinsic
type Guarantee struct {
WorkReport WorkReport // The work report being guaranteed
Credentials []CredentialSignature // The credentials proving the guarantee's validity
Timeslot jamtime.Timeslot // The timeslot when this guarantee was made
}

// CredentialSignature represents a single signature within the credential
type CredentialSignature struct {
ValidatorIndex uint32 // Index of the validator providing this signature
Signature [crypto.Ed25519SignatureSize]byte // The Ed25519 signature
}

// WorkReport represents a report of completed work
type WorkReport struct {
Specification WorkPackageSpecification
Context RefinementContext
CoreIndex uint16 // N_C
AuthorizerHash crypto.Hash // H
Output []byte // Y a set of octet strings
Results []WorkResult // r ∈ ⟦L⟧_1:I: Sequence of 1 to I work results
}

// WorkPackageSpecification defines the specification of a work package
type WorkPackageSpecification struct {
Hash crypto.Hash // h ∈ H: Work package hash
Length uint32 // l ∈ N_L: Auditable work bundle length (N_L is the set of blob length values)
ErasureRoot crypto.Hash // u ∈ H: Erasure root
SegmentRoot crypto.Hash // e ∈ H: Segment root
}

// RefinementContext provides context for the refinement process
type RefinementContext struct {
AnchorHeaderHash crypto.Hash // a ∈ H: Anchor header hash
AnchorPosteriorStateRoot crypto.Hash // s ∈ H: Anchor state root
AnchorPosteriorBeefyRoot crypto.Hash // b ∈ H: Anchor Beefy root
LookupAnchorHeaderHash crypto.Hash // l ∈ H: Lookup anchor hash
LookupAnchorTimeslot jamtime.Timeslot // t ∈ N_T: Lookup anchor timeslot
PrerequisiteHash *crypto.Hash // p ∈ H?: Optional prerequisite work package hash
}

// WorkResult represents the result of a single work item
type WorkResult struct {
ServiceIndex uint32 // s ∈ N_S: Service index (N_S is the set of service indices)
CodeHash crypto.Hash // c ∈ H: Code hash
PayloadHash crypto.Hash // l ∈ H: Payload hash
GasRatio uint64 // g ∈ N_G: Gas prioritization ratio
Output WorkResultOutput // o ∈ Y ∪ J: Output or error (Y is the set of octet strings, J is the set of work execution errors)
}

// WorkResultOutput represents either the successful output or an error from a work result
type WorkResultOutput struct {
Data []byte // Represents successful output (Y)
Error WorkResultError // Represents error output (J)
}
22 changes: 11 additions & 11 deletions internal/block/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package block

import (
"github.com/eigerco/strawberry/internal/crypto"
"github.com/eigerco/strawberry/internal/time"
"github.com/eigerco/strawberry/internal/jamtime"
)

const NumberOfValidators uint16 = 1023

// Header as defined in the section 5 in the paper
type Header struct {
ParentHash crypto.Hash // Hp
PriorStateRoot crypto.Hash // Hr
ExtrinsicHash crypto.Hash // Hx
TimeSlotIndex time.Timeslot // Ht
EpochMarker *EpochMarker // He
WinningTicketsMarker [time.TimeslotsPerEpoch]*Ticket // Hw
Verdicts []crypto.Hash // Hj
ParentHash crypto.Hash // Hp
PriorStateRoot crypto.Hash // Hr
ExtrinsicHash crypto.Hash // Hx
TimeSlotIndex jamtime.Timeslot // Ht
EpochMarker *EpochMarker // He
WinningTicketsMarker [jamtime.TimeslotsPerEpoch]*Ticket // Hw
Verdicts []crypto.Hash // Hj
OffendersMarkers []crypto.Ed25519PublicKey // Ho, the culprit's and fault's public keys
BlockAuthorIndex uint16 // Hi
VRFSignature crypto.BandersnatchSignature // Hv
BlockSealSignature crypto.BandersnatchSignature // Hs
BlockAuthorIndex uint16 // Hi
VRFSignature crypto.BandersnatchSignature // Hv
BlockSealSignature crypto.BandersnatchSignature // Hs
}

// EpochMarker consists of epoch randomness and a sequence of
Expand Down
5 changes: 5 additions & 0 deletions internal/block/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ type TicketProof struct {
EntryIndex uint8 // r ∈ Nn (0, 1)
Proof [ticketProofSize]byte // RingVRF proof
}

// TicketExtrinsic represents the E_T extrinsic
type TicketExtrinsic struct {
TicketProofs []TicketProof
}
5 changes: 3 additions & 2 deletions internal/crypto/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package crypto

import (
"crypto/ed25519"
"github.com/eigerco/strawberry/internal/time"

"github.com/eigerco/strawberry/internal/jamtime"
)

type Ed25519PublicKey ed25519.PublicKey
Expand All @@ -11,4 +12,4 @@ type BlsKey [BLSSize]byte
type BandersnatchKey [BandersnatchSize]byte
type MetadataKey [MetadataSize]byte
type RingCommitment [BandersnatchRingSize]byte
type EpochKeys [time.TimeslotsPerEpoch]BandersnatchKey
type EpochKeys [jamtime.TimeslotsPerEpoch]BandersnatchKey
81 changes: 81 additions & 0 deletions internal/jamtime/epoch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package jamtime

import (
"time"
)

const (
// MinEpoch represents the first epoch in the JAM protocol.
// It corresponds to the epoch containing the JAM Epoch start time
// (12:00pm on January 1, 2024 UTC).
MinEpoch Epoch = 0

// MaxEpoch represents the last possible epoch in the JAM protocol.
// It is calculated as the maximum value of Epoch (uint32) divided by
// TimeslotsPerEpoch. This ensures that the last epoch can contain
// a full complement of timeslots without overflowing.
MaxEpoch Epoch = ^Epoch(0) / TimeslotsPerEpoch

// TimeslotsPerEpoch defines the number of timeslots in each epoch.
// In the JAM protocol, each epoch consists of exactly 600 timeslots,
// as specified in the JAM Graypaper.
TimeslotsPerEpoch = 600

// EpochDuration defines the total duration of each epoch.
// It is calculated by multiplying TimeslotsPerEpoch by TimeslotDuration,
// resulting in a duration of 1 hour per epoch.
EpochDuration = TimeslotsPerEpoch * TimeslotDuration
)

// Epoch represents a JAM Epoch
type Epoch uint32

// FromEpoch creates a JamTime from an Epoch (start of the epoch)
func FromEpoch(e Epoch) JamTime {
return JamTime{Seconds: uint64(e) * uint64(EpochDuration.Seconds())}
}

// CurrentEpoch returns the current epoch
func CurrentEpoch() Epoch {
now := Now()
return now.ToEpoch()
}

// EpochStart returns the JamTime at the start of the epoch
func (e Epoch) EpochStart() JamTime {
return FromEpoch(e)
}

// EpochEnd returns the JamTime at the end of the epoch
func (e Epoch) EpochEnd() (JamTime, error) {
if e == MaxEpoch {
// For the last epoch, we calculate its end based on the last timeslot
return FromTimeslot(MaxTimeslot), nil
}

return FromEpoch(e + 1).Add(-time.Nanosecond)
}

// NextEpoch returns the next epoch
func (e Epoch) NextEpoch() (Epoch, error) {
if e == MaxEpoch {
return e, ErrMaxEpochReached
}
return e + 1, nil
}

// PreviousEpoch returns the previous epoch
func (e Epoch) PreviousEpoch() (Epoch, error) {
if e == MinEpoch {
return e, ErrMinEpochReached
}
return e - 1, nil
}

// ValidateEpoch checks if a given Epoch is within the valid range
func ValidateEpoch(e Epoch) error {
if e > MaxEpoch {
return ErrEpochExceedsMaxJamTime
}
return nil
}
Loading

0 comments on commit 5616cc1

Please sign in to comment.