From fb55a79c0f36cb412c9f8ebb25265b1c47137b46 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 14:44:03 -0400 Subject: [PATCH] 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