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 BlockWitness *common.BitMap
28 TransactionsMerkleRoot bc.Hash
29 TransactionStatusHash bc.Hash
32 func NewBlockNode(bh *types.BlockHeader, parent *BlockNode) (*BlockNode, error) {
33 if bh.Height != 0 && parent == nil {
34 return nil, errors.New("parent node can not be nil")
42 Timestamp: bh.Timestamp,
43 TransactionsMerkleRoot: bh.TransactionsMerkleRoot,
44 TransactionStatusHash: bh.TransactionStatusHash,
47 node.BlockWitness = common.NewBitMap(uint32(len(bh.Witness)))
48 for i, witness := range bh.Witness {
49 if len(witness) != 0 {
50 node.BlockWitness.Set(uint32(i))
56 // blockHeader convert a node to the header struct
57 func (node *BlockNode) BlockHeader() *types.BlockHeader {
58 previousBlockHash := bc.Hash{}
59 if node.Parent != nil {
60 previousBlockHash = node.Parent.Hash
62 return &types.BlockHeader{
63 Version: node.Version,
65 PreviousBlockHash: previousBlockHash,
66 Timestamp: node.Timestamp,
67 BlockCommitment: types.BlockCommitment{
68 TransactionsMerkleRoot: node.TransactionsMerkleRoot,
69 TransactionStatusHash: node.TransactionStatusHash,
74 func (node *BlockNode) CalcPastMedianTime() uint64 {
75 timestamps := []uint64{}
77 for i := 0; i < consensus.MedianTimeBlocks && iterNode != nil; i++ {
78 timestamps = append(timestamps, iterNode.Timestamp)
79 iterNode = iterNode.Parent
82 sort.Sort(common.TimeSorter(timestamps))
83 return timestamps[len(timestamps)/2]
86 // BlockIndex is the struct for help chain trace block chain as tree
87 type BlockIndex struct {
90 index map[bc.Hash]*BlockNode
91 heightIndex map[uint64][]*BlockNode
92 mainChain []*BlockNode
95 // NewBlockIndex will create a empty BlockIndex
96 func NewBlockIndex() *BlockIndex {
98 index: make(map[bc.Hash]*BlockNode),
99 heightIndex: make(map[uint64][]*BlockNode),
100 mainChain: make([]*BlockNode, 0, approxNodesPerDay),
104 // AddNode will add node to the index map
105 func (bi *BlockIndex) AddNode(node *BlockNode) {
107 bi.index[node.Hash] = node
108 bi.heightIndex[node.Height] = append(bi.heightIndex[node.Height], node)
112 // GetNode will search node from the index map
113 func (bi *BlockIndex) GetNode(hash *bc.Hash) *BlockNode {
116 return bi.index[*hash]
119 func (bi *BlockIndex) BestNode() *BlockNode {
122 return bi.mainChain[len(bi.mainChain)-1]
125 // BlockExist check does the block existed in blockIndex
126 func (bi *BlockIndex) BlockExist(hash *bc.Hash) bool {
128 _, ok := bi.index[*hash]
133 // TODO: THIS FUNCTION MIGHT BE DELETED
134 func (bi *BlockIndex) InMainchain(hash bc.Hash) bool {
138 node, ok := bi.index[hash]
142 return bi.nodeByHeight(node.Height) == node
145 func (bi *BlockIndex) nodeByHeight(height uint64) *BlockNode {
146 if height >= uint64(len(bi.mainChain)) {
149 return bi.mainChain[height]
152 // NodeByHeight returns the block node at the specified height.
153 func (bi *BlockIndex) NodeByHeight(height uint64) *BlockNode {
156 return bi.nodeByHeight(height)
159 // NodesByHeight return all block nodes at the specified height.
160 func (bi *BlockIndex) NodesByHeight(height uint64) []*BlockNode {
163 return bi.heightIndex[height]
166 // SetMainChain will set the the mainChain array
167 func (bi *BlockIndex) SetMainChain(node *BlockNode) {
171 needed := node.Height + 1
172 if uint64(cap(bi.mainChain)) < needed {
173 nodes := make([]*BlockNode, needed, needed+approxNodesPerDay)
174 copy(nodes, bi.mainChain)
177 i := uint64(len(bi.mainChain))
178 bi.mainChain = bi.mainChain[0:needed]
179 for ; i < needed; i++ {
180 bi.mainChain[i] = nil
184 for node != nil && bi.mainChain[node.Height] != node {
185 bi.mainChain[node.Height] = node