-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,395 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.