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-06-18 09:00:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
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"
|
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
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
block := New(netmode.TestNet, 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
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
trimmedBlock, err := NewBlockFromTrimmedBytes(netmode.TestNet, 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{
|
2020-01-15 08:29:50 +00:00
|
|
|
Base: Base{
|
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{
|
2020-06-18 09:00:51 +00:00
|
|
|
transaction.New(netmode.UnitTestNet, []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()
|
|
|
|
|
2018-03-09 15:55:25 +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-02-05 15:53:30 +00:00
|
|
|
// block taken from testnet: 256
|
2021-03-01 12:20:27 +00:00
|
|
|
rawblock := "AAAAAFIZHAfkJpTDtc9SysVfmBLqDbwXeM7Z7KgaRpWsaCv9qRn3glL7lmCRuSDAE+a5DanThVwfQjtf/1ewuJroTAoqSYJodwEAAAABAAAE4KPFXK1yAo+1kBdIsZonviH2VAQB/UoBDEAZyIVwKWzpgvm8bSzWuVF1awS0QBguORR682YoVfkJ05KGG7pz9OsLBZ9/F1BHO7TCIcEcc8Sr9AcqbonrSRoCDEDhhZnqTUi8tXYbrTBrVj8i9Yxl8NraAMID4DJryWdD6u42OzAHr0of27qc5nojujhLffBfljuBSyQpyeQcUOpyDEAZr7sLz6n4TgNApkwATGXiP/DZ8AummAGDtznrW6Y8XWHngB7F0j3NKvUQ7lyRt11rT+zNjz5R5vIbBrs8C2ktDEDgNW0xGP/OB8CRHXX/l/+0tPO3drkyCddpG9PKCgyd4oEhQu8cwbSv4Q0V1Y0dZcJBoxUB5Qus/U+X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nsE8MV5IVQPOjRyCGw7UHOPznpFDUpc+yO4f79Ak4QbnCWEL+dUA9W7A5ywJ5CfNj9Cmi9nvOyEXbor9gKrvcQxAItM9wyF75LsiFE2YVS7zXyOx8QNnkktMa+px1sKAzyov/xXzyfI1zVYgvzEHCapT1kA+bS/m0H8T2DgDosE6f/39ABUMIQMAm3VA4Q8lYuX9j6yersJRZqWLJuQSNI/1qGknv6wiogwhAwIF6c766lod/FgK8gyNWqJGi7AUjxpeRgX8YiyA5gS6DCECFLrwzuo6ZvF+fh6DnqJf2L7WzYLmu25oJQGJBl9E/wEMIQI+mzLqiblNBm5kmxJP1Q45bukTaejipq4bEcFw0CIlbQwhA0CNzUFjlvZHg6xYfqHhWTxX2f6ogMimoZIOkqJZR3gGDCECp4NL6bMuKYHRV8tbvTrLQs/RHqXDsQIk16ROmMWRDxsMIQK6LHD1mW81ekMZhwWFn64s/qE+EXKWKAB3Kz1Yip1KvRcLQRON768="
|
2021-02-05 15:53:30 +00:00
|
|
|
rawblockBytes, _ := base64.StdEncoding.DecodeString(rawblock)
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
b := New(netmode.TestNet, 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-02-05 15:53:30 +00:00
|
|
|
"affad44bb6acacabc058db0bf1e12ab1239ae5e04007b4d4a2ea0cda868e284a": 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
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
testserdes.MarshalUnmarshalJSON(t, b, New(netmode.TestNet, false))
|
2019-08-30 08:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockSizeCalculation(t *testing.T) {
|
2021-02-05 15:53:30 +00:00
|
|
|
// block taken from testnet at height 256: 51ee44e12cdc1d3041a50d352063127fa65d86670686f14cc08f01b3cee7de17
|
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-03-01 12:20:27 +00:00
|
|
|
rawBlock := "AAAAAFIZHAfkJpTDtc9SysVfmBLqDbwXeM7Z7KgaRpWsaCv9qRn3glL7lmCRuSDAE+a5DanThVwfQjtf/1ewuJroTAoqSYJodwEAAAABAAAE4KPFXK1yAo+1kBdIsZonviH2VAQB/UoBDEAZyIVwKWzpgvm8bSzWuVF1awS0QBguORR682YoV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OstCz9EepcOxAiTXpE6YxZEPGwtBlUQNeP1KAQxA4lRhOsKzvXVtkHkysVJ45isJSj6rkO/1qixWtZgWGFJ3XL12B2rAH+SK/fiXB3hFxBWzyniEZo6UOWcZtttm4wxAp0J+PNUjjjpdSrO0Nho3hyrz3YvjNrnx8Z8L1Jcu24ebeB0MRdxoTaeyILjTmb6hlIwf6STape27jfEXiUEwUwxAmaFRZ5IyYIPlDZnUqXQlsxxCkTaUlogIFhkIRAPgBlxrpXagpFidkRXjz3HIQ8UuR2ti/0NJxzg4ssLLOfbEPQxAnsE8MV5IVQPOjRyCGw7UHOPznpFDUpc+yO4f79Ak4QbnCWEL+dUA9W7A5ywJ5CfNj9Cmi9nvOyEXbor9gKrvcQxAItM9wyF75LsiFE2YVS7zXyOx8QNnkktMa+px1sKAzyov/xXzyfI1zVYgvzEHCapT1kA+bS/m0H8T2DgDosE6f/39ABUMIQMAm3VA4Q8lYuX9j6yersJRZqWLJuQSNI/1qGknv6wiogwhAwIF6c766lod/FgK8gyNWqJGi7AUjxpeRgX8YiyA5gS6DCECFLrwzuo6ZvF+fh6DnqJf2L7WzYLmu25oJQGJBl9E/wEMIQI+mzLqiblNBm5kmxJP1Q45bukTaejipq4bEcFw0CIlbQwhA0CNzUFjlvZHg6xYfqHhWTxX2f6ogMimoZIOkqJZR3gGDCECp4NL6bMuKYHRV8tbvTrLQs/RHqXDsQIk16ROmMWRDxsMIQK6LHD1mW81ekMZhwWFn64s/qE+EXKWKAB3Kz1Yip1KvRcLQRON768="
|
2021-02-05 15:53:30 +00:00
|
|
|
rawBlockBytes, _ := base64.StdEncoding.DecodeString(rawBlock)
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2020-11-17 12:57:50 +00:00
|
|
|
b := New(netmode.TestNet, 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
|
2020-08-18 10:45:24 +00:00
|
|
|
}{ // 1 trans
|
2021-02-05 15:53:30 +00:00
|
|
|
{ID: "affad44bb6acacabc058db0bf1e12ab1239ae5e04007b4d4a2ea0cda868e284a", Size: 864, Version: 0, SignersLen: 2, AttributesLen: 0, WitnessesLen: 2},
|
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-02-05 15:53:30 +00:00
|
|
|
assert.Equal(t, "fd2b68ac95461aa8ecd9ce7817bc0dea12985fc5ca52cfb5c39426e4071c1952", b.PrevHash.StringLE())
|
|
|
|
assert.Equal(t, "0a4ce89ab8b057ff5f3b421f5c85d3a90db9e613c020b9916096fb5282f719a9", b.MerkleRoot.StringLE())
|
|
|
|
assert.Equal(t, 1612366104874, int(b.Timestamp))
|
|
|
|
assert.Equal(t, 256, 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-02-05 15:53:30 +00:00
|
|
|
assert.Equal(t, "NgPkjjLTNcQad99iRYeXRUuowE4gxLAnDL", nextConsensus)
|
2019-08-30 08:29:59 +00:00
|
|
|
|
2021-02-05 15:53:30 +00:00
|
|
|
assert.Equal(t, "DEAZyIVwKWzpgvm8bSzWuVF1awS0QBguORR682YoVfkJ05KGG7pz9OsLBZ9/F1BHO7TCIcEcc8Sr9AcqbonrSRoCDEDhhZnqTUi8tXYbrTBrVj8i9Yxl8NraAMID4DJryWdD6u42OzAHr0of27qc5nojujhLffBfljuBSyQpyeQcUOpyDEAZr7sLz6n4TgNApkwATGXiP/DZ8AummAGDtznrW6Y8XWHngB7F0j3NKvUQ7lyRt11rT+zNjz5R5vIbBrs8C2ktDEDgNW0xGP/OB8CRHXX/l/+0tPO3drkyCddpG9PKCgyd4oEhQu8cwbSv4Q0V1Y0dZcJBoxUB5Qus/U+Xli7jb6OJDEADaXZxdEb56jlYhF4oM8I8KnthEzGw1Q7pfeI97fehTm78ZOJXWIxESThZzI3D2ndVz0Rcdr5MeA/espG6J2ao", base64.StdEncoding.EncodeToString(b.Script.InvocationScript))
|
|
|
|
assert.Equal(t, "FQwhAwCbdUDhDyVi5f2PrJ6uwlFmpYsm5BI0j/WoaSe/rCKiDCEDAgXpzvrqWh38WAryDI1aokaLsBSPGl5GBfxiLIDmBLoMIQIUuvDO6jpm8X5+HoOeol/YvtbNgua7bmglAYkGX0T/AQwhAj6bMuqJuU0GbmSbEk/VDjlu6RNp6OKmrhsRwXDQIiVtDCEDQI3NQWOW9keDrFh+oeFZPFfZ/qiAyKahkg6SollHeAYMIQKng0vpsy4pgdFXy1u9OstCz9EepcOxAiTXpE6YxZEPGwwhAroscPWZbzV6QxmHBYWfriz+oT4RcpYoAHcrPViKnUq9FwtBE43vrw==", base64.StdEncoding.EncodeToString(b.Script.VerificationScript))
|
2021-03-01 12:20:27 +00:00
|
|
|
assert.Equal(t, "63d18734a3edbe92a9480b690734b85aaf9c24361d034afbea37d749cfc62d6a", 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-03-01 12:20:27 +00:00
|
|
|
assert.Equal(t, 1556, 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) {
|
2020-01-15 08:29:50 +00:00
|
|
|
b1 := Block{Base: Base{Index: 1}}
|
|
|
|
b2 := Block{Base: Base{Index: 2}}
|
|
|
|
b3 := Block{Base: Base{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 12:20:27 +00:00
|
|
|
b.Transactions = make([]*transaction.Transaction, MaxContentsPerBlock+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))
|
|
|
|
})
|
|
|
|
}
|