rpc: adjust getrawtransaction and gettransactionheight RPC call

We should return verbose transaction in case if it is in the mempool
from `getrawtransaction`. We also shouldn't return height from
`gettransactionheight` in case if transaction is in the mempool.
This commit is contained in:
Anna Shaleva 2020-11-13 16:54:38 +03:00
parent 860d2ca7a7
commit 083879838c
4 changed files with 37 additions and 39 deletions

View file

@ -6,6 +6,7 @@ import (
"errors"
"io"
"io/ioutil"
"math"
"strings"
"testing"
"time"
@ -123,7 +124,7 @@ func (e *executor) GetTransaction(t *testing.T, h util.Uint256) (*transaction.Tr
require.Eventually(t, func() bool {
var err error
tx, height, err = e.Chain.GetTransaction(h)
return err == nil && height != 0
return err == nil && height != math.MaxUint32
}, time.Second*2, time.Millisecond*100, "too long time waiting for block")
return tx, height
}

View file

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"math"
"math/big"
"sort"
"sync"
@ -942,10 +943,10 @@ func (bc *Blockchain) persist() error {
return nil
}
// GetTransaction returns a TX and its height by the given hash.
// GetTransaction returns a TX and its height by the given hash. The height is MaxUint32 if tx is in the mempool.
func (bc *Blockchain) GetTransaction(hash util.Uint256) (*transaction.Transaction, uint32, error) {
if tx, ok := bc.memPool.TryGetValue(hash); ok {
return tx, 0, nil // the height is not actually defined for memPool transaction. Not sure if zero is a good number in this case.
return tx, math.MaxUint32, nil // the height is not actually defined for memPool transaction.
}
return bc.dao.GetTransaction(hash)
}

View file

@ -23,32 +23,31 @@ type TransactionMetadata struct {
Blockhash util.Uint256 `json:"blockhash,omitempty"`
Confirmations int `json:"confirmations,omitempty"`
Timestamp uint64 `json:"blocktime,omitempty"`
VMState string `json:"vmstate"`
VMState string `json:"vmstate,omitempty"`
}
// NewTransactionOutputRaw returns a new ransactionOutputRaw object.
func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header, appExecResult *state.AppExecResult, chain blockchainer.Blockchainer) TransactionOutputRaw {
result := TransactionOutputRaw{
Transaction: *tx,
}
if header == nil {
return result
}
// confirmations formula
confirmations := int(chain.BlockHeight() - header.Base.Index + 1)
return TransactionOutputRaw{
Transaction: *tx,
TransactionMetadata: TransactionMetadata{
Blockhash: header.Hash(),
Confirmations: confirmations,
Timestamp: header.Timestamp,
VMState: appExecResult.VMState.String(),
},
result.TransactionMetadata = TransactionMetadata{
Blockhash: header.Hash(),
Confirmations: confirmations,
Timestamp: header.Timestamp,
VMState: appExecResult.VMState.String(),
}
return result
}
// MarshalJSON implements json.Marshaler interface.
func (t TransactionOutputRaw) MarshalJSON() ([]byte, error) {
output, err := json.Marshal(TransactionMetadata{
Blockhash: t.Blockhash,
Confirmations: t.Confirmations,
Timestamp: t.Timestamp,
VMState: t.VMState,
})
output, err := json.Marshal(t.TransactionMetadata)
if err != nil {
return nil, err
}
@ -76,10 +75,6 @@ func (t *TransactionOutputRaw) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
t.Blockhash = output.Blockhash
t.Confirmations = output.Confirmations
t.Timestamp = output.Timestamp
t.VMState = output.VMState
t.TransactionMetadata = *output
return json.Unmarshal(data, &t.Transaction)
}

View file

@ -846,33 +846,34 @@ func (s *Server) getStorage(ps request.Params) (interface{}, *response.Error) {
}
func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, *response.Error) {
var resultsErr *response.Error
var results interface{}
if txHash, err := reqParams.Value(0).GetUint256(); err != nil {
resultsErr = response.ErrInvalidParams
} else if tx, height, err := s.chain.GetTransaction(txHash); err != nil {
txHash, err := reqParams.Value(0).GetUint256()
if err != nil {
return nil, response.ErrInvalidParams
}
tx, height, err := s.chain.GetTransaction(txHash)
if err != nil {
err = fmt.Errorf("invalid transaction %s: %w", txHash, err)
return nil, response.NewRPCError("Unknown transaction", err.Error(), err)
} else if reqParams.Value(1).GetBoolean() {
}
if reqParams.Value(1).GetBoolean() {
if height == math.MaxUint32 {
return result.NewTransactionOutputRaw(tx, nil, nil, s.chain), nil
}
_header := s.chain.GetHeaderHash(int(height))
header, err := s.chain.GetHeader(_header)
if err != nil {
return nil, response.NewInvalidParamsError(err.Error(), err)
return nil, response.NewRPCError("Failed to get header for the transaction", err.Error(), err)
}
aers, err := s.chain.GetAppExecResults(txHash, trigger.Application)
if err != nil {
return nil, response.NewRPCError("Unknown transaction", err.Error(), err)
return nil, response.NewRPCError("Failed to get application log for the transaction", err.Error(), err)
}
if len(aers) == 0 {
return nil, response.NewRPCError("Unknown transaction", "", nil)
return nil, response.NewRPCError("Application log for the transaction is empty", "", nil)
}
results = result.NewTransactionOutputRaw(tx, header, &aers[0], s.chain)
} else {
results = tx.Bytes()
return result.NewTransactionOutputRaw(tx, header, &aers[0], s.chain), nil
}
return results, resultsErr
return tx.Bytes(), nil
}
func (s *Server) getTransactionHeight(ps request.Params) (interface{}, *response.Error) {
@ -882,7 +883,7 @@ func (s *Server) getTransactionHeight(ps request.Params) (interface{}, *response
}
_, height, err := s.chain.GetTransaction(h)
if err != nil {
if err != nil || height == math.MaxUint32 {
return nil, response.NewRPCError("unknown transaction", "", nil)
}