From 0f7c72f66992293ec934579f4b1be43d036ab562 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 13:47:46 -0400 Subject: [PATCH 1/2] add getalldocks fn --- core/blockchain.go | 1 + shyftdb/shyft_database_util.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index d3ff0ce2e5..fe3cdbd066 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -910,6 +910,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } + //shyftdb.GetAllBlocks(bc.blockExplorerDb) //result := shyftdb.GetBlock(bc.blockExplorerDb, block) // this is WIP for decoding bytes rather than hex strings diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 8920f4ef32..ded671da04 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -7,6 +7,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/util" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" ) @@ -58,6 +59,28 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH } // Meant for internal tests +func GetAllBlocks(db *leveldb.DB) { + 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) + if(len(strs2) > 0){ + fmt.Println("ALL TRANSACTIONS:") + fmt.Printf("%v", strs2) + fmt.Println("") + } + + //fmt.Println("\n ALL BK BK VALUE" + string(result)) + } + + iter.Release() +} func GetBlock(db *leveldb.DB, block *types.Block) []byte { hash := block.Header().Hash().Bytes() From 37f48c0f7385bf2232bc3bafdd4a3d8cbf769873 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 14:37:08 -0400 Subject: [PATCH 2/2] getallblocks now returns a data structure --- core/blockchain.go | 2 +- shyftdb/shyft_database_util.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index fe3cdbd066..2f94166fec 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -910,7 +910,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } - //shyftdb.GetAllBlocks(bc.blockExplorerDb) + //fmt.Println(shyftdb.GetAllBlocks(bc.blockExplorerDb)) //result := shyftdb.GetBlock(bc.blockExplorerDb, block) // this is WIP for decoding bytes rather than hex strings diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index ded671da04..4e6f5e0b32 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -11,6 +11,11 @@ import ( "github.com/ethereum/go-ethereum/log" ) +type SBlock struct { + hash string + txes []string +} + func WriteBlock(db *leveldb.DB, block *types.Block) error { leng := block.Transactions().Len() var tx_strs = make([]string, leng) @@ -59,27 +64,26 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH } // Meant for internal tests -func GetAllBlocks(db *leveldb.DB) { +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") + //fmt.Println("the key is") hash := common.BytesToHash(iter.Key()) hex := hash.Hex() - fmt.Println(hex) - if(len(strs2) > 0){ - fmt.Println("ALL TRANSACTIONS:") - fmt.Printf("%v", strs2) - fmt.Println("") - } + //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 {