blockchain

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2019 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TaskChanCap     = 4
	BlocksCacheSize = 2
)
View Source
const (
	// MinDepositAmount is the minimum deposit as a producer.
	MinDepositAmount = 5000 * 100000000

	// DepositLockupBlocks indicates how many blocks need to wait when cancel
	// producer was triggered, and can submit return deposit coin request.
	DepositLockupBlocks = 2160

	// MaxStringLength is the maximum length of a string field.
	MaxStringLength = 100
)
View Source
const BITSPERKEY = 10

used to compute the size of bloom filter bits array . too small will lead to high false positive rate.

View Source
const (
	MaxTimeOffsetSeconds = 2 * 60 * 60
)

Variables

View Source
var FoundationAddress Uint168

Functions

func AddChildrenWork

func AddChildrenWork(node *BlockNode, work *big.Int)

func BigToCompact

func BigToCompact(n *big.Int) uint32

func CalcPastMedianTime

func CalcPastMedianTime(node *BlockNode) time.Time

func CalcWork

func CalcWork(bits uint32) *big.Int

(1 << 256) / (difficultyNum + 1)

func CalculateTxsFee added in v0.3.3

func CalculateTxsFee(block *Block)

func CheckDPOSIllegalBlocks added in v0.3.0

func CheckDPOSIllegalBlocks(d *payload.DPOSIllegalBlocks) error

func CheckDPOSIllegalProposals added in v0.3.0

func CheckDPOSIllegalProposals(d *payload.DPOSIllegalProposals) error

func CheckDPOSIllegalVotes added in v0.3.0

func CheckDPOSIllegalVotes(d *payload.DPOSIllegalVotes) error

func CheckInactiveArbitrators added in v0.3.0

func CheckInactiveArbitrators(txn *Transaction) error

func CheckProofOfWork

func CheckProofOfWork(header *Header, powLimit *big.Int) error

func CheckSideChainPowConsensus added in v0.1.1

func CheckSideChainPowConsensus(txn *Transaction, arbitrator []byte) error

func CheckSidechainIllegalEvidence added in v0.3.0

func CheckSidechainIllegalEvidence(p *payload.SidechainIllegalData) error

func CompactToBig

func CompactToBig(compact uint32) *big.Int

func ConfirmContextCheck added in v0.3.0

func ConfirmContextCheck(confirm *payload.Confirm) error

func ConfirmSanityCheck added in v0.3.0

func ConfirmSanityCheck(confirm *payload.Confirm) error

func GetTxFee

func GetTxFee(tx *Transaction, assetId Uint256) Fixed64

func GetTxFeeMap

func GetTxFeeMap(tx *Transaction) (map[Uint256]Fixed64, error)

func GetTxProgramHashes

func GetTxProgramHashes(tx *Transaction, references map[*Input]*Output) ([]common.Uint168, error)

func GetUint16Array

func GetUint16Array(source []byte) ([]uint16, error)

func HashToBig

func HashToBig(hash *Uint256) *big.Int

func IsFinalizedTransaction

func IsFinalizedTransaction(msgTx *Transaction, blockHeight uint32) bool

func PreProcessSpecialTx added in v0.3.2

func PreProcessSpecialTx(block *Block) error

func ProposalCheck added in v0.3.2

func ProposalCheck(proposal *payload.DPOSProposal) error

func ProposalCheckByHeight added in v0.3.2

func ProposalCheckByHeight(proposal *payload.DPOSProposal,
	height uint32) error

func ProposalContextCheck added in v0.3.0

func ProposalContextCheck(proposal *payload.DPOSProposal) error

func ProposalContextCheckByHeight added in v0.3.2

func ProposalContextCheckByHeight(proposal *payload.DPOSProposal,
	height uint32) error

func ProposalSanityCheck added in v0.3.0

func ProposalSanityCheck(proposal *payload.DPOSProposal) error

func RunPrograms

func RunPrograms(data []byte, programHashes []common.Uint168, programs []*Program) error

func SortPrograms added in v0.1.1

func SortPrograms(programs []*Program)

func ToByteArray

func ToByteArray(source []uint16) []byte

func VoteCheck added in v0.3.2

func VoteCheck(vote *payload.DPOSProposalVote) error

func VoteCheckByHeight added in v0.3.2

func VoteCheckByHeight(vote *payload.DPOSProposalVote, height uint32) error

func VoteContextCheck added in v0.3.0

func VoteContextCheck(vote *payload.DPOSProposalVote) error

func VoteContextCheckByHeight added in v0.3.2

func VoteContextCheckByHeight(vote *payload.DPOSProposalVote,
	height uint32) error

func VoteSanityCheck added in v0.3.0

func VoteSanityCheck(vote *payload.DPOSProposalVote) error

Types

type BlockChain added in v0.3.0

type BlockChain struct {
	GenesisHash Uint256

	BestChain *BlockNode
	Root      *BlockNode
	Index     map[Uint256]*BlockNode
	IndexLock sync.RWMutex
	DepNodes  map[Uint256][]*BlockNode

	TimeSource     MedianTimeSource
	MedianTimePast time.Time
	// contains filtered or unexported fields
}

func New added in v0.3.0

func New(db IChainStore, chainParams *config.Params, state *state.State) (*BlockChain, error)

func (*BlockChain) AddNodeToIndex added in v0.3.0

func (b *BlockChain) AddNodeToIndex(node *BlockNode)

func (*BlockChain) AddOrphanBlock added in v0.3.0

func (b *BlockChain) AddOrphanBlock(block *Block)

func (*BlockChain) AddOrphanConfirm added in v0.3.0

func (b *BlockChain) AddOrphanConfirm(confirm *payload.Confirm)

func (*BlockChain) BlockExists added in v0.3.0

func (b *BlockChain) BlockExists(hash *Uint256) bool

func (*BlockChain) BlockLocatorFromHash added in v0.3.0

func (b *BlockChain) BlockLocatorFromHash(inhash *Uint256) []*Uint256

func (*BlockChain) CalcCurrentDifficulty added in v0.3.0

func (b *BlockChain) CalcCurrentDifficulty(currentBits uint32) string

func (*BlockChain) CalcNextRequiredDifficulty added in v0.3.0

func (b *BlockChain) CalcNextRequiredDifficulty(prevNode *BlockNode, newBlockTime time.Time) (uint32, error)

func (*BlockChain) CheckBlockContext added in v0.3.4

func (b *BlockChain) CheckBlockContext(block *Block, prevNode *BlockNode) error

func (*BlockChain) CheckBlockSanity added in v0.3.0

func (b *BlockChain) CheckBlockSanity(block *Block) error

func (*BlockChain) CheckTransactionContext added in v0.3.0

func (b *BlockChain) CheckTransactionContext(blockHeight uint32, txn *Transaction) ErrCode

CheckTransactionContext verifies a transaction with history transaction in ledger

func (*BlockChain) CheckTransactionSanity added in v0.3.0

func (b *BlockChain) CheckTransactionSanity(blockHeight uint32, txn *Transaction) ErrCode

CheckTransactionSanity verifies received single transaction

func (*BlockChain) ContainsTransaction added in v0.3.0

func (b *BlockChain) ContainsTransaction(hash Uint256) bool

func (*BlockChain) CurrentBlockHash added in v0.3.0

func (b *BlockChain) CurrentBlockHash() Uint256

func (*BlockChain) GetBlockByHash added in v0.3.0

func (b *BlockChain) GetBlockByHash(hash Uint256) (*Block, error)

Get block with block hash.

func (*BlockChain) GetDposBlockByHash added in v0.3.0

func (b *BlockChain) GetDposBlockByHash(hash Uint256) (*DposBlock, error)

Get DPOS block with block hash.

func (*BlockChain) GetHeader added in v0.3.0

func (b *BlockChain) GetHeader(hash Uint256) (*Header, error)

func (*BlockChain) GetHeight added in v0.3.0

func (b *BlockChain) GetHeight() uint32

func (*BlockChain) GetNetworkHashPS added in v0.3.4

func (b *BlockChain) GetNetworkHashPS() *big.Int

GetNetworkHashPS return average network hashes per second based on the last 'lookup' blocks.

func (*BlockChain) GetOrphan added in v0.3.2

func (b *BlockChain) GetOrphan(hash *Uint256) *OrphanBlock

func (*BlockChain) GetOrphanConfirm added in v0.3.0

func (b *BlockChain) GetOrphanConfirm(hash *Uint256) (*payload.Confirm, bool)

func (*BlockChain) GetOrphanRoot added in v0.3.0

func (b *BlockChain) GetOrphanRoot(hash *Uint256) *Uint256

func (*BlockChain) GetState added in v0.3.0

func (b *BlockChain) GetState() *state.State

GetState returns the DPOS state instance that stores producers and votes information.

func (*BlockChain) HaveBlock added in v0.3.0

func (b *BlockChain) HaveBlock(hash *Uint256) (bool, error)

func (*BlockChain) InitProducerState added in v0.3.2

func (b *BlockChain) InitProducerState(interrupt <-chan struct{},
	start func(total uint32), increase func()) (err error)

InitProducerState go through all blocks since the start of DPOS consensus to initialize producers and votes state.

func (*BlockChain) IsKnownOrphan added in v0.3.0

func (b *BlockChain) IsKnownOrphan(hash *Uint256) bool

func (*BlockChain) LatestBlockLocator added in v0.3.0

func (b *BlockChain) LatestBlockLocator() ([]*Uint256, error)

func (*BlockChain) LoadBlockNode added in v0.3.0

func (b *BlockChain) LoadBlockNode(blockHeader *Header, hash *Uint256) (*BlockNode, error)

func (*BlockChain) LocateBlocks added in v0.3.0

func (b *BlockChain) LocateBlocks(locator []*Uint256, hashStop *Uint256, maxHashes uint32) []*Uint256

LocateBlocks returns the hashes of the blocks after the first known block in the locator until the provided stop hash is reached, or up to the provided max number of block hashes.

In addition, there are two special cases:

  • When no locators are provided, the stop hash is treated as a request for that block, so it will either return the stop hash itself if it is known, or nil if it is unknown
  • When locators are provided, but none of them are known, hashes starting after the genesis block will be returned

This function is safe for concurrent access.

func (*BlockChain) LookupNodeInIndex added in v0.3.0

func (b *BlockChain) LookupNodeInIndex(hash *Uint256) (*BlockNode, bool)

func (*BlockChain) MedianAdjustedTime added in v0.3.0

func (b *BlockChain) MedianAdjustedTime() time.Time

func (*BlockChain) ProcessBlock added in v0.3.0

func (b *BlockChain) ProcessBlock(block *Block, confirm *payload.Confirm) (bool, bool, error)

func (*BlockChain) ProcessIllegalBlock added in v0.3.0

func (b *BlockChain) ProcessIllegalBlock(payload *payload.DPOSIllegalBlocks)

func (*BlockChain) ProcessInactiveArbiter added in v0.3.2

func (b *BlockChain) ProcessInactiveArbiter(payload *payload.InactiveArbitrators)

func (*BlockChain) ProcessOrphans added in v0.3.0

func (b *BlockChain) ProcessOrphans(hash *Uint256) error

func (*BlockChain) RemoveNodeFromIndex added in v0.3.0

func (b *BlockChain) RemoveNodeFromIndex(node *BlockNode)

func (*BlockChain) RemoveOrphanBlock added in v0.3.0

func (b *BlockChain) RemoveOrphanBlock(orphan *OrphanBlock)

func (*BlockChain) ReorganizeChain added in v0.3.0

func (b *BlockChain) ReorganizeChain(block *Block) error

ReorganizeChain reorganize chain by specify a block, this method shall not be called normally because it can cause reorganizing without node work sum checking

type BlockNode

type BlockNode struct {
	Hash        *Uint256
	ParentHash  *Uint256
	Height      uint32
	Version     uint32
	Bits        uint32
	Timestamp   uint32
	WorkSum     *big.Int
	InMainChain bool
	Parent      *BlockNode
	Children    []*BlockNode
}

func NewBlockNode

func NewBlockNode(header *Header, hash *Uint256) *BlockNode

func RemoveChildNode

func RemoveChildNode(children []*BlockNode, node *BlockNode) []*BlockNode

type ChainStore

type ChainStore struct {
	IStore
	// contains filtered or unexported fields
}

func (*ChainStore) Close

func (c *ChainStore) Close()

func (*ChainStore) ContainsUnspent

func (c *ChainStore) ContainsUnspent(txID Uint256, index uint16) (bool, error)

func (*ChainStore) GetAsset

func (c *ChainStore) GetAsset(hash Uint256) (*payload.Asset, error)

func (*ChainStore) GetAssets

func (c *ChainStore) GetAssets() map[Uint256]*payload.Asset

func (*ChainStore) GetBlock

func (c *ChainStore) GetBlock(hash Uint256) (*Block, error)

func (*ChainStore) GetBlockHash

func (c *ChainStore) GetBlockHash(height uint32) (Uint256, error)

func (*ChainStore) GetConfirm added in v0.2.2

func (c *ChainStore) GetConfirm(hash Uint256) (*payload.Confirm, error)

func (*ChainStore) GetCurrentBlockHash

func (c *ChainStore) GetCurrentBlockHash() Uint256

func (*ChainStore) GetHeader

func (c *ChainStore) GetHeader(hash Uint256) (*Header, error)

func (*ChainStore) GetHeight

func (c *ChainStore) GetHeight() uint32

func (*ChainStore) GetSidechainTx added in v0.1.1

func (c *ChainStore) GetSidechainTx(sidechainTxHash Uint256) (byte, error)

func (*ChainStore) GetTransaction

func (c *ChainStore) GetTransaction(txID Uint256) (*Transaction, uint32, error)

func (*ChainStore) GetTxReference

func (c *ChainStore) GetTxReference(tx *Transaction) (map[*Input]*Output, error)

func (*ChainStore) GetUnspent

func (c *ChainStore) GetUnspent(txID Uint256, index uint16) (*Output, error)

func (*ChainStore) GetUnspentElementFromProgramHash

func (c *ChainStore) GetUnspentElementFromProgramHash(programHash Uint168, assetid Uint256, height uint32) ([]*UTXO, error)

func (*ChainStore) GetUnspentFromProgramHash

func (c *ChainStore) GetUnspentFromProgramHash(programHash Uint168, assetid Uint256) ([]*UTXO, error)

func (*ChainStore) GetUnspentsFromProgramHash

func (c *ChainStore) GetUnspentsFromProgramHash(programHash Uint168) (map[Uint256][]*UTXO, error)

func (*ChainStore) IsBlockInStore

func (c *ChainStore) IsBlockInStore(hash *Uint256) bool

func (*ChainStore) IsDoubleSpend

func (c *ChainStore) IsDoubleSpend(txn *Transaction) bool

func (*ChainStore) IsSidechainTxHashDuplicate added in v0.1.1

func (c *ChainStore) IsSidechainTxHashDuplicate(sidechainTxHash Uint256) bool

func (*ChainStore) IsTxHashDuplicate

func (c *ChainStore) IsTxHashDuplicate(txhash Uint256) bool

func (*ChainStore) PersistAsset

func (c *ChainStore) PersistAsset(assetID Uint256, asset payload.Asset) error

func (*ChainStore) PersistConfirm added in v0.2.2

func (c *ChainStore) PersistConfirm(
	confirm *payload.Confirm) error

func (*ChainStore) PersistSidechainTx added in v0.1.1

func (c *ChainStore) PersistSidechainTx(sidechainTxHash Uint256)

func (*ChainStore) PersistTransactions

func (c *ChainStore) PersistTransactions(b *Block) error

func (*ChainStore) PersistUnspentWithProgramHash

func (c *ChainStore) PersistUnspentWithProgramHash(programHash Uint168, assetid Uint256, height uint32, unspents []*UTXO) error

func (*ChainStore) RollbackBlock

func (c *ChainStore) RollbackBlock(blockHash Uint256) error

func (*ChainStore) RollbackBlockHash

func (c *ChainStore) RollbackBlockHash(b *Block) error

func (*ChainStore) RollbackConfirm added in v0.2.2

func (c *ChainStore) RollbackConfirm(b *Block) error

func (*ChainStore) RollbackCurrentBlock

func (c *ChainStore) RollbackCurrentBlock(b *Block) error

func (*ChainStore) RollbackTransactions

func (c *ChainStore) RollbackTransactions(b *Block) error

func (*ChainStore) RollbackTrimmedBlock

func (c *ChainStore) RollbackTrimmedBlock(b *Block) error

func (*ChainStore) RollbackUnspend

func (c *ChainStore) RollbackUnspend(b *Block) error

func (*ChainStore) RollbackUnspendUTXOs

func (c *ChainStore) RollbackUnspendUTXOs(b *Block) error

func (*ChainStore) SaveBlock

func (c *ChainStore) SaveBlock(b *Block, confirm *payload.Confirm) error

type DataEntryPrefix

type DataEntryPrefix byte

DataEntryPrefix

const (
	// DATA
	DATABlockHash   DataEntryPrefix = 0x00
	DATAHeader      DataEntryPrefix = 0x01
	DATATransaction DataEntryPrefix = 0x02
	DATAConfirm     DataEntryPrefix = 0x03

	//SYSTEM
	SYSCurrentBlock      DataEntryPrefix = 0x40
	SYSCurrentBookKeeper DataEntryPrefix = 0x42

	// INDEX
	IXHeaderHashList DataEntryPrefix = 0x80
	IXUnspent        DataEntryPrefix = 0x90
	IXUnspentUTXO    DataEntryPrefix = 0x91
	IXSideChainTx    DataEntryPrefix = 0x92

	// ASSET
	STInfo DataEntryPrefix = 0xc0

	// DPOS
	DPOSIllegalProducer DataEntryPrefix = 0xd1

	//CONFIG
	CFGVersion DataEntryPrefix = 0xf0
)

type IChainStore

type IChainStore interface {
	SaveBlock(b *Block, confirm *payload.Confirm) error
	GetBlock(hash Uint256) (*Block, error)
	GetBlockHash(height uint32) (Uint256, error)
	IsDoubleSpend(tx *Transaction) bool

	GetConfirm(hash Uint256) (*payload.Confirm, error)

	GetHeader(hash Uint256) (*Header, error)

	RollbackBlock(hash Uint256) error

	GetTransaction(txID Uint256) (*Transaction, uint32, error)
	GetTxReference(tx *Transaction) (map[*Input]*Output, error)

	PersistAsset(assetid Uint256, asset payload.Asset) error
	GetAsset(hash Uint256) (*payload.Asset, error)

	PersistSidechainTx(sidechainTxHash Uint256)
	GetSidechainTx(sidechainTxHash Uint256) (byte, error)

	GetCurrentBlockHash() Uint256
	GetHeight() uint32

	GetUnspent(txID Uint256, index uint16) (*Output, error)
	ContainsUnspent(txID Uint256, index uint16) (bool, error)
	GetUnspentFromProgramHash(programHash Uint168, assetid Uint256) ([]*UTXO, error)
	GetUnspentsFromProgramHash(programHash Uint168) (map[Uint256][]*UTXO, error)
	GetAssets() map[Uint256]*payload.Asset

	IsTxHashDuplicate(txhash Uint256) bool
	IsSidechainTxHashDuplicate(sidechainTxHash Uint256) bool
	IsBlockInStore(hash *Uint256) bool

	Close()
}

IChainStore provides func with store package.

func NewChainStore

func NewChainStore(dataDir string, genesisBlock *Block) (IChainStore, error)

type IIterator

type IIterator interface {
	Next() bool
	Prev() bool
	First() bool
	Last() bool
	Seek(key []byte) bool
	Key() []byte
	Value() []byte
	Release()
}

type IStore

type IStore interface {
	Put(key []byte, value []byte) error
	Get(key []byte) ([]byte, error)
	Delete(key []byte) error
	NewBatch()
	BatchPut(key []byte, value []byte)
	BatchDelete(key []byte)
	BatchCommit() error
	Close() error
	NewIterator(prefix []byte) IIterator
}

type Iterator

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

func (*Iterator) First

func (it *Iterator) First() bool

func (*Iterator) Key

func (it *Iterator) Key() []byte

func (*Iterator) Last

func (it *Iterator) Last() bool

func (*Iterator) Next

func (it *Iterator) Next() bool

func (*Iterator) Prev

func (it *Iterator) Prev() bool

func (*Iterator) Release

func (it *Iterator) Release()

func (*Iterator) Seek

func (it *Iterator) Seek(key []byte) bool

func (*Iterator) Value

func (it *Iterator) Value() []byte

type Ledger

type Ledger struct {
	Blockchain  *BlockChain
	Store       IChainStore
	Arbitrators state.Arbitrators
}

Ledger - the struct for ledger

var DefaultLedger *Ledger

func (*Ledger) AppendDposBlocks added in v0.2.2

func (l *Ledger) AppendDposBlocks(confirms []*DposBlock) error

Append blocks and confirms directly

func (*Ledger) GetAsset

func (l *Ledger) GetAsset(assetID Uint256) (*payload.Asset, error)

Get the Asset from store.

func (*Ledger) GetBlockWithHash

func (l *Ledger) GetBlockWithHash(hash Uint256) (*Block, error)

Get block with block hash.

func (*Ledger) GetBlockWithHeight

func (l *Ledger) GetBlockWithHeight(height uint32) (*Block, error)

Get Block With Height.

func (*Ledger) GetDposBlocks added in v0.2.2

func (l *Ledger) GetDposBlocks(start, end uint32) ([]*DposBlock, error)

Get blocks and confirms by given height range, if end equals zero will be treat as current highest block height

func (*Ledger) GetLocalBlockChainHeight

func (l *Ledger) GetLocalBlockChainHeight() uint32

Get local block chain height.

func (*Ledger) GetTransactionWithHash

func (l *Ledger) GetTransactionWithHash(hash Uint256) (*Transaction, error)

Get transaction with hash.

func (*Ledger) IsDoubleSpend

func (l *Ledger) IsDoubleSpend(Tx *Transaction) bool

check weather the transaction contains the doubleSpend.

type LevelDB

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

func NewLevelDB

func NewLevelDB(file string) (*LevelDB, error)

func (*LevelDB) BatchCommit

func (ldb *LevelDB) BatchCommit() error

func (*LevelDB) BatchDelete

func (ldb *LevelDB) BatchDelete(key []byte)

func (*LevelDB) BatchPut

func (ldb *LevelDB) BatchPut(key []byte, value []byte)

func (*LevelDB) Close

func (ldb *LevelDB) Close() error

func (*LevelDB) Delete

func (ldb *LevelDB) Delete(key []byte) error

func (*LevelDB) Get

func (ldb *LevelDB) Get(key []byte) ([]byte, error)

func (*LevelDB) NewBatch

func (ldb *LevelDB) NewBatch()

func (*LevelDB) NewIterator

func (ldb *LevelDB) NewIterator(prefix []byte) IIterator

func (*LevelDB) Put

func (ldb *LevelDB) Put(key []byte, value []byte) error

type MedianTimeSource

type MedianTimeSource interface {
	// AdjustedTime returns the current time adjusted by the median time
	// offset as calculated from the time samples added by AddTimeSample.
	AdjustedTime() time.Time

	// AddTimeSample adds a time sample that is used when determining the
	// median time of the added samples.
	AddTimeSample(id string, timeVal time.Time)

	// Offset returns the number of seconds to adjust the local clock based
	// upon the median of the time samples added by AddTimeData.
	Offset() time.Duration
}

MedianTimeSource provides a mechanism to add several time samples which are used to determine a median time which is then used as an offset to the local clock.

func NewMedianTime

func NewMedianTime() MedianTimeSource

NewMedianTime returns a new instance of concurrency-safe implementation of the MedianTimeSource interface. The returned implementation contains the rules necessary for proper time handling in the chain consensus rules and expects the time samples to be added from the timestamp field of the version message received from remote peers that successfully connect and negotiate.

type OrphanBlock

type OrphanBlock struct {
	Block      *Block
	Expiration time.Time
}

type ProducerInfo added in v0.2.2

type ProducerInfo struct {
	Payload   *payload.ProducerInfo
	RegHeight uint32
	Vote      Fixed64
}

type ProducerState added in v0.2.2

type ProducerState byte

type UTXO

type UTXO struct {
	TxID  common.Uint256
	Index uint32
	Value common.Fixed64
}

func (*UTXO) Deserialize

func (uu *UTXO) Deserialize(r io.Reader) error

func (*UTXO) Serialize

func (uu *UTXO) Serialize(w io.Writer)

Jump to

Keyboard shortcuts

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