Clean up code, add error checks for most functions

This commit is contained in:
greg 2018-04-15 21:39:38 -04:00
commit c2bf700bfb
3 changed files with 47 additions and 19 deletions

View file

@ -20,6 +20,8 @@ package core
import ( import (
"errors" "errors"
"fmt" "fmt"
//"bytes"
//"encoding/gob"
"io" "io"
"math/big" "math/big"
mrand "math/rand" mrand "math/rand"
@ -908,6 +910,23 @@ 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
} }
//result := shyftdb.GetBlock(bc.blockExplorerDb, block)
// this is WIP for decoding bytes rather than hex strings
// maybe this will be dropped
/*for i, txhash := range result {
//dst := make([]byte, hex.DecodedLen(len(txhash)))
content := hex.Dump(txhash)
fmt.Printf("%s", content)
}*/
//buf := bytes.NewBuffer(result)
//strs2 := []string{}
//gob.NewDecoder(buf).Decode(&strs2)
//fmt.Println("ALL TRANSACTIONS:")
//
//fmt.Println("the returned array is")
//fmt.Printf("%v", strs2)
root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
if err != nil { if err != nil {

View file

@ -447,6 +447,7 @@ func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) erro
// WriteBlock serializes a block into the database, header and body separately. // WriteBlock serializes a block into the database, header and body separately.
func WriteBlock(db ethdb.Putter, block *types.Block) error { func WriteBlock(db ethdb.Putter, block *types.Block) error {
// Store the body first to retain database consistency
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
return err return err
} }

View file

@ -2,7 +2,8 @@ package shyftdb
import ( import (
"fmt" "fmt"
"bytes"
"encoding/gob"
"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"
@ -10,13 +11,21 @@ import (
) )
func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteBlock(db *leveldb.DB, block *types.Block) error {
hash := block.Header().Hash().Bytes() leng := block.Transactions().Len()
fmt.Println(hash) var tx_strs = make([]string, leng)
if err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil); err != nil { //var tx_bytes = make([]byte, leng)
log.Crit("Failed to store block", "err", err) fmt.Println("The tx_strs is")
fmt.Println(tx_strs)
//strs := []string{"foo", "bar"}
buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(tx_strs)
bs := buf.Bytes()
hash := block.Header().Hash().Bytes()
if err := db.Put(hash, bs, nil); err != nil {
log.Crit("Failed to store block", "err", err)
return nil // Do we want to force an exit here?
} }
// If the block was not succesfully stored we will not store the tx data if block.Transactions().Len() > 0 {
if block.Transactions().Len() > 0 {
WriteTransactions(db, block.Transactions(), hash) WriteTransactions(db, block.Transactions(), hash)
} }
return nil return nil
@ -38,30 +47,29 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH
func GetBlock(db *leveldb.DB, block *types.Block) { func GetBlock(db *leveldb.DB, block *types.Block) {
hash := block.Header().Hash().Bytes() hash := block.Header().Hash().Bytes()
bar, err := db.Get(hash, nil) data, err := db.Get(hash, nil)
fmt.Println("Our error is ") if err != nil {
fmt.Println(err) log.Crit("Could not retrieve block", "err", err)
fmt.Println("Our result is ") }
fmt.Println(bar) fmt.Println("\nBLOCK Value: " + string(data))
} }
func GetAllTransactions(db *leveldb.DB) { func GetAllTransactions(db *leveldb.DB) {
//iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) //iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil)
iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil)
for iter.Next() { for iter.Next() {
fmt.Println(iter.Key()) fmt.Println("\nALL TX VALUE: " + string(iter.Value()))
fmt.Println(iter.Value())
} }
iter.Release() iter.Release()
} }
func GetTransaction (db *leveldb.DB, tx *types.Transaction) { func GetTransaction (db *leveldb.DB, tx *types.Transaction) {
key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...)
value, err := db.Get(key, nil) data, err := db.Get(key, nil)
if err != nil { if err != nil {
log.Crit("Could not retrieve data") log.Crit("Could not retrieve TX", "err", err)
} }
if len(value) > 0 { if len(data) > 0 {
fmt.Println(value) fmt.Println("\nTX Value: " + string(data))
} }
} }