core

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2017 License: LGPL-3.0 Imports: 15 Imported by: 0

README

core

Core blockchain data structures, logical codes are all located in this directory.

Documentation

Index

Constants

View Source
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
)
View Source
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
)
View Source
const (
	// TestNetID chain id for test net.
	TestNetID = 1

	// EagleNebula chain id for 1.x
	EagleNebula = 1 << 4
)

Variables

View Source
var (
	// ErrInvalidAddress invalid address error.
	ErrInvalidAddress = errors.New("address: invalid address")

	// ErrInvalidAddressDataLength invalid data length error.
	ErrInvalidAddressDataLength = errors.New("address: invalid address data length")
)
View Source
var (
	ErrInvalidBlockHash      = errors.New("invalid block hash")
	ErrInvalidBlockStateRoot = errors.New("invalid block state root hash")
)

Errors in block

View Source
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

func NewAddress(s []byte) (*Address, error)

NewAddress create new #Address according to data bytes.

func NewAddressFromPublicKey

func NewAddressFromPublicKey(s []byte) (*Address, error)

NewAddressFromPublicKey return new address from publickey bytes

func Parse

func Parse(s string) (*Address, error)

Parse parse address string.

func ParseFromBytes

func ParseFromBytes(s []byte) (*Address, error)

ParseFromBytes parse address from bytes.

func (*Address) ToHex

func (a *Address) ToHex() string

ToHex convert address to hex

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

func NewGenesisBlock(stateTrie *trie.Trie) *Block

NewGenesisBlock create genesis @Block from file.

func (*Block) AddTransactions

func (block *Block) AddTransactions(txs ...*Transaction) *Block

AddTransactions add transactions to block.

func (*Block) Deserialize

func (block *Block) Deserialize(blob []byte) error

Deserialize a block

func (*Block) Hash

func (block *Block) Hash() Hash

Hash return block hash.

func (*Block) Height

func (block *Block) Height() uint64

Height return height from genesis block.

func (*Block) LinkParentBlock

func (block *Block) LinkParentBlock(parentBlock *Block) bool

LinkParentBlock link parent block, return true if hash is the same; false otherwise.

func (*Block) Nonce

func (block *Block) Nonce() uint64

Nonce return nonce.

func (*Block) ParentBlock

func (block *Block) ParentBlock() *Block

ParentBlock return parent block.

func (*Block) ParentHash

func (block *Block) ParentHash() Hash

ParentHash return parent hash.

func (*Block) Seal

func (block *Block) Seal()

Seal seal block, calculate stateRoot and block hash.

func (*Block) Sealed

func (block *Block) Sealed() bool

Sealed return true if block seals. Otherwise return false.

func (*Block) Serialize

func (block *Block) Serialize() ([]byte, error)

Serialize Block to bytes

func (*Block) SetNonce

func (block *Block) SetNonce(nonce uint64)

SetNonce set nonce.

func (*Block) SetTimestamp

func (block *Block) SetTimestamp(timestamp time.Time)

SetTimestamp set timestamp

func (*Block) StateRoot

func (block *Block) StateRoot() Hash

StateRoot return state root hash.

func (*Block) String

func (block *Block) String() string

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

func (block *Block) VerifyStateRoot() error

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) ChainID

func (bc *BlockChain) ChainID() int

ChainID return the chainID.

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) Dump

func (bc *BlockChain) Dump() string

Dump dump full chain.

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 NewBlockPool

func NewBlockPool() *BlockPool

NewBlockPool return new #BlockPool instance.

func (*BlockPool) AddLocalBlock

func (pool *BlockPool) AddLocalBlock(block *Block)

AddLocalBlock add local minted block.

func (*BlockPool) ReceivedBlockCh

func (pool *BlockPool) ReceivedBlockCh() chan *Block

ReceivedBlockCh return received block chan.

func (*BlockPool) RegisterInNetwork

func (pool *BlockPool) RegisterInNetwork(nm net.Manager)

RegisterInNetwork register message subscriber in network.

func (*BlockPool) Start

func (pool *BlockPool) Start()

Start start loop.

func (*BlockPool) Stop

func (pool *BlockPool) Stop()

Stop stop loop.

type Consensus

type Consensus interface {
	VerifyBlock(*Block) error
}

Consensus interface

type Hash

type Hash []byte

Hash by Sha3-256

func HashBlock

func HashBlock(block *Block) Hash

HashBlock return the hash of block.

func HashBlockStateRoot

func HashBlockStateRoot(block *Block) Hash

HashBlockStateRoot return the hash of state trie of block.

func HashTransaction

func HashTransaction(tx *Transaction) Hash

HashTransaction hash the transaction.

func (Hash) Equals

func (h Hash) Equals(b Hash) bool

Equals compare two Hash. True is equal, otherwise false.

func (Hash) Hex

func (h Hash) Hex() HexHash

Hex return hex encoded hash.

func (Hash) String

func (h Hash) String() string

type HexHash

type HexHash string

HexHash is the hex string of a hash

func (HexHash) Hash

func (hh HexHash) Hash() Hash

Hash return hex decoded hash.

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) Hash

func (tx *Transaction) Hash() Hash

Hash return the hash of transaction.

func (*Transaction) Serialize

func (tx *Transaction) Serialize() ([]byte, error)

Serialize a transaction

func (*Transaction) Sign

func (tx *Transaction) Sign() error

Sign sign transaction.

func (*Transaction) Verify

func (tx *Transaction) Verify() error

Verify return transaction verify result, including Hash and Signature.

func (*Transaction) VerifySign

func (tx *Transaction) VerifySign() (bool, error)

VerifySign tx

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

func (*Transactions) Serialize

func (txs *Transactions) Serialize() ([]byte, error)

Serialize txs

Jump to

Keyboard shortcuts

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