2020-01-14 12:32:07 +00:00
|
|
|
package block
|
2018-02-01 20:28:45 +00:00
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
import (
|
2020-08-18 10:45:24 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2020-10-02 14:25:55 +00:00
|
|
|
"errors"
|
2020-08-18 10:45:24 +00:00
|
|
|
"strings"
|
2018-02-04 19:54:51 +00:00
|
|
|
"testing"
|
2018-02-01 20:28:45 +00:00
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-08-18 10:45:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-03-15 10:00:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-06-05 09:28:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-18 10:45:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-04 19:54:51 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 10:45:24 +00:00
|
|
|
func trim0x(value interface{}) string {
|
|
|
|
s := value.(string)
|
|
|
|
return strings.TrimPrefix(s, "0x")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test blocks are blocks from testnet with their corresponding index.
|
2018-03-17 11:53:21 +00:00
|
|
|
func TestDecodeBlock1(t *testing.T) {
|
|
|
|
data, err := getBlockData(1)
|
2020-01-15 08:35:02 +00:00
|
|
|
require.NoError(t, err)
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
b, err := hex.DecodeString(data["raw"].(string))
|
2020-01-15 08:35:02 +00:00
|
|
|
require.NoError(t, err)
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
block := New(false)
|
2020-03-26 14:43:24 +00:00
|
|
|
assert.NoError(t, testserdes.DecodeBinary(b, block))
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
assert.Equal(t, uint32(data["index"].(float64)), block.Index)
|
|
|
|
assert.Equal(t, uint32(data["version"].(float64)), block.Version)
|
2020-08-18 10:45:24 +00:00
|
|
|
assert.Equal(t, trim0x(data["hash"]), block.Hash().StringLE())
|
|
|
|
assert.Equal(t, trim0x(data["previousblockhash"]), block.PrevHash.StringLE())
|
|
|
|
assert.Equal(t, trim0x(data["merkleroot"]), block.MerkleRoot.StringLE())
|
|
|
|
assert.Equal(t, trim0x(data["nextconsensus"]), address.Uint160ToString(block.NextConsensus))
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-08-18 10:45:24 +00:00
|
|
|
scripts := data["witnesses"].([]interface{})
|
|
|
|
script := scripts[0].(map[string]interface{})
|
|
|
|
assert.Equal(t, script["invocation"].(string), base64.StdEncoding.EncodeToString(block.Script.InvocationScript))
|
|
|
|
assert.Equal(t, script["verification"].(string), base64.StdEncoding.EncodeToString(block.Script.VerificationScript))
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
tx := data["tx"].([]interface{})
|
2020-08-18 10:45:24 +00:00
|
|
|
tx0 := tx[0].(map[string]interface{})
|
2018-03-17 11:53:21 +00:00
|
|
|
assert.Equal(t, len(tx), len(block.Transactions))
|
2020-08-18 10:45:24 +00:00
|
|
|
assert.Equal(t, len(tx0["attributes"].([]interface{})), len(block.Transactions[0].Attributes))
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrimmedBlock(t *testing.T) {
|
|
|
|
block := getDecodedBlock(t, 1)
|
|
|
|
|
|
|
|
b, err := block.Trim()
|
2020-01-15 08:35:02 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
trimmedBlock, err := NewBlockFromTrimmedBytes(false, b)
|
2020-01-15 08:35:02 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
assert.True(t, trimmedBlock.Trimmed)
|
|
|
|
assert.Equal(t, block.Version, trimmedBlock.Version)
|
|
|
|
assert.Equal(t, block.PrevHash, trimmedBlock.PrevHash)
|
|
|
|
assert.Equal(t, block.MerkleRoot, trimmedBlock.MerkleRoot)
|
|
|
|
assert.Equal(t, block.Timestamp, trimmedBlock.Timestamp)
|
|
|
|
assert.Equal(t, block.Index, trimmedBlock.Index)
|
|
|
|
assert.Equal(t, block.NextConsensus, trimmedBlock.NextConsensus)
|
|
|
|
|
|
|
|
assert.Equal(t, block.Script, trimmedBlock.Script)
|
|
|
|
assert.Equal(t, len(block.Transactions), len(trimmedBlock.Transactions))
|
|
|
|
for i := 0; i < len(block.Transactions); i++ {
|
|
|
|
assert.Equal(t, block.Transactions[i].Hash(), trimmedBlock.Transactions[i].Hash())
|
|
|
|
assert.True(t, trimmedBlock.Transactions[i].Trimmed)
|
|
|
|
}
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
2020-08-18 10:45:24 +00:00
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
func newDumbBlock() *Block {
|
|
|
|
return &Block{
|
2021-03-01 13:44:47 +00:00
|
|
|
Header: Header{
|
2019-10-15 09:52:10 +00:00
|
|
|
Version: 0,
|
|
|
|
PrevHash: hash.Sha256([]byte("a")),
|
|
|
|
MerkleRoot: hash.Sha256([]byte("b")),
|
2020-04-21 11:26:57 +00:00
|
|
|
Timestamp: 100500,
|
2019-10-15 09:52:10 +00:00
|
|
|
Index: 1,
|
|
|
|
NextConsensus: hash.Hash160([]byte("a")),
|
2019-12-09 14:14:10 +00:00
|
|
|
Script: transaction.Witness{
|
2019-10-15 09:52:10 +00:00
|
|
|
VerificationScript: []byte{0x51}, // PUSH1
|
|
|
|
InvocationScript: []byte{0x61}, // NOP
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Transactions: []*transaction.Transaction{
|
2021-03-25 16:18:01 +00:00
|
|
|
transaction.New([]byte{byte(opcode.PUSH1)}, 0),
|
2019-10-15 09:52:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:54:51 +00:00
|
|
|
func TestHashBlockEqualsHashHeader(t *testing.T) {
|
2019-10-15 09:52:10 +00:00
|
|
|
block := newDumbBlock()
|
|
|
|
|
2021-03-01 13:44:47 +00:00
|
|
|
assert.Equal(t, block.Hash(), block.Header.Hash())
|
2018-02-04 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 08:29:59 +00:00
|
|
|
func TestBinBlockDecodeEncode(t *testing.T) {
|
2021-03-17 13:40:24 +00:00
|
|
|
// block with two transfer transactions taken from C# privnet
|
2021-07-14 08:32:54 +00:00
|
|
|
rawblock := "AAAAAAwIVa2D6Yha3tArd5XnwkAf7deJBsdyyvpYb2xMZGBbkOUNHAsfre0rKA/F+Ox05/bQSXmcRZnzK3M6Z+/TxJUh0MNFeAEAAAAAAAAAAAAAAQAAAADe7nnBifMAmLC6ai65CzqSWKbH/wHGDEDgwCcXkcaFw5MGOp1cpkgApzDTX2/RxKlmPeXTgWYtfEA8g9svUSbZA4TeoGyWvX8LiN0tJKrzajdMGvTVGqVmDEDp6PBmZmRx9CxswtLht6oWa2Uq4rl5diPsLtqXZeZepMlxUSbaCdlFTB7iWQG9yKXWR5hc0sScevvuVwwsUYdlDEDwlhwZrP07E5fEQKttVMYAiL7edd/eW2yoMGZe6Q95g7yXQ69edVHfQb61fBw3DjCpMDZ5lsxp3BgzXglJwMSKkxMMIQIQOn990BZVhZf3lg0nxRakOU/ZaLnmUVXrSwE+QEBAbgwhAqe8Vf6GhOARl2jRBLoweVvcyGYZ6GSt0mFWcj7Rhc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Ds+W+3QD7rI+SFs0OhIn0gooCUU6f/13WjDEDr9XdeT5CGTO8CL0JigzcTcucs0GBcqHs8fToO6zPuuCfS7Wh6dyxSCijT4A4S+7BUdW3dsO7828ke1fj8oNxmkxMMIQIQOn990BZVhZf3lg0nxRakOU/ZaLnmUVXrSwE+QEBAbgwhAqe8Vf6GhOARl2jRBLoweVvcyGYZ6GSt0mFWcj7Rhc1iDCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcIMIQPZDAffY+aQzneRLhCrUazJRLZoYCN7YIxPj4MJ5x7mmRRBe85spQ=="
|
2021-02-05 15:53:30 +00:00
|
|
|
rawblockBytes, _ := base64.StdEncoding.DecodeString(rawblock)
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
b := New(false)
|
2020-08-18 10:45:24 +00:00
|
|
|
|
2021-02-05 15:53:30 +00:00
|
|
|
assert.NoError(t, testserdes.DecodeBinary(rawblockBytes, b))
|
2020-08-18 10:45:24 +00:00
|
|
|
expected := map[string]bool{ // 1 trans
|
2021-03-17 13:40:24 +00:00
|
|
|
"5a30127b16a628de6aa6823418d76214b97a0fc6db865024d3d2c6e939ce3433": false,
|
|
|
|
"25426643feed564cd3e57f346d6c68692f5622b3063da11c5572d99ee1a5b49a": false,
|
2019-08-30 08:29:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 15:38:33 +00:00
|
|
|
var hashes []string
|
2019-08-30 08:29:59 +00:00
|
|
|
|
|
|
|
for _, tx := range b.Transactions {
|
2020-08-18 10:45:24 +00:00
|
|
|
hashes = append(hashes, tx.Hash().StringLE())
|
2019-08-30 08:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, len(expected), len(hashes))
|
|
|
|
|
|
|
|
// changes value in map to true, if hash found
|
|
|
|
for _, hash := range hashes {
|
|
|
|
expected[hash] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate map; all vlaues should be true
|
|
|
|
val := true
|
|
|
|
for _, v := range expected {
|
|
|
|
if v == false {
|
|
|
|
val = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, true, val)
|
|
|
|
|
2020-08-18 10:45:24 +00:00
|
|
|
data, err := testserdes.EncodeBinary(b)
|
2020-03-26 14:43:24 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-05 15:53:30 +00:00
|
|
|
assert.Equal(t, rawblock, base64.StdEncoding.EncodeToString(data))
|
2020-05-13 18:27:08 +00:00
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
testserdes.MarshalUnmarshalJSON(t, b, New(false))
|
2019-08-30 08:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockSizeCalculation(t *testing.T) {
|
2021-03-17 13:40:24 +00:00
|
|
|
// block taken from C# privnet: 02d7c7801742cd404eb178780c840477f1eef4a771ecc8cc9434640fe8f2bb09
|
2019-08-30 08:29:59 +00:00
|
|
|
// The Size in golang is given by counting the number of bytes of an object. (len(Bytes))
|
2019-09-09 08:25:58 +00:00
|
|
|
// its implementation is different from the corresponding C# and python implementations. But the result should
|
2019-08-30 08:29:59 +00:00
|
|
|
// should be the same.In this test we provide more details then necessary because in case of failure we can easily debug the
|
|
|
|
// root cause of the size calculation missmatch.
|
|
|
|
|
2021-07-14 08:32:54 +00:00
|
|
|
rawBlock := "AAAAAAwIVa2D6Yha3tArd5XnwkAf7deJBsdyyvpYb2xMZGBbkOUNHAsfre0rKA/F+Ox05/bQSXmcRZnzK3M6Z+/TxJUh0MNFeAEAAAAAAAAAAAAAAQAAAADe7nnBifMAmLC6ai65CzqSWKbH/wHGDEDgwCcXkcaFw5MGOp1cpkgApzDTX2/RxKlmPeXTgWYtfEA8g9svUSbZA4TeoGyWvX8LiN0tJKrzajdMGvTVGqVmDEDp6PBmZmRx9CxswtLht6oWa2Uq4rl5diPsLtqXZeZepMlxUSbaCdlFTB7iWQG9yKXWR5hc0sScevvuVwwsUYdlDEDwlhwZ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zZmVyDBTPduKL0AYsSkeO41VhARMZ88+k0kFifVtSOQHGDEDWn0D7z2ELqpN8ghcM/PtfFwo56/BfEasfHuSKECJMYxvU47r2ZtSihg59lGxSZzHsvxTy6nsyvJ22ycNhINdJDECl61cg937N/HujKsLMu2wJMS7C54bzJ3q22Czqllvw3Yp809USgKDs+W+3QD7rI+SFs0OhIn0gooCUU6f/13WjDEDr9XdeT5CGTO8CL0JigzcTcucs0GBcqHs8fToO6zPuuCfS7Wh6dyxSCijT4A4S+7BUdW3dsO7828ke1fj8oNxmkxMMIQIQOn990BZVhZf3lg0nxRakOU/ZaLnmUVXrSwE+QEBAbgwhAqe8Vf6GhOARl2jRBLoweVvcyGYZ6GSt0mFWcj7Rhc1iDCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcIMIQPZDAffY+aQzneRLhCrUazJRLZoYCN7YIxPj4MJ5x7mmRRBe85spQ=="
|
2021-02-05 15:53:30 +00:00
|
|
|
rawBlockBytes, _ := base64.StdEncoding.DecodeString(rawBlock)
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
b := New(false)
|
2020-08-18 10:45:24 +00:00
|
|
|
assert.NoError(t, testserdes.DecodeBinary(rawBlockBytes, b))
|
2019-08-30 08:29:59 +00:00
|
|
|
|
|
|
|
expected := []struct {
|
|
|
|
ID string
|
|
|
|
Size int
|
|
|
|
Version int
|
2020-08-18 10:45:24 +00:00
|
|
|
SignersLen int
|
2019-08-30 08:29:59 +00:00
|
|
|
AttributesLen int
|
|
|
|
WitnessesLen int
|
2021-03-17 13:40:24 +00:00
|
|
|
}{ // 2 transactions
|
|
|
|
{ID: "5a30127b16a628de6aa6823418d76214b97a0fc6db865024d3d2c6e939ce3433", Size: 488, Version: 0, SignersLen: 1, AttributesLen: 0, WitnessesLen: 1},
|
|
|
|
{ID: "25426643feed564cd3e57f346d6c68692f5622b3063da11c5572d99ee1a5b49a", Size: 492, Version: 0, SignersLen: 1, AttributesLen: 0, WitnessesLen: 1},
|
2019-08-30 08:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tx := range b.Transactions {
|
|
|
|
txID := tx.Hash()
|
2019-11-27 09:23:18 +00:00
|
|
|
assert.Equal(t, expected[i].ID, txID.StringLE())
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2019-09-16 13:08:00 +00:00
|
|
|
assert.Equal(t, expected[i].Size, io.GetVarSize(tx))
|
2019-08-30 08:29:59 +00:00
|
|
|
assert.Equal(t, expected[i].Version, int(tx.Version))
|
2020-08-18 10:45:24 +00:00
|
|
|
assert.Equal(t, expected[i].SignersLen, len(tx.Signers))
|
2019-08-30 08:29:59 +00:00
|
|
|
assert.Equal(t, expected[i].AttributesLen, len(tx.Attributes))
|
|
|
|
assert.Equal(t, expected[i].WitnessesLen, len(tx.Scripts))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, len(expected), len(b.Transactions))
|
|
|
|
|
|
|
|
// Block specific tests
|
|
|
|
assert.Equal(t, 0, int(b.Version))
|
2021-03-17 13:40:24 +00:00
|
|
|
assert.Equal(t, "5b60644c6c6f58faca72c70689d7ed1f40c2e795772bd0de5a88e983ad55080c", b.PrevHash.StringLE())
|
|
|
|
assert.Equal(t, "95c4d3ef673a732bf399459c7949d0f6e774ecf8c50f282bedad1f0b1c0de590", b.MerkleRoot.StringLE())
|
|
|
|
assert.Equal(t, 1616078164001, int(b.Timestamp))
|
|
|
|
assert.Equal(t, 1, int(b.Index))
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2019-12-25 14:34:18 +00:00
|
|
|
nextConsensus := address.Uint160ToString(b.NextConsensus)
|
2021-03-17 13:40:24 +00:00
|
|
|
assert.Equal(t, "NgEisvCqr2h8wpRxQb7bVPWUZdbVCY8Uo6", nextConsensus)
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2021-03-17 13:40:24 +00:00
|
|
|
assert.Equal(t, "DEDgwCcXkcaFw5MGOp1cpkgApzDTX2/RxKlmPeXTgWYtfEA8g9svUSbZA4TeoGyWvX8LiN0tJKrzajdMGvTVGqVmDEDp6PBmZmRx9CxswtLht6oWa2Uq4rl5diPsLtqXZeZepMlxUSbaCdlFTB7iWQG9yKXWR5hc0sScevvuVwwsUYdlDEDwlhwZrP07E5fEQKttVMYAiL7edd/eW2yoMGZe6Q95g7yXQ69edVHfQb61fBw3DjCpMDZ5lsxp3BgzXglJwMSK", base64.StdEncoding.EncodeToString(b.Script.InvocationScript))
|
|
|
|
assert.Equal(t, "EwwhAhA6f33QFlWFl/eWDSfFFqQ5T9loueZRVetLAT5AQEBuDCECp7xV/oaE4BGXaNEEujB5W9zIZhnoZK3SYVZyPtGFzWIMIQKzYiv0AXvf4xfFiu1fTHU/IGt9uJYEb6fXdLvEv3+NwgwhA9kMB99j5pDOd5EuEKtRrMlEtmhgI3tgjE+PgwnnHuaZFEF7zmyl", base64.StdEncoding.EncodeToString(b.Script.VerificationScript))
|
2021-07-14 08:32:54 +00:00
|
|
|
assert.Equal(t, "0285238e7b5d3ca50ae8f8636dedb4fa486bc0536016e1f1edfd74442af7412e", b.Hash().StringLE())
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2020-08-18 10:45:24 +00:00
|
|
|
benc, err := testserdes.EncodeBinary(b)
|
2020-03-26 14:43:24 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-30 08:29:59 +00:00
|
|
|
// test size of the block
|
2021-07-14 08:32:54 +00:00
|
|
|
assert.Equal(t, 1438, len(benc))
|
2021-02-05 15:53:30 +00:00
|
|
|
assert.Equal(t, rawBlock, base64.StdEncoding.EncodeToString(benc))
|
2019-08-30 08:29:59 +00:00
|
|
|
}
|
2019-09-25 16:54:31 +00:00
|
|
|
|
|
|
|
func TestBlockCompare(t *testing.T) {
|
2021-03-01 13:44:47 +00:00
|
|
|
b1 := Block{Header: Header{Index: 1}}
|
|
|
|
b2 := Block{Header: Header{Index: 2}}
|
|
|
|
b3 := Block{Header: Header{Index: 3}}
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 1, b2.Compare(&b1))
|
|
|
|
assert.Equal(t, 0, b2.Compare(&b2))
|
|
|
|
assert.Equal(t, -1, b2.Compare(&b3))
|
|
|
|
}
|
2020-10-02 14:25:55 +00:00
|
|
|
|
|
|
|
func TestBlockEncodeDecode(t *testing.T) {
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
b := newDumbBlock()
|
|
|
|
b.Transactions = []*transaction.Transaction{}
|
|
|
|
_ = b.Hash()
|
|
|
|
testserdes.EncodeDecodeBinary(t, b, new(Block))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad contents count", func(t *testing.T) {
|
|
|
|
b := newDumbBlock()
|
2021-03-01 13:49:24 +00:00
|
|
|
b.Transactions = make([]*transaction.Transaction, MaxTransactionsPerBlock+1)
|
2020-10-02 14:25:55 +00:00
|
|
|
for i := range b.Transactions {
|
|
|
|
b.Transactions[i] = &transaction.Transaction{
|
|
|
|
Script: []byte("my_pretty_script"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = b.Hash()
|
|
|
|
data, err := testserdes.EncodeBinary(b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, errors.Is(testserdes.DecodeBinary(data, new(Block)), ErrMaxContentsPerBlock))
|
|
|
|
})
|
|
|
|
}
|
2021-03-15 10:00:04 +00:00
|
|
|
|
|
|
|
func TestGetExpectedBlockSize(t *testing.T) {
|
|
|
|
check := func(t *testing.T, stateRootEnabled bool) {
|
|
|
|
t.Run("without transactions", func(t *testing.T) {
|
|
|
|
b := newDumbBlock()
|
|
|
|
b.StateRootEnabled = stateRootEnabled
|
|
|
|
b.Transactions = []*transaction.Transaction{}
|
|
|
|
require.Equal(t, io.GetVarSize(b), b.GetExpectedBlockSize())
|
|
|
|
require.Equal(t, io.GetVarSize(b), b.GetExpectedBlockSizeWithoutTransactions(0))
|
|
|
|
})
|
|
|
|
t.Run("with one transaction", func(t *testing.T) {
|
|
|
|
b := newDumbBlock()
|
|
|
|
b.StateRootEnabled = stateRootEnabled
|
|
|
|
expected := io.GetVarSize(b)
|
|
|
|
require.Equal(t, expected, b.GetExpectedBlockSize())
|
|
|
|
require.Equal(t, expected-b.Transactions[0].Size(), b.GetExpectedBlockSizeWithoutTransactions(len(b.Transactions)))
|
|
|
|
})
|
|
|
|
t.Run("with multiple transactions", func(t *testing.T) {
|
|
|
|
b := newDumbBlock()
|
|
|
|
b.StateRootEnabled = stateRootEnabled
|
|
|
|
b.Transactions = make([]*transaction.Transaction, 123)
|
|
|
|
for i := range b.Transactions {
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New([]byte{byte(opcode.RET)}, int64(i))
|
2021-03-15 10:00:04 +00:00
|
|
|
tx.Signers = []transaction.Signer{{Account: util.Uint160{1, 2, 3}}}
|
|
|
|
tx.Scripts = []transaction.Witness{{}}
|
|
|
|
b.Transactions[i] = tx
|
|
|
|
}
|
|
|
|
expected := io.GetVarSize(b)
|
|
|
|
require.Equal(t, expected, b.GetExpectedBlockSize())
|
|
|
|
for _, tx := range b.Transactions {
|
|
|
|
expected -= tx.Size()
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, b.GetExpectedBlockSizeWithoutTransactions(len(b.Transactions)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
t.Run("StateRoot enabled", func(t *testing.T) {
|
|
|
|
check(t, true)
|
|
|
|
})
|
|
|
|
t.Run("StateRoot disabled", func(t *testing.T) {
|
|
|
|
check(t, false)
|
|
|
|
})
|
|
|
|
}
|