blockchain

package
v0.0.0-...-7ede18c Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2019 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VM_CALL_TIMEOUT = 200 * time.Millisecond
	EMPTY_TX        = "ca4510738395af1429224dd785675309c344b2b549632e20275c69b15ed1d210"
)
View Source
const (
	// 1xx未处理
	BLOCK_TO_BE_HANDLE = 100

	// 2xx 处理成功待后续处理
	BLOCK_VALID = 201
	BLOCK_VOTED = 202

	// 3xx 错误的区块
	BLOCK_ERROR_START          = 300
	BLOCK_ERROR_PACK_TIME      = 301
	BLOCK_ERROR_BROADCAST_TIME = 302
	BLOCK_ERROR_HASH           = 303
	BLOCK_ERROR_SIGN           = 304
	BLOCK_ERROR_BODY           = 305
	BLOCK_ERROR_END            = 399

	// 400已经写入区块链
	BLOCK_SAVED = 400
)
View Source
const (
	HEADER_VERSION_PURE_MTP = 0
	HEADER_VERSION_MIXED    = 1
	HEADER_VERSION_MERKLER  = 2
)
View Source
const (
	BackboneBlockInterval = 3 * time.Second
)

Variables

This section is empty.

Functions

func Decimals

func Decimals(decimal int64) int64

Types

type Block

type Block struct {
	Header              *Header                `json:"header"`
	Hash                types.HexBytes         `json:"hash"`
	Signature           types.HexBytes         `json:"signature"`
	Miner               types.Peer             `json:"miner"`
	Transactions        userevent.Transactions `json:"-"`
	TransactionReceipts userevent.Receipts     `json:"-"`
}

func CreateBlock

func CreateBlock(last Header, time int64, peer types.Peer) *Block

func CreateGenesisBlock

func CreateGenesisBlock(accounts []types.Account) Block

func GetBlockFromBytes

func GetBlockFromBytes(data []byte) *Block

func NewBlock_V2

func NewBlock_V2(last Header, time int64, peer types.Peer) *Block

func (Block) Bytes

func (block Block) Bytes() []byte

func (*Block) CheckSubTransaction

func (block *Block) CheckSubTransaction(tx userevent.Transaction, subTxs userevent.SubTransactions) bool

func (*Block) ContractCall

func (block *Block) ContractCall(tx userevent.Transaction) *userevent.TransactionReceipt

func (*Block) DeployContract

func (block *Block) DeployContract(tx userevent.Transaction) *userevent.TransactionReceipt

func (*Block) Finish

func (block *Block) Finish()

func (Block) GetHeader

func (block Block) GetHeader() *Header

func (*Block) GetSticker

func (block *Block) GetSticker() *context.Sticker

func (Block) GetTransactions

func (block Block) GetTransactions() []userevent.Transaction

func (Block) GetTxReceipts

func (block Block) GetTxReceipts() []userevent.TransactionReceipt

func (*Block) NewTransaction

func (block *Block) NewTransaction(tx userevent.Transaction) *userevent.TransactionReceipt

func (*Block) NormalTransfer

func (block *Block) NormalTransfer(tx userevent.Transaction) *userevent.TransactionReceipt

func (*Block) SetHeader

func (block *Block) SetHeader(header *Header)

func (*Block) Sign

func (block *Block) Sign(privKey []byte) error

func (*Block) UpgradeContract

func (block *Block) UpgradeContract(tx userevent.Transaction) *userevent.TransactionReceipt

type BlockChain

type BlockChain struct {
	ChainId int64

	Pool *pool.TxPool
	// contains filtered or unexported fields
}

func NewBlockChain

func NewBlockChain(chainId int64) *BlockChain

func (*BlockChain) GetLastHeight

func (chain *BlockChain) GetLastHeight() int64

func (*BlockChain) LastHeader

func (chain *BlockChain) LastHeader() Header

func (*BlockChain) NewTransaction

func (chain *BlockChain) NewTransaction(tx *userevent.Transaction) bool

func (*BlockChain) NotifyPool

func (chain *BlockChain) NotifyPool(txs []userevent.Transaction)

当区块写入区块时,notify交易池,一些nonce比较大的交易可以进行打包

func (*BlockChain) PackTime

func (chain *BlockChain) PackTime(block *Block) time.Duration

func (*BlockChain) PackTransaction

func (chain *BlockChain) PackTransaction(clog *ctxlog.ContextLog, block *Block)

func (*BlockChain) SetLastHeader

func (chain *BlockChain) SetLastHeader(header Header)

func (*BlockChain) ValidateBlock

func (chain *BlockChain) ValidateBlock(next Block) bool

type BlockManager

type BlockManager struct {
	Blocks        *sync.Map
	BlockStatus   *sync.Map // 根据区块hash计算,主要是从peer来的区块 100:待处理 	101:已经处理成功,未写入区块 	400:错误的区块头 		200:处理成功,已经写入区块
	HeightManager *sync.Map // 根据block的height进行计算,主要是防止内部多次进行打包 100代表未打包,101代表已打包
	HeightVote    *sync.Map //上次在某个高度的投票时间,防止重复投票
}

内部操作不加lock,外部在需要加锁的地方加锁,保证操作的原子性

func NewBlockManager

func NewBlockManager() *BlockManager

func (*BlockManager) CheckHeightInterval

func (manager *BlockManager) CheckHeightInterval(height, interval int64) bool

根据区块高度判断自己是否可以对此高度进行打包 一个区块在1个interval内不可以对同一个高度的区块进行打包

func (*BlockManager) GetBlock

func (manager *BlockManager) GetBlock(hash []byte) *Block

func (*BlockManager) GetBlockStatus

func (manager *BlockManager) GetBlockStatus(hash []byte) int

获取指定区块的状态, -1表示不存在

func (*BlockManager) GetVoteTime

func (manager *BlockManager) GetVoteTime(height int64) int64

func (*BlockManager) Insert

func (manager *BlockManager) Insert(block *Block)

将指定区块插入,默认是100

func (*BlockManager) SetBlockStatus

func (manager *BlockManager) SetBlockStatus(hash []byte, status int)

func (*BlockManager) SetBlockStatusByHeight

func (manager *BlockManager) SetBlockStatusByHeight(height, ms int64)

func (*BlockManager) SetVoteTime

func (manager *BlockManager) SetVoteTime(height int64, time int64)

type BlockVoteDetail

type BlockVoteDetail struct {
	BlockchainId int64          `json:"blockchainId"`
	BlockHash    types.HexBytes `json:"blockHash"`
	BlockHeight  int64          `json:"blockHeight"`
	VoteResult   bool           `json:"voteResult"`
}
type Header struct {
	Height       int64          `json:"height"`
	Timestamp    int64          `json:"timestamp"`
	TotalFee     int64          `json:"totalFee"`
	PreviousHash types.HexBytes `json:"previousHash"`
	Coinbase     types.HexBytes `json:"miner"`
	StatTree     *MPTPlus.MTP   `json:"statRoot"`
	TokenTree    *MPTPlus.MTP   `json:"tokenRoot"`
	TxHash       types.HexBytes `json:"txHash"`
	TxRoot       *MPTPlus.MTP   `json:"txRoot"`
	ReceiptHash  types.HexBytes `json:"receiptHash"`
	ReceiptRoot  *MPTPlus.MTP   `json:"receiptRoot"`

	ChainStat  *MPTPlus.MTP `json:"chainStat"`
	ChainEvent *MPTPlus.MTP `json:"chainEvent"`

	Version int `json:"version"`
}

func FromBytes2Header

func FromBytes2Header(data []byte) *Header

func GenesisHeader

func GenesisHeader(accounts []types.Account) *Header

func NewHeader

func NewHeader(last Header, packTime int64, parentHash types.HexBytes, coinbase types.HexBytes) *Header

func NewHeader_V2

func NewHeader_V2(last Header, packTime int64, parentHash types.HexBytes, coinbase types.HexBytes) *Header

func (Header) Author

func (header Header) Author() []byte

func (*Header) Bytes

func (header *Header) Bytes() []byte

func (*Header) CalculateHash

func (header *Header) CalculateHash() []byte

func (*Header) CheckFromAndBurnGas

func (header *Header) CheckFromAndBurnGas(tx userevent.Transaction) bool

func (*Header) CheckSubTx

func (header *Header) CheckSubTx(from, to *types.Account, tx userevent.SubTransaction) bool

func (*Header) CheckTransfer

func (header *Header) CheckTransfer(tx userevent.Transaction) bool

func (Header) Equal

func (header Header) Equal(peerHeader Header) bool

func (Header) ExistAddress

func (header Header) ExistAddress(address []byte) bool

func (Header) GetAccount

func (header Header) GetAccount(address []byte) (*types.Account, error)

func (Header) GetParent

func (header Header) GetParent() []byte

func (Header) GetTimestamp

func (header Header) GetTimestamp() int64

func (*Header) HandleTx

func (header *Header) HandleTx(from, to *types.Account, tx userevent.SubTransaction) bool

func (*Header) ModifyContract

func (header *Header) ModifyContract(address, data []byte) error

func (*Header) NewSubTransaction

func (header *Header) NewSubTransaction(txs userevent.SubTransactions) bool

func (*Header) Transfer

func (header *Header) Transfer(from, to *types.Account, tx userevent.SubTransaction) bool

func (*Header) UpdateMiner

func (header *Header) UpdateMiner()

type IBlock

type IBlock interface {
	GetTransactions() []userevent.Transaction
	GetTxReceipts() []userevent.TransactionReceipt
}

type PeerBlockVote

type PeerBlockVote struct {
	Vote      BlockVoteDetail `json:"vote"`
	Peer      types.Peer      `json:"peer"`
	Signature types.HexBytes  `json:"signature"`
}

func (PeerBlockVote) Bytes

func (vote PeerBlockVote) Bytes() []byte

func (PeerBlockVote) Equal

func (vote1 PeerBlockVote) Equal(vote2 PeerBlockVote) bool

func (PeerBlockVote) Msg

func (vote PeerBlockVote) Msg() []byte

func (*PeerBlockVote) Sign

func (vote *PeerBlockVote) Sign(PrivKey []byte) error

func (PeerBlockVote) Validate

func (vote PeerBlockVote) Validate() bool

type VoteResults

type VoteResults struct {
	// contains filtered or unexported fields
}
var VoteResultManager VoteResults

func NewVoteResults

func NewVoteResults() VoteResults

func (VoteResults) Broadcasted

func (voteResults VoteResults) Broadcasted(blockHash []byte) bool

func (VoteResults) GetVoteResults

func (voteResults VoteResults) GetVoteResults(hash string) Votes

func (VoteResults) Insert

func (voteResults VoteResults) Insert(vote PeerBlockVote)

func (VoteResults) Number

func (voteResults VoteResults) Number(blockHash []byte) int

func (VoteResults) SetVoteResults

func (voteResults VoteResults) SetVoteResults(hash string, votes Votes)

type Votes

type Votes []PeerBlockVote

func (Votes) Bytes

func (vote Votes) Bytes() []byte

func (Votes) Len

func (vote Votes) Len() int

func (Votes) Less

func (vote Votes) Less(i, j int) bool

func (Votes) Swap

func (vote Votes) Swap(i, j int)

func (Votes) Validate

func (votes Votes) Validate() bool

Jump to

Keyboard shortcuts

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