write block hash to db and be able to query it

This commit is contained in:
Tim Williams 2018-04-15 18:35:27 -04:00
parent 9c547c6c78
commit d1c117c525
3 changed files with 44 additions and 4 deletions

View file

@ -20,6 +20,8 @@ package core
import (
"errors"
"fmt"
"bytes"
"encoding/gob"
"io"
"math/big"
mrand "math/rand"
@ -908,7 +910,23 @@ 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)
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()))
if err != nil {

View file

@ -452,6 +452,8 @@ func WriteBlock(db ethdb.Putter, block *types.Block) error {
fmt.Println(block.Transactions())
if block.Transactions().Len() > 0 {
for _, tx := range block.Transactions() {
fmt.Println(tx.Hash())
fmt.Println("TX HASH")
fmt.Println(tx.To().Hex())
}
}

View file

@ -2,12 +2,13 @@ package shyftdb
import (
"fmt"
"bytes"
"encoding/gob"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/core/types"
)
func GetBlock(db *leveldb.DB, block *types.Block) {
func GetBlock(db *leveldb.DB, block *types.Block) []byte {
hash := block.Header().Hash().Bytes()
bar, err := db.Get(hash, nil)
fmt.Println("Our error is ")
@ -15,12 +16,31 @@ func GetBlock(db *leveldb.DB, block *types.Block) {
fmt.Println("Our result is ")
fmt.Println(bar)
return bar
}
func WriteBlock(db *leveldb.DB, block *types.Block) error {
leng := block.Transactions().Len()
var tx_strs = make([]string, leng)
//var tx_bytes = make([]byte, leng)
if block.Transactions().Len() > 0 {
for i, tx := range block.Transactions() {
fmt.Println(tx.Hash())
fmt.Println("TX HASH")
fmt.Println(tx.To().Hex())
tx_strs[i] = tx.Hash().String()
//tx_bytes[i] = tx.Hash().Bytes()
}
}
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()
fmt.Println(hash)
err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil)
err := db.Put(hash, bs, nil)
fmt.Println("the error is: ++++++++++++++")
fmt.Println(err)
return nil