Version uint64
Height uint64
Timestamp uint64
+ BlockWitness *common.BitMap
TransactionsMerkleRoot bc.Hash
TransactionStatusHash bc.Hash
}
TransactionsMerkleRoot: bh.TransactionsMerkleRoot,
TransactionStatusHash: bh.TransactionStatusHash,
}
+
+ node.BlockWitness = common.NewBitMap(uint32(len(bh.Witness)))
+ for i, witness := range bh.Witness {
+ if len(witness) != 0 {
+ if err := node.BlockWitness.Set(uint32(i)); err != nil {
+ return nil, err
+ }
+ }
+ }
return node, nil
}
type BlockIndex struct {
sync.RWMutex
- index map[bc.Hash]*BlockNode
- mainChain []*BlockNode
+ index map[bc.Hash]*BlockNode
+ heightIndex map[uint64][]*BlockNode
+ mainChain []*BlockNode
}
// NewBlockIndex will create a empty BlockIndex
func NewBlockIndex() *BlockIndex {
return &BlockIndex{
- index: make(map[bc.Hash]*BlockNode),
- mainChain: make([]*BlockNode, 0, approxNodesPerDay),
+ index: make(map[bc.Hash]*BlockNode),
+ heightIndex: make(map[uint64][]*BlockNode),
+ mainChain: make([]*BlockNode, 0, approxNodesPerDay),
}
}
func (bi *BlockIndex) AddNode(node *BlockNode) {
bi.Lock()
bi.index[node.Hash] = node
+ bi.heightIndex[node.Height] = append(bi.heightIndex[node.Height], node)
bi.Unlock()
}
return bi.nodeByHeight(height)
}
+// NodesByHeight return all block nodes at the specified height.
+func (bi *BlockIndex) NodesByHeight(height uint64) []*BlockNode {
+ bi.RLock()
+ defer bi.RUnlock()
+ return bi.heightIndex[height]
+}
+
// SetMainChain will set the the mainChain array
func (bi *BlockIndex) SetMainChain(node *BlockNode) {
bi.Lock()