From 651691f3c7a726106c30d75388109b6ae02eb428 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 11:10:54 -0400 Subject: [PATCH 1/8] TX struct --- shyftdb/shyft_database_util.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 0c75308327..8c70cea88e 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -10,6 +10,10 @@ import ( "github.com/ethereum/go-ethereum/log" ) +type txEntry struct { + To +} + func WriteBlock(db *leveldb.DB, block *types.Block) error { leng := block.Transactions().Len() var tx_strs = make([]string, leng) @@ -33,6 +37,7 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { for _, tx := range transactions { + key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) if err := db.Put(key, []byte("Hello hello"), nil); err != nil { log.Crit("Failed to store TX", "err", err) From 0c4aaa89260da72dd720810b9813ae7585d5de1e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 12:11:39 -0400 Subject: [PATCH 2/8] fix getBlock function --- shyftdb/shyft_database_util.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 0c75308327..8920f4ef32 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -14,20 +14,34 @@ 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) + + hash := block.Header().Hash().Bytes() + if block.Transactions().Len() > 0 { + // this is inefficient, there are 2 loops over + // block.Transactions + // TODO: Fix this so there is only one loop + WriteTransactions(db, block.Transactions(), hash) + 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() - if err := db.Put(hash, bs, nil); err != nil { + + key := append([]byte("bk-")[:], hash[:]...) + if err := db.Put(key, bs, nil); err != nil { log.Crit("Failed to store block", "err", err) return nil // Do we want to force an exit here? } - if block.Transactions().Len() > 0 { - WriteTransactions(db, block.Transactions(), hash) - } return nil } @@ -45,13 +59,15 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH // Meant for internal tests -func GetBlock(db *leveldb.DB, block *types.Block) { +func GetBlock(db *leveldb.DB, block *types.Block) []byte { hash := block.Header().Hash().Bytes() - data, err := db.Get(hash, nil) + key := append([]byte("bk-")[:], hash[:]...) + data, err := db.Get(key, nil) if err != nil { log.Crit("Could not retrieve block", "err", err) } fmt.Println("\nBLOCK Value: " + string(data)) + return data } func GetAllTransactions(db *leveldb.DB) { From 815733e8661cc47808b052531c15f98825bf0e39 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 12:19:27 -0400 Subject: [PATCH 3/8] pre merge --- shyftdb/shyft_database_util.go | 39 ++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 8c70cea88e..cf17414ec6 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -4,14 +4,25 @@ import ( "fmt" "bytes" "encoding/gob" + "math/big" + "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/util" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/common" ) type txEntry struct { - To + TxHash common.Hash + To *common.Address + From *common.Address + BlockHash []byte + Amount *big.Int + GasPrice *big.Int + Gas uint64 + Nonce uint64 + Data []byte } func WriteBlock(db *leveldb.DB, block *types.Block) error { @@ -37,7 +48,19 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { for _, tx := range transactions { - + var from = GenerateFromAddr() + txData := txEntry{ + TxHash: tx.Hash(), + To: tx.To(), + From: from, + BlockHash: blockHash, + Amount: tx.Value(), + GasPrice: tx.GasPrice(), + Gas: tx.Gas(), + Nonce: tx.Nonce(), + Data: tx.Data(), + } + fmt.Println(txData) key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) if err := db.Put(key, []byte("Hello hello"), nil); err != nil { log.Crit("Failed to store TX", "err", err) @@ -48,6 +71,18 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH return nil } +// Helper functions + +func GenerateFromAddr(tx *types.Transaction) *common.Address { + var from *common.Address + signer := deriveSigner(tx.data.V) + if f, err := Sender(signer, tx); err != nil { // derive but don't cache + from = "[invalid sender: invalid sig]" + } else { + from = fmt.Sprintf("%x", f[:]) + } +} + // Meant for internal tests func GetBlock(db *leveldb.DB, block *types.Block) { From c697b3d097ac92ad143977eca9527696adc584bc Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 13:27:01 -0400 Subject: [PATCH 4/8] add custom tx method From() --- core/types/transaction.go | 16 +++++++++ shyftdb/shyft_database_util.go | 60 ++++++++++------------------------ 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 5660582baf..a2710b2cc7 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -195,6 +195,22 @@ func (tx *Transaction) To() *common.Address { return &to } +//@NOTE:SHYFT - Custom function to require FROM +func (tx *Transaction) From() *common.Address { + if tx.data.V != nil { + // make a best guess about the signer and use that to derive + // the sender. + signer := deriveSigner(tx.data.V) + if from, err := Sender(signer, tx); err != nil { // derive but don't cache + return nil + } else { + return &from + } + } else { + return nil + } +} + // Hash hashes the RLP encoding of tx. // It uniquely identifies the transaction. func (tx *Transaction) Hash() common.Hash { diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 5d4c94725a..a19628b4a4 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -32,16 +32,9 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() if block.Transactions().Len() > 0 { - // this is inefficient, there are 2 loops over - // block.Transactions - // TODO: Fix this so there is only one loop - WriteTransactions(db, block.Transactions(), hash) 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() + tx_strs[i] = WriteTransactions(db, tx, hash) + //tx_bytes[i] = tx.Hash().Bytes() } } @@ -60,41 +53,24 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { return nil } -func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { - for _, tx := range transactions { - var from = GenerateFromAddr() - txData := txEntry{ - TxHash: tx.Hash(), - To: tx.To(), - From: from, - BlockHash: blockHash, - Amount: tx.Value(), - GasPrice: tx.GasPrice(), - Gas: tx.Gas(), - Nonce: tx.Nonce(), - Data: tx.Data(), - } - fmt.Println(txData) - key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - if err := db.Put(key, []byte("Hello hello"), nil); err != nil { - log.Crit("Failed to store TX", "err", err) - } - GetTransaction(db, tx) +func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) string { + txData := txEntry{ + TxHash: tx.Hash(), + To: tx.To(), + From: tx.From(), + BlockHash: blockHash, + Amount: tx.Value(), + GasPrice: tx.GasPrice(), + Gas: tx.Gas(), + Nonce: tx.Nonce(), + Data: tx.Data(), } - GetAllTransactions(db) - return nil -} - -// Helper functions - -func GenerateFromAddr(tx *types.Transaction) *common.Address { - var from *common.Address - signer := deriveSigner(tx.data.V) - if f, err := Sender(signer, tx); err != nil { // derive but don't cache - from = "[invalid sender: invalid sig]" - } else { - from = fmt.Sprintf("%x", f[:]) + fmt.Println(txData) + key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) + if err := db.Put(key, []byte("Hello hello"), nil); err != nil { + log.Crit("Failed to store TX", "err", err) } + return tx.Hash().String() } // Meant for internal tests From 0f7c72f66992293ec934579f4b1be43d036ab562 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 13:47:46 -0400 Subject: [PATCH 5/8] 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 6/8] 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 { From fb55a79c0f36cb412c9f8ebb25265b1c47137b46 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 14:44:03 -0400 Subject: [PATCH 7/8] add decoder and encoder to the tx --- shyftdb/shyft_database_util.go | 52 ++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index a19628b4a4..8c36b51731 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -13,11 +13,11 @@ import ( "github.com/ethereum/go-ethereum/common" ) -type txEntry struct { +type ShyftTxEntry struct { TxHash common.Hash To *common.Address From *common.Address - BlockHash []byte + BlockHash common.Hash Amount *big.Int GasPrice *big.Int Gas uint64 @@ -33,7 +33,7 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() if block.Transactions().Len() > 0 { for i, tx := range block.Transactions() { - tx_strs[i] = WriteTransactions(db, tx, hash) + tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash()) //tx_bytes[i] = tx.Hash().Bytes() } } @@ -53,8 +53,8 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { return nil } -func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) string { - txData := txEntry{ +func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash common.Hash) string { + txData := ShyftTxEntry{ TxHash: tx.Hash(), To: tx.To(), From: tx.From(), @@ -65,11 +65,17 @@ func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) Nonce: tx.Nonce(), Data: tx.Data(), } - fmt.Println(txData) + var encodedData bytes.Buffer + encoder := gob.NewEncoder(&encodedData) + if err := encoder.Encode(txData); err != nil { + log.Crit("Faild to encode TX data", "err", err) + } key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - if err := db.Put(key, []byte("Hello hello"), nil); err != nil { + if err := db.Put(key, encodedData.Bytes(), nil); err != nil { log.Crit("Failed to store TX", "err", err) } + GetTransaction(db, tx) + GetAllTransactions(db) return tx.Hash().String() } @@ -89,7 +95,21 @@ func GetBlock(db *leveldb.DB, block *types.Block) []byte { func GetAllTransactions(db *leveldb.DB) { iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) for iter.Next() { - fmt.Println("\nALL TX VALUE: " + string(iter.Value())) + var txData ShyftTxEntry + d := gob.NewDecoder(bytes.NewBuffer(iter.Value())) + if err := d.Decode(&txData); err != nil { + log.Crit("Failed to decode tx:", "err", err) + } + fmt.Println("DECODED TX") + fmt.Println("Tx Hash: ", txData.TxHash.Hex()) + fmt.Println("From: ", txData.From.Hex()) + fmt.Println("To: ", txData.To.Hex()) + fmt.Println("BlockHash: ", txData.BlockHash.Hex()) + fmt.Println("Amount: ", txData.Amount) + fmt.Println("Gas: ", txData.Gas) + fmt.Println("GasPrice: ", txData.GasPrice) + fmt.Println("Nonce: ", txData.Nonce) + fmt.Println("Data: ", txData.Data) } iter.Release() } @@ -101,6 +121,20 @@ func GetTransaction (db *leveldb.DB, tx *types.Transaction) { log.Crit("Could not retrieve TX", "err", err) } if len(data) > 0 { - fmt.Println("\nTX Value: " + string(data)) + var txData ShyftTxEntry + d := gob.NewDecoder(bytes.NewBuffer(data)) + if err := d.Decode(&txData); err != nil { + log.Crit("Failed to decode tx:", "err", err) + } + fmt.Println("DECODED TX") + fmt.Println("Tx Hash: ", txData.TxHash.Hex()) + fmt.Println("From: ", txData.From.Hex()) + fmt.Println("To: ", txData.To.Hex()) + fmt.Println("BlockHash: ", txData.BlockHash.Hex()) + fmt.Println("Amount: ", txData.Amount) + fmt.Println("Gas: ", txData.Gas) + fmt.Println("GasPrice: ", txData.GasPrice) + fmt.Println("Nonce: ", txData.Nonce) + fmt.Println("Data: ", txData.Data) } } \ No newline at end of file From 45a2654ffd7e59dfefe07caf75cdb4f7f1bc50a3 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 14:48:28 -0400 Subject: [PATCH 8/8] remove duplicate declaration --- shyftdb/shyft_database_util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index bd338dfa5b..dfbf5c7e84 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/common" )