add support for gRPC GetTransaction() (mined tx)

This commit is contained in:
Larry Ruane 2020-05-07 16:41:37 -06:00 committed by Larry Ruane
parent e3aca9bec7
commit d08d8980a8
2 changed files with 42 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package common
import (
"bufio"
"bytes"
"encoding/hex"
"encoding/json"
"errors"
@ -253,7 +254,7 @@ func darksideRawRequest(method string, params []json.RawMessage) (json.RawMessag
case "getrawtransaction":
// Not required for minimal reorg testing.
return nil, errors.New("not implemented yet")
return darksideGetRawTransaction(params)
case "sendrawtransaction":
var rawtx string
@ -272,3 +273,35 @@ func darksideRawRequest(method string, params []json.RawMessage) (json.RawMessag
return nil, errors.New("there was an attempt to call an unsupported RPC")
}
}
func darksideGetRawTransaction(params []json.RawMessage) (json.RawMessage, error) {
// remove the double-quotes from the beginning and end of the hex txid string
txbytes, err := hex.DecodeString(string(params[0][1 : 1+64]))
if err != nil {
return nil, errors.New("-9: " + err.Error())
}
// Linear search for the tx, somewhat inefficient but this is test code
// and there aren't many blocks. If this becomes a performance problem,
// we can maintain a map of transactions indexed by txid.
for _, b := range state.blocks {
block := parser.NewBlock()
rest, err := block.ParseFromSlice(b)
if err != nil {
// this would be strange; we've already parsed this block
return nil, errors.New("-9: " + err.Error())
}
if len(rest) != 0 {
return nil, errors.New("-9: block serialization is too long")
}
for _, tx := range block.Transactions() {
if bytes.Equal(tx.GetDisplayHash(), txbytes) {
reply := struct {
Hex string `json:"hex"`
Height int `json:"height"`
}{hex.EncodeToString(tx.Bytes()), block.GetHeight()}
return json.Marshal(reply)
}
}
}
return nil, errors.New("-5: No information available about transaction")
}

View File

@ -170,14 +170,18 @@ func (s *LwdStreamer) GetTransaction(ctx context.Context, txf *walletrpc.TxFilte
common.Log.Errorf("GetTransaction error: %s", rpcErr.Error())
return nil, errors.New((strings.Split(rpcErr.Error(), ":"))[0])
}
var txinfo interface{}
var txinfo struct {
Hex string
Height int
}
err := json.Unmarshal(result, &txinfo)
if err != nil {
return nil, err
}
txBytes := txinfo.(map[string]interface{})["hex"].(string)
txHeight := txinfo.(map[string]interface{})["height"].(float64)
return &walletrpc.RawTransaction{Data: []byte(txBytes), Height: uint64(txHeight)}, nil
return &walletrpc.RawTransaction{
Data: []byte(txinfo.Hex),
Height: uint64(txinfo.Height),
}, nil
}
if txf.Block != nil && txf.Block.Hash != nil {