transaction: fail creating tx if there is additional data present

This commit is contained in:
Evgenii Stratonikov 2020-07-03 17:52:44 +03:00
parent 27b3054df4
commit 5787d7775f
2 changed files with 24 additions and 0 deletions

View file

@ -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
}

View file

@ -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)