8 "github.com/vapor/common"
9 "github.com/vapor/consensus"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
14 // approxNodesPerDay is an approximation of the number of new blocks there are
15 // in a day on average.
16 const approxNodesPerDay = 24 * 24
18 // BlockNode represents a block within the block chain and is primarily used to
19 // aid in selecting the best chain to be the main chain.
20 type BlockNode struct {
21 Parent *BlockNode // parent is the parent block for this node.
22 Hash bc.Hash // hash of the block.
27 TransactionsMerkleRoot bc.Hash
28 TransactionStatusHash bc.Hash
31 func NewBlockNode(bh *types.BlockHeader, parent *BlockNode) (*BlockNode, error) {
32 if bh.Height != 0 && parent == nil {
33 return nil, errors.New("parent node can not be nil")
41 Timestamp: bh.Timestamp,
42 TransactionsMerkleRoot: bh.TransactionsMerkleRoot,
43 TransactionStatusHash: bh.TransactionStatusHash,
48 // blockHeader convert a node to the header struct
49 func (node *BlockNode) BlockHeader() *types.BlockHeader {
50 previousBlockHash := bc.Hash{}
51 if node.Parent != nil {
52 previousBlockHash = node.Parent.Hash
54 return &types.BlockHeader{
55 Version: node.Version,
57 PreviousBlockHash: previousBlockHash,
58 Timestamp: node.Timestamp,
59 BlockCommitment: types.BlockCommitment{
60 TransactionsMerkleRoot: node.TransactionsMerkleRoot,
61 TransactionStatusHash: node.TransactionStatusHash,
66 func (node *BlockNode) CalcPastMedianTime() uint64 {
67 timestamps := []uint64{}
69 for i := 0; i < consensus.MedianTimeBlocks && iterNode != nil; i++ {
70 timestamps = append(timestamps, iterNode.Timestamp)
71 iterNode = iterNode.Parent
74 sort.Sort(common.TimeSorter(timestamps))
75 return timestamps[len(timestamps)/2]
78 // BlockIndex is the struct for help chain trace block chain as tree
79 type BlockIndex struct {
82 index map[bc.Hash]*BlockNode
83 mainChain []*BlockNode
86 // NewBlockIndex will create a empty BlockIndex
87 func NewBlockIndex() *BlockIndex {
89 index: make(map[bc.Hash]*BlockNode),
90 mainChain: make([]*BlockNode, 0, approxNodesPerDay),
94 // AddNode will add node to the index map
95 func (bi *BlockIndex) AddNode(node *BlockNode) {
97 bi.index[node.Hash] = node
101 // GetNode will search node from the index map
102 func (bi *BlockIndex) GetNode(hash *bc.Hash) *BlockNode {
105 return bi.index[*hash]
108 func (bi *BlockIndex) BestNode() *BlockNode {
111 return bi.mainChain[len(bi.mainChain)-1]
114 // BlockExist check does the block existed in blockIndex
115 func (bi *BlockIndex) BlockExist(hash *bc.Hash) bool {
117 _, ok := bi.index[*hash]
122 // TODO: THIS FUNCTION MIGHT BE DELETED
123 func (bi *BlockIndex) InMainchain(hash bc.Hash) bool {
127 node, ok := bi.index[hash]
131 return bi.nodeByHeight(node.Height) == node
134 func (bi *BlockIndex) nodeByHeight(height uint64) *BlockNode {
135 if height >= uint64(len(bi.mainChain)) {
138 return bi.mainChain[height]
141 // NodeByHeight returns the block node at the specified height.
142 func (bi *BlockIndex) NodeByHeight(height uint64) *BlockNode {
145 return bi.nodeByHeight(height)
148 // SetMainChain will set the the mainChain array
149 func (bi *BlockIndex) SetMainChain(node *BlockNode) {
153 needed := node.Height + 1
154 if uint64(cap(bi.mainChain)) < needed {
155 nodes := make([]*BlockNode, needed, needed+approxNodesPerDay)
156 copy(nodes, bi.mainChain)
159 i := uint64(len(bi.mainChain))
160 bi.mainChain = bi.mainChain[0:needed]
161 for ; i < needed; i++ {
162 bi.mainChain[i] = nil
166 for node != nil && bi.mainChain[node.Height] != node {
167 bi.mainChain[node.Height] = node