var (
// ErrBadBlock is returned when a block is invalid.
ErrBadBlock = errors.New("invalid block")
-
// ErrBadStateRoot is returned when the computed assets merkle root
// disagrees with the one declared in a block header.
ErrBadStateRoot = errors.New("invalid state merkle root")
return c.store.GetBlock(hash)
}
-// GetBlockByHeight return a block by given height
+// GetBlockByHeight return a block header by given height
func (c *Chain) GetBlockByHeight(height uint64) (*types.Block, error) {
node := c.index.NodeByHeight(height)
if node == nil {
- return nil, errors.New("can't find block in given hight")
+ return nil, errors.New("can't find block in given height")
}
return c.store.GetBlock(&node.Hash)
}
+// GetHeaderByHash return a block header by given hash
+func (c *Chain) GetHeaderByHash(hash *bc.Hash) (*types.BlockHeader, error) {
+ node := c.index.GetNode(hash)
+ if node == nil {
+ return nil, errors.New("can't find block header in given hash")
+ }
+ return node.BlockHeader(), nil
+}
+
+// GetHeaderByHeight return a block header by given height
+func (c *Chain) GetHeaderByHeight(height uint64) (*types.BlockHeader, error) {
+ node := c.index.NodeByHeight(height)
+ if node == nil {
+ return nil, errors.New("can't find block header in given height")
+ }
+ return node.BlockHeader(), nil
+}
+
+func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode, []*state.BlockNode) {
+ var attachNodes []*state.BlockNode
+ var detachNodes []*state.BlockNode
+
+ attachNode := node
+ for c.index.NodeByHeight(attachNode.Height) != attachNode {
+ attachNodes = append([]*state.BlockNode{attachNode}, attachNodes...)
+ attachNode = attachNode.Parent
+ }
+
+ detachNode := c.bestNode
+ for detachNode != attachNode {
+ detachNodes = append(detachNodes, detachNode)
+ detachNode = detachNode.Parent
+ }
+ return attachNodes, detachNodes
+}
+
func (c *Chain) connectBlock(block *types.Block) (err error) {
bcBlock := types.MapBlock(block)
if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil {
return nil
}
-func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode, []*state.BlockNode) {
- var attachNodes []*state.BlockNode
- var detachNodes []*state.BlockNode
-
- attachIter := node
- for c.index.NodeByHeight(attachIter.Height) != attachIter {
- attachNodes = append([]*state.BlockNode{attachIter}, attachNodes...)
- attachIter = attachIter.Parent
- }
-
- detachIter := c.bestNode
- for detachIter != attachIter {
- detachNodes = append(detachNodes, detachIter)
- detachIter = detachIter.Parent
- }
- return attachNodes, detachNodes
-}
-
func (c *Chain) reorganizeChain(node *state.BlockNode) error {
attachNodes, detachNodes := c.calcReorganizeNodes(node)
utxoView := state.NewUtxoViewpoint()