statetransition

package
v0.0.0-...-b58972c Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2025 License: MIT Imports: 27 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")
	ErrReportTimeout        = errors.New("report timeout")
	ErrBadAttestationParent = errors.New("bad attestation parent")
	ErrBadOrder             = errors.New("bad order")
	ErrCoreNotEngaged       = errors.New("core not engaged")
	ErrBadValidatorIndex    = errors.New("bad validator index")
)

Functions

func CalculateIntermediateCoreAssignmentsFromAvailability

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

CalculateIntermediateCoreAssignmentsFromAvailability implements equation 26: ρ‡ ≺ (EA, ρ†) It calculates the intermediate core assignments based on availability assurances.

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)

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 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 CalculateNewJudgements

func CalculateNewJudgements(newTimeslot 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 CalculateNewTimeState

func CalculateNewTimeState(header block.Header) jamtime.Timeslot

CalculateNewTimeState Equation 16: τ′ ≺ H

func CalculateNewValidatorStatistics

func CalculateNewValidatorStatistics(block block.Block, timeslot jamtime.Timeslot, validatorStatistics validator.ValidatorStatisticsState, reporters crypto.ED25519PublicKeySet, currValidators safrole.ValidatorsData) validator.ValidatorStatisticsState

CalculateNewValidatorStatistics implements equation 30: π′ ≺ (EG, EP, EA, ET, τ, κ′, π, H)

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, serviceIndex block.ServiceId, transfers []service.DeferredTransfer) service.ServiceAccount

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 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,
) (
	uint64,
	state.AccumulationState,
	[]service.DeferredTransfer,
	ServiceHashPairs,
)

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,
)

SequentialDelta implements equation 12.16 (∆+)

type SafroleInput

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

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
}

Output from UpdateSafroleState.

func UpdateSafroleState

func UpdateSafroleState(
	input SafroleInput,
	preTimeSlot jamtime.Timeslot,
	entropyPool state.EntropyPool,
	validatorState validator.ValidatorState,
) (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 ServiceHashPair

type ServiceHashPair struct {
	ServiceId block.ServiceId
	Hash      crypto.Hash
}

type ServiceHashPairs

type ServiceHashPairs []ServiceHashPair

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

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,
	hashPairs ServiceHashPairs,
)

CalculateWorkReportsAndAccumulate implements equation 29: (ϑ′, ξ′, δ′, χ′, ι′, φ′, C) ≺ (W*, ϑ, ξ, δ†, χ, ι, φ) with the only difference that we take in available work reports and calculate the accumulatable WR eq. 4.16 W* ≺ (EA, ρ′) and eq. 4.17: (ϑ′, ξ′, δ‡, χ′, ι′, φ′, C) ≺ (W*, ϑ, ξ, δ, χ, ι, φ)

Jump to

Keyboard shortcuts

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