add decoder and encoder to the tx

This commit is contained in:
greg 2018-04-16 14:44:03 -04:00
parent c697b3d097
commit fb55a79c0f

View file

@ -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)
}
}