blockchain

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 27, 2019 License: MIT Imports: 17 Imported by: 23

Documentation

Index

Constants

View Source
const (
	ValueNone   = 0
	ValueExist  = 1
	TaskChanCap = 4
)
View Source
const (
	MaxTimeOffsetSeconds = 2 * 60 * 60
)

Variables

View Source
var (
	ValidateFuncNames = blockValidateFuncs{
		PowCheckBlockSanity:       "checkblocksanity",
		CheckHeader:               "checkheader",
		CheckTransactionsCount:    "checktransactionscount",
		CheckBlockSize:            "checkblocksize",
		CheckCoinBaseTransaction:  "checkcoinbasetransaction",
		CheckTransactionsMerkle:   "checktransactionsmerkle",
		PowCheckBlockContext:      "checkblockcontext",
		CheckProofOfWork:          "checkproofofwork",
		CheckFinalizedTransaction: "checkfinalizedtransaction",
	}

	StoreFuncNames = storeFuncs{
		PersistTrimmedBlock:  "persisttrimmedblock",
		PersistBlockHash:     "persistblockhash",
		PersistCurrentBlock:  "persistcurrentblock",
		PersistUnspendUTXOs:  "persistunspendutxos",
		PersistTransactions:  "persisttransactions",
		PersistUnspend:       "persistunspend",
		RollbackTrimmedBlock: "rollbacktrimmedblock",
		RollbackBlockHash:    "rollbackblockhash",
		RollbackCurrentBlock: "rollbackcurrentblock",
		RollbackUnspendUTXOs: "rollbackunspendutxos",
		RollbackTransactions: "rollbacktransactions",
		RollbackUnspend:      "rollbackunspend",
	}
)

Functions

func AddChildrenWork

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

func AssertBigInt added in v0.1.1

func AssertBigInt(value interface{}) *big.Int

func AssertBlock added in v0.1.1

func AssertBlock(value interface{}) *types.Block

func AssertBlockHeight added in v0.1.1

func AssertBlockHeight(value interface{}) uint32

func AssertHeader added in v0.1.1

func AssertHeader(value interface{}) *types.Header

func AssertTransaction added in v0.1.1

func AssertTransaction(value interface{}) *types.Transaction

func AssertUint32 added in v0.1.1

func AssertUint32(value interface{}) uint32

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 CheckTransactionFinalize added in v0.1.1

func CheckTransactionFinalize(tx *types.Transaction, blockHeight uint32) error

func CompactToBig

func CompactToBig(compact uint32) *big.Int

func DisableLog added in v0.1.1

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until either UseLogger or SetLogWriter are called.

func GetUint16Array

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

func HashToBig

func HashToBig(hash *common.Uint256) *big.Int

func ToByteArray

func ToByteArray(source []uint16) []byte

func UseLogger added in v0.1.1

func UseLogger(logger elalog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using elalog.

Types

type BlockChain added in v0.1.1

type BlockChain struct {
	GenesisHash common.Uint256

	BestChain      *BlockNode
	Root           *BlockNode
	Index          map[common.Uint256]*BlockNode
	IndexLock      sync.RWMutex
	DepNodes       map[common.Uint256][]*BlockNode
	Orphans        map[common.Uint256]*OrphanBlock
	PrevOrphans    map[common.Uint256][]*OrphanBlock
	OldestOrphan   *OrphanBlock
	BlockCache     map[common.Uint256]*types.Block
	TimeSource     MedianTimeSource
	MedianTimePast time.Time
	OrphanLock     sync.RWMutex
	// contains filtered or unexported fields
}

func New added in v0.1.1

func New(cfg *Config) (*BlockChain, error)

func (*BlockChain) AddNodeToIndex added in v0.1.1

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

func (*BlockChain) AddOrphanBlock added in v0.1.1

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

func (*BlockChain) BlockExists added in v0.1.1

func (b *BlockChain) BlockExists(hash *common.Uint256) (bool, error)

func (*BlockChain) BlockLocatorFromHash added in v0.1.1

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

func (*BlockChain) CalcCurrentDifficulty added in v0.1.1

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

func (*BlockChain) CalcNextRequiredDifficulty added in v0.1.1

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

func (*BlockChain) CheckBlockContext added in v0.1.1

func (b *BlockChain) CheckBlockContext(block *types.Block) error

func (*BlockChain) ContainsTransaction added in v0.1.1

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

func (*BlockChain) CurrentBlockHash added in v0.1.1

func (b *BlockChain) CurrentBlockHash() common.Uint256

func (*BlockChain) DumpState added in v0.1.1

func (b *BlockChain) DumpState()

func (*BlockChain) GetAsset added in v0.1.1

func (b *BlockChain) GetAsset(assetId common.Uint256) (*types.Asset, error)

Get the Asset from store.

func (*BlockChain) GetAssetUnspents added in v0.1.1

func (b *BlockChain) GetAssetUnspents(programHash common.Uint168, assetid common.Uint256) ([]*types.UTXO, error)

func (*BlockChain) GetAssets added in v0.1.1

func (b *BlockChain) GetAssets() map[common.Uint256]*types.Asset

func (*BlockChain) GetBestHeight added in v0.1.1

func (b *BlockChain) GetBestHeight() uint32

func (*BlockChain) GetBlockByHash added in v0.1.1

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

Get block with block hash.

func (*BlockChain) GetBlockHash added in v0.1.1

func (b *BlockChain) GetBlockHash(height uint32) (common.Uint256, error)

Get Block hash with height

func (*BlockChain) GetBlockWithHeight added in v0.1.1

func (b *BlockChain) GetBlockWithHeight(height uint32) (*types.Block, error)

Get Block With Height.

func (*BlockChain) GetHeader added in v0.1.1

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

func (*BlockChain) GetOrphanRoot added in v0.1.1

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

func (*BlockChain) GetPrevNodeFromBlock added in v0.1.1

func (b *BlockChain) GetPrevNodeFromBlock(block *types.Block) (*BlockNode, error)

getPrevNodeFromBlock returns a block node for the block previous to the passed block (the passed block's parent). When it is already in the memory block chain, it simply returns it. Otherwise, it loads the previous block from the block database, creates a new block node from it, and returns it. The returned node will be nil if the genesis block is passed.

func (*BlockChain) GetPrevNodeFromNode added in v0.1.1

func (b *BlockChain) GetPrevNodeFromNode(node *BlockNode) (*BlockNode, error)

getPrevNodeFromNode returns a block node for the block previous to the passed block node (the passed block node's parent). When the node is already connected to a parent, it simply returns it. Otherwise, it loads the associated block from the database to obtain the previous hash and uses that to dynamically create a new block node and return it. The memory block chain is updated accordingly. The returned node will be nil if the genesis block is passed.

func (*BlockChain) GetTransaction added in v0.1.1

func (b *BlockChain) GetTransaction(hash common.Uint256) (*types.Transaction, uint32, error)

Get transaction with hash.

func (*BlockChain) GetTxReference added in v0.1.1

func (b *BlockChain) GetTxReference(tx *types.Transaction) (map[*types.Input]*types.Output, error)

func (*BlockChain) GetUnspents added in v0.1.1

func (b *BlockChain) GetUnspents(programHash common.Uint168) (map[common.Uint256][]*types.UTXO, error)

func (*BlockChain) HaveBlock added in v0.1.1

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

func (*BlockChain) IsDoubleSpend added in v0.1.1

func (b *BlockChain) IsDoubleSpend(tx *types.Transaction) bool

check weather the transaction contains the doubleSpend.

func (*BlockChain) IsDuplicateMainchainTx added in v0.1.1

func (b *BlockChain) IsDuplicateMainchainTx(txId common.Uint256) bool

func (*BlockChain) IsDuplicateTx added in v0.1.1

func (b *BlockChain) IsDuplicateTx(txId common.Uint256) bool

func (*BlockChain) IsKnownOrphan added in v0.1.1

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

func (*BlockChain) LatestBlockLocator added in v0.1.1

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

func (*BlockChain) LoadBlockNode added in v0.1.1

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

func (*BlockChain) LocateBlocks added in v0.1.1

func (b *BlockChain) LocateBlocks(locator []*common.Uint256, hashStop *common.Uint256, maxHashes uint32) []*common.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.1.1

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

func (*BlockChain) MedianAdjustedTime added in v0.1.1

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

func (*BlockChain) ProcessBlock added in v0.1.1

func (b *BlockChain) ProcessBlock(block *types.Block) (bool, bool, error)

(bool, bool, error) 1. inMainChain 2. isOphan 3. error

func (*BlockChain) ProcessOrphans added in v0.1.1

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

func (*BlockChain) PruneBlockNodes added in v0.1.1

func (b *BlockChain) PruneBlockNodes() error

func (*BlockChain) RemoveBlockNode added in v0.1.1

func (b *BlockChain) RemoveBlockNode(node *BlockNode) error

func (*BlockChain) RemoveNodeFromIndex added in v0.1.1

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

func (*BlockChain) RemoveOrphanBlock added in v0.1.1

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

type BlockNode

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

func AssertBlockNode added in v0.1.1

func AssertBlockNode(value interface{}) *BlockNode

func NewBlockNode

func NewBlockNode(header *types.Header, hash *common.Uint256) *BlockNode

func RemoveChildNode

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

type BlockValidateAction added in v0.1.1

type BlockValidateAction struct {
	Name    ValidateFuncName
	Handler func(params ...interface{}) error
}

type ChainStore

type ChainStore struct {
	database.Database
	// contains filtered or unexported fields
}

func NewChainStore

func NewChainStore(path string, genesisBlock *types.Block) (*ChainStore, error)

func (*ChainStore) Close

func (s *ChainStore) Close()

func (*ChainStore) ContainsUnspent

func (s *ChainStore) ContainsUnspent(txid common.Uint256, index uint16) (bool, error)

func (*ChainStore) GetAsset

func (s *ChainStore) GetAsset(hash common.Uint256) (*types.Asset, error)

func (*ChainStore) GetAssetUnspents added in v0.1.1

func (s *ChainStore) GetAssetUnspents(programHash common.Uint168, assetid common.Uint256) ([]*types.UTXO, error)

func (*ChainStore) GetAssets

func (s *ChainStore) GetAssets() map[common.Uint256]*types.Asset

func (*ChainStore) GetBlock

func (s *ChainStore) GetBlock(hash common.Uint256) (*types.Block, error)

func (*ChainStore) GetBlockHash

func (s *ChainStore) GetBlockHash(height uint32) (common.Uint256, error)

func (*ChainStore) GetCurrentBlockHash

func (s *ChainStore) GetCurrentBlockHash() common.Uint256

func (*ChainStore) GetHeader

func (s *ChainStore) GetHeader(hash common.Uint256) (*types.Header, error)

func (*ChainStore) GetHeight

func (s *ChainStore) GetHeight() uint32

func (*ChainStore) GetMainchainTx

func (s *ChainStore) GetMainchainTx(mainchainTxHash common.Uint256) (byte, error)

func (*ChainStore) GetTransaction

func (s *ChainStore) GetTransaction(txId common.Uint256) (*types.Transaction, uint32, error)

func (*ChainStore) GetTxReference

func (s *ChainStore) GetTxReference(tx *types.Transaction) (map[*types.Input]*types.Output, error)

func (*ChainStore) GetUnspent

func (s *ChainStore) GetUnspent(txid common.Uint256, index uint16) (*types.Output, error)

func (*ChainStore) GetUnspentElementFromProgramHash

func (s *ChainStore) GetUnspentElementFromProgramHash(programHash common.Uint168, assetid common.Uint256, height uint32) ([]*types.UTXO, error)

func (*ChainStore) GetUnspents added in v0.1.1

func (s *ChainStore) GetUnspents(programHash common.Uint168) (map[common.Uint256][]*types.UTXO, error)

func (*ChainStore) IsBlockInStore

func (s *ChainStore) IsBlockInStore(hash *common.Uint256) bool

func (*ChainStore) IsDoubleSpend

func (s *ChainStore) IsDoubleSpend(txn *types.Transaction) bool

func (*ChainStore) IsDuplicateMainchainTx added in v0.1.1

func (s *ChainStore) IsDuplicateMainchainTx(mainchainTxHash common.Uint256) bool

func (*ChainStore) IsDuplicateTx added in v0.1.1

func (s *ChainStore) IsDuplicateTx(txId common.Uint256) bool

func (*ChainStore) PersistAsset

func (s *ChainStore) PersistAsset(batch database.Batch, assetId common.Uint256, asset types.Asset) error

func (*ChainStore) PersistMainchainTx

func (s *ChainStore) PersistMainchainTx(batch database.Batch, mainchainTxHash common.Uint256)

func (*ChainStore) PersistTransaction

func (s *ChainStore) PersistTransaction(batch database.Batch, tx *types.Transaction, height uint32) error

func (*ChainStore) PersistUnspentWithProgramHash

func (s *ChainStore) PersistUnspentWithProgramHash(batch database.Batch, programHash common.Uint168, assetid common.Uint256, height uint32, unspents []*types.UTXO) error

func (*ChainStore) RegisterFunctions added in v0.1.1

func (s *ChainStore) RegisterFunctions(isPersist bool, name StoreFuncName, handler func(batch database.Batch, b *types.Block) error)

func (*ChainStore) RollbackAsset

func (s *ChainStore) RollbackAsset(batch database.Batch, assetId common.Uint256) error

func (*ChainStore) RollbackBlock

func (s *ChainStore) RollbackBlock(blockHash common.Uint256) error

func (*ChainStore) RollbackMainchainTx

func (s *ChainStore) RollbackMainchainTx(batch database.Batch, mainchainTxHash common.Uint256) error

func (*ChainStore) RollbackTransaction

func (s *ChainStore) RollbackTransaction(batch database.Batch, txn *types.Transaction) error

func (*ChainStore) SaveBlock

func (s *ChainStore) SaveBlock(b *types.Block) error

type Config added in v0.1.1

type Config struct {
	ChainStore     *ChainStore
	ChainParams    *config.Params
	Validator      *Validator
	CheckTxSanity  func(*types.Transaction) error
	CheckTxContext func(*types.Transaction) error
	GetTxFee       func(tx *types.Transaction, assetId common.Uint256) (common.Fixed64, error)
}

type EntryPrefix added in v0.1.1

type EntryPrefix byte

EntryPrefix

const (
	// DATA
	DATA_BlockHash   EntryPrefix = 0x00
	DATA_Header      EntryPrefix = 0x01
	DATA_Transaction EntryPrefix = 0x02

	// INDEX
	IX_HeaderHashList EntryPrefix = 0x80
	IX_Unspent        EntryPrefix = 0x90
	IX_Unspent_UTXO   EntryPrefix = 0x91
	IX_SideChain_Tx   EntryPrefix = 0x92
	IX_MainChain_Tx   EntryPrefix = 0x93
	IX_Identification EntryPrefix = 0x94

	// ASSET
	ST_Info EntryPrefix = 0xc0

	//NEOVM
	ST_Contract   EntryPrefix = 0xc1
	ST_Storage    EntryPrefix = 0xc2
	ST_Account    EntryPrefix = 0xc3
	ST_AssetState EntryPrefix = 0xc4

	//SYSTEM
	SYS_CurrentBlock      EntryPrefix = 0x40
	SYS_CurrentBookKeeper EntryPrefix = 0x42

	//CONFIG
	CFG_Version EntryPrefix = 0xf0
)

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 AssertMedianTimeSource added in v0.1.1

func AssertMedianTimeSource(value interface{}) MedianTimeSource

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      *types.Block
	Expiration time.Time
}

type StoreFuncName added in v0.1.1

type StoreFuncName string

type ValidateFuncName added in v0.1.1

type ValidateFuncName string

type Validator added in v0.1.1

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

func NewValidator added in v0.1.1

func NewValidator(chain *BlockChain, spv *spv.Service) *Validator

func (*Validator) CheckBlockContext added in v0.1.1

func (v *Validator) CheckBlockContext(block *types.Block, prevNode *BlockNode) (err error)

func (*Validator) CheckBlockSanity added in v0.1.1

func (v *Validator) CheckBlockSanity(block *types.Block, powLimit *big.Int, timeSource MedianTimeSource) error

func (*Validator) RegisterFunc added in v0.1.1

func (v *Validator) RegisterFunc(name ValidateFuncName, function func(params ...interface{}) error)

Jump to

Keyboard shortcuts

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