Merge pull request #5 from ShyftNetwork/get-all-blocks-function

Get all blocks function
This commit is contained in:
greg 2018-04-16 14:47:32 -04:00 committed by GitHub
commit d898236c11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View file

@ -910,6 +910,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil {
return NonStatTy, err return NonStatTy, err
} }
//fmt.Println(shyftdb.GetAllBlocks(bc.blockExplorerDb))
//result := shyftdb.GetBlock(bc.blockExplorerDb, block) //result := shyftdb.GetBlock(bc.blockExplorerDb, block)
// this is WIP for decoding bytes rather than hex strings // this is WIP for decoding bytes rather than hex strings

View file

@ -9,10 +9,17 @@ import (
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util" "github.com/syndtr/goleveldb/leveldb/util"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
type SBlock struct {
hash string
txes []string
}
type ShyftTxEntry struct { type ShyftTxEntry struct {
TxHash common.Hash TxHash common.Hash
To *common.Address To *common.Address
@ -80,6 +87,27 @@ func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash common.H
} }
// Meant for internal tests // Meant for internal tests
func GetAllBlocks(db *leveldb.DB) []SBlock{
var arr []SBlock
iter := db.NewIterator(util.BytesPrefix([]byte("bk-")), nil)
for iter.Next() {
result := iter.Value()
buf := bytes.NewBuffer(result)
strs2 := []string{}
gob.NewDecoder(buf).Decode(&strs2)
//fmt.Println("the key is")
hash := common.BytesToHash(iter.Key())
hex := hash.Hex()
//fmt.Println(hex)
sblock := SBlock{hex, strs2}
arr = append(arr, sblock)
//fmt.Println("\n ALL BK BK VALUE" + string(result))
}
iter.Release()
return arr
}
func GetBlock(db *leveldb.DB, block *types.Block) []byte { func GetBlock(db *leveldb.DB, block *types.Block) []byte {
hash := block.Header().Hash().Bytes() hash := block.Header().Hash().Bytes()