From d1f92a585bb92f56bfc2a16a01c3e58753058ef1 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 13 Apr 2020 12:02:18 +0300 Subject: [PATCH] core: add NewTransactionFromBytes method to Transaction Added `NewTransactionFromBytes(b []byte)` method to transaction.Transaction in order to avoid code duplication. --- pkg/core/transaction/helper_test.go | 5 ++--- pkg/core/transaction/transaction.go | 11 +++++++++++ pkg/rpc/client/rpc.go | 8 +++----- pkg/rpc/server/server.go | 6 ++---- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/core/transaction/helper_test.go b/pkg/core/transaction/helper_test.go index 32f2dd743..de49b9f6f 100644 --- a/pkg/core/transaction/helper_test.go +++ b/pkg/core/transaction/helper_test.go @@ -4,7 +4,6 @@ import ( "encoding/hex" "testing" - "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/stretchr/testify/assert" ) @@ -20,7 +19,7 @@ var ( func decodeTransaction(rawTX string, t *testing.T) *Transaction { b, err1 := hex.DecodeString(rawTX) assert.Nil(t, err1) - tx := &Transaction{} - assert.NoError(t, testserdes.DecodeBinary(b, tx)) + tx, err := NewTransactionFromBytes(b) + assert.NoError(t, err) return tx } diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index f26726034..21bce78b4 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -238,6 +238,17 @@ func (t *Transaction) Bytes() []byte { return buf.Bytes() } +// NewTransactionFromBytes decodes byte array into *Transaction +func NewTransactionFromBytes(b []byte) (*Transaction, error) { + tx := &Transaction{} + r := io.NewBinReaderFromBuf(b) + tx.DecodeBinary(r) + if r.Err != nil { + return nil, r.Err + } + return tx, nil +} + // transactionJSON is a wrapper for Transaction and // used for correct marhalling of transaction.Data type transactionJSON struct { diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index bb0144582..fa9d3e9c9 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -279,11 +279,9 @@ func (c *Client) GetRawTransaction(hash util.Uint256) (*transaction.Transaction, if err != nil { return nil, err } - r := io.NewBinReaderFromBuf(txBytes) - tx := new(transaction.Transaction) - tx.DecodeBinary(r) - if r.Err != nil { - return nil, r.Err + tx, err := transaction.NewTransactionFromBytes(txBytes) + if err != nil { + return nil, err } return tx, nil } diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 3d5da1e2b..0a4c37ed2 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -931,10 +931,8 @@ func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, erro } else if byteTx, err := reqParams[0].GetBytesHex(); err != nil { return nil, response.ErrInvalidParams } else { - r := io.NewBinReaderFromBuf(byteTx) - tx := &transaction.Transaction{} - tx.DecodeBinary(r) - if r.Err != nil { + tx, err := transaction.NewTransactionFromBytes(byteTx) + if err != nil { return nil, response.ErrInvalidParams } relayReason := s.coreServer.RelayTxn(tx)