Documentation ¶
Index ¶
- Constants
- Variables
- func BatchValidate(handler func(index int) error, len int) error
- func BatchValidateDebt(debts []*Debt, verifier DebtVerifier) error
- func BatchValidateTxs(txs []*Transaction) error
- func DebtArrayToMap(debts []*Debt) [][]*Debt
- func DebtMerkleRootHash(debts []*Debt) common.Hash
- func GetDebtTrie(debts []*Debt) *trie.Trie
- func GetDebtsSize(debts []*Debt) int
- func GetReceiptTrie(receipts []*Receipt) *trie.Trie
- func GetTransactionsSize(txs []*Transaction) int
- func GetTxTrie(txs []*Transaction) *trie.Trie
- func MerkleRootHash(txs []*Transaction) common.Hash
- func NewDebtMap(txs []*Transaction) [][]*Debt
- func NewTestAccount(amount *big.Int, nonce uint64, shard uint) *testAccount
- func ReceiptMerkleRootHash(receipts []*Receipt) common.Hash
- type Block
- func (block *Block) FindTransaction(txHash common.Hash) *Transaction
- func (block *Block) GetExcludeRewardTransactions() []*Transaction
- func (block *Block) GetShardNumber() uint
- func (b *Block) Hash() common.Hash
- func (b *Block) Height() uint64
- func (b *Block) ParentHash() common.Hash
- func (b *Block) Time() *big.Int
- func (block *Block) Validate() error
- func (block *Block) WithSeal(header *BlockHeader) *Block
- type BlockHeader
- type ConsensusType
- type Debt
- func (d *Debt) Fee() *big.Int
- func (d *Debt) FromAccount() common.Address
- func (d *Debt) GetHash() common.Hash
- func (d *Debt) Nonce() uint64
- func (d *Debt) Price() *big.Int
- func (d *Debt) Size() int
- func (d *Debt) ToAccount() common.Address
- func (d *Debt) Validate(verifier DebtVerifier, isPool bool, targetShard uint) (recoverable bool, retErr error)
- type DebtData
- type DebtIndex
- type DebtVerifier
- type IstanbulExtra
- type Log
- type Receipt
- type ReceiptIndex
- type SubTransaction
- func NewChallengedUserExitSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
- func NewDepositSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
- func NewEndUserExitSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
- func NewStartUserExitSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
- type SubTransactionData
- type TestVerifier
- type TestVerifierWithFunc
- type Transaction
- func NewContractTransaction(from common.Address, amount *big.Int, price *big.Int, gasLimit uint64, ...) (*Transaction, error)
- func NewMessageTransaction(from, to common.Address, amount *big.Int, price *big.Int, gasLimit uint64, ...) (*Transaction, error)
- func NewTestCrossShardTransaction() *Transaction
- func NewTestCrossShardTransactionWithNonce(nonce uint64) *Transaction
- func NewTestTransaction() *Transaction
- func NewTestTransactionWithNonce(nonce uint64) *Transaction
- func NewTestTxDetail(amount, price, nonce uint64) *Transaction
- func NewTransaction(from, to common.Address, amount *big.Int, price *big.Int, nonce uint64) (*Transaction, error)
- func (tx *Transaction) CalculateHash() common.Hash
- func (tx *Transaction) FromAccount() common.Address
- func (tx *Transaction) GetHash() common.Hash
- func (tx *Transaction) IntrinsicGas() uint64
- func (tx *Transaction) IsCrossShardTx() bool
- func (tx *Transaction) Nonce() uint64
- func (tx *Transaction) Price() *big.Int
- func (tx *Transaction) Sign(privKey *ecdsa.PrivateKey)
- func (tx *Transaction) Size() int
- func (tx *Transaction) ToAccount() common.Address
- func (tx *Transaction) Validate(statedb stateDB) error
- func (tx *Transaction) ValidateState(statedb stateDB) error
- func (tx *Transaction) ValidateWithoutState(signNeeded bool, shardNeeded bool) error
- type TransactionData
- type TxIndex
- type TxType
Constants ¶
const ( UserDeposit = iota // deposit StartUserExit // start exit EndUserExit // end exit ChallengedUserExit // challenge result )
const DebtSize = 118
DebtSize debt serialized size
const TestDebtTargetShard = 2
const TestGenesisShard = 1
const (
// TransactionPreSize is the transaction size excluding payload size
TransactionPreSize = 177
)
Variables ¶
var ( // ErrBlockHeaderNil is returned when the block header is nil. ErrBlockHeaderNil = errors.New("block header is nil") // ErrBlockHashMismatch is returned when the block hash does not match the header hash. ErrBlockHashMismatch = errors.New("block header hash mismatch") // ErrBlockTxsHashMismatch is returned when the block transactions hash does not match // the transaction root hash in the header. ErrBlockTxsHashMismatch = errors.New("block transactions root hash mismatch") // ErrBlockTxDebtHashMismatch is returned when the calculated tx debts hash of block // does not match the debts root hash in block header. ErrBlockTxDebtHashMismatch = errors.New("block transaction debts hash mismatch") // ErrBlockDebtHashMismatch is returned when the calculated debts hash of block // does not match the debts root hash in block header. ErrBlockDebtHashMismatch = errors.New("block debts hash mismatch") )
var ( // IstanbulDigest represents a hash of "Istanbul practical byzantine fault tolerance" // to identify whether the block is from Istanbul consensus engine IstanbulWitness = common.MustHexToHash("0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365").Bytes() IstanbulExtraVanity = 32 // Fixed number of extra-data bytes reserved for validator vanity IstanbulExtraSeal = 65 // Fixed number of extra-data bytes reserved for validator seal // ErrInvalidIstanbulHeaderExtra is returned if the length of extra-data is less than 32 bytes ErrInvalidIstanbulHeaderExtra = errors.New("invalid istanbul header extra-data") )
var ( // ErrAmountNegative is returned when the transaction amount is negative. ErrAmountNegative = errors.New("amount is negative") // ErrAmountNil is returned when the transaction amount is nil. ErrAmountNil = errors.New("amount is null") // ErrPriceNegative is returned when the transaction gas price is negative or zero. ErrPriceNegative = errors.New("gas price is negative or zero") // ErrPriceNil is returned when the transaction gas price is nil. ErrPriceNil = errors.New("gas price is nil") // ErrIntrinsicGas is returned if the tx gas is too low. ErrIntrinsicGas = errors.New("intrinsic gas too low") // ErrHashMismatch is returned when the transaction hash and data mismatch. ErrHashMismatch = errors.New("hash mismatch") // ErrPayloadOversized is returned when the payload size is larger than the MaxPayloadSize. ErrPayloadOversized = errors.New("oversized payload") // ErrPayloadEmpty is returned when create or call a contract without payload. ErrPayloadEmpty = errors.New("empty payload") // ErrSigInvalid is returned when the transaction signature is invalid. ErrSigInvalid = errors.New("signature is invalid") // ErrSigMissing is returned when the transaction signature is missing. ErrSigMissing = errors.New("signature missing") // MaxPayloadSize limits the payload size to prevent malicious transactions. MaxPayloadSize = defaultMaxPayloadSize // TransferAmountIntrinsicGas is the intrinsic gas to transfer amount. TransferAmountIntrinsicGas = ethIntrinsicGas(nil) CrossShardTransactionGas = TransferAmountIntrinsicGas DebtGas = 2 * TransferAmountIntrinsicGas CrossShardTotalGas = CrossShardTransactionGas + DebtGas )
var (
ErrMsgVerifierFailed = "failed to validate debt via verifier"
)
var TestGenesisAccount = NewTestAccount(new(big.Int).Mul(big.NewInt(100000), common.SeeleToFan), 0, TestGenesisShard)
genesis account with enough balance (100K seele) for benchmark test
Functions ¶
func BatchValidateDebt ¶
func BatchValidateDebt(debts []*Debt, verifier DebtVerifier) error
func BatchValidateTxs ¶
func BatchValidateTxs(txs []*Transaction) error
BatchValidateTxs validates the state independent fields of specified txs in multiple threads. Because the signature verification is time consuming (see test Benchmark_Transaction_ValidateWithoutState), once a block includes too many txs (e.g. 5000), the txs validation will consume too much time.
func DebtArrayToMap ¶
DebtArrayToMap transfer debt array to debt map
func DebtMerkleRootHash ¶
DebtMerkleRootHash calculates and returns the merkle root hash of the specified debts. If the given receipts are empty, return empty hash.
func GetDebtTrie ¶
GetDebtTrie generates a debt trie for the specified debts.
func GetReceiptTrie ¶
GetReceiptTrie generate trie according the receipts
func GetTransactionsSize ¶
func GetTransactionsSize(txs []*Transaction) int
GetTransactionsSize return the transaction size
func GetTxTrie ¶
func GetTxTrie(txs []*Transaction) *trie.Trie
GetTxTrie generate trie according the txs
func MerkleRootHash ¶
func MerkleRootHash(txs []*Transaction) common.Hash
MerkleRootHash calculates and returns the merkle root hash of the specified transactions. If the given transactions are empty, return empty hash.
func ReceiptMerkleRootHash ¶
ReceiptMerkleRootHash calculates and returns the merkle root hash of the specified receipts. If the given receipts are empty, return empty hash.
Types ¶
type Block ¶
type Block struct { HeaderHash common.Hash // HeaderHash is the hash of the RLP encoded header bytes Header *BlockHeader // Header is the block header, a block header is about 165byte Transactions []*Transaction // Transactions is the block payload Debts []*Debt // Debts for cross shard transaction }
Block represents a block in the blockchain.
func NewBlock ¶
func NewBlock(header *BlockHeader, txs []*Transaction, receipts []*Receipt, debts []*Debt) *Block
NewBlock creates a new block. The input header is copied so that any change will not affect the block. The input transaction array is copied, but each transaction is not copied. So any change of the input transaction will affect the block. The input receipt array is the same behavior with transaction array.
func NewBlockWithHeader ¶
func NewBlockWithHeader(header *BlockHeader) *Block
NewBlockWithHeader creates a block with the given header data. The header data is copied, changes to header and to the field values will not affect the block.
func (*Block) FindTransaction ¶
func (block *Block) FindTransaction(txHash common.Hash) *Transaction
FindTransaction returns the transaction of the specified hash if found. Otherwise, it returns nil.
func (*Block) GetExcludeRewardTransactions ¶
func (block *Block) GetExcludeRewardTransactions() []*Transaction
GetExcludeRewardTransactions returns all txs of a block except for the reward transaction
func (*Block) GetShardNumber ¶
GetShardNumber returns the shard number of the block, which means the shard number of the creator.
func (*Block) ParentHash ¶
func (*Block) WithSeal ¶
func (block *Block) WithSeal(header *BlockHeader) *Block
type BlockHeader ¶
type BlockHeader struct { PreviousBlockHash common.Hash // PreviousBlockHash represents the hash of the parent block Creator common.Address // Creator is the coinbase of the miner which mined the block StateHash common.Hash // StateHash is the root hash of the state trie TxHash common.Hash // TxHash is the root hash of the transaction merkle tree ReceiptHash common.Hash // ReceiptHash is the root hash of the receipt merkle tree TxDebtHash common.Hash // TxDebtHash is the root hash of the tx's debt merkle tree DebtHash common.Hash // DebtHash is the root hash of the debt merkle tree Difficulty *big.Int // Difficulty is the difficulty of the block Height uint64 // Height is the number of the block CreateTimestamp *big.Int // CreateTimestamp is the timestamp when the block is created // in pow consensus, witness is the nonce that proof whether the block is validate; // in spow consensus, witness and secondWitness are the nonce pair // in BFT consensus, witness is used to vote for validator candidates and Creator is the candidate address. Witness []byte SecondWitness []byte Consensus ConsensusType ExtraData []byte // ExtraData stores the extra info of block header. }
BlockHeader represents the header of a block in the blockchain.
func IstanbulFilteredHeader ¶
func IstanbulFilteredHeader(h *BlockHeader, keepSeal bool) *BlockHeader
IstanbulFilteredHeader returns a filtered header which some information (like seal, committed seals) are clean to fulfill the Istanbul hash rules. It returns nil if the extra-data cannot be decoded/encoded by rlp.
func (*BlockHeader) Clone ¶
func (header *BlockHeader) Clone() *BlockHeader
Clone returns a clone of the block header.
func (*BlockHeader) Hash ¶
func (header *BlockHeader) Hash() common.Hash
Hash calculates and returns the hash of the block header.
type ConsensusType ¶
type ConsensusType uint
const ( PowConsensus ConsensusType = iota IstanbulConsensus )
type Debt ¶
Debt debt class
func NewDebtWithContext ¶
func NewDebtWithContext(tx *Transaction) *Debt
NewDebtWithContext new a debt
func NewDebtWithoutContext ¶
func NewDebtWithoutContext(tx *Transaction) *Debt
NewDebtWithoutContext new debt
func NewTestDebt ¶
func NewTestDebt() *Debt
func NewTestDebtDetail ¶
func (*Debt) FromAccount ¶
type DebtData ¶
type DebtData struct { TxHash common.Hash // the hash of the executed transaction From common.Address Nonce uint64 Account common.Address // debt for account Amount *big.Int // debt amount Price *big.Int // debt price Code common.Bytes // debt contract code }
DebtData debt data
type DebtVerifier ¶
type DebtVerifier interface { // ValidateDebt validate debt // returns packed whether debt is packed // returns confirmed whether debt is confirmed // returns retErr error info ValidateDebt(debt *Debt) (packed bool, confirmed bool, err error) // IfDebtPacked // returns packed whether debt is packed // returns confirmed whether debt is confirmed // returns retErr error info IfDebtPacked(debt *Debt) (packed bool, confirmed bool, err error) }
DebtVerifier interface
type IstanbulExtra ¶
func ExtractIstanbulExtra ¶
func ExtractIstanbulExtra(h *BlockHeader) (*IstanbulExtra, error)
ExtractIstanbulExtra extracts all values of the IstanbulExtra from the header. It returns an error if the length of the given extra-data is less than 32 bytes or the extra-data can not be decoded.
type Log ¶
type Log struct { // Consensus fields: // address of the contract that generated the event Address common.Address // list of topics provided by the contract. Topics []common.Hash // supplied by the contract, usually ABI-encoded Data []byte // Derived fields. These fields are filled in by the node // but not secured by consensus. // block in which the transaction was included BlockNumber uint64 // index of the transaction in the block TxIndex uint }
Log represents the contract execution log.
func (*Log) MarshalJSON ¶
MarshalJSON marshal in hex string instead of base64
type Receipt ¶
type Receipt struct { Result []byte // the execution result of the tx Failed bool // indicates if execution failed UsedGas uint64 // tx used gas PostState common.Hash // the root hash of the state trie after the tx is processed. Logs []*Log // the log objects TxHash common.Hash // the hash of the executed transaction ContractAddress []byte // Used when the tx (nil To address) is to create a contract. TotalFee uint64 // the full cost of the transaction }
Receipt represents the transaction processing receipt.
type ReceiptIndex ¶
type ReceiptIndex indexInBlock
ReceiptIndex represents an index that used to query block info by tx hash.
type SubTransaction ¶
type SubTransaction struct { Hash common.Hash // SubTransaction hash of SubTransactionData Data SubTransactionData }
SubTransaction SubTransaction class
func NewChallengedUserExitSubTransaction ¶
func NewChallengedUserExitSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
func NewDepositSubTransaction ¶
func NewDepositSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
func NewEndUserExitSubTransaction ¶
func NewEndUserExitSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
func NewStartUserExitSubTransaction ¶
func NewStartUserExitSubTransaction(txHash common.Hash, args []interface{}) (*SubTransaction, error)
type SubTransactionData ¶
type SubTransactionData struct { TxHash common.Hash // the hash of the executed transaction From common.Address To common.Address Nonce uint64 Amount *big.Int Bond *big.Int }
SubTransactionData subchain transaction data
func (*SubTransactionData) Hash ¶
func (data *SubTransactionData) Hash() common.Hash
type TestVerifier ¶
type TestVerifier struct {
// contains filtered or unexported fields
}
func NewTestVerifier ¶
func NewTestVerifier(p bool, c bool, err error) *TestVerifier
func (*TestVerifier) IfDebtPacked ¶
func (v *TestVerifier) IfDebtPacked(debt *Debt) (packed bool, confirmed bool, err error)
func (*TestVerifier) ValidateDebt ¶
func (v *TestVerifier) ValidateDebt(debt *Debt) (packed bool, confirmed bool, err error)
type TestVerifierWithFunc ¶
type TestVerifierWithFunc struct {
// contains filtered or unexported fields
}
func NewTestVerifierWithFunc ¶
func NewTestVerifierWithFunc(fun func(debt *Debt) (bool, bool, error)) *TestVerifierWithFunc
func (*TestVerifierWithFunc) IfDebtPacked ¶
func (v *TestVerifierWithFunc) IfDebtPacked(debt *Debt) (packed bool, confirmed bool, err error)
func (*TestVerifierWithFunc) ValidateDebt ¶
func (v *TestVerifierWithFunc) ValidateDebt(debt *Debt) (packed bool, confirmed bool, err error)
type Transaction ¶
type Transaction struct { Hash common.Hash // Hash is the hash of the transaction data Data TransactionData // Data is the transaction data Signature crypto.Signature // Signature is the signature of the transaction }
Transaction represents a transaction in the blockchain.
func NewContractTransaction ¶
func NewContractTransaction(from common.Address, amount *big.Int, price *big.Int, gasLimit uint64, nonce uint64, code []byte) (*Transaction, error)
NewContractTransaction returns a transaction to create a smart contract.
func NewMessageTransaction ¶
func NewMessageTransaction(from, to common.Address, amount *big.Int, price *big.Int, gasLimit uint64, nonce uint64, msg []byte) (*Transaction, error)
NewMessageTransaction returns a transaction with the specified message.
func NewTestCrossShardTransaction ¶
func NewTestCrossShardTransaction() *Transaction
func NewTestCrossShardTransactionWithNonce ¶
func NewTestCrossShardTransactionWithNonce(nonce uint64) *Transaction
func NewTestTransaction ¶
func NewTestTransaction() *Transaction
func NewTestTransactionWithNonce ¶
func NewTestTransactionWithNonce(nonce uint64) *Transaction
func NewTestTxDetail ¶
func NewTestTxDetail(amount, price, nonce uint64) *Transaction
func NewTransaction ¶
func NewTransaction(from, to common.Address, amount *big.Int, price *big.Int, nonce uint64) (*Transaction, error)
NewTransaction creates a new transaction to transfer asset. The transaction data hash is also calculated.
func (*Transaction) CalculateHash ¶
func (tx *Transaction) CalculateHash() common.Hash
CalculateHash calculates and returns the transaction hash.
func (*Transaction) FromAccount ¶
func (tx *Transaction) FromAccount() common.Address
func (*Transaction) GetHash ¶
func (tx *Transaction) GetHash() common.Hash
func (*Transaction) IntrinsicGas ¶
func (tx *Transaction) IntrinsicGas() uint64
IntrinsicGas computes the 'intrinsic gas' for a tx.
func (*Transaction) IsCrossShardTx ¶
func (tx *Transaction) IsCrossShardTx() bool
IsCrossShardTx indicates whether the tx is to another shard.
func (*Transaction) Nonce ¶
func (tx *Transaction) Nonce() uint64
func (*Transaction) Price ¶
func (tx *Transaction) Price() *big.Int
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(privKey *ecdsa.PrivateKey)
Sign signs the transaction with the specified private key.
func (*Transaction) ToAccount ¶
func (tx *Transaction) ToAccount() common.Address
func (*Transaction) Validate ¶
func (tx *Transaction) Validate(statedb stateDB) error
Validate validates all fields in tx.
func (*Transaction) ValidateState ¶
func (tx *Transaction) ValidateState(statedb stateDB) error
ValidateState validates state dependent fields in tx.
func (*Transaction) ValidateWithoutState ¶
func (tx *Transaction) ValidateWithoutState(signNeeded bool, shardNeeded bool) error
ValidateWithoutState validates state independent fields in tx.
type TransactionData ¶
type TransactionData struct { Type TxType // Transaction type From common.Address // From is the address of the sender To common.Address // To is the receiver address, and empty address is used to create contract Amount *big.Int // Amount is the amount to be transferred AccountNonce uint64 // AccountNonce is the nonce of the sender account GasPrice *big.Int // Transaction gas price GasLimit uint64 // Maximum gas for contract creation/execution Timestamp uint64 // Timestamp is used for the miner reward transaction, referring to the block timestamp Payload common.Bytes // Payload is the extra data of the transaction }
TransactionData wraps the data in a transaction.