statetransition

package
v0.6.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OnTransferCost = 10
	AccumulateCost = 10
)

Variables

View Source
var (
	ErrTimeslotOutOfRange   = errors.New("timeslot out of range")
	ErrWrongAssignment      = errors.New("wrong assignment")
	ErrBadSignature         = errors.New("bad signature")
	ErrBadAttestationParent = errors.New("bad attestation parent")
	ErrCoreNotEngaged       = errors.New("core not engaged")
	ErrBadValidatorIndex    = errors.New("bad validator index")
)

Functions

func CalculateIntermediateCoreAssignments

func CalculateIntermediateCoreAssignments(assurances block.AssurancesExtrinsic, coreAssignments state.CoreAssignments, header block.Header) (state.CoreAssignments, []*block.WorkReport, error)

CalculateIntermediateCoreAssignments implements equations

4.13: ρ‡ ≺ (EA, ρ†)
4.15: W* ≺ (EA, ρ†). Note there's a typo in the paper, which states ρ' but that isn't correct.

It calculates the intermediate core assignments based on availability assurances, and also returns the set of now avaiable work reports. (GP v0.6.5)

func CalculateIntermediateCoreAssignmentsFromExtrinsics

func CalculateIntermediateCoreAssignmentsFromExtrinsics(disputes block.DisputeExtrinsic, coreAssignments state.CoreAssignments) state.CoreAssignments

CalculateIntermediateCoreAssignmentsFromExtrinsics Equation 25: ρ† ≺ (ED , ρ)

func CalculateIntermediateCoreFromAssurances

func CalculateIntermediateCoreFromAssurances(validators safrole.ValidatorsData, assignments state.CoreAssignments, header block.Header, assurances block.AssurancesExtrinsic) (state.CoreAssignments, []*block.WorkReport, error)

CalculateIntermediateCoreFromAssurances implements equations

4.13: ρ‡ ≺ (EA, ρ†)
4.15: W* ≺ (EA, ρ†). Note there's a typo in the paper, which states ρ' but that isn't correct.

It calculates the intermediate core assignments based on availability assurances, and also returns the set of now avaiable work reports. It also validates that the assurance extrinsic, checking signatures and that ordering is correct with no duplicates. Signatures should be checked using the prior state active validators, ie κ. (GP v0.6.5)

func CalculateIntermediateServiceState

func CalculateIntermediateServiceState(
	preimages block.PreimageExtrinsic,
	serviceState service.ServiceState,
	newTimeslot jamtime.Timeslot,
) (service.ServiceState, error)

CalculateIntermediateServiceState implements Equations 12.28–12.33 v0.6.3 This function calculates the intermediate service state δ† based on: - The current service state δ (serviceState) - The preimage extrinsic EP (preimages) - The new timeslot τ′ (newTimeslot)

For each preimage in EP:

  1. It adds the preimage p to the PreimageLookup of service s, keyed by its hash H(p)
  2. It adds a new entry to the PreimageMeta of service s, keyed by the hash H(p) and length |p|, with the value being the new timeslot τ′

The function returns a new ServiceState without modifying the input state.

func CalculateNewActivityStatistics

func CalculateNewActivityStatistics(
	blk block.Block,
	prevTimeslot jamtime.Timeslot,
	activityStatistics validator.ActivityStatisticsState,
	reporters crypto.ED25519PublicKeySet,
	currValidators safrole.ValidatorsData,
	availableWorkReports []block.WorkReport,
	accumulationStats AccumulationStats,
	transferStats DeferredTransfersStats,
) validator.ActivityStatisticsState

CalculateNewActivityStatistics updates activity statistics. It implements equation 4.20: π′ ≺ (EG, EP , EA, ET , τ, κ′, π, H, I, X) And the entire section 13. TODO complete service and core stats. For now we only support service stats for preimages, and no core stats yet.

func CalculateNewCoreAssignments

func CalculateNewCoreAssignments(
	guarantees block.GuaranteesExtrinsic,
	intermediateAssignments state.CoreAssignments,
	validatorState validator.ValidatorState,
	newTimeslot jamtime.Timeslot,
	entropyPool state.EntropyPool,
) (newAssignments state.CoreAssignments, reporters crypto.ED25519PublicKeySet, err error)

CalculateNewCoreAssignments updates the core assignments based on new guarantees. This implements equation 27: ρ′ ≺ (EG, ρ‡, κ, τ′)

It also implements part of equation 11.26 v0.6.2 regarding timeslot validation: R(⌊τ′/R⌋ - 1) ≤ t ≤ τ′

func CalculateNewCoreAuthorizations

func CalculateNewCoreAuthorizations(header block.Header, guarantees block.GuaranteesExtrinsic, pendingAuthorizations state.PendingAuthorizersQueues, currentAuthorizations state.CoreAuthorizersPool) state.CoreAuthorizersPool

CalculateNewCoreAuthorizations implements equation 4.19: α' ≺ (H, EG, φ', α) . Graypaper 0.5.4

func CalculateNewCoreStatistics

func CalculateNewCoreStatistics(
	blk block.Block,
	coreStats [common.TotalNumberOfCores]validator.CoreStatistics,
	availableReports []block.WorkReport,
) [common.TotalNumberOfCores]validator.CoreStatistics

CalculateNewCoreStatistics updates core statistics. It implements equations 13.8 - 13.10.

func CalculateNewJudgements

func CalculateNewJudgements(priorTimeslot jamtime.Timeslot, disputes block.DisputeExtrinsic, stateJudgements state.Judgements, validators validator.ValidatorState) (state.Judgements, error)

calculateNewJudgements Equation 23: ψ′ ≺ (ED, ψ) Equations 112-115:(v0.4.5) ψ'g ≡ ψg ∪ {r | {r, ⌊2/3V⌋ + 1} ∈ V} ψ'b ≡ ψb ∪ {r | {r, 0} ∈ V} ψ'w ≡ ψw ∪ {r | {r, ⌊1/3V⌋} ∈ V} ψ'o ≡ ψo ∪ {k | (r, k, s) ∈ c} ∪ {k | (r, v, k, s) ∈ f}

func CalculateNewServiceStatistics

func CalculateNewServiceStatistics(
	blk block.Block,
	accumulationStats AccumulationStats,
	transferStats DeferredTransfersStats,
) validator.ServiceStatistics

CalculateNewServiceStatistics updates service statistics. It implements equation 13.11 - 13.15. TODO complete service stats, for now this only supports preimage stats.

func CalculateNewTimeState

func CalculateNewTimeState(header block.Header) jamtime.Timeslot

CalculateNewTimeState Equation 16: τ′ ≺ H

func CalculateNewValidatorStatistics

func CalculateNewValidatorStatistics(
	blk block.Block,
	prevTimeslot jamtime.Timeslot,
	validatorStatsCurrent, validatorStatsLast [common.NumberOfValidators]validator.ValidatorStatistics,
	reporters crypto.ED25519PublicKeySet,
	currValidators safrole.ValidatorsData,
) ([common.NumberOfValidators]validator.ValidatorStatistics, [common.NumberOfValidators]validator.ValidatorStatistics)

CalculateNewValidatorStatistics updates validator statistics. It implements equations 13.3 - 13.5.

func CalculateWorkReportsAndAccumulate

func CalculateWorkReportsAndAccumulate(header *block.Header, currentState *state.State, newTimeslot jamtime.Timeslot, workReports []block.WorkReport) (
	newAccumulationQueue state.AccumulationQueue,
	newAccumulationHistory state.AccumulationHistory,
	postAccumulationServiceState service.ServiceState,
	newPrivilegedServices service.PrivilegedServices,
	newValidatorKeys safrole.ValidatorsData,
	newPendingAuthorizersQueues state.PendingAuthorizersQueues,
	accumulationOutputLog ServiceHashPairs,
	accumulationStats AccumulationStats,
	transfersStats DeferredTransfersStats,
)

CalculateWorkReportsAndAccumulate implements equations. We pass W instead of W* because we also need WQ for updating the state queue. eq. 4.16: (ϑ′, ξ′, δ‡, χ′, ι′, φ′, θ′, I, X) ≺ (W*, ϑ, ξ, δ, χ, ι, φ, τ, τ′)

func GetAvailableWorkReports

func GetAvailableWorkReports(coreAssignments state.CoreAssignments) (workReports []block.WorkReport)

W ≡ [ ρ†[c]w | c <− N_C, ∑ [a∈E_A] a_f [c] > 2/3 V ]

func InvokePVMOnTransfer

func InvokePVMOnTransfer(serviceState service.ServiceState, slot jamtime.Timeslot, serviceIndex block.ServiceId, transfers []service.DeferredTransfer) (service.ServiceAccount, uint64)

InvokePVMOnTransfer On-Transfer service-account invocation (ΨT). The only state alteration it facilitates are basic alteration to the storage of the subject account

func PermuteAssignments

func PermuteAssignments(entropy crypto.Hash, timeslot jamtime.Timeslot) ([]uint32, error)

PermuteAssignments generates the core assignments for validators. Implements Equation (11.20 v0.6.2): P(e, t) ≡ R(F([⌊C ⋅ i/V⌋ ∣i ∈ NV], e), ⌊t mod E/R⌋)

func RotateSequence

func RotateSequence(sequence []uint32, n uint32) []uint32

RotateSequence rotates the sequence by n positions modulo C. Implements Equation (11.18 v.0.5.0): R(c, n) ≡ [(x + n) mod C ∣ x ∈ shuffledSequence]

func UpdateRecentBlocks

func UpdateRecentBlocks(
	headerHash crypto.Hash,
	priorStateRoot crypto.Hash,
	accumulationRoot crypto.Hash,
	intermediateRecentBlocks []state.BlockState,
	workPackageMapping map[crypto.Hash]crypto.Hash) (newRecentBlocks []state.BlockState, err error)

UpdateRecentBlocks updates β. It takes the final inputs from Equation 83: let n = {p, h ▸▸ H(H), b, s ▸▸ H_0} and produces Equation 84: β′ ≡ ←────── β† n_H. We separate out this logic for ease of testing aganist the recent history test vectors.

func UpdateState

func UpdateState(s *state.State, newBlock block.Block, chain *store.Chain) error

UpdateState updates the state TODO: all the calculations which are not dependent on intermediate / new state can be done in parallel

it might be worth making State immutable and make it so that UpdateState returns a new State with all the updated fields

func ValidateExtrinsicGuarantees

func ValidateExtrinsicGuarantees(
	header block.Header,
	currentState *state.State,
	guarantees block.GuaranteesExtrinsic,
	currentAssignment state.CoreAssignments,
	newTimeslot jamtime.Timeslot,
	chain *store.Chain,
) error

Types

type AccumulationStatEntry

type AccumulationStatEntry struct {
	AccumulateGasUsed uint64
	AccumulateCount   uint32
}

type AccumulationStats

type AccumulationStats map[block.ServiceId]AccumulationStatEntry

AccumulationStats I ∈ D⟨NS →(NG, N)⟩ (eq. 12.24)

type Accumulator

type Accumulator struct {
	// contains filtered or unexported fields
}

func NewAccumulator

func NewAccumulator(state *state.State, header *block.Header, newTimeslot jamtime.Timeslot) *Accumulator

func (*Accumulator) Delta1

func (a *Accumulator) Delta1(
	accumulationState state.AccumulationState,
	workReports []block.WorkReport,
	alwaysAccumulate map[block.ServiceId]uint64,
	serviceIndex block.ServiceId,
) (state.AccumulationState, []service.DeferredTransfer, *crypto.Hash, uint64)

Delta1 implements equation 12.19 ∆1 (U, ⟦W⟧, D⟨NS → NG⟩, NS ) → (U, ⟦T⟧, H?, NG)

func (*Accumulator) InvokePVM

InvokePVM ΨA(U, N_S , N_G, ⟦O⟧) → (U, ⟦T⟧, H?, N_G) Equation (B.8)

func (*Accumulator) ParallelDelta

func (a *Accumulator) ParallelDelta(
	initialAccState state.AccumulationState,
	workReports []block.WorkReport,
	alwaysAccumulate map[block.ServiceId]uint64,
) (
	state.AccumulationState,
	[]service.DeferredTransfer,
	ServiceHashPairs,
	ServiceGasPairs,
)

ParallelDelta implements equation 12.17 (∆*)

func (*Accumulator) SequentialDelta

func (a *Accumulator) SequentialDelta(
	gasLimit uint64,
	workReports []block.WorkReport,
	ctx state.AccumulationState,
	alwaysAccumulate map[block.ServiceId]uint64,
) (
	uint32,
	state.AccumulationState,
	[]service.DeferredTransfer,
	ServiceHashPairs,
	ServiceGasPairs,
)

SequentialDelta implements equation 12.16 (∆+(NG, ⟦W⟧, U, D⟨NS → NG⟩) → (N, U, ⟦T⟧, B, U))

type DeferredTransfersStatEntry

type DeferredTransfersStatEntry struct {
	OnTransfersCount   uint32
	OnTransfersGasUsed uint64
}

type DeferredTransfersStats

type DeferredTransfersStats map[block.ServiceId]DeferredTransfersStatEntry

DeferredTransfersStats X ∈ D⟨NS →(N, NG)⟩ (eq. 12.30)

type SafroleInput

type SafroleInput struct {
	// Next timeslot.
	TimeSlot jamtime.Timeslot
	// Ticket extrinsic (E_T).
	Tickets []block.TicketProof
	// Y(Hv)
	Entropy crypto.BandersnatchOutputHash
}

Input to UpdateSafroleState. Derived from the incoming block.

func NewSafroleInputFromBlock

func NewSafroleInputFromBlock(block block.Block) (SafroleInput, error)

type SafroleOutput

type SafroleOutput struct {
	// H_e
	EpochMark *block.EpochMarker
	// H_w
	WinningTicketMark *block.WinningTicketMarker

	// Entropies for use by downstream functions that might also use this output.
	TicketEntropy  crypto.Hash // n_2
	SealingEntropy crypto.Hash // n_3
}

Output from UpdateSafroleState.

func UpdateSafroleState

func UpdateSafroleState(
	input SafroleInput,
	preTimeSlot jamtime.Timeslot,
	entropyPool state.EntropyPool,
	validatorState validator.ValidatorState,
	offenders []ed25519.PublicKey,
) (state.EntropyPool, validator.ValidatorState, SafroleOutput, error)

Implements section 6 of the graypaper. Updates all state associated with the SAFROLE protocol. Implements key equations (v.0.4.5) γ′k ≡ Φ(ι) if e′ > e (58) γ′s ≡ Z(γa) if e′ = e + 1 ∧ m ≥ Y ∧ |γa| = E

γs if e′ = e
F(η′2, κ′) otherwise                (69)

He ≡ (η′1, [kb S k <− γ′k]) if e′ > e

∅ otherwise                          (72)

Hw ≡ Z(γa) if e′ = e ∧ m < Y ≤ m′ ∧ |γa| = E

∅ otherwise                          (73)

type ServiceGasPair

type ServiceGasPair struct {
	ServiceId block.ServiceId
	Gas       uint64
}

type ServiceGasPairs

type ServiceGasPairs []ServiceGasPair

ServiceGasPairs U ≡ ⟦(NS , NG)⟧

type ServiceHashPairs

type ServiceHashPairs []state.ServiceHashPair

ServiceHashPairs B ≡ {(NS , H)} (eq. 12.15)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL