mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
add decoder and encoder to the tx
This commit is contained in:
parent
c697b3d097
commit
fb55a79c0f
1 changed files with 43 additions and 9 deletions
|
|
@ -13,11 +13,11 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type txEntry struct {
|
type ShyftTxEntry struct {
|
||||||
TxHash common.Hash
|
TxHash common.Hash
|
||||||
To *common.Address
|
To *common.Address
|
||||||
From *common.Address
|
From *common.Address
|
||||||
BlockHash []byte
|
BlockHash common.Hash
|
||||||
Amount *big.Int
|
Amount *big.Int
|
||||||
GasPrice *big.Int
|
GasPrice *big.Int
|
||||||
Gas uint64
|
Gas uint64
|
||||||
|
|
@ -33,7 +33,7 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error {
|
||||||
hash := block.Header().Hash().Bytes()
|
hash := block.Header().Hash().Bytes()
|
||||||
if block.Transactions().Len() > 0 {
|
if block.Transactions().Len() > 0 {
|
||||||
for i, tx := range block.Transactions() {
|
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()
|
//tx_bytes[i] = tx.Hash().Bytes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -53,8 +53,8 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) string {
|
func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash common.Hash) string {
|
||||||
txData := txEntry{
|
txData := ShyftTxEntry{
|
||||||
TxHash: tx.Hash(),
|
TxHash: tx.Hash(),
|
||||||
To: tx.To(),
|
To: tx.To(),
|
||||||
From: tx.From(),
|
From: tx.From(),
|
||||||
|
|
@ -65,11 +65,17 @@ func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte)
|
||||||
Nonce: tx.Nonce(),
|
Nonce: tx.Nonce(),
|
||||||
Data: tx.Data(),
|
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()[:]...)
|
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)
|
log.Crit("Failed to store TX", "err", err)
|
||||||
}
|
}
|
||||||
|
GetTransaction(db, tx)
|
||||||
|
GetAllTransactions(db)
|
||||||
return tx.Hash().String()
|
return tx.Hash().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +95,21 @@ func GetBlock(db *leveldb.DB, block *types.Block) []byte {
|
||||||
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)
|
||||||
for iter.Next() {
|
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()
|
iter.Release()
|
||||||
}
|
}
|
||||||
|
|
@ -101,6 +121,20 @@ func GetTransaction (db *leveldb.DB, tx *types.Transaction) {
|
||||||
log.Crit("Could not retrieve TX", "err", err)
|
log.Crit("Could not retrieve TX", "err", err)
|
||||||
}
|
}
|
||||||
if len(data) > 0 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue