2018-03-04 13:56:49 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
2020-08-18 12:09:30 +00:00
|
|
|
"encoding/base64"
|
2018-03-04 13:56:49 +00:00
|
|
|
"encoding/hex"
|
2021-01-22 15:22:09 +00:00
|
|
|
"encoding/json"
|
2020-08-19 13:39:49 +00:00
|
|
|
"math"
|
2018-03-04 13:56:49 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2024-04-27 17:16:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-05 15:53:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-07-29 16:57:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-02-15 13:40:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2021-02-05 15:53:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
func TestWitnessEncodeDecode(t *testing.T) {
|
|
|
|
verif, err := hex.DecodeString("552102486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a7021024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d2102aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e2103b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c2103b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a2102ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba5542102df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e89509357ae")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
invoc, err := hex.DecodeString("404edf5005771de04619235d5a4c7a9a11bb78e008541f1da7725f654c33380a3c87e2959a025da706d7255cb3a3fa07ebe9c6559d0d9e6213c68049168eb1056f4038a338f879930c8adc168983f60aae6f8542365d844f004976346b70fb0dd31aa1dbd4abd81e4a4aeef9941ecd4e2dd2c1a5b05e1cc74454d0403edaee6d7a4d4099d33c0b889bf6f3e6d87ab1b11140282e9a3265b0b9b918d6020b2c62d5a040c7e0c2c7c1dae3af9b19b178c71552ebd0b596e401c175067c70ea75717c8c00404e0ebd369e81093866fe29406dbf6b402c003774541799d08bf9bb0fc6070ec0f6bad908ab95f05fa64e682b485800b3c12102a8596e6c715ec76f4564d5eff34070e0521979fcd2cbbfa1456d97cc18d9b4a6ad87a97a2a0bcdedbf71b6c9676c645886056821b6f3fec8694894c66f41b762bc4e29e46ad15aee47f05d27d822")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
lenInvoc := len(invoc)
|
|
|
|
lenVerif := len(verif)
|
|
|
|
t.Log(lenInvoc)
|
|
|
|
t.Log(lenVerif)
|
|
|
|
|
|
|
|
wit := &Witness{
|
|
|
|
InvocationScript: invoc,
|
|
|
|
VerificationScript: verif,
|
|
|
|
}
|
|
|
|
|
|
|
|
witDecode := &Witness{}
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, wit, witDecode)
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
t.Log(len(witDecode.VerificationScript))
|
|
|
|
t.Log(len(witDecode.InvocationScript))
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
func TestDecodeEncodeInvocationTX(t *testing.T) {
|
2019-08-27 16:58:50 +00:00
|
|
|
tx := decodeTransaction(rawInvocationTX, t)
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2021-03-17 13:40:24 +00:00
|
|
|
script := "CwMAQNndiE0KAAwUgM7HtvW1b1BXj3N/Fi06sU1GZQ0MFN7uecGJ8wCYsLpqLrkLOpJYpsf/FMAfDAh0cmFuc2ZlcgwUz3bii9AGLEpHjuNVYQETGfPPpNJBYn1bUjk="
|
2020-08-18 12:09:30 +00:00
|
|
|
assert.Equal(t, script, base64.StdEncoding.EncodeToString(tx.Script))
|
2021-03-17 13:40:24 +00:00
|
|
|
assert.Equal(t, uint32(431760600), tx.Nonce)
|
|
|
|
assert.Equal(t, int64(11000000), tx.SystemFee)
|
|
|
|
assert.Equal(t, int64(4500000), tx.NetworkFee)
|
|
|
|
assert.Equal(t, uint32(1000), tx.ValidUntilBlock)
|
|
|
|
assert.Equal(t, "25426643feed564cd3e57f346d6c68692f5622b3063da11c5572d99ee1a5b49a", tx.Hash().StringLE())
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(tx.Signers))
|
|
|
|
assert.Equal(t, CalledByEntry, tx.Signers[0].Scopes)
|
|
|
|
assert.Equal(t, "ffc7a658923a0bb92e6abab09800f389c179eede", tx.Signers[0].Account.StringLE())
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-08-18 12:09:30 +00:00
|
|
|
assert.Equal(t, 0, len(tx.Attributes))
|
2021-03-17 13:40:24 +00:00
|
|
|
invoc1 := "DEDWn0D7z2ELqpN8ghcM/PtfFwo56/BfEasfHuSKECJMYxvU47r2ZtSihg59lGxSZzHsvxTy6nsyvJ22ycNhINdJDECl61cg937N/HujKsLMu2wJMS7C54bzJ3q22Czqllvw3Yp809USgKDs+W+3QD7rI+SFs0OhIn0gooCUU6f/13WjDEDr9XdeT5CGTO8CL0JigzcTcucs0GBcqHs8fToO6zPuuCfS7Wh6dyxSCijT4A4S+7BUdW3dsO7828ke1fj8oNxm"
|
|
|
|
verif1 := "EwwhAhA6f33QFlWFl/eWDSfFFqQ5T9loueZRVetLAT5AQEBuDCECp7xV/oaE4BGXaNEEujB5W9zIZhnoZK3SYVZyPtGFzWIMIQKzYiv0AXvf4xfFiu1fTHU/IGt9uJYEb6fXdLvEv3+NwgwhA9kMB99j5pDOd5EuEKtRrMlEtmhgI3tgjE+PgwnnHuaZFEF7zmyl"
|
|
|
|
assert.Equal(t, 1, len(tx.Scripts))
|
2021-02-05 15:53:30 +00:00
|
|
|
assert.Equal(t, invoc1, base64.StdEncoding.EncodeToString(tx.Scripts[0].InvocationScript))
|
|
|
|
assert.Equal(t, verif1, base64.StdEncoding.EncodeToString(tx.Scripts[0].VerificationScript))
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
data, err := testserdes.EncodeBinary(tx)
|
|
|
|
assert.NoError(t, err)
|
2021-03-17 13:40:24 +00:00
|
|
|
assert.Equal(t, rawInvocationTX, base64.StdEncoding.EncodeToString(data))
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
2019-08-27 16:58:50 +00:00
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
func TestNew(t *testing.T) {
|
2019-10-23 08:49:43 +00:00
|
|
|
script := []byte{0x51}
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := New(script, 1)
|
2020-07-29 16:57:38 +00:00
|
|
|
tx.Signers = []Signer{{Account: util.Uint160{1, 2, 3}}}
|
2021-02-15 13:40:42 +00:00
|
|
|
tx.Scripts = []Witness{{InvocationScript: []byte{}, VerificationScript: []byte{}}}
|
2020-06-23 14:15:35 +00:00
|
|
|
assert.Equal(t, int64(1), tx.SystemFee)
|
2020-06-05 13:07:04 +00:00
|
|
|
assert.Equal(t, script, tx.Script)
|
2019-10-23 08:49:43 +00:00
|
|
|
// Update hash fields to match tx2 that is gonna autoupdate them on decode.
|
|
|
|
_ = tx.Hash()
|
2020-09-10 16:28:16 +00:00
|
|
|
_ = tx.Size()
|
2021-03-25 16:18:01 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, tx, &Transaction{})
|
2019-10-23 08:49:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:52:44 +00:00
|
|
|
func TestNewTransactionFromBytes(t *testing.T) {
|
|
|
|
script := []byte{0x51}
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := New(script, 1)
|
2020-07-03 14:52:44 +00:00
|
|
|
tx.NetworkFee = 123
|
2020-07-29 16:57:38 +00:00
|
|
|
tx.Signers = []Signer{{Account: util.Uint160{1, 2, 3}}}
|
2021-02-15 13:40:42 +00:00
|
|
|
tx.Scripts = []Witness{{InvocationScript: []byte{}, VerificationScript: []byte{}}}
|
2020-07-03 14:52:44 +00:00
|
|
|
data, err := testserdes.EncodeBinary(tx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// set cached fields
|
|
|
|
tx.Hash()
|
|
|
|
tx.FeePerByte()
|
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
tx1, err := NewTransactionFromBytes(data)
|
2020-07-03 14:52:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tx, tx1)
|
|
|
|
|
2021-08-20 19:08:09 +00:00
|
|
|
tx2 := new(Transaction)
|
|
|
|
err = testserdes.DecodeBinary(data, tx2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tx1, tx2)
|
|
|
|
|
2020-07-03 14:52:44 +00:00
|
|
|
data = append(data, 42)
|
2021-03-25 16:18:01 +00:00
|
|
|
_, err = NewTransactionFromBytes(data)
|
2020-07-03 14:52:44 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
func TestEncodingTXWithNoScript(t *testing.T) {
|
2020-03-26 14:43:24 +00:00
|
|
|
_, err := testserdes.EncodeBinary(new(Transaction))
|
core: do not panic if transaction with empty script is being serialized
See https://github.com/nspcc-dev/neo-go/pull/2485#discussion_r872695786.
Allows to avoid panic:
```
(18:28:29)[sergio@transient:morph-control-load]% downloads/neofs-adm morph -c configuration/config.yaml init --contracts downloads/neofs-contract-v0.15.1
Stage 1: transfer GAS to alphabet nodes.
Waiting for transactions to persist...
Stage 2: set notary and alphabet nodes in designate contract.
Stage 2: already performed.
Stage 3: deploy NNS contract.
NNS contract is already deployed.
Stage 4: deploy NeoFS contracts.
Alphabet contract #0 is already deployed.
Alphabet contract #1 is already deployed.
Alphabet contract #2 is already deployed.
Alphabet contract #3 is already deployed.
audit contract is already deployed.
balance contract is already deployed.
container contract is already deployed.
neofsid contract is already deployed.
netmap contract is already deployed.
proxy contract is already deployed.
reputation contract is already deployed.
subnet contract is already deployed.
Waiting for transactions to persist...
Stage 4.1: Transfer GAS to proxy contract.
Stage 5: register candidates.
Stage 6: transfer NEO to alphabet contracts.
Stage 7: set addresses in NNS.
NNS: Set alphabet0.neofs -> 2efeb27866fa774ec6bfb43babaadcc7b28f9bb7
NNS: Set alphabet1.neofs -> 5606bc6598a538ac59ded3caa842247b9b26097a
NNS: Set alphabet2.neofs -> 2a8308cfd2706ddd5f67adfdf3954c6836806b5d
NNS: Set alphabet3.neofs -> 718d0459e6787f0f13f1e1898bf2ce6bc4bb452d
NNS: Set audit.neofs -> 54d516b36c3380efa2b2f26bda300b6c6302b8e7
NNS: Set balance.neofs -> 72b3255944524cb822788a7542c9d06cba208c0c
NNS: Set container.neofs -> 20a20f4352a49f161eccb7d5a806fd46dcab81ea
NNS: Set neofsid.neofs -> 04a3a71535689b820093cefe7ae188ed7591ad27
NNS: Set netmap.neofs -> 9a34c97b2ba6bd986fd9140a9c3d59059fa430f4
NNS: Set proxy.neofs -> 4eba34ec016f8709a511b02765a1195e31a921ea
NNS: Set reputation.neofs -> 77b5b2cf596957cc05ca3d57503ef08d72792301
NNS: Set subnet.neofs -> b0abe16bc7f56b2e7e0694ae2b36207c6aaa8636
panic: error serializing *transaction.Transaction: transaction has no script
goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/io.GetVarSize({0xbf6240, 0xc0003634a0})
github.com/nspcc-dev/neo-go@v0.98.2/pkg/io/size.go:68 +0x6fd
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).AddNetworkFee(0xc0004080c0, 0xc0003634a0, 0x0, {0xc00000e248, 0x1, 0x93})
github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/rpc.go:966 +0x6b
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).CreateTxFromScript(0x0, {0x0, 0x0, 0x0}, 0x1, 0x0, 0x0, {0xc0001c7990, 0x1, 0x1})
github.com/nspcc-dev/neo-go@v0.98.2/pkg/rpc/client/nep17.go:128 +0x345
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph.(*initializeContext).sendCommitteeTx(0xc00014a370, {0x0, 0x0, 0x0}, 0xc0016bcf30, 0x2)
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph/initialize.go:336 +0x131
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph.(*initializeContext).updateNNSGroup(0xc0001c7c88, {0x13, 0xd3, 0x6b, 0x85, 0x3e, 0x86, 0xbe, 0x77, 0xb7, ...}, ...)
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph/initialize_nns.go:89 +0x134
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph.(*initializeContext).setNNS(0xc00014a370)
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph/initialize_nns.go:74 +0x8c5
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph.initializeSideChainCmd(0x12a00e0, {0xc22a22, 0x4, 0x4})
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/morph/initialize.go:94 +0x2bc
github.com/spf13/cobra.(*Command).execute(0x12a00e0, {0xc00013a6c0, 0x4, 0x4})
github.com/spf13/cobra@v1.1.3/command.go:852 +0x60e
github.com/spf13/cobra.(*Command).ExecuteC(0x129c760)
github.com/spf13/cobra@v1.1.3/command.go:960 +0x3ad
github.com/spf13/cobra.(*Command).Execute(...)
github.com/spf13/cobra@v1.1.3/command.go:897
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules.Execute(...)
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/root.go:48
main.main()
github.com/nspcc-dev/neofs-node/cmd/neofs-adm/main.go:10 +0x25
zsh: exit 2 downloads/neofs-adm morph -c configuration/config.yaml init --contracts
```
2022-05-13 04:30:01 +00:00
|
|
|
require.NoError(t, err) // Garbage in -> garbage out.
|
2020-01-29 13:38:58 +00:00
|
|
|
}
|
2020-03-03 08:25:00 +00:00
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
func TestDecodingTXWithNoScript(t *testing.T) {
|
|
|
|
txBin, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = testserdes.DecodeBinary(txBin, new(Transaction))
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:40:42 +00:00
|
|
|
func TestDecodingTxWithInvalidWitnessesNumber(t *testing.T) {
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := New([]byte{byte(opcode.RET)}, 1)
|
2021-02-15 13:40:42 +00:00
|
|
|
tx.Signers = []Signer{{Account: util.Uint160{1, 2, 3}}}
|
|
|
|
tx.Scripts = []Witness{{InvocationScript: []byte{}, VerificationScript: []byte{}}, {InvocationScript: []byte{}, VerificationScript: []byte{}}}
|
|
|
|
data, err := testserdes.EncodeBinary(tx)
|
|
|
|
require.NoError(t, err)
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, testserdes.DecodeBinary(data, new(Transaction)), ErrInvalidWitnessNum)
|
2021-02-15 13:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 15:22:09 +00:00
|
|
|
func TestUnmarshalNeoFSTX(t *testing.T) {
|
|
|
|
txjson := []byte(`
|
|
|
|
{
|
2021-03-17 13:40:24 +00:00
|
|
|
"hash": "0xc90c77d6c17fbb959b16df820370204a534ca13826cbd9fc027beb1b55d31e5a",
|
2021-01-22 15:22:09 +00:00
|
|
|
"size": 232,
|
|
|
|
"version": 0,
|
|
|
|
"nonce": 737880259,
|
|
|
|
"sender": "NiRqSd5MtRZT5yUhgWd7oG11brkDG76Jim",
|
2021-02-09 08:16:18 +00:00
|
|
|
"sysfee": "223719420",
|
|
|
|
"netfee": "1215550",
|
2021-01-22 15:22:09 +00:00
|
|
|
"validuntilblock": 1931,
|
|
|
|
"attributes": [],
|
|
|
|
"signers": [
|
|
|
|
{
|
|
|
|
"account": "0x8f0ecd714c31c5624b6647e5fd661e5031c8f8f6",
|
|
|
|
"scopes": "Global"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"script": "DCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcIRwBESwAwEdm90ZQwUo4Hyc4fSGC6JbZtdrGb9LBbtWJtBYn1bUg==",
|
|
|
|
"witnesses": [
|
|
|
|
{
|
|
|
|
"invocation": "DEDr2gA/8T/wxQvgOZVfCdkbj6uGrprkDgJvpOJCcbl+tvlKZkZytCZEWm6NoZhJyIlEI3VQSLtU3AHuJfShAT5L",
|
|
|
|
"verification": "DCEDAS1H52IQrsc745qz0YbgpA/o2Gv6PU+r/aV7oTuI+WoLQZVEDXg="
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
tx := new(Transaction)
|
|
|
|
require.NoError(t, json.Unmarshal(txjson, tx))
|
|
|
|
}
|
|
|
|
|
2020-03-23 14:31:28 +00:00
|
|
|
func TestMarshalUnmarshalJSONInvocationTX(t *testing.T) {
|
|
|
|
tx := &Transaction{
|
2020-06-05 13:07:04 +00:00
|
|
|
Version: 0,
|
2020-07-29 16:57:38 +00:00
|
|
|
Signers: []Signer{{Account: util.Uint160{1, 2, 3}}},
|
2020-06-05 13:07:04 +00:00
|
|
|
Script: []byte{1, 2, 3, 4},
|
2020-09-16 10:39:53 +00:00
|
|
|
Attributes: []Attribute{{Type: HighPriority}},
|
2020-06-05 14:26:11 +00:00
|
|
|
Scripts: []Witness{},
|
2020-12-01 08:40:58 +00:00
|
|
|
SystemFee: int64(fixedn.Fixed8FromFloat(123.45)),
|
|
|
|
NetworkFee: int64(fixedn.Fixed8FromFloat(0.123)),
|
2020-06-05 14:26:11 +00:00
|
|
|
Trimmed: false,
|
2020-03-23 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testserdes.MarshalUnmarshalJSON(t, tx, new(Transaction))
|
|
|
|
}
|
2020-08-19 13:20:48 +00:00
|
|
|
|
|
|
|
func TestTransaction_HasAttribute(t *testing.T) {
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := New([]byte{1}, 0)
|
2020-08-19 13:20:48 +00:00
|
|
|
require.False(t, tx.HasAttribute(HighPriority))
|
|
|
|
tx.Attributes = append(tx.Attributes, Attribute{Type: HighPriority})
|
|
|
|
require.True(t, tx.HasAttribute(HighPriority))
|
|
|
|
tx.Attributes = append(tx.Attributes, Attribute{Type: HighPriority})
|
|
|
|
require.True(t, tx.HasAttribute(HighPriority))
|
|
|
|
}
|
2020-08-19 13:39:49 +00:00
|
|
|
|
|
|
|
func TestTransaction_isValid(t *testing.T) {
|
|
|
|
newTx := func() *Transaction {
|
|
|
|
return &Transaction{
|
|
|
|
Version: 0,
|
|
|
|
SystemFee: 100,
|
|
|
|
NetworkFee: 100,
|
|
|
|
Signers: []Signer{
|
|
|
|
{Account: util.Uint160{1, 2, 3}},
|
|
|
|
{
|
|
|
|
Account: util.Uint160{4, 5, 6},
|
|
|
|
Scopes: Global,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Script: []byte{1, 2, 3, 4},
|
|
|
|
Attributes: []Attribute{},
|
|
|
|
Scripts: []Witness{},
|
|
|
|
Trimmed: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
|
|
t.Run("NoAttributes", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
require.NoError(t, tx.isValid())
|
|
|
|
})
|
|
|
|
t.Run("HighPriority", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Attributes = []Attribute{{Type: HighPriority}}
|
|
|
|
require.NoError(t, tx.isValid())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("InvalidVersion", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Version = 1
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrInvalidVersion)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("NegativeSystemFee", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.SystemFee = -1
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrNegativeSystemFee)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("NegativeNetworkFee", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.NetworkFee = -1
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrNegativeNetworkFee)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("TooBigFees", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.SystemFee = math.MaxInt64 - tx.NetworkFee + 1
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrTooBigFees)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("EmptySigners", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Signers = tx.Signers[:0]
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrEmptySigners)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("NonUniqueSigners", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Signers[1].Account = tx.Signers[0].Account
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrNonUniqueSigners)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("MultipleHighPriority", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Attributes = []Attribute{
|
|
|
|
{Type: HighPriority},
|
|
|
|
{Type: HighPriority},
|
|
|
|
}
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrInvalidAttribute)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
2020-09-16 11:50:31 +00:00
|
|
|
t.Run("MultipleOracle", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Attributes = []Attribute{
|
|
|
|
{Type: OracleResponseT},
|
|
|
|
{Type: OracleResponseT},
|
|
|
|
}
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrInvalidAttribute)
|
2020-09-16 11:50:31 +00:00
|
|
|
})
|
2020-08-19 13:39:49 +00:00
|
|
|
t.Run("NoScript", func(t *testing.T) {
|
|
|
|
tx := newTx()
|
|
|
|
tx.Script = []byte{}
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, tx.isValid(), ErrEmptyScript)
|
2020-08-19 13:39:49 +00:00
|
|
|
})
|
|
|
|
}
|
2020-10-20 15:39:01 +00:00
|
|
|
|
|
|
|
func TestTransaction_GetAttributes(t *testing.T) {
|
|
|
|
attributesTypes := []AttrType{
|
|
|
|
HighPriority,
|
|
|
|
OracleResponseT,
|
|
|
|
NotValidBeforeT,
|
|
|
|
}
|
|
|
|
t.Run("no attributes", func(t *testing.T) {
|
|
|
|
tx := new(Transaction)
|
|
|
|
for _, typ := range attributesTypes {
|
|
|
|
require.Nil(t, tx.GetAttributes(typ))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("single attributes", func(t *testing.T) {
|
|
|
|
attrs := make([]Attribute, len(attributesTypes))
|
|
|
|
for i, typ := range attributesTypes {
|
|
|
|
attrs[i] = Attribute{Type: typ}
|
|
|
|
}
|
|
|
|
tx := &Transaction{Attributes: attrs}
|
|
|
|
for _, typ := range attributesTypes {
|
|
|
|
require.Equal(t, []Attribute{{Type: typ}}, tx.GetAttributes(typ))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("multiple attributes", func(t *testing.T) {
|
|
|
|
typ := AttrType(ReservedLowerBound + 1)
|
|
|
|
conflictsAttrs := []Attribute{{Type: typ}, {Type: typ}}
|
|
|
|
tx := Transaction{
|
|
|
|
Attributes: append([]Attribute{{Type: HighPriority}}, conflictsAttrs...),
|
|
|
|
}
|
|
|
|
require.Equal(t, conflictsAttrs, tx.GetAttributes(typ))
|
|
|
|
})
|
|
|
|
}
|
2020-10-23 12:12:36 +00:00
|
|
|
|
|
|
|
func TestTransaction_HasSigner(t *testing.T) {
|
|
|
|
u1, u2 := random.Uint160(), random.Uint160()
|
|
|
|
tx := Transaction{
|
|
|
|
Signers: []Signer{
|
|
|
|
{Account: u1}, {Account: u2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.True(t, tx.HasSigner(u1))
|
|
|
|
require.False(t, tx.HasSigner(util.Uint160{}))
|
|
|
|
}
|
2021-12-02 11:48:35 +00:00
|
|
|
|
|
|
|
func BenchmarkTxHash(b *testing.B) {
|
|
|
|
script := []byte{0x51}
|
|
|
|
tx := New(script, 1)
|
|
|
|
tx.NetworkFee = 123
|
|
|
|
tx.Signers = []Signer{{Account: util.Uint160{1, 2, 3}}}
|
|
|
|
tx.Scripts = []Witness{{InvocationScript: []byte{}, VerificationScript: []byte{}}}
|
|
|
|
|
|
|
|
// Prime cache.
|
|
|
|
tx.Hash()
|
2024-08-30 18:41:02 +00:00
|
|
|
for range b.N {
|
2021-12-02 11:48:35 +00:00
|
|
|
_ = tx.Hash()
|
|
|
|
}
|
|
|
|
}
|
2024-04-27 17:16:05 +00:00
|
|
|
|
|
|
|
func TestTransaction_DeepCopy(t *testing.T) {
|
|
|
|
origTx := New([]byte{0x01, 0x02, 0x03}, 1000)
|
|
|
|
origTx.NetworkFee = 2000
|
|
|
|
origTx.SystemFee = 500
|
|
|
|
origTx.Nonce = 12345678
|
|
|
|
origTx.ValidUntilBlock = 100
|
|
|
|
origTx.Version = 1
|
|
|
|
require.Nil(t, (*Transaction)(nil).Copy())
|
|
|
|
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
origTx.Signers = []Signer{
|
|
|
|
{Account: random.Uint160(), Scopes: Global, AllowedContracts: []util.Uint160{random.Uint160()}, AllowedGroups: keys.PublicKeys{priv.PublicKey()}, Rules: []WitnessRule{{Action: 0x01, Condition: ConditionCalledByEntry{}}}},
|
|
|
|
{Account: random.Uint160(), Scopes: CalledByEntry},
|
|
|
|
}
|
|
|
|
origTx.Attributes = []Attribute{
|
|
|
|
{Type: HighPriority, Value: &OracleResponse{
|
|
|
|
ID: 0,
|
|
|
|
Code: Success,
|
|
|
|
Result: []byte{4, 8, 15, 16, 23, 42},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
origTx.Scripts = []Witness{
|
|
|
|
{
|
|
|
|
InvocationScript: []byte{0x04, 0x05},
|
|
|
|
VerificationScript: []byte{0x06, 0x07},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
origTxHash := origTx.Hash()
|
|
|
|
|
|
|
|
copyTx := origTx.Copy()
|
|
|
|
|
|
|
|
require.Equal(t, origTx.Hash(), copyTx.Hash())
|
|
|
|
require.Equal(t, origTx, copyTx)
|
|
|
|
require.Equal(t, origTx.Size(), copyTx.Size())
|
|
|
|
|
|
|
|
copyTx.NetworkFee = 3000
|
|
|
|
copyTx.Signers[0].Scopes = None
|
|
|
|
copyTx.Attributes[0].Type = NotaryAssistedT
|
|
|
|
copyTx.Scripts[0].InvocationScript[0] = 0x08
|
|
|
|
copyTx.hashed = false
|
|
|
|
modifiedCopyTxHash := copyTx.Hash()
|
|
|
|
|
|
|
|
require.NotEqual(t, origTx.NetworkFee, copyTx.NetworkFee)
|
|
|
|
require.NotEqual(t, origTx.Signers[0].Scopes, copyTx.Signers[0].Scopes)
|
|
|
|
require.NotEqual(t, origTx.Attributes, copyTx.Attributes)
|
|
|
|
require.NotEqual(t, origTxHash, modifiedCopyTxHash)
|
|
|
|
|
|
|
|
require.NotEqual(t, &origTx.Scripts[0].InvocationScript[0], ©Tx.Scripts[0].InvocationScript[0])
|
|
|
|
require.NotEqual(t, &origTx.Scripts, ©Tx.Scripts)
|
|
|
|
require.Equal(t, origTx.Scripts[0].VerificationScript, copyTx.Scripts[0].VerificationScript)
|
|
|
|
require.Equal(t, origTx.Signers[0].AllowedContracts, copyTx.Signers[0].AllowedContracts)
|
|
|
|
require.Equal(t, origTx.Signers[0].AllowedGroups, copyTx.Signers[0].AllowedGroups)
|
|
|
|
origGroup := origTx.Signers[0].AllowedGroups[0]
|
|
|
|
copyGroup := copyTx.Signers[0].AllowedGroups[0]
|
|
|
|
require.True(t, origGroup.Equal(copyGroup))
|
|
|
|
|
|
|
|
copyTx.Signers[0].AllowedGroups[0] = nil
|
|
|
|
require.NotEqual(t, origTx.Signers[0].AllowedGroups[0], copyTx.Signers[0].AllowedGroups[0])
|
|
|
|
|
|
|
|
require.Equal(t, origTx.Signers[0].Rules[0], copyTx.Signers[0].Rules[0])
|
|
|
|
copyTx.Signers[0].Rules[0].Action = 0x02
|
|
|
|
require.NotEqual(t, origTx.Signers[0].Rules[0].Action, copyTx.Signers[0].Rules[0].Action)
|
|
|
|
}
|