Store and retrieve tx context metadata #608
Improving this in the future will allow for cleaning up a bit of legacy code.
This commit is contained in:
parent
7e3875b527
commit
40ea466200
|
@ -233,8 +233,9 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
|
|||
sm.txpool.RemoveSet(block.Transactions())
|
||||
}
|
||||
|
||||
for _, tx := range block.Transactions() {
|
||||
putTx(sm.extraDb, tx)
|
||||
// This puts transactions in a extra db for rpc
|
||||
for i, tx := range block.Transactions() {
|
||||
putTx(sm.extraDb, tx, block, i)
|
||||
}
|
||||
|
||||
if uncle {
|
||||
|
@ -358,11 +359,32 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
|
|||
return state.Logs(), nil
|
||||
}
|
||||
|
||||
func putTx(db common.Database, tx *types.Transaction) {
|
||||
func putTx(db common.Database, tx *types.Transaction, block *types.Block, i int) {
|
||||
rlpEnc, err := rlp.EncodeToBytes(tx)
|
||||
if err != nil {
|
||||
statelogger.Infoln("Failed encoding tx", err)
|
||||
return
|
||||
}
|
||||
db.Put(tx.Hash().Bytes(), rlpEnc)
|
||||
|
||||
rlpEnc, err = rlp.EncodeToBytes(block.Hash().Bytes())
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -199,9 +199,13 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
|
|||
args := new(HashIndexArgs)
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
}
|
||||
tx := api.xeth().EthTransactionByHash(args.Hash)
|
||||
tx, bhash, bnum, txi := api.xeth().EthTransactionByHash(args.Hash)
|
||||
if tx != nil {
|
||||
*reply = NewTransactionRes(tx)
|
||||
v := NewTransactionRes(tx)
|
||||
v.BlockHash = newHexData(bhash)
|
||||
v.BlockNumber = newHexNum(bnum)
|
||||
v.TxIndex = newHexNum(txi)
|
||||
*reply = v
|
||||
}
|
||||
case "eth_getTransactionByBlockHashAndIndex":
|
||||
args := new(HashIndexArgs)
|
||||
|
|
23
xeth/xeth.go
23
xeth/xeth.go
|
@ -185,12 +185,29 @@ func (self *XEth) EthBlockByHash(strHash string) *types.Block {
|
|||
return block
|
||||
}
|
||||
|
||||
func (self *XEth) EthTransactionByHash(hash string) *types.Transaction {
|
||||
func (self *XEth) EthTransactionByHash(hash string) (tx *types.Transaction, blhash common.Hash, blnum *big.Int, txi uint64) {
|
||||
data, _ := self.backend.ExtraDb().Get(common.FromHex(hash))
|
||||
if len(data) != 0 {
|
||||
return types.NewTransactionFromBytes(data)
|
||||
tx = types.NewTransactionFromBytes(data)
|
||||
}
|
||||
return nil
|
||||
|
||||
// blockhash
|
||||
data, _ = self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0001))
|
||||
if len(data) != 0 {
|
||||
blhash = common.BytesToHash(data)
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (self *XEth) BlockByNumber(num int64) *Block {
|
||||
|
|
Loading…
Reference in New Issue