Documentation
¶
Overview ¶
Package consensus implements the Sia consensus algorithms.
Index ¶
- func ApplyBlock(s State, b types.Block, bs V1BlockSupplement, targetTimestamp time.Time) (State, ApplyUpdate)
- func ValidateBlock(s State, b types.Block, bs V1BlockSupplement) error
- func ValidateOrphan(s State, b types.Block) error
- func ValidateTransaction(ms *MidState, txn types.Transaction, ts V1TransactionSupplement) error
- func ValidateV2Transaction(ms *MidState, txn types.V2Transaction) error
- type ApplyUpdate
- func (au ApplyUpdate) ChainIndexElement() types.ChainIndexElement
- func (au ApplyUpdate) ForEachFileContractElement(...)
- func (au ApplyUpdate) ForEachSiacoinElement(fn func(sce types.SiacoinElement, created, spent bool))
- func (au ApplyUpdate) ForEachSiafundElement(fn func(sfe types.SiafundElement, created, spent bool))
- func (au ApplyUpdate) ForEachTreeNode(fn func(row, col uint64, h types.Hash256))
- func (au ApplyUpdate) ForEachV2FileContractElement(fn func(fce types.V2FileContractElement, created bool, ...))
- func (au ApplyUpdate) MarshalJSON() ([]byte, error)
- func (au *ApplyUpdate) UnmarshalJSON(b []byte) error
- func (au ApplyUpdate) UpdateElementProof(e *types.StateElement)
- type ElementAccumulator
- type MidState
- type Network
- type RevertUpdate
- func (ru RevertUpdate) ForEachFileContractElement(...)
- func (ru RevertUpdate) ForEachSiacoinElement(fn func(sce types.SiacoinElement, created, spent bool))
- func (ru RevertUpdate) ForEachSiafundElement(fn func(sfe types.SiafundElement, created, spent bool))
- func (ru RevertUpdate) ForEachTreeNode(fn func(row, col uint64, h types.Hash256))
- func (ru RevertUpdate) ForEachV2FileContractElement(fn func(fce types.V2FileContractElement, created bool, ...))
- func (ru RevertUpdate) MarshalJSON() ([]byte, error)
- func (ru *RevertUpdate) UnmarshalJSON(b []byte) error
- func (ru RevertUpdate) UpdateElementProof(e *types.StateElement)
- type State
- func (s State) AncestorDepth() uint64
- func (s State) AttestationSigHash(a types.Attestation) types.Hash256
- func (s State) BlockInterval() time.Duration
- func (s State) BlockReward() types.Currency
- func (s State) Commitment(txnsHash types.Hash256, minerAddr types.Address) types.Hash256
- func (s State) ContractSigHash(fc types.V2FileContract) types.Hash256
- func (s *State) DecodeFrom(d *types.Decoder)
- func (s State) EncodeTo(e *types.Encoder)
- func (s State) FileContractTax(fc types.FileContract) types.Currency
- func (s State) FoundationSubsidy() (sco types.SiacoinOutput, exists bool)
- func (s State) InputSigHash(txn types.V2Transaction) types.Hash256
- func (s State) MaturityHeight() uint64
- func (s State) MaxBlockWeight() uint64
- func (s State) MaxFutureTimestamp(currentTime time.Time) time.Time
- func (s State) NonceFactor() uint64
- func (s State) PartialSigHash(txn types.Transaction, cf types.CoveredFields) types.Hash256
- func (s State) RenewalSigHash(fcr types.V2FileContractRenewal) types.Hash256
- func (s State) SiafundCount() uint64
- func (s State) StorageProofLeafHash(leaf []byte) types.Hash256
- func (s State) StorageProofLeafIndex(filesize uint64, windowID types.BlockID, fcid types.FileContractID) uint64
- func (s State) SufficientlyHeavierThan(t State) bool
- func (s State) TransactionWeight(txn types.Transaction) uint64
- func (s State) TransactionsCommitment(txns []types.Transaction, v2txns []types.V2Transaction) types.Hash256
- func (s State) V2FileContractTax(fc types.V2FileContract) types.Currency
- func (s State) V2TransactionWeight(txn types.V2Transaction) uint64
- func (s State) WholeSigHash(txn types.Transaction, parentID types.Hash256, pubkeyIndex uint64, ...) types.Hash256
- type V1BlockSupplement
- type V1StorageProofSupplement
- type V1TransactionSupplement
- type Work
- func (w Work) Cmp(v Work) int
- func (w *Work) DecodeFrom(d *types.Decoder)
- func (w Work) EncodeTo(e *types.Encoder)
- func (w Work) MarshalJSON() ([]byte, error)
- func (w Work) MarshalText() ([]byte, error)
- func (w Work) String() string
- func (w *Work) UnmarshalJSON(b []byte) error
- func (w *Work) UnmarshalText(b []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyBlock ¶
func ApplyBlock(s State, b types.Block, bs V1BlockSupplement, targetTimestamp time.Time) (State, ApplyUpdate)
ApplyBlock applies b to s, producing a new state and a set of effects.
func ValidateBlock ¶
func ValidateBlock(s State, b types.Block, bs V1BlockSupplement) error
ValidateBlock validates b in the context of s.
This function does not check whether the header's timestamp is too far in the future. That check should be performed at the time the block is received, e.g. in p2p networking code; see MaxFutureTimestamp.
func ValidateOrphan ¶
ValidateOrphan validates b in the context of s.
func ValidateTransaction ¶
func ValidateTransaction(ms *MidState, txn types.Transaction, ts V1TransactionSupplement) error
ValidateTransaction validates txn within the context of ms and store.
func ValidateV2Transaction ¶
func ValidateV2Transaction(ms *MidState, txn types.V2Transaction) error
ValidateV2Transaction validates txn within the context of ms.
Types ¶
type ApplyUpdate ¶
type ApplyUpdate struct {
// contains filtered or unexported fields
}
An ApplyUpdate represents the effects of applying a block to a state.
func (ApplyUpdate) ChainIndexElement ¶
func (au ApplyUpdate) ChainIndexElement() types.ChainIndexElement
ChainIndexElement returns the chain index element for the applied block.
func (ApplyUpdate) ForEachFileContractElement ¶
func (au ApplyUpdate) ForEachFileContractElement(fn func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, valid bool))
ForEachFileContractElement calls fn on each file contract element related to au. If the contract was revised, rev is non-nil.
func (ApplyUpdate) ForEachSiacoinElement ¶
func (au ApplyUpdate) ForEachSiacoinElement(fn func(sce types.SiacoinElement, created, spent bool))
ForEachSiacoinElement calls fn on each siacoin element related to au.
func (ApplyUpdate) ForEachSiafundElement ¶
func (au ApplyUpdate) ForEachSiafundElement(fn func(sfe types.SiafundElement, created, spent bool))
ForEachSiafundElement calls fn on each siafund element related to au.
func (ApplyUpdate) ForEachTreeNode ¶
func (au ApplyUpdate) ForEachTreeNode(fn func(row, col uint64, h types.Hash256))
ForEachTreeNode calls fn on each node in the accumulator affected by au.
func (ApplyUpdate) ForEachV2FileContractElement ¶
func (au ApplyUpdate) ForEachV2FileContractElement(fn func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType))
ForEachV2FileContractElement calls fn on each V2 file contract element related to au. If the contract was revised, rev is non-nil. If the contract was resolved, res is non-nil.
func (ApplyUpdate) MarshalJSON ¶
func (au ApplyUpdate) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*ApplyUpdate) UnmarshalJSON ¶
func (au *ApplyUpdate) UnmarshalJSON(b []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (ApplyUpdate) UpdateElementProof ¶
func (au ApplyUpdate) UpdateElementProof(e *types.StateElement)
UpdateElementProof updates the Merkle proof of the supplied element to incorporate the changes made to the accumulator. The element's proof must be up-to-date; if it is not, UpdateElementProof may panic.
type ElementAccumulator ¶
An ElementAccumulator tracks the state of an unbounded number of elements without storing the elements themselves.
func (*ElementAccumulator) DecodeFrom ¶
func (acc *ElementAccumulator) DecodeFrom(d *types.Decoder)
DecodeFrom implements types.DecoderFrom.
func (ElementAccumulator) EncodeTo ¶
func (acc ElementAccumulator) EncodeTo(e *types.Encoder)
EncodeTo implements types.EncoderTo.
func (ElementAccumulator) MarshalJSON ¶
func (acc ElementAccumulator) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*ElementAccumulator) UnmarshalJSON ¶
func (acc *ElementAccumulator) UnmarshalJSON(b []byte) error
UnmarshalJSON implements json.Unmarshaler.
type MidState ¶
type MidState struct {
// contains filtered or unexported fields
}
A MidState represents the state of the chain within a block.
func NewMidState ¶
NewMidState constructs a MidState initialized to the provided base state.
func (*MidState) ApplyBlock ¶
func (ms *MidState) ApplyBlock(b types.Block, bs V1BlockSupplement)
ApplyBlock applies a block to the MidState.
func (*MidState) ApplyTransaction ¶
func (ms *MidState) ApplyTransaction(txn types.Transaction, ts V1TransactionSupplement)
ApplyTransaction applies a transaction to the MidState.
func (*MidState) ApplyV2Transaction ¶
func (ms *MidState) ApplyV2Transaction(txn types.V2Transaction)
ApplyV2Transaction applies a v2 transaction to the MidState.
type Network ¶
type Network struct { Name string `json:"name"` InitialCoinbase types.Currency `json:"initialCoinbase"` MinimumCoinbase types.Currency `json:"minimumCoinbase"` InitialTarget types.BlockID `json:"initialTarget"` BlockInterval time.Duration `json:"blockInterval"` MaturityDelay uint64 `json:"maturityDelay"` HardforkDevAddr struct { Height uint64 `json:"height"` OldAddress types.Address `json:"oldAddress"` NewAddress types.Address `json:"newAddress"` } `json:"hardforkDevAddr"` HardforkTax struct { Height uint64 `json:"height"` } `json:"hardforkTax"` HardforkStorageProof struct { Height uint64 `json:"height"` } `json:"hardforkStorageProof"` HardforkOak struct { Height uint64 `json:"height"` FixHeight uint64 `json:"fixHeight"` GenesisTimestamp time.Time `json:"genesisTimestamp"` } `json:"hardforkOak"` //nolint:tagliatelle HardforkASIC struct { Height uint64 `json:"height"` OakTime time.Duration `json:"oakTime"` OakTarget types.BlockID `json:"oakTarget"` } `json:"hardforkASIC"` HardforkFoundation struct { Height uint64 `json:"height"` PrimaryAddress types.Address `json:"primaryAddress"` FailsafeAddress types.Address `json:"failsafeAddress"` } `json:"hardforkFoundation"` HardforkV2 struct { AllowHeight uint64 `json:"allowHeight"` RequireHeight uint64 `json:"requireHeight"` } `json:"hardforkV2"` }
A Network specifies the fixed parameters of a Sia blockchain.
func (*Network) GenesisState ¶
GenesisState returns the state to which the genesis block should be applied.
type RevertUpdate ¶
type RevertUpdate struct {
// contains filtered or unexported fields
}
A RevertUpdate represents the effects of reverting to a prior state.
func RevertBlock ¶
func RevertBlock(s State, b types.Block, bs V1BlockSupplement) RevertUpdate
RevertBlock reverts b, producing the effects undone by the block.
func (RevertUpdate) ForEachFileContractElement ¶
func (ru RevertUpdate) ForEachFileContractElement(fn func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, valid bool))
ForEachFileContractElement calls fn on each file contract element related to ru. If the contract was revised, rev is non-nil.
func (RevertUpdate) ForEachSiacoinElement ¶
func (ru RevertUpdate) ForEachSiacoinElement(fn func(sce types.SiacoinElement, created, spent bool))
ForEachSiacoinElement calls fn on each siacoin element related to ru.
func (RevertUpdate) ForEachSiafundElement ¶
func (ru RevertUpdate) ForEachSiafundElement(fn func(sfe types.SiafundElement, created, spent bool))
ForEachSiafundElement calls fn on each siafund element related to ru.
func (RevertUpdate) ForEachTreeNode ¶
func (ru RevertUpdate) ForEachTreeNode(fn func(row, col uint64, h types.Hash256))
ForEachTreeNode calls fn on each node in the accumulator affected by ru.
func (RevertUpdate) ForEachV2FileContractElement ¶
func (ru RevertUpdate) ForEachV2FileContractElement(fn func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType))
ForEachV2FileContractElement calls fn on each V2 file contract element related to au. If the contract was revised, rev is non-nil. If the contract was resolved, res is non-nil.
func (RevertUpdate) MarshalJSON ¶
func (ru RevertUpdate) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*RevertUpdate) UnmarshalJSON ¶
func (ru *RevertUpdate) UnmarshalJSON(b []byte) error
UnmarshalJSON implments json.Unmarshaler.
func (RevertUpdate) UpdateElementProof ¶
func (ru RevertUpdate) UpdateElementProof(e *types.StateElement)
UpdateElementProof updates the Merkle proof of the supplied element to incorporate the changes made to the accumulator. The element's proof must be up-to-date; if it is not, UpdateElementProof may panic.
type State ¶
type State struct { Network *Network `json:"-"` // network parameters are not encoded Index types.ChainIndex `json:"index"` PrevTimestamps [11]time.Time `json:"prevTimestamps"` // newest -> oldest Depth types.BlockID `json:"depth"` ChildTarget types.BlockID `json:"childTarget"` SiafundTaxRevenue types.Currency `json:"siafundTaxRevenue"` // Oak hardfork state OakTime time.Duration `json:"oakTime"` OakTarget types.BlockID `json:"oakTarget"` // Foundation hardfork state FoundationSubsidyAddress types.Address `json:"foundationSubsidyAddress"` FoundationManagementAddress types.Address `json:"foundationManagementAddress"` // v2 hardfork state TotalWork Work `json:"totalWork"` Difficulty Work `json:"difficulty"` OakWork Work `json:"oakWork"` Elements ElementAccumulator `json:"elements"` Attestations uint64 `json:"attestations"` }
State represents the state of the chain as of a particular block.
func ApplyOrphan ¶
ApplyOrphan applies the work of b to s, returning the resulting state. Only the PoW-related fields are updated.
func (State) AncestorDepth ¶
AncestorDepth is the depth used to determine the target timestamp in the pre-Oak difficulty adjustment algorithm.
func (State) AttestationSigHash ¶
func (s State) AttestationSigHash(a types.Attestation) types.Hash256
AttestationSigHash returns the hash that must be signed for an attestation.
func (State) BlockInterval ¶
BlockInterval is the expected wall clock time between consecutive blocks.
func (State) BlockReward ¶
BlockReward returns the reward for mining a child block.
func (State) Commitment ¶
Commitment computes the commitment hash for a child block with the given transactions and miner address.
func (State) ContractSigHash ¶
func (s State) ContractSigHash(fc types.V2FileContract) types.Hash256
ContractSigHash returns the hash that must be signed for a v2 contract revision.
func (*State) DecodeFrom ¶
DecodeFrom implements types.DecoderFrom.
func (State) FileContractTax ¶
func (s State) FileContractTax(fc types.FileContract) types.Currency
FileContractTax computes the tax levied on a given contract.
func (State) FoundationSubsidy ¶
func (s State) FoundationSubsidy() (sco types.SiacoinOutput, exists bool)
FoundationSubsidy returns the Foundation subsidy output for the child block.
func (State) InputSigHash ¶
func (s State) InputSigHash(txn types.V2Transaction) types.Hash256
InputSigHash returns the hash that must be signed for each v2 transaction input.
func (State) MaturityHeight ¶
MaturityHeight is the height at which various outputs created in the child block will "mature" (become spendable).
func (State) MaxBlockWeight ¶
MaxBlockWeight is the maximum "weight" of a valid child block.
func (State) MaxFutureTimestamp ¶
MaxFutureTimestamp returns a reasonable maximum value for a child block's timestamp. Note that this is not a consensus rule.
func (State) NonceFactor ¶
NonceFactor is the factor by which all block nonces must be divisible.
func (State) PartialSigHash ¶
func (s State) PartialSigHash(txn types.Transaction, cf types.CoveredFields) types.Hash256
PartialSigHash computes the hash of the transaction data specified by cf. It panics if cf references fields not present in txn.
func (State) RenewalSigHash ¶
func (s State) RenewalSigHash(fcr types.V2FileContractRenewal) types.Hash256
RenewalSigHash returns the hash that must be signed for a file contract renewal.
func (State) SiafundCount ¶
SiafundCount is the number of siafunds in existence.
func (State) StorageProofLeafHash ¶
StorageProofLeafHash computes the leaf hash of file contract data. If len(leaf) < 64, it will be extended with zeros.
func (State) StorageProofLeafIndex ¶
func (s State) StorageProofLeafIndex(filesize uint64, windowID types.BlockID, fcid types.FileContractID) uint64
StorageProofLeafIndex returns the leaf index used when computing or validating a storage proof.
func (State) SufficientlyHeavierThan ¶
SufficientlyHeavierThan returns whether s is sufficiently heavier than t. Nodes should use this method rather than directly comparing the Depth or TotalWork fields. Note that this is not a consensus rule.
func (State) TransactionWeight ¶
func (s State) TransactionWeight(txn types.Transaction) uint64
TransactionWeight computes the weight of a txn.
func (State) TransactionsCommitment ¶
func (s State) TransactionsCommitment(txns []types.Transaction, v2txns []types.V2Transaction) types.Hash256
TransactionsCommitment returns the commitment hash covering the transactions that comprise a child block.
func (State) V2FileContractTax ¶
func (s State) V2FileContractTax(fc types.V2FileContract) types.Currency
V2FileContractTax computes the tax levied on a given v2 contract.
func (State) V2TransactionWeight ¶
func (s State) V2TransactionWeight(txn types.V2Transaction) uint64
V2TransactionWeight computes the weight of a txn.
type V1BlockSupplement ¶
type V1BlockSupplement struct { Transactions []V1TransactionSupplement ExpiringFileContracts []types.FileContractElement }
A V1BlockSupplement contains elements that are associated with a v1 block, but not included in the block. This includes supplements for each v1 transaction, as well as any file contracts that expired at the block's height.
func (*V1BlockSupplement) DecodeFrom ¶
func (bs *V1BlockSupplement) DecodeFrom(d *types.Decoder)
DecodeFrom implements types.DecoderFrom.
func (V1BlockSupplement) EncodeTo ¶
func (bs V1BlockSupplement) EncodeTo(e *types.Encoder)
EncodeTo implements types.EncoderTo.
type V1StorageProofSupplement ¶
type V1StorageProofSupplement struct { FileContract types.FileContractElement WindowID types.BlockID }
A V1StorageProofSupplement pairs a file contract with the block ID used to derive its storage proof leaf index.
func (*V1StorageProofSupplement) DecodeFrom ¶
func (sps *V1StorageProofSupplement) DecodeFrom(d *types.Decoder)
DecodeFrom implements types.DecoderFrom.
func (V1StorageProofSupplement) EncodeTo ¶
func (sps V1StorageProofSupplement) EncodeTo(e *types.Encoder)
EncodeTo implements types.EncoderTo.
type V1TransactionSupplement ¶
type V1TransactionSupplement struct { SiacoinInputs []types.SiacoinElement SiafundInputs []types.SiafundElement RevisedFileContracts []types.FileContractElement StorageProofs []V1StorageProofSupplement }
A V1TransactionSupplement contains elements that are associated with a v1 transaction, but not included in the transaction. For example, v1 transactions reference the ID of each SiacoinOutput they spend, but do not contain the output itself. Consequently, in order to validate the transaction, those outputs must be loaded from a Store. Collecting these elements into an explicit struct allows us to preserve them even after the Store has been mutated.
func (*V1TransactionSupplement) DecodeFrom ¶
func (ts *V1TransactionSupplement) DecodeFrom(d *types.Decoder)
DecodeFrom implements types.DecoderFrom.
func (V1TransactionSupplement) EncodeTo ¶
func (ts V1TransactionSupplement) EncodeTo(e *types.Encoder)
EncodeTo implements types.EncoderTo.
type Work ¶
type Work struct {
// contains filtered or unexported fields
}
Work represents a quantity of work.
func (*Work) DecodeFrom ¶
DecodeFrom implements types.DecoderFrom.
func (Work) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Work) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Work) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Work) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.