chain

package
v0.0.0-...-36360bf Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: Unlicense Imports: 4 Imported by: 0

Documentation

Overview

Package chain provides a blockchain data storage engine

Index

Constants

This section is empty.

Variables

View Source
var MinDifficulty = new(big.Int).SetBytes(MinTargetBytes)

MinDifficulty represents the bytes of a proof that cannot be any smaller, it has the most significant bit as a zero, meaning it is one less than the completely impossible all bits 1 coming from a hash (same impossible as all zero).

View Source
var MinTargetBytes = []byte{
	0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
}

MinTargetBytes is the difficulty of 1 as a 32 byte slice, the largest number a proof can be without triggering a divide by zero error, and the base from which the difficulty value is subtracted to yield a value to compare to a block proof

Functions

This section is empty.

Types

type ABCI

type ABCI struct {
	*Blockchain
}

func (*ABCI) ApplySnapshotChunk

func (a *ABCI) ApplySnapshotChunk(
	reqApplySnapChunk RequestApplySnapshotChunk,
) (rasc ResponseApplySnapshotChunk)

func (*ABCI) BeginBlock

func (a *ABCI) BeginBlock(reqBegBlk RequestBeginBlock) (
	rbb ResponseBeginBlock)

func (*ABCI) CheckTx

func (a *ABCI) CheckTx(reqChkTx RequestCheckTx) (rct ResponseCheckTx)

func (*ABCI) Commit

func (a *ABCI) Commit() (rc ResponseCommit)

func (*ABCI) DeliverTx

func (a *ABCI) DeliverTx(reqDelTx RequestDeliverTx) (rdt ResponseDeliverTx)

func (*ABCI) EndBlock

func (a *ABCI) EndBlock(reqEndBlk RequestEndBlock) (reb ResponseEndBlock)

func (*ABCI) Info

func (a *ABCI) Info(reqInfo RequestInfo) (ri ResponseInfo)

func (*ABCI) InitChain

func (a *ABCI) InitChain(reqInitChain RequestInitChain) (
	ric ResponseInitChain)

func (*ABCI) ListSnapshots

func (a *ABCI) ListSnapshots(reqListSnap RequestListSnapshots) (
	rls ResponseListSnapshots)

func (*ABCI) LoadSnapshotChunk

func (a *ABCI) LoadSnapshotChunk(
	reqLoadSnapChunk RequestLoadSnapshotChunk,
) (rlsc ResponseLoadSnapshotChunk)

func (*ABCI) OfferSnapshot

func (a *ABCI) OfferSnapshot(
	reqOfferSnap RequestOfferSnapshot,
) (ros ResponseOfferSnapshot)

func (*ABCI) Query

func (a *ABCI) Query(reqQuery RequestQuery) (rq ResponseQuery)

func (*ABCI) SetOption

func (a *ABCI) SetOption(reqSetOpt RequestSetOption) (rso ResponseSetOption)

type Address

type Address [20]byte

An Address is a fixed length array representing the blake 3 256-bit hash of the public key truncated to 20 bytes used to identify an account in the database, created by sending funds to the address, generated from the public key, derived from the private key

type Block

type Block struct {
	Header
	// Transactions is the list of transactions ordered in the merkle
	// tree structure with the necessary empty nodes in the list as
	// required. The MerkleRoot is computed from this list which must be
	// structured correctly for this.
	Transactions []interface{}
	// MasternodeSignature - After a block is proposed, masternodes run a
	// vote/compare/sign cycle which generates a Schnorr signature with each
	// masternode's signature overlaid on it, the masternode list being
	// visible as the issued tokens and their currently controlling account.
	//
	// This signature indicates to nodes that no previous block can now be
	// mined on, making the block immediately final, a requirement for
	// Cosmos IBC protocol.
	//
	// This signature is the product of the peer validation process for
	// signatures on the block, the first signature chain to acquire the 2/3
	// quorum of masternodes triggers the acceptance and finality of the
	// new block.
	//
	// The signature is applied to the Hash of the Header, which a verifier
	// must generate from the serialized block header.
	// Without this signature a block may not be mined on,
	// and the masternodes only accept one and finalize it immediately
	// before a new block is allowed to happen (15 second minimum)
	MasternodeSignature []byte
}

A Block represents a unit of progress for the network, and carries a payload of transactions to change the state of the accounts database for users

type BlockNode

type BlockNode struct {

	// Block is the block itself
	*Block
	// contains filtered or unexported fields
}

A BlockNode is a node in a dual linked list that allows walking the chain history for various purposes

type Blockchain

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

func NewBlockchain

func NewBlockchain(abciParams *types.ConsensusParams, genesis *Block) *Blockchain

type Difficulty

type Difficulty []byte

Difficulty is a variable length long integer of maximum 32 byte length representing the difference between minimum difficulty and the block proof

func BigToDifficulty

func BigToDifficulty(bi *big.Int) Difficulty

BigToDifficulty converts big. Int from a proof or target into a difficulty value, used to compute a new difficulty adjustment and return a value for the block

func (Difficulty) ToBigInt

func (d Difficulty) ToBigInt() *big.Int

ToBigInt converts a difficulty to big.Int by subtracting it from MinDifficulty

type Hash

type Hash [32]byte

Hash is your standard 32 byte fixed length array as returned by many hash functions and used in the blocks as identifiers of other data

func (Hash) ToAddress

func (h Hash) ToAddress() (a Address)

ToAddress returns the address derived from a hash (which would be a public key, presumably)

func (Hash) ToBigInt

func (h Hash) ToBigInt() *big.Int

ToBigInt converts a hash to big.Int so it can be compared as in the case of a proof or other partial hash collision

type Header struct {
	// Parent is the hash of the previous block
	Parent Hash
	// Time is the Unix timestamp of the block
	Time int64
	// Provenance is a value computed by the app state hash of the block
	// nearest the staking cooldown period, as displaced from the block's
	// timestamp, combined with the coinbase of the parent block, two values
	// that don't exist until a block has been mined.
	//
	// Before the cooldown period the genesis block is always the app
	// state hash used to generate this
	Provenance Hash
	// Difficulty is a value that is subtracted from the lowest difficulty
	// (all but the largest bit 1 in a 256 bit hash) to create a difficulty
	// target which defines the largest proof that can be accepted.
	//
	// If the following is true:
	//
	//   stake * proof < target
	//
	// then the block has valid proof and could be accepted.
	Difficulty Difficulty
	// Coinbase is the account to which the reward for this block is paid
	Coinbase Address
	// Stake is the necessary amount of stake to normalize the difficulty
	// target, minimum of 1. This functions as a multiplier on the proof
	// to produce the value that is compared to the difficulty target
	Stake uint64
	// MerkleRoot is the root hash of the transaction merkle tree
	MerkleRoot Hash
}

Header is the metadata for the block and the entropy for mining

func Deserialize

func Deserialize(serialized []byte) (h *Header)

Deserialize the block

func (*Header) GetStake

func (h *Header) GetStake() (stake uint64)

GetStake returns the divider that makes the Hash of the Header equal to or below the value generated by the Difficulty, which is locked up for the cool down period (SaphoConsensus.StakeCoolDown).

If the proof is divided by the stake it will produce a number smaller than the value generated from Difficulty, and thus pass

func (*Header) Hash

func (h *Header) Hash() (ha Hash)

func (*Header) Serialize

func (h *Header) Serialize() (serialized []byte)

Serialize the block

func (*Header) ValidateStake

func (h *Header) ValidateStake() bool

ValidateStake divides the proof by the stake and if the result is smaller than the target then it returns true, otherwise false.

Miners should use GetStake to determine the stake that will be used, so they stake the minimal required for the solution, and reject solutions that require more stake than they want to place on one block.

More stake can be used, but it makes no sense to lock up more than required. Staking's main benefit is getting ahead of the crowd for mining a block as it effectively lowers the target.

type SaphoConsensus

type SaphoConsensus struct {
	// MinBlockInterval is the minimum difference between a new block and
	// the parent block
	MinBlockInterval int
	// BlockIntervalTarget is the set point for the difficulty adjustment,
	// important for regulating the token emission rate
	BlockIntervalTarget int
	// AdjustmentInverseBase is the reciprocal of the scaling value
	// compounded with the difference between the block timestamp and the
	// time target computed by the difficulty for a given parent block and
	// the timestamp of the proposed block.
	//
	// The closer the timestamp is to the computed target timestamp the less
	// that difficulty is adjusted.
	AdjustmentInverseBase int
	// EMAWindow is the number of previous blocks used to compute the
	// average used to derive the difficulty
	EMAWindow int
	// EMANumer is the numerator of the EMA smoothing factor (multiplies)
	EMANumer int
	// EMADenom is the denominator of the EMA smoothing factor (divides)
	EMADenom int
	// StakeCoolDown is the number of seconds that stake is subtracted
	// from available balance when querying to perform another
	// transaction. During this period the stake cannot be spent or staked.
	StakeCoolDown int
}

func GetDefaultSaphoConsensus

func GetDefaultSaphoConsensus() (sc *SaphoConsensus)

GetDefaultSaphoConsensus returns a default configured Sapho consensus structure

type Target

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

A Target is calculated by the difficulty adjustment algorithm and defines the minimum timestamp and the center point of the timestamp displacement adjustment

Jump to

Keyboard shortcuts

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