mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-22 19:29:39 +00:00
core: add NewTransactionFromBytes method to Transaction
Added `NewTransactionFromBytes(b []byte)` method to transaction.Transaction in order to avoid code duplication.
This commit is contained in:
parent
5a22651e36
commit
d1f92a585b
4 changed files with 18 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue