From 5787d7775f66f6c9edbdd09bfdb5baa96ed9b330 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 3 Jul 2020 17:52:44 +0300 Subject: [PATCH] transaction: fail creating tx if there is additional data present --- pkg/core/transaction/transaction.go | 4 ++++ pkg/core/transaction/transaction_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pkg/core/transaction/transaction.go b/pkg/core/transaction/transaction.go index 657300fe9..c4e0fe539 100644 --- a/pkg/core/transaction/transaction.go +++ b/pkg/core/transaction/transaction.go @@ -275,6 +275,10 @@ func NewTransactionFromBytes(network netmode.Magic, b []byte) (*Transaction, err if r.Err != nil { return nil, r.Err } + _ = r.ReadB() + if r.Err == nil { + return nil, errors.New("additional data after the transaction") + } tx.feePerByte = tx.NetworkFee / int64(len(b)) return tx, nil } diff --git a/pkg/core/transaction/transaction_test.go b/pkg/core/transaction/transaction_test.go index 37cd3771b..fe2d38a55 100644 --- a/pkg/core/transaction/transaction_test.go +++ b/pkg/core/transaction/transaction_test.go @@ -71,6 +71,26 @@ func TestNew(t *testing.T) { testserdes.EncodeDecodeBinary(t, tx, &Transaction{Network: netmode.UnitTestNet}) } +func TestNewTransactionFromBytes(t *testing.T) { + script := []byte{0x51} + tx := New(netmode.UnitTestNet, script, 1) + tx.NetworkFee = 123 + data, err := testserdes.EncodeBinary(tx) + require.NoError(t, err) + + // set cached fields + tx.Hash() + tx.FeePerByte() + + tx1, err := NewTransactionFromBytes(netmode.UnitTestNet, data) + require.NoError(t, err) + require.Equal(t, tx, tx1) + + data = append(data, 42) + _, err = NewTransactionFromBytes(netmode.UnitTestNet, data) + require.Error(t, err) +} + func TestEncodingTXWithNoScript(t *testing.T) { _, err := testserdes.EncodeBinary(new(Transaction)) require.Error(t, err)