From 309c57d39e0d599ca279508a87d9c07deedac613 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 17:20:47 -0400 Subject: [PATCH] Tx succesfully stored to db --- core/blockchain.go | 1 - shyftdb/shyft_database_util.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 63ac0a2498..d3fb32e847 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -908,7 +908,6 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } - shyftdb.GetBlock(bc.blockExplorerDb, block) root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) if err != nil { diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 470531259e..c14be73f1f 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -5,6 +5,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" ) func GetBlock(db *leveldb.DB, block *types.Block) { @@ -20,8 +21,34 @@ func GetBlock(db *leveldb.DB, block *types.Block) { func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() fmt.Println(hash) - err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil) - fmt.Println("the error is: ++++++++++++++") - fmt.Println(err) + if err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil); err != nil { + log.Crit("Failed to store block", "err", err) + } + // If the block was not succesfully stored we will not store the tx data + if block.Transactions().Len() > 0 { + WriteTransactions(db, block.Transactions(), hash) + } return nil +} + +func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { + for _, tx := range transactions { + txHash := tx.Hash().Bytes() + if err := db.Put(txHash, []byte("Hello hello"), nil); err != nil { + log.Crit("Failed to store TX", "err", err) + } + GetTransaction(db, tx) + } + return nil +} + +func GetTransaction(db *leveldb.DB, tx *types.Transaction) { + data, err := db.Get(tx.Hash().Bytes(), nil) + if err != nil { + log.Crit("Could not retrieve data") + } + if len(data) > 0 { + fmt.Println("\n\t\t\t DATA \n") + fmt.Println(data) + } } \ No newline at end of file