core

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: GPL-3.0 Imports: 36 Imported by: 0

README

Core

Documentation

Index

Constants

View Source
const (
	ChainDataTypeLatestH = "latestH" // latest block hash
	ChainDataTypeLatestN = "latestN" // latest block number
	ChainDataTypeBlockH  = "blkH"    // block for given hash
	ChainDataTypeBlockN  = "blkN"    // block for given number
)

chainData types used to query from peers

View Source
const (
	KeyChainID = "ChainID"

	KeyLastBlock = "LastBlock"

	KeyPrefixStateTrie = "sTrie-" // stateTrie Hash => trie node

	KeyPrefixTx     = "tx-"   // txHash => encodedTx
	KeyPrefixHeader = "blkH-" // blockHash => encodedBlockHeader
	KeyPrefixBody   = "blkB-" // blockHash => encodedBlockBody

	KeyPrefixBlockNum2Hash = "bn2h-" // blockNum => blockHash
	KeyPrefixBlockHash2Num = "bh2n-" // blockHash => blockNum
)

Key / KeyPrefix for blockchain used in persistent.Storage

View Source
const (
	EventReqDhtGetMaxRetry  = 128
	EventReqDhtGetTimeout   = 60 * time.Second
	OutputTxsDhtGetMaxRetry = 1024
	OutputTxsDhtGetTimeout  = 60 * time.Second
)
View Source
const TooFarBlocks = 120
View Source
const TooFarTx = 8192

Variables

View Source
var (
	EmptyRootHash = DeriveHash(Transactions{})

	ErrBlockBodyTxsMismatch = errors.New("block body txs mismatch")
)
View Source
var (
	ErrBlockChainID        = errors.New("block chainID mismatch")
	ErrBlockTooFarForChain = errors.New("block too far for chain head")
)
View Source
var (
	ErrBlockChainNoStorage    = errors.New("core.chain: must provide block chain storage")
	ErrBlockChainIDMismatch   = errors.New("core.chain: chainID mismatch")
	ErrBlockStateTrieMismatch = errors.New("core.chain: trie root hash mismatch")
	ErrBlockParentMissing     = errors.New("core.chain: block parent missing")
	ErrBlockParentMismatch    = errors.New("core.chain: block parent mismatch")
	ErrBlockSignatureMismatch = errors.New("core.chain: block signature mismatch")
)
View Source
var (
	ErrNoCoinbase          = errors.New("coinbase not provided")
	ErrNoCoinbasePwdFile   = errors.New("coinbase keystore password file not provided")
	ErrCoinbaseKeyNotFound = errors.New("coinbase not found in keystore")
)
View Source
var (
	ErrNoSignature       = errors.New("no signature with tx")
	ErrNoSigner          = errors.New("no signer found")
	ErrSignatureMismatch = errors.New("signature mismatch")
	ErrTxFromMismatch    = errors.New("tx sender mismatch")
)
View Source
var (
	ErrInvalidProtoToTransaction = errors.New("failed to parse ProtoBuf msg to Transaction")
)
View Source
var (
	ErrTxChainID = errors.New("transaction chainID mismatch")
)

Functions

func DeriveHash

func DeriveHash(list DerivableList) common.Hash

Add list elements to a in-mem trie, with index as key, trie root hash is returned.

func GetStateDB

func GetStateDB(storage persistent.Storage) state.Database

Types

type Block

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

In-memory representative for the block concept

func CopyBlock

func CopyBlock(b *Block) *Block

func NewBlock

func NewBlock(header *BlockHeader, txs []*Transaction) *Block

func ParseBlock

func ParseBlock(enc []byte) (*Block, error)

func (*Block) ChainID

func (b *Block) ChainID() uint32

func (*Block) ConsensusRoot

func (b *Block) ConsensusRoot() common.Hash

func (*Block) Extra

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

func (*Block) GetAccount

func (b *Block) GetAccount(address common.Address) state.Account

func (*Block) Hash

func (b *Block) Hash() common.Hash

func (*Block) Number

func (b *Block) Number() uint64

func (*Block) ParentHash

func (b *Block) ParentHash() common.Hash

func (*Block) ReceiptsRoot

func (b *Block) ReceiptsRoot() common.Hash

func (*Block) Sign

func (b *Block) Sign(signer crypto.Signer) error

func (*Block) Signers

func (b *Block) Signers() (map[common.Address]crypto.Signature, error)

func (*Block) StateRoot

func (b *Block) StateRoot() common.Hash

func (*Block) Time

func (b *Block) Time() uint64

func (*Block) ToBytes

func (b *Block) ToBytes() ([]byte, error)

func (*Block) TxsRoot

func (b *Block) TxsRoot() common.Hash

func (*Block) ValidatorAddr

func (b *Block) ValidatorAddr() []common.Address

func (*Block) VerifyBody

func (b *Block) VerifyBody() error

Verify block body match with hash in header

func (*Block) Write

func (b *Block) Write(putter persistent.Putter) error

type BlockChain

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

BlockChain is a Data Manager that

created with a Storage, for chain trie/data storage
created with a Genesis block
handles tx / block lookup within the chain
check on  block arrival, receive block on signatures confirmation
notify sub routines to stop, while wait for them to stop

func NewBlockChain

func NewBlockChain(chainID ChainID, storage persistent.Storage, engine consensus.Engine) (*BlockChain, error)

func NewBlockChainWithCore

func NewBlockChainWithCore(core *Core) (*BlockChain, error)

func (*BlockChain) AddBlock

func (bc *BlockChain) AddBlock(b *Block) error

add a checked block to block chain, as last block

func (*BlockChain) BuildNextBlock

func (bc *BlockChain) BuildNextBlock(parent *Block, t uint64, txs Transactions) (*Block, error)

Build Next block from parent block, with transactions

func (*BlockChain) ChainID

func (bc *BlockChain) ChainID() ChainID

func (*BlockChain) CurrentBlockHeight

func (bc *BlockChain) CurrentBlockHeight() uint64

func (*BlockChain) GetBlockByHash

func (bc *BlockChain) GetBlockByHash(hash common.Hash) *Block

func (*BlockChain) GetBlockByNumber

func (bc *BlockChain) GetBlockByNumber(number uint64) *Block

func (*BlockChain) GetBlockNum2Hash

func (bc *BlockChain) GetBlockNum2Hash(number uint64) *common.Hash

func (*BlockChain) GetTxByHash

func (bc *BlockChain) GetTxByHash(hash common.Hash) *Transaction

func (*BlockChain) GetValidators

func (bc *BlockChain) GetValidators() []string

func (*BlockChain) LastBlock

func (bc *BlockChain) LastBlock() *Block

func (*BlockChain) Reset

func (bc *BlockChain) Reset() error

reset chain to genesis block

func (*BlockChain) State

func (bc *BlockChain) State() (state.AccountTrie, error)

func (*BlockChain) StateAt

func (bc *BlockChain) StateAt(root common.Hash) (state.AccountTrie, error)

func (*BlockChain) Stop

func (bc *BlockChain) Stop()

type BlockHeader

type BlockHeader struct {
	// chain
	ChainID    uint32      `json:"chainID"`
	Number     uint64      `json:"number"`
	ParentHash common.Hash `json:"parentHash"`

	// trie root hashes
	ConsensusRoot common.Hash `json:"consensusRoot"`
	StateRoot     common.Hash `json:"stateRoot"`
	TxsRoot       common.Hash `json:"transactionsRoot"`
	ReceiptsRoot  common.Hash `json:"receiptsRoot"`

	// block time in milli seconds
	Time uint64 `json:"timestamp"`

	// extra binary data
	Extra []byte `json:"extraData"`
}

Block Header of yee chain

Encoded with RLP into byte[] for hashing stored as value in Storage, with hash as key

func CopyHeader

func CopyHeader(header *BlockHeader) *BlockHeader

func (*BlockHeader) Hash

func (bh *BlockHeader) Hash() ([]byte, error)

func (*BlockHeader) ToBytes

func (bh *BlockHeader) ToBytes() ([]byte, error)

type BlockPool

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

func NewBlockPool

func NewBlockPool(core *Core) (*BlockPool, error)

func (*BlockPool) AddSealRequest

func (bp *BlockPool) AddSealRequest(h, t uint64, txs Transactions)

func (*BlockPool) GetBlockByHash

func (bp *BlockPool) GetBlockByHash(hash common.Hash) *Block

func (*BlockPool) GetBlockByNumber

func (bp *BlockPool) GetBlockByNumber(number uint64) *Block

func (*BlockPool) GetBlockNum2Hash

func (bp *BlockPool) GetBlockNum2Hash(number uint64) *common.Hash

func (*BlockPool) Start

func (bp *BlockPool) Start()

func (*BlockPool) Stop

func (bp *BlockPool) Stop()

type ChainID

type ChainID uint32
const (
	MainNetID ChainID = 0
	TestNetID ChainID = 1
)

type ChainReader

type ChainReader interface {
	GetBlockByNumber(number uint64) *Block
	GetBlockByHash(hash common.Hash) *Block
	GetBlockNum2Hash(number uint64) *common.Hash
	GetTxByHash(hash common.Hash) *Transaction
}

type Core

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

func NewCore

func NewCore(node INode, conf *config.Config) (*Core, error)

func NewCoreWithGenesis

func NewCoreWithGenesis(node INode, conf *config.Config, genesis *Genesis) (*Core, error)

func (*Core) AddressFromPublicKey

func (c *Core) AddressFromPublicKey(publicKey []byte) ([]byte, error)

func (*Core) Chain

func (c *Core) Chain() *BlockChain

func (*Core) FakeP2pRecv

func (c *Core) FakeP2pRecv(msg *p2p.Message)

as if msg was received from p2p module

func (*Core) GetChainData

func (c *Core) GetChainData(kind string, key []byte) []byte

func (*Core) GetMinerSigner

func (c *Core) GetMinerSigner() (crypto.Signer, error)

func (*Core) GetPrivateKeyOfDefaultAccount

func (c *Core) GetPrivateKeyOfDefaultAccount() ([]byte, error)

func (*Core) GetRemoteBlockByHash

func (c *Core) GetRemoteBlockByHash(hash common.Hash) (*Block, error)

func (*Core) GetRemoteBlockByNumber

func (c *Core) GetRemoteBlockByNumber(n uint64) (*Block, error)

func (*Core) GetRemoteLatestHash

func (c *Core) GetRemoteLatestHash() (*common.Hash, error)

func (*Core) GetRemoteLatestNumber

func (c *Core) GetRemoteLatestNumber() (uint64, error)

func (*Core) GetSigner

func (c *Core) GetSigner() crypto.Signer

ICORE

func (*Core) IsSyncing

func (c *Core) IsSyncing() bool

func (*Core) MinerAddr

func (c *Core) MinerAddr() *address.Address

func (*Core) Start

func (c *Core) Start() error

func (*Core) Stop

func (c *Core) Stop() error

func (*Core) TriggerSync

func (c *Core) TriggerSync()

func (*Core) TxBroadcast

func (c *Core) TxBroadcast(tx *Transaction) error

type DerivableList

type DerivableList interface {
	Len() int
	GetEncoded(index int) []byte
}

DerivableList can be add to a trie for hashing, getting a proof to verify the list

type Genesis

type Genesis struct {
	ChainID   ChainID
	Time      int64
	Extra     string
	Consensus struct {
		Tetris struct {
			Validators []string
		}
	}
	InitYeeDist []InitYeeDist
	// block header hash generated with info above
	Hash string
}

func LoadGenesis

func LoadGenesis(id ChainID) (*Genesis, error)

func NewGenesis

func NewGenesis(chainID ChainID, initDist map[string]*big.Int, validators []string) (*Genesis, error)

func (*Genesis) Commit

func (g *Genesis) Commit(stateDB state.Database, putter persistent.Putter) (*Block, error)

commit genesis to stateDB, assuming service not started, no lock required

type INode

type INode interface {
	NodeID() string
	AccountManager() *accounts.AccountManager
	Core() *Core
	P2pService() p2p.Service
}

type InitYeeDist

type InitYeeDist struct {
	Address, Value string
}

type Receipt

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

type Receipts

type Receipts []*Receipt

func (Receipts) GetEncoded

func (rs Receipts) GetEncoded(index int) []byte

func (Receipts) Len

func (rs Receipts) Len() int

type Transaction

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

func NewTransaction

func NewTransaction(chainID uint32, nonce uint64, recipient *common.Address, amount *big.Int) *Transaction

func NewTransactionFromProto

func NewTransactionFromProto(msg proto.Message) (*Transaction, error)

func (*Transaction) Amount

func (t *Transaction) Amount() *big.Int

func (*Transaction) ChainID

func (t *Transaction) ChainID() uint32

func (*Transaction) Decode

func (t *Transaction) Decode(enc []byte) error

func (*Transaction) Encode

func (t *Transaction) Encode() ([]byte, error)

func (*Transaction) From

func (t *Transaction) From() *common.Address

func (*Transaction) FromProto

func (t *Transaction) FromProto(msg proto.Message) error

func (*Transaction) Hash

func (t *Transaction) Hash() *common.Hash

func (*Transaction) Nonce

func (t *Transaction) Nonce() uint64

func (*Transaction) Recipient

func (t *Transaction) Recipient() *common.Address

func (*Transaction) Sign

func (t *Transaction) Sign(signer crypto.Signer) error

func (*Transaction) String

func (t *Transaction) String() string

func (*Transaction) To

func (t *Transaction) To() *common.Address

func (*Transaction) ToProto

func (t *Transaction) ToProto() (*corepb.Transaction, error)

func (*Transaction) VerifySig

func (t *Transaction) VerifySig() error

type TransactionPool

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

func NewTransactionPool

func NewTransactionPool(core *Core) (*TransactionPool, error)

func (*TransactionPool) Start

func (tp *TransactionPool) Start()

func (*TransactionPool) Stop

func (tp *TransactionPool) Stop()

func (*TransactionPool) TxBroadcast

func (tp *TransactionPool) TxBroadcast(tx *Transaction) error

type Transactions

type Transactions []*Transaction

func (Transactions) GetEncoded

func (txs Transactions) GetEncoded(index int) []byte

func (Transactions) Len

func (txs Transactions) Len() int

func (Transactions) String

func (txs Transactions) String() string

func (Transactions) Write

func (txs Transactions) Write(putter persistent.Putter) error

Directories

Path Synopsis
Package state implements block chain state trie
Package state implements block chain state trie

Jump to

Keyboard shortcuts

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