From 86ba7432a965d9469ce1b4ccc2397ed2e08c9a3c Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 1 Apr 2015 12:14:35 +0200 Subject: [PATCH] txMeta storage as struct --- core/block_processor.go | 30 ++++++++++++------------------ xeth/xeth.go | 27 ++++++++++++++------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/core/block_processor.go b/core/block_processor.go index ceb0ef8a7c..c463bd0836 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -235,7 +235,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big // This puts transactions in a extra db for rpc for i, tx := range block.Transactions() { - putTx(sm.extraDb, tx, block, i) + putTx(sm.extraDb, tx, block, uint64(i)) } if uncle { @@ -359,7 +359,7 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro return state.Logs(), nil } -func putTx(db common.Database, tx *types.Transaction, block *types.Block, i int) { +func putTx(db common.Database, tx *types.Transaction, block *types.Block, i uint64) { rlpEnc, err := rlp.EncodeToBytes(tx) if err != nil { statelogger.Infoln("Failed encoding tx", err) @@ -367,24 +367,18 @@ func putTx(db common.Database, tx *types.Transaction, block *types.Block, i int) } db.Put(tx.Hash().Bytes(), rlpEnc) - rlpEnc, err = rlp.EncodeToBytes(block.Hash().Bytes()) + var txExtra struct { + BlockHash common.Hash + BlockIndex uint64 + Index uint64 + } + txExtra.BlockHash = block.Hash() + txExtra.BlockIndex = block.NumberU64() + txExtra.Index = i + rlpMeta, err := rlp.EncodeToBytes(txExtra) if err != nil { statelogger.Infoln("Failed encoding meta", err) return } - db.Put(append(tx.Hash().Bytes(), 0x0001), rlpEnc) - - rlpEnc, err = rlp.EncodeToBytes(block.Number().Bytes()) - if err != nil { - statelogger.Infoln("Failed encoding meta", err) - return - } - db.Put(append(tx.Hash().Bytes(), 0x0002), rlpEnc) - - rlpEnc, err = rlp.EncodeToBytes(i) - if err != nil { - statelogger.Infoln("Failed encoding meta", err) - return - } - db.Put(append(tx.Hash().Bytes(), 0x0003), rlpEnc) + db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta) } diff --git a/xeth/xeth.go b/xeth/xeth.go index fcc03b85f2..33fda9b4be 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -19,6 +19,7 @@ import ( "github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/miner" + "github.com/ethereum/go-ethereum/rlp" ) var ( @@ -191,20 +192,20 @@ func (self *XEth) EthTransactionByHash(hash string) (tx *types.Transaction, blha tx = types.NewTransactionFromBytes(data) } - // blockhash - data, _ = self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0001)) - if len(data) != 0 { - blhash = common.BytesToHash(data) + // meta + var txExtra struct { + BlockHash common.Hash + BlockIndex int64 + Index uint64 } - // blocknum - data, _ = self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0002)) - if len(data) != 0 { - blnum = common.Bytes2Big(data) - } - // txindex - data, _ = self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0003)) - if len(data) != 0 { - txi = common.BytesToNumber(data) + + v, _ := self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0001)) + r := bytes.NewReader(v) + err := rlp.Decode(r, &txExtra) + if err == nil { + blhash = txExtra.BlockHash + blnum = big.NewInt(txExtra.BlockIndex) + txi = txExtra.Index } return