aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-02-21 20:05:59 +0800
committerobscuren <geffobscura@gmail.com>2014-02-21 20:05:59 +0800
commitcca8585554119a4dc02c6720948012bf876a1db8 (patch)
treec5c71264f63136cc08179c614a5d0b72216580e6
parent18cc35338afc8a3843716af0d96bd03d36e735ea (diff)
downloaddexon-cca8585554119a4dc02c6720948012bf876a1db8.tar.gz
dexon-cca8585554119a4dc02c6720948012bf876a1db8.tar.zst
dexon-cca8585554119a4dc02c6720948012bf876a1db8.zip
Get a chain of blocks made simple
-rw-r--r--ethchain/block_chain.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go
index 5b55782a9..21f540b96 100644
--- a/ethchain/block_chain.go
+++ b/ethchain/block_chain.go
@@ -2,6 +2,7 @@ package ethchain
import (
"bytes"
+ "fmt"
"github.com/ethereum/eth-go/ethutil"
"log"
"math"
@@ -111,6 +112,25 @@ func (bc *BlockChain) GetChainFromHash(hash []byte, max uint64) []interface{} {
return chain
}
+func (bc *BlockChain) GetChain(hash []byte, amount int) []*Block {
+ genHash := bc.genesisBlock.Hash()
+
+ block := bc.GetBlock(hash)
+ var blocks []*Block
+
+ for i := 0; i < amount && block != nil; block = bc.GetBlock(block.PrevHash) {
+ fmt.Println(block)
+ blocks = append([]*Block{block}, blocks...)
+
+ if bytes.Compare(genHash, block.Hash()) == 0 {
+ break
+ }
+ i++
+ }
+
+ return blocks
+}
+
func (bc *BlockChain) setLastBlock() {
data, _ := ethutil.Config.Db.Get([]byte("LastBlock"))
if len(data) != 0 {
@@ -147,6 +167,9 @@ func (bc *BlockChain) Add(block *Block) {
func (bc *BlockChain) GetBlock(hash []byte) *Block {
data, _ := ethutil.Config.Db.Get(hash)
+ if len(data) == 0 {
+ return nil
+ }
return NewBlockFromData(data)
}