Chain #


A Chain is a sequence of tipsets, linked together. It is a single history of execution in the Filecoin blockchain. Randomness is drawn from the chain, as mentioned in Ticket Chain.

import abi "github.com/filecoin-project/specs-actors/actors/abi"
import block "github.com/filecoin-project/specs/systems/filecoin_blockchain/struct/block"

type Chain struct {
    HeadTipset Tipset

    TipsetAtEpoch(epoch abi.ChainEpoch) Tipset
    RandomnessSeedAtEpoch(epoch abi.ChainEpoch) abi.RandomnessSeed

    // call by StorageMiningSubsystem during block production
    GetTicketProductionRandSeed(epoch abi.ChainEpoch) abi.RandomnessSeed

    // call by StorageMiningSubsystem in sealing sector
    GetSealRandSeed(epoch abi.ChainEpoch) abi.RandomnessSeed

    // call by StorageMiningSubsystem after sealing
    GetPoStChallengeRandSeed(epoch abi.ChainEpoch) abi.RandomnessSeed
}

// Checkpoint represents a particular block to use as a trust anchor
// in Consensus and ChainSync
//
// Note: a Block uniquely identifies a tipset (the parents)
// from here, we may consider many tipsets that _include_ Block
// but we must indeed include t and not consider tipsets that
// fork from Block.Parents, but do not include Block.
type Checkpoint &block.BlockHeader

// SoftCheckpoint is a checkpoint that Filecoin nodes may use as they
// gain confidence in the blockchain. It is a unilateral checkpoint,
// and derived algorithmically from notions of probabilistic consensus
// and finality.
type SoftCheckpoint Checkpoint

// TrustedCheckpoint is a Checkpoint that is trusted by the broader
// Filecoin Network. These TrustedCheckpoints are arrived at through
// the higher level economic consensus that surrounds Filecoin.
// TrustedCheckpoints:
// - MUST be at least 200,000 blocks old (>1mo)
// - MUST be at least
// - MUST be widely known and accepted
// - MAY ship with Filecoin software implementations
// - MAY be propagated through other side-channel systems
// For more, see the Checkpoints section.
// TODO: consider renaming as EconomicCheckpoint
type TrustedCheckpoint Checkpoint
package chain

import (
	abi "github.com/filecoin-project/specs-actors/actors/abi"
	builtin "github.com/filecoin-project/specs-actors/actors/builtin"
	node_base "github.com/filecoin-project/specs/systems/filecoin_nodes/node_base"
)

// Returns the tipset at or immediately prior to `epoch`.
// For negative epochs, it should return a tipset composed of the genesis block.
func (chain *Chain_I) TipsetAtEpoch(epoch abi.ChainEpoch) Tipset {
	current := chain.HeadTipset()
	genesisEpoch := abi.ChainEpoch(0)
	for current.Epoch() > epoch && epoch >= genesisEpoch {
		// for epoch <= genesisEpoch, this should return a single-block tipset that includes the genesis block
		current = current.Parents()
	}

	return current
}

// Draws randomness from the tipset at or immediately prior to `epoch`.
func (chain *Chain_I) RandomnessSeedAtEpoch(epoch abi.ChainEpoch) abi.RandomnessSeed {

	ts := chain.TipsetAtEpoch(epoch)
	//	return ts.MinTicket().Digest()
	return ts.MinTicket().DrawRandomness(epoch)
}

func (chain *Chain_I) GetTicketProductionRandSeed(epoch abi.ChainEpoch) abi.RandomnessSeed {
	return chain.RandomnessSeedAtEpoch(epoch - node_base.SPC_LOOKBACK_TICKET)
}

func (chain *Chain_I) GetSealRandSeed(epoch abi.ChainEpoch) abi.RandomnessSeed {
	return chain.RandomnessSeedAtEpoch(epoch - builtin.SPC_LOOKBACK_SEAL)
}

func (chain *Chain_I) GetPoStChallengeRandSeed(epoch abi.ChainEpoch) abi.RandomnessSeed {
	return chain.RandomnessSeedAtEpoch(epoch - builtin.SPC_LOOKBACK_POST)
}