Documentation
¶
Index ¶
- Constants
- Variables
- type Address
- type Block
- func (block *Block) AddTransactions(txs ...*Transaction) *Block
- func (block *Block) Deserialize(blob []byte) error
- func (block *Block) Hash() Hash
- func (block *Block) Height() uint64
- func (block *Block) LinkParentBlock(parentBlock *Block) bool
- func (block *Block) Nonce() uint64
- func (block *Block) ParentBlock() *Block
- func (block *Block) ParentHash() Hash
- func (block *Block) Seal()
- func (block *Block) Sealed() bool
- func (block *Block) Serialize() ([]byte, error)
- func (block *Block) SetNonce(nonce uint64)
- func (block *Block) SetTimestamp(timestamp time.Time)
- func (block *Block) StateRoot() Hash
- func (block *Block) String() string
- func (block *Block) Verify(bc *BlockChain) error
- func (block *Block) VerifyHash(bc *BlockChain) error
- func (block *Block) VerifyStateRoot() error
- type BlockChain
- func (bc *BlockChain) BlockPool() *BlockPool
- func (bc *BlockChain) ChainID() int
- func (bc *BlockChain) ConsensusHandler() Consensus
- func (bc *BlockChain) DetachedTailBlocks() []*Block
- func (bc *BlockChain) Dump() string
- func (bc *BlockChain) GetBlock(hash Hash) *Block
- func (bc *BlockChain) NewBlock(coinbase *Address) *Block
- func (bc *BlockChain) NewBlockFromParent(coinbase *Address, parentBlock *Block) *Block
- func (bc *BlockChain) PutVerifiedNewBlocks(allBlocks, tailBlocks []*Block)
- func (bc *BlockChain) SetConsensusHandler(handler Consensus)
- func (bc *BlockChain) SetTailBlock(block *Block)
- func (bc *BlockChain) TailBlock() *Block
- type BlockHeader
- type BlockPool
- type Consensus
- type Hash
- type HexHash
- type Transaction
- func (tx *Transaction) Deserialize(blob []byte) error
- func (tx *Transaction) Execute(stateTrie *trie.Trie) error
- func (tx *Transaction) Hash() Hash
- func (tx *Transaction) Serialize() ([]byte, error)
- func (tx *Transaction) Sign() error
- func (tx *Transaction) Verify() error
- func (tx *Transaction) VerifySign() (bool, error)
- type TransactionPool
- type Transactions
Constants ¶
const ( // AddressDataLength the length of data of address in byte. AddressDataLength = 20 // AddressChecksumLength the checksum of address in byte. AddressChecksumLength = 4 // AddressLength the length of address in byte. AddressLength = AddressDataLength + AddressChecksumLength )
const ( // BlockHashLength define a const of the length of Hash of Block in byte. BlockHashLength = 32 // BlockReward given to coinbase // TODO: block reward should calculates dynamic. BlockReward = 16 )
const ( // TestNetID chain id for test net. TestNetID = 1 // EagleNebula chain id for 1.x EagleNebula = 1 << 4 )
Variables ¶
var ( // ErrInvalidAddress invalid address error. ErrInvalidAddress = errors.New("address: invalid address") // ErrInvalidAddressDataLength invalid data length error. ErrInvalidAddressDataLength = errors.New("address: invalid address data length") )
var ( ErrInvalidBlockHash = errors.New("invalid block hash") ErrInvalidBlockStateRoot = errors.New("invalid block state root hash") )
Errors in block
var ( // ErrInsufficientBalance insufficient balance error. ErrInsufficientBalance = errors.New("insufficient balance") // ErrInvalidSignature the signature is not sign by from address. ErrInvalidSignature = errors.New("invalid transaction signature") // ErrInvalidTransactionHash invalid hash. ErrInvalidTransactionHash = errors.New("invalid transaction hash") // ErrFromAddressLocked from address locked. ErrFromAddressLocked = errors.New("from address locked") )
Functions ¶
This section is empty.
Types ¶
type Address ¶
type Address struct {
// contains filtered or unexported fields
}
Address Similar to Bitcoin and Ethereum, Nebulas also adopts elliptic curve algorithm as its basic encryption algorithm for Nebulas accounts. A user’s private key is a randomly generated 256-bit binary number, based on which a 64-byte public key can be generated via elliptic curve multiplication. Bitcoin and Ethereum addresses are computed by public key via the deterministic Hash algorithm, and the difference between them lies in: Bitcoin address has the checksum design aiming to prevent a user from sending Bitcoins to a wrong user account accidentally due to entry of several incorrect characters; while Ethereum doesn’t have such checksum design.
We believe that checksum design is reasonable from the perspective of users, so Nebulas address also includes checksum, for which the specific calculation method is provided as follows:
Data = sha3_256(Public Key)[-20:] CheckSum = sha3_256(Data)[0:4] Address = "0x" + Hex(Data + CheckSum)
The last 20 bytes of SHA3-256 digest of a public key serve as the major component of an address, for which another SHA3-256 digest should be conducted and the first 4 bytes should be used as a checksum, which is equivalent to the practice of adding a 4-byte checksum to the end of an Ethereum address. For example:
The standard address of Alice’s Ethereum wallet is 0xdf4d22611412132d3e9bd322f82e2940674ec1bc; The final address of Nebulas Wallet should be: 0xdf4d22611412132d3e9bd322f82e2940674ec1bc03b20e40
In addition to standard address with 50 characters, we also support extended address in order to ensure the security of transfers conducted by users. The traditional bank transfer design is used for reference: In the process of a bank transfer, bank card number of the remittee should be verified, in addition to which the remitter must enter the name of the remittee. The transfer can be correctly processed only when the bank card number and the name match each other. The generating algorithm for extended address is described as follows:
Data = sha3_256(Public Key)[-20:] CheckSum = sha3_256(Data)[0:4] Address = "0x" + Hex(Data + CheckSum) ExtData = Utf8Bytes({Nickname or any string}) ExtHash = sha3_256(Data + ExtData)[0:2] ExtAddress = Address + Hex(ExtHash)
An extended address is generated through addition of 2-byte extended verification to the end of a standard address and contains a total of 54 characters. Addition of extended information allows the addition of another element verification to the Nebulas Wallet APP. For example:
The standard address of Alice’s wallet is 0xdf4d22611412132d3e9bd322f82e2940674ec1bc03b20e40, and the extended address after addition of the nickname "alice" should be 0xdf4d22611412132d3e9bd322f82e2940674ec1bc03b20e40e345. Alice tells Bob the extended address 0xdf4d22611412132d3e9bd322f82e2940674ec1bc03b20e40e345 and her nickname alice. Bob enters 0xdf4d22611412132d3e9bd322f82e2940674ec1bc03b20e40e345 and alice in the Wallet App. The Wallet App verifies the consistency between the wallet address and the nickname in order to avoid the circumstance that Bob enters the account number of another user by mistake.
func NewAddress ¶
NewAddress create new #Address according to data bytes.
func NewAddressFromPublicKey ¶
NewAddressFromPublicKey return new address from publickey bytes
func ParseFromBytes ¶
ParseFromBytes parse address from bytes.
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
Block structure
func NewBlock ¶
func NewBlock(coinbase *Address, parentHash Hash, nonce uint64, stateTrie *trie.Trie, txPool *TransactionPool) *Block
NewBlock return new block.
func NewGenesisBlock ¶
NewGenesisBlock create genesis @Block from file.
func (*Block) AddTransactions ¶
func (block *Block) AddTransactions(txs ...*Transaction) *Block
AddTransactions add transactions to block.
func (*Block) LinkParentBlock ¶
LinkParentBlock link parent block, return true if hash is the same; false otherwise.
func (*Block) ParentBlock ¶
ParentBlock return parent block.
func (*Block) Seal ¶
func (block *Block) Seal()
Seal seal block, calculate stateRoot and block hash.
func (*Block) SetTimestamp ¶
SetTimestamp set timestamp
func (*Block) Verify ¶
func (block *Block) Verify(bc *BlockChain) error
Verify return block verify result, including Hash, Nonce and StateRoot.
func (*Block) VerifyHash ¶
func (block *Block) VerifyHash(bc *BlockChain) error
VerifyHash return hash verify result.
func (*Block) VerifyStateRoot ¶
VerifyStateRoot return hash verify result.
type BlockChain ¶
type BlockChain struct {
// contains filtered or unexported fields
}
BlockChain the BlockChain core type.
func NewBlockChain ¶
func NewBlockChain(chainID int) *BlockChain
NewBlockChain create new #BlockChain instance.
func (*BlockChain) BlockPool ¶
func (bc *BlockChain) BlockPool() *BlockPool
BlockPool return block pool.
func (*BlockChain) ConsensusHandler ¶
func (bc *BlockChain) ConsensusHandler() Consensus
ConsensusHandler return consensus handler.
func (*BlockChain) DetachedTailBlocks ¶
func (bc *BlockChain) DetachedTailBlocks() []*Block
DetachedTailBlocks return detached tail blocks, used by Fork Choice algorithm.
func (*BlockChain) GetBlock ¶
func (bc *BlockChain) GetBlock(hash Hash) *Block
GetBlock return block of given hash from local storage and detachedBlocks.
func (*BlockChain) NewBlock ¶
func (bc *BlockChain) NewBlock(coinbase *Address) *Block
NewBlock create new #Block instance.
func (*BlockChain) NewBlockFromParent ¶
func (bc *BlockChain) NewBlockFromParent(coinbase *Address, parentBlock *Block) *Block
NewBlockFromParent create new block from parent block and return it.
func (*BlockChain) PutVerifiedNewBlocks ¶
func (bc *BlockChain) PutVerifiedNewBlocks(allBlocks, tailBlocks []*Block)
PutVerifiedNewBlocks put verified new blocks and tails.
func (*BlockChain) SetConsensusHandler ¶
func (bc *BlockChain) SetConsensusHandler(handler Consensus)
SetConsensusHandler set consensus handler.
func (*BlockChain) SetTailBlock ¶
func (bc *BlockChain) SetTailBlock(block *Block)
SetTailBlock set tail block.
func (*BlockChain) TailBlock ¶
func (bc *BlockChain) TailBlock() *Block
TailBlock return the tail block.
type BlockHeader ¶
type BlockHeader struct {
// contains filtered or unexported fields
}
BlockHeader of a block
func (*BlockHeader) Deserialize ¶
func (b *BlockHeader) Deserialize(blob []byte) error
Deserialize a block
func (*BlockHeader) Serialize ¶
func (b *BlockHeader) Serialize() ([]byte, error)
Serialize Block to bytes
type BlockPool ¶
type BlockPool struct {
// contains filtered or unexported fields
}
BlockPool a pool of all received blocks from network. Blocks will be sent to Consensus when it passes signature verification.
func (*BlockPool) AddLocalBlock ¶
AddLocalBlock add local minted block.
func (*BlockPool) ReceivedBlockCh ¶
ReceivedBlockCh return received block chan.
func (*BlockPool) RegisterInNetwork ¶
RegisterInNetwork register message subscriber in network.
type Hash ¶
type Hash []byte
Hash by Sha3-256
func HashBlockStateRoot ¶
HashBlockStateRoot return the hash of state trie of block.
func HashTransaction ¶
func HashTransaction(tx *Transaction) Hash
HashTransaction hash the transaction.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction type is used to handle all transaction data.
func NewTransaction ¶
func NewTransaction(from, to Address, value uint64, nonce uint64, data []byte) *Transaction
NewTransaction create #Transaction instance.
func (*Transaction) Deserialize ¶
func (tx *Transaction) Deserialize(blob []byte) error
Deserialize a transaction
func (*Transaction) Execute ¶
func (tx *Transaction) Execute(stateTrie *trie.Trie) error
Execute execute transaction, eg. transfer Nas, call smart contract.
func (*Transaction) Serialize ¶
func (tx *Transaction) Serialize() ([]byte, error)
Serialize a transaction
func (*Transaction) Verify ¶
func (tx *Transaction) Verify() error
Verify return transaction verify result, including Hash and Signature.
type TransactionPool ¶
type TransactionPool struct {
// contains filtered or unexported fields
}
TransactionPool contains a pool of transactions
func NewTransactionPool ¶
func NewTransactionPool() *TransactionPool
NewTransactionPool create a new TransactionPool
func (*TransactionPool) Put ¶
func (txPool *TransactionPool) Put(tx *Transaction)
Put put transaction to pool
type Transactions ¶
type Transactions []*Transaction
Transactions is an alias of Transaction array.
func (*Transactions) Deserialize ¶
func (txs *Transactions) Deserialize(blob []byte) error
Deserialize txs