ledger

package
v0.0.2-d277 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2018 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BlockVersion     uint32 = 0
	GenesisNonce     uint32 = 2083236893
	InvalidBlockSize int    = -1
)
View Source
const (
	FoundationAddress = "8VYXVxKKSAxkmRrfmGpQR2Kc66XhG6m3ta"
)
View Source
const (
	MaxBlockLocatorsPerMsg = 500
)
View Source
const (
	MaxTimeOffsetSeconds = 2 * 60 * 60
)

Variables

View Source
var (
	MaxBlockSize = config.Parameters.MaxBlockSize
)
View Source
var (
	MinMemoryNodes = config.Parameters.ChainParam.MinMemoryNodes
)

Functions

func AddChildrenWork

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

func BigToCompact

func BigToCompact(n *big.Int) uint32

func CalcCurrentDifficulty

func CalcCurrentDifficulty(currentBits uint32) string

func CalcNextRequiredDifficulty

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

func CalcPastMedianTime

func CalcPastMedianTime(node *BlockNode) time.Time

func CalcWork

func CalcWork(bits uint32) *big.Int

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

func CheckAssetPrecision

func CheckAssetPrecision(Tx *tx.Transaction) error

func CheckAttributeProgram

func CheckAttributeProgram(txn *tx.Transaction) error

func CheckProofOfWork

func CheckProofOfWork(bd *Blockdata, powLimit *big.Int) error

func CheckTransactionBalance

func CheckTransactionBalance(Tx *tx.Transaction) error

func CheckTransactionContext

func CheckTransactionContext(txn *tx.Transaction, ledger *Ledger) ErrCode

CheckTransactionContext verifys a transaction with history transaction in ledger

func CheckTransactionInput

func CheckTransactionInput(txn *tx.Transaction) error

validate the transaction of duplicate UTXO input

func CheckTransactionOutput

func CheckTransactionOutput(txn *tx.Transaction) error

func CheckTransactionPayload

func CheckTransactionPayload(Tx *tx.Transaction) error

func CheckTransactionSanity

func CheckTransactionSanity(txn *tx.Transaction) ErrCode

CheckTransactionSanity verifys received single transaction

func CheckTransactionSignature

func CheckTransactionSignature(txn *tx.Transaction) error

func CheckTransactionSize

func CheckTransactionSize(txn *tx.Transaction) error

func CheckTransactionUTXOLock

func CheckTransactionUTXOLock(txn *tx.Transaction) error

func CompactToBig

func CompactToBig(compact uint32) *big.Int

func DumpBlockNode

func DumpBlockNode(node *BlockNode)

func HashToBig

func HashToBig(hash *Uint256) *big.Int

func InAddressList

func InAddressList(address string) bool

func IsDoubleSpend

func IsDoubleSpend(tx *tx.Transaction, ledger *Ledger) bool

func IsFinalizedTransaction

func IsFinalizedTransaction(msgTx *tx.Transaction, blockHeight uint32) bool

func PowCheckBlockContext

func PowCheckBlockContext(block *Block, prevNode *BlockNode, ledger *Ledger) error

func PowCheckBlockSanity

func PowCheckBlockSanity(block *Block, powLimit *big.Int, timeSource MedianTimeSource) error

Types

type Block

type Block struct {
	Blockdata    *Blockdata
	Transactions []*tx.Transaction
	// contains filtered or unexported fields
}

func GenesisBlockInit

func GenesisBlockInit() (*Block, error)

func (*Block) Deserialize

func (b *Block) Deserialize(r io.Reader) error

func (*Block) FromTrimmedData

func (b *Block) FromTrimmedData(r io.Reader) error

func (*Block) GetDataContent

func (b *Block) GetDataContent() []byte

func (*Block) GetProgramHashes

func (b *Block) GetProgramHashes() ([]Uint168, error)

func (*Block) GetPrograms

func (b *Block) GetPrograms() []*program.Program

func (*Block) GetSize

func (tx *Block) GetSize() int

func (*Block) Hash

func (b *Block) Hash() Uint256

func (*Block) RebuildMerkleRoot

func (b *Block) RebuildMerkleRoot() error

func (*Block) Serialize

func (b *Block) Serialize(w io.Writer) error

func (*Block) SerializeUnsigned

func (bd *Block) SerializeUnsigned(w io.Writer) error

func (*Block) SetPrograms

func (b *Block) SetPrograms(prog []*program.Program)

func (*Block) Trim

func (b *Block) Trim(w io.Writer) error

func (*Block) Verify

func (b *Block) Verify() error

type BlockLocator

type BlockLocator []Uint256

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(blockHeader *Blockdata, blockSha *Uint256) *BlockNode

func RemoveChildNode

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

type Blockchain

type Blockchain struct {
	BlockHeight    uint32
	GenesisHash    Uint256
	BestChain      *BlockNode
	Root           *BlockNode
	Index          map[Uint256]*BlockNode
	IndexLock      sync.RWMutex
	DepNodes       map[Uint256][]*BlockNode
	Orphans        map[Uint256]*OrphanBlock
	PrevOrphans    map[Uint256][]*OrphanBlock
	OldestOrphan   *OrphanBlock
	BlockCache     map[Uint256]*Block
	TimeSource     MedianTimeSource
	MedianTimePast time.Time
	OrphanLock     sync.RWMutex
	BCEvents       *events.Event

	Ledger  *Ledger
	AssetID Uint256
	// contains filtered or unexported fields
}

func NewBlockchain

func NewBlockchain(height uint32, ledger *Ledger) *Blockchain

func NewBlockchainWithGenesisBlock

func NewBlockchainWithGenesisBlock() (*Blockchain, error)

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(block *Block) (bool, bool, error)

func (*Blockchain) AddNodeToIndex

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

func (*Blockchain) AddOrphanBlock

func (bc *Blockchain) AddOrphanBlock(block *Block)

func (*Blockchain) BlockExists

func (bc *Blockchain) BlockExists(hash *Uint256) (bool, error)

func (*Blockchain) BlockLocatorFromHash

func (b *Blockchain) BlockLocatorFromHash(inhash *Uint256) BlockLocator

func (*Blockchain) ConnectBestChain

func (bc *Blockchain) ConnectBestChain(node *BlockNode, block *Block) (bool, error)

func (*Blockchain) ConnectBlock

func (bc *Blockchain) ConnectBlock(node *BlockNode, block *Block) error

connectBlock handles connecting the passed node/block to the end of the main (best) chain.

func (*Blockchain) ContainsTransaction

func (bc *Blockchain) ContainsTransaction(hash Uint256) bool

func (*Blockchain) CurrentBlockHash

func (bc *Blockchain) CurrentBlockHash() Uint256

func (*Blockchain) DisconnectBlock

func (bc *Blockchain) DisconnectBlock(node *BlockNode, block *Block) error

// disconnectBlock handles disconnecting the passed node/block from the end of // the main (best) chain.

func (*Blockchain) DumpState

func (bc *Blockchain) DumpState()

func (*Blockchain) GetBestHeight

func (bc *Blockchain) GetBestHeight() uint32

func (*Blockchain) GetHeader

func (bc *Blockchain) GetHeader(hash Uint256) (*Header, error)

func (*Blockchain) GetOrphanRoot

func (bc *Blockchain) GetOrphanRoot(hash *Uint256) *Uint256

func (*Blockchain) GetPrevNodeFromBlock

func (bc *Blockchain) GetPrevNodeFromBlock(block *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

func (bc *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) GetReorganizeNodes

func (bc *Blockchain) GetReorganizeNodes(node *BlockNode) (*list.List, *list.List)

getReorganizeNodes finds the fork point between the main chain and the passed node and returns a list of block nodes that would need to be detached from the main chain and a list of block nodes that would need to be attached to the fork point (which will be the end of the main chain after detaching the returned list of block nodes) in order to reorganize the chain such that the passed node is the new end of the main chain. The lists will be empty if the passed node is not on a side chain.

func (*Blockchain) IsKnownOrphan

func (bc *Blockchain) IsKnownOrphan(hash *Uint256) bool

func (*Blockchain) LatestBlockLocator

func (b *Blockchain) LatestBlockLocator() (BlockLocator, error)

func (*Blockchain) LatestLocatorHash

func (b *Blockchain) LatestLocatorHash(locator BlockLocator) Uint256

func (*Blockchain) LoadBlockNode

func (bc *Blockchain) LoadBlockNode(blockHeader *Blockdata, hash *Uint256) (*BlockNode, error)

func (*Blockchain) LookupNodeInIndex

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

func (*Blockchain) MedianAdjustedTime

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

func (*Blockchain) ProcessBlock

func (bc *Blockchain) ProcessBlock(block *Block, timeSource MedianTimeSource, flags uint32) (bool, bool, error)

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

func (*Blockchain) ProcessOrphans

func (bc *Blockchain) ProcessOrphans(hash *Uint256) error

func (*Blockchain) PruneBlockNodes

func (bc *Blockchain) PruneBlockNodes() error

func (*Blockchain) RemoveBlockNode

func (bc *Blockchain) RemoveBlockNode(node *BlockNode) error

func (*Blockchain) RemoveNodeFromIndex

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

func (*Blockchain) RemoveOrphanBlock

func (bc *Blockchain) RemoveOrphanBlock(orphan *OrphanBlock)

func (*Blockchain) ReorganizeChain

func (bc *Blockchain) ReorganizeChain(detachNodes, attachNodes *list.List) error

reorganizeChain reorganizes the block chain by disconnecting the nodes in the detachNodes list and connecting the nodes in the attach list. It expects that the lists are already in the correct order and are in sync with the end of the current best chain. Specifically, nodes that are being disconnected must be in reverse order (think of popping them off the end of the chain) and nodes the are being attached must be in forwards order (think pushing them onto the end of the chain).

func (*Blockchain) UpdateBestHeight

func (bc *Blockchain) UpdateBestHeight(val uint32)

type Blockdata

type Blockdata struct {
	Version          uint32
	PrevBlockHash    Uint256
	TransactionsRoot Uint256
	Timestamp        uint32
	Bits             uint32
	Height           uint32
	Nonce            uint32
	AuxPow           auxpow.AuxPow
	Program          *program.Program
	// contains filtered or unexported fields
}

func (*Blockdata) Deserialize

func (bd *Blockdata) Deserialize(r io.Reader) error

func (*Blockdata) DeserializeUnsigned

func (bd *Blockdata) DeserializeUnsigned(r io.Reader) error

func (*Blockdata) GetDataContent

func (bd *Blockdata) GetDataContent() []byte

func (*Blockdata) GetPreBlockHash

func (bd *Blockdata) GetPreBlockHash() Uint256

func (*Blockdata) GetProgramHashes

func (bd *Blockdata) GetProgramHashes() ([]Uint168, error)

func (*Blockdata) GetPrograms

func (bd *Blockdata) GetPrograms() []*program.Program

func (*Blockdata) Hash

func (bd *Blockdata) Hash() Uint256

func (*Blockdata) Serialize

func (bd *Blockdata) Serialize(w io.Writer)

Serialize the blockheader

func (*Blockdata) SerializeUnsigned

func (bd *Blockdata) SerializeUnsigned(w io.Writer) error

Serialize the blockheader data without program

func (*Blockdata) SetPrograms

func (bd *Blockdata) SetPrograms(programs []*program.Program)
type Header struct {
	Blockdata *Blockdata
}

func (*Header) Deserialize

func (h *Header) Deserialize(r io.Reader) error

func (*Header) Serialize

func (h *Header) Serialize(w io.Writer)

Serialize the blockheader

type ILedgerStore

type ILedgerStore interface {
	SaveBlock(b *Block, ledger *Ledger) error
	GetBlock(hash Uint256) (*Block, error)
	BlockInCache(hash Uint256) bool
	GetBlockHash(height uint32) (Uint256, error)
	InitLedgerStore(ledger *Ledger) error
	IsDoubleSpend(tx *tx.Transaction) bool

	//SaveHeader(header *Header,ledger *Ledger) error
	GetHeader(hash Uint256) (*Header, error)

	RollbackBlock(blockHash Uint256) error

	GetTransaction(hash Uint256) (*tx.Transaction, uint32, error)

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

	GetCurrentBlockHash() Uint256
	GetHeight() uint32

	RemoveHeaderListElement(hash Uint256)

	InitLedgerStoreWithGenesisBlock(genesisblock *Block) (uint32, error)

	GetUnspent(txid Uint256, index uint16) (*tx.TxOutput, error)
	ContainsUnspent(txid Uint256, index uint16) (bool, error)
	GetUnspentFromProgramHash(programHash Uint168, assetid Uint256) ([]*tx.UTXOUnspent, error)
	GetUnspentsFromProgramHash(programHash Uint168) (map[Uint256][]*tx.UTXOUnspent, error)
	GetAssets() map[Uint256]*Asset

	IsTxHashDuplicate(txhash Uint256) bool
	IsBlockInStore(hash Uint256) bool
	Close()
}

ILedgerStore provides func with store package.

type Ledger

type Ledger struct {
	Blockchain *Blockchain
	Store      ILedgerStore
}

Ledger - the struct for ledger

var DefaultLedger *Ledger

func (*Ledger) BlockInLedger

func (l *Ledger) BlockInLedger(hash Uint256) bool

BlockInLedger checks if the block existed in ledger

func (*Ledger) GetAsset

func (l *Ledger) GetAsset(assetId Uint256) (*asset.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) GetLocalBlockChainHeight

func (l *Ledger) GetLocalBlockChainHeight() uint32

Get local block chain height.

func (*Ledger) GetTransactionWithHash

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

Get transaction with hash.

func (*Ledger) IsDoubleSpend

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

check weather the transaction contains the doubleSpend.

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
}

Jump to

Keyboard shortcuts

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