Documentation ¶
Index ¶
- Variables
- func MaybeProgress(n int) *maybeProgress
- type Block
- type BlockHeader
- func (bh *BlockHeader) CalculateBlockID() (id BlockID)
- func (bh *BlockHeader) CalculateProofOfWork(ctx context.Context, n int) (nonce uint32, found bool)
- func (bh *BlockHeader) GetTime() time.Time
- func (bh *BlockHeader) IsGenesis() bool
- func (bh *BlockHeader) Validate(workLevel int) error
- func (bh *BlockHeader) VerifyProofOfWork(n int) bool
- type BlockID
- type BlockValidator
- type Chain
- type PayloadHashValidator
- type SQLiteStorage
- type Storage
Constants ¶
This section is empty.
Variables ¶
var ErrBlockMissing = errors.New("Block Not Found")
ErrBlockMissing should be returned from Storage.Get for missing blocks
Functions ¶
func MaybeProgress ¶
func MaybeProgress(n int) *maybeProgress
Types ¶
type BlockHeader ¶
type BlockHeader struct { ID BlockID PrevID BlockID EpochSeconds uint32 PayloadHash [sha256.Size]byte PayloadHint uint8 Proof uint32 Depth uint64 }
BlockHeader is the detail of a block without the payload. We only ever keep the headers in memory. We read the payload on demand, or when validating/iterating the chain.
func (*BlockHeader) CalculateBlockID ¶
func (bh *BlockHeader) CalculateBlockID() (id BlockID)
CalculateBlockID works out the hash given the current state of the header and puts it in the slice given. This can be `nil` and a new slice will be allocated and returned.
func (*BlockHeader) CalculateProofOfWork ¶
CalculateProofOfWork tries to calculate a proof of work with the current block if the context if cancelled it will return early. if it cannot it will return false as the bool
func (*BlockHeader) GetTime ¶
func (bh *BlockHeader) GetTime() time.Time
func (*BlockHeader) IsGenesis ¶
func (bh *BlockHeader) IsGenesis() bool
IsGenesis checks if this is the initial block
func (*BlockHeader) Validate ¶
func (bh *BlockHeader) Validate(workLevel int) error
Validate a BlockHeader, assuming the PrevId and PayloadHash are good. This means you will need to validate the PayloadHash matches the payload seperately. It _will_ validate the proof of work though as that is not included in the hash
func (*BlockHeader) VerifyProofOfWork ¶
func (bh *BlockHeader) VerifyProofOfWork(n int) bool
VerifyProofOfWork checks the current Nonce is correct
type BlockValidator ¶
BlockValidator is a "stateful" system, it should always be expecting the "next" block. But we should have a way to clone that state at a point in time (hopefully cheaply) so we can speculatively validate without commiting. That is what the Clone method is for
type Chain ¶
type Chain interface { ID() BlockID // get the chain ID of this chain (the ID of the genesis block) Head() (*BlockHeader, error) // get the current head of the chain // GetHeader retrieves a block from the current chain // with the given hash Header(hash BlockID) (*BlockHeader, error) // GetPayload retrieves the payload for a block with the given hash Payload(hash BlockID) ([]byte, error) // given a certain depth, find the ID of the block at that depth, or not. AtDepth(d uint64) (BlockID, bool) // give a blockID, find the "next" block, or not if there isn't one Next(prev BlockID) (BlockID, bool) // Add a block to the chain // the block should be fully populated and implementations should validate that // This is now an "official" block and so we add to the current state by "validating" it. Add(*Block) error // mint a new block with work level w and timestamp ts, then add it to the chain Mint(b *Block, w int, ts uint32) error }
Chain is actually a convenience for the concept of a chain. We keep a fixed number of blocks and speculate on blocks we recieve. The consensus mechanism says that the network may broadcast a new chain with a higher depth than ours. At this point we need to verify that the new chain is indeed valid and longer than ours.
type PayloadHashValidator ¶
type PayloadHashValidator int
this validator does nothing but validate that the payload matches the hash. all other validators should do that. as well
func (PayloadHashValidator) Validate ¶
func (p PayloadHashValidator) Validate(b *Block) error
stateless hash validation
func (PayloadHashValidator) WorkLevel ¶
func (p PayloadHashValidator) WorkLevel() int
stateful work level
type SQLiteStorage ¶
type SQLiteStorage struct {
// contains filtered or unexported fields
}
SQLiteStorage is backed by SQLite
func NewSQLiteStorage ¶
func NewSQLiteStorage(path string) (*SQLiteStorage, error)
NewSQLiteStorage does what is says on the tin
func (*SQLiteStorage) Head ¶
func (s *SQLiteStorage) Head() (BlockID, error)
will return nil if the chain is empty.
func (*SQLiteStorage) Header ¶
func (s *SQLiteStorage) Header(hash BlockID, blk *BlockHeader) (*BlockHeader, error)
GetHeader fetches a block by it's hash. find and populate the block, or error validate the hash over this single block
func (*SQLiteStorage) Payload ¶
func (s *SQLiteStorage) Payload(hash BlockID, b []byte) ([]byte, error)
GetPayload fetches the block payload by the block id.
func (*SQLiteStorage) Write ¶
func (s *SQLiteStorage) Write(blk *Block) error