2021-10-23 11:28:03 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2021-11-03 12:28:29 +00:00
|
|
|
"sort"
|
2021-10-23 11:28:03 +00:00
|
|
|
"testing"
|
2022-12-02 16:10:45 +00:00
|
|
|
"time"
|
2021-10-23 11:28:03 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-11-03 10:44:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
2021-11-03 12:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-10-23 11:28:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
2022-03-23 20:06:34 +00:00
|
|
|
const (
|
|
|
|
// MaxTraceableBlocks is the default MaxTraceableBlocks setting used for test chains.
|
|
|
|
// We don't need a lot of traceable blocks for tests.
|
|
|
|
MaxTraceableBlocks = 1000
|
|
|
|
|
2022-12-02 16:10:45 +00:00
|
|
|
// TimePerBlock is the default TimePerBlock setting used for test chains (1s).
|
2022-03-23 20:06:34 +00:00
|
|
|
// Usually blocks are created by tests bypassing this setting.
|
2022-12-02 16:10:45 +00:00
|
|
|
TimePerBlock = time.Second
|
2022-03-23 20:06:34 +00:00
|
|
|
)
|
|
|
|
|
2021-11-03 12:28:29 +00:00
|
|
|
const singleValidatorWIF = "KxyjQ8eUa4FHt3Gvioyt1Wz29cTUrE4eTqX3yFSk1YFCsPL8uNsY"
|
2021-10-23 11:28:03 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// committeeWIFs is a list of unencrypted WIFs sorted by the public key.
|
2021-11-03 12:28:29 +00:00
|
|
|
var committeeWIFs = []string{
|
|
|
|
"KzfPUYDC9n2yf4fK5ro4C8KMcdeXtFuEnStycbZgX3GomiUsvX6W",
|
|
|
|
"KzgWE3u3EDp13XPXXuTKZxeJ3Gi8Bsm8f9ijY3ZsCKKRvZUo1Cdn",
|
|
|
|
singleValidatorWIF,
|
|
|
|
"L2oEXKRAAMiPEZukwR5ho2S6SMeQLhcK9mF71ZnF7GvT8dU4Kkgz",
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Provide 2 committee extra members so that the committee address differs from
|
2021-11-03 12:28:29 +00:00
|
|
|
// the validators one.
|
|
|
|
"L1Tr1iq5oz1jaFaMXP21sHDkJYDDkuLtpvQ4wRf1cjKvJYvnvpAb",
|
|
|
|
"Kz6XTUrExy78q8f4MjDHnwz8fYYyUE8iPXwPRAkHa3qN2JcHYm7e",
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-04-20 18:30:09 +00:00
|
|
|
// committeeAcc is an account used to sign a tx as a committee.
|
2021-11-03 12:28:29 +00:00
|
|
|
committeeAcc *wallet.Account
|
|
|
|
|
|
|
|
// multiCommitteeAcc contains committee accounts used in a multi-node setup.
|
|
|
|
multiCommitteeAcc []*wallet.Account
|
|
|
|
|
|
|
|
// multiValidatorAcc contains validator accounts used in a multi-node setup.
|
|
|
|
multiValidatorAcc []*wallet.Account
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// standByCommittee contains a list of committee public keys to use in config.
|
2021-11-03 12:28:29 +00:00
|
|
|
standByCommittee []string
|
|
|
|
)
|
2021-10-23 11:28:03 +00:00
|
|
|
|
|
|
|
func init() {
|
2021-11-03 12:28:29 +00:00
|
|
|
committeeAcc, _ = wallet.NewAccountFromWIF(singleValidatorWIF)
|
2022-09-01 14:52:44 +00:00
|
|
|
pubs := keys.PublicKeys{committeeAcc.PublicKey()}
|
2021-10-23 11:28:03 +00:00
|
|
|
err := committeeAcc.ConvertMultisig(1, pubs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-03 12:28:29 +00:00
|
|
|
|
|
|
|
mc := smartcontract.GetMajorityHonestNodeCount(len(committeeWIFs))
|
|
|
|
mv := smartcontract.GetDefaultHonestNodeCount(4)
|
|
|
|
accs := make([]*wallet.Account, len(committeeWIFs))
|
|
|
|
pubs = make(keys.PublicKeys, len(accs))
|
|
|
|
for i := range committeeWIFs {
|
|
|
|
accs[i], _ = wallet.NewAccountFromWIF(committeeWIFs[i])
|
2022-09-01 14:52:44 +00:00
|
|
|
pubs[i] = accs[i].PublicKey()
|
2021-11-03 12:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config entry must contain validators first in a specific order.
|
|
|
|
standByCommittee = make([]string, len(pubs))
|
|
|
|
standByCommittee[0] = hex.EncodeToString(pubs[2].Bytes())
|
|
|
|
standByCommittee[1] = hex.EncodeToString(pubs[0].Bytes())
|
|
|
|
standByCommittee[2] = hex.EncodeToString(pubs[3].Bytes())
|
|
|
|
standByCommittee[3] = hex.EncodeToString(pubs[1].Bytes())
|
|
|
|
standByCommittee[4] = hex.EncodeToString(pubs[4].Bytes())
|
|
|
|
standByCommittee[5] = hex.EncodeToString(pubs[5].Bytes())
|
|
|
|
|
2021-12-03 13:37:09 +00:00
|
|
|
multiValidatorAcc = make([]*wallet.Account, 4)
|
2021-11-03 12:28:29 +00:00
|
|
|
sort.Sort(pubs[:4])
|
|
|
|
|
2021-12-03 13:33:34 +00:00
|
|
|
sort.Slice(accs[:4], func(i, j int) bool {
|
2022-09-01 14:52:44 +00:00
|
|
|
p1 := accs[i].PublicKey()
|
|
|
|
p2 := accs[j].PublicKey()
|
2021-12-03 13:33:34 +00:00
|
|
|
return p1.Cmp(p2) == -1
|
|
|
|
})
|
|
|
|
for i := range multiValidatorAcc {
|
|
|
|
multiValidatorAcc[i] = wallet.NewAccountFromPrivateKey(accs[i].PrivateKey())
|
|
|
|
err := multiValidatorAcc[i].ConvertMultisig(mv, pubs[:4])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-11-03 12:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 13:37:09 +00:00
|
|
|
multiCommitteeAcc = make([]*wallet.Account, len(committeeWIFs))
|
2021-11-03 12:28:29 +00:00
|
|
|
sort.Sort(pubs)
|
|
|
|
|
2021-12-03 13:33:34 +00:00
|
|
|
sort.Slice(accs, func(i, j int) bool {
|
2022-09-01 14:52:44 +00:00
|
|
|
p1 := accs[i].PublicKey()
|
|
|
|
p2 := accs[j].PublicKey()
|
2021-12-03 13:33:34 +00:00
|
|
|
return p1.Cmp(p2) == -1
|
|
|
|
})
|
|
|
|
for i := range multiCommitteeAcc {
|
|
|
|
multiCommitteeAcc[i] = wallet.NewAccountFromPrivateKey(accs[i].PrivateKey())
|
|
|
|
err := multiCommitteeAcc[i].ConvertMultisig(mc, pubs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-11-03 12:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewSingle creates a new blockchain instance with a single validator and
|
2022-03-23 20:07:02 +00:00
|
|
|
// setups cleanup functions. The configuration used is with netmode.UnitTestNet
|
2022-12-02 16:10:45 +00:00
|
|
|
// magic and TimePerBlock/MaxTraceableBlocks options defined by constants in
|
2022-03-23 20:07:02 +00:00
|
|
|
// this package. MemoryStore is used as the backend storage, so all of the chain
|
2022-04-20 18:30:09 +00:00
|
|
|
// contents is always in RAM. The Signer returned is the validator (and the committee at
|
2022-03-23 20:07:02 +00:00
|
|
|
// the same time).
|
2022-03-18 15:11:24 +00:00
|
|
|
func NewSingle(t testing.TB) (*core.Blockchain, neotest.Signer) {
|
2021-12-03 15:45:55 +00:00
|
|
|
return NewSingleWithCustomConfig(t, nil)
|
|
|
|
}
|
|
|
|
|
2022-03-23 20:07:02 +00:00
|
|
|
// NewSingleWithCustomConfig is similar to NewSingle, but allows to override the
|
|
|
|
// default configuration.
|
2022-12-06 13:34:38 +00:00
|
|
|
func NewSingleWithCustomConfig(t testing.TB, f func(*config.Blockchain)) (*core.Blockchain, neotest.Signer) {
|
2022-03-11 14:47:59 +00:00
|
|
|
return NewSingleWithCustomConfigAndStore(t, f, nil, true)
|
2021-12-09 17:12:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 20:07:02 +00:00
|
|
|
// NewSingleWithCustomConfigAndStore is similar to NewSingleWithCustomConfig, but
|
|
|
|
// also allows to override backend Store being used. The last parameter controls if
|
2022-04-20 18:30:09 +00:00
|
|
|
// Run method is called on the Blockchain instance. If not, it is its caller's
|
|
|
|
// responsibility to do that before using the chain and
|
|
|
|
// to properly Close the chain when done.
|
2022-12-06 13:34:38 +00:00
|
|
|
func NewSingleWithCustomConfigAndStore(t testing.TB, f func(cfg *config.Blockchain), st storage.Store, run bool) (*core.Blockchain, neotest.Signer) {
|
|
|
|
var cfg = config.Blockchain{
|
|
|
|
ProtocolConfiguration: config.ProtocolConfiguration{
|
|
|
|
Magic: netmode.UnitTestNet,
|
|
|
|
MaxTraceableBlocks: MaxTraceableBlocks,
|
|
|
|
TimePerBlock: TimePerBlock,
|
|
|
|
StandbyCommittee: []string{hex.EncodeToString(committeeAcc.PublicKey().Bytes())},
|
|
|
|
ValidatorsCount: 1,
|
|
|
|
VerifyTransactions: true,
|
|
|
|
},
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
2022-12-06 13:34:38 +00:00
|
|
|
|
2021-12-03 15:45:55 +00:00
|
|
|
if f != nil {
|
2022-12-06 13:34:38 +00:00
|
|
|
f(&cfg)
|
2021-12-03 15:45:55 +00:00
|
|
|
}
|
2022-03-11 14:47:59 +00:00
|
|
|
if st == nil {
|
|
|
|
st = storage.NewMemoryStore()
|
|
|
|
}
|
2021-10-23 11:28:03 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
2022-12-06 13:34:38 +00:00
|
|
|
bc, err := core.NewBlockchain(st, cfg, log)
|
2021-10-23 11:28:03 +00:00
|
|
|
require.NoError(t, err)
|
2021-12-09 17:12:15 +00:00
|
|
|
if run {
|
|
|
|
go bc.Run()
|
|
|
|
t.Cleanup(bc.Close)
|
|
|
|
}
|
2021-11-03 10:44:46 +00:00
|
|
|
return bc, neotest.NewMultiSigner(committeeAcc)
|
2021-10-23 11:28:03 +00:00
|
|
|
}
|
2021-11-03 12:28:29 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewMulti creates a new blockchain instance with four validators and six
|
|
|
|
// committee members. Otherwise, it does not differ much from NewSingle. The
|
|
|
|
// second value returned contains the validators Signer, the third -- the committee one.
|
2022-03-18 15:11:24 +00:00
|
|
|
func NewMulti(t testing.TB) (*core.Blockchain, neotest.Signer, neotest.Signer) {
|
2021-12-03 15:45:55 +00:00
|
|
|
return NewMultiWithCustomConfig(t, nil)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewMultiWithCustomConfig is similar to NewMulti, except it allows to override the
|
2022-03-23 20:07:02 +00:00
|
|
|
// default configuration.
|
2022-12-06 13:34:38 +00:00
|
|
|
func NewMultiWithCustomConfig(t testing.TB, f func(*config.Blockchain)) (*core.Blockchain, neotest.Signer, neotest.Signer) {
|
2022-03-22 16:54:27 +00:00
|
|
|
return NewMultiWithCustomConfigAndStore(t, f, nil, true)
|
|
|
|
}
|
|
|
|
|
2022-03-18 15:11:24 +00:00
|
|
|
// NewMultiWithCustomConfigAndStore is similar to NewMultiWithCustomConfig, but
|
|
|
|
// also allows to override backend Store being used. The last parameter controls if
|
2022-04-20 18:30:09 +00:00
|
|
|
// Run method is called on the Blockchain instance. If not, it is its caller's
|
|
|
|
// responsibility to do that before using the chain and
|
|
|
|
// to properly Close the chain when done.
|
2022-12-06 13:34:38 +00:00
|
|
|
func NewMultiWithCustomConfigAndStore(t testing.TB, f func(*config.Blockchain), st storage.Store, run bool) (*core.Blockchain, neotest.Signer, neotest.Signer) {
|
2022-03-22 16:54:27 +00:00
|
|
|
bc, validator, committee, err := NewMultiWithCustomConfigAndStoreNoCheck(t, f, st)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if run {
|
|
|
|
go bc.Run()
|
|
|
|
t.Cleanup(bc.Close)
|
|
|
|
}
|
|
|
|
return bc, validator, committee
|
|
|
|
}
|
|
|
|
|
2022-03-18 15:11:24 +00:00
|
|
|
// NewMultiWithCustomConfigAndStoreNoCheck is similar to NewMultiWithCustomConfig,
|
|
|
|
// but do not perform Blockchain run and do not check Blockchain constructor error.
|
2022-12-06 13:34:38 +00:00
|
|
|
func NewMultiWithCustomConfigAndStoreNoCheck(t testing.TB, f func(*config.Blockchain), st storage.Store) (*core.Blockchain, neotest.Signer, neotest.Signer, error) {
|
|
|
|
var cfg = config.Blockchain{
|
|
|
|
ProtocolConfiguration: config.ProtocolConfiguration{
|
|
|
|
Magic: netmode.UnitTestNet,
|
|
|
|
MaxTraceableBlocks: MaxTraceableBlocks,
|
|
|
|
TimePerBlock: TimePerBlock,
|
|
|
|
StandbyCommittee: standByCommittee,
|
|
|
|
ValidatorsCount: 4,
|
|
|
|
VerifyTransactions: true,
|
|
|
|
},
|
2021-11-03 12:28:29 +00:00
|
|
|
}
|
2021-12-03 15:45:55 +00:00
|
|
|
if f != nil {
|
2022-12-06 13:34:38 +00:00
|
|
|
f(&cfg)
|
2021-12-03 15:45:55 +00:00
|
|
|
}
|
2022-03-22 16:54:27 +00:00
|
|
|
if st == nil {
|
|
|
|
st = storage.NewMemoryStore()
|
|
|
|
}
|
2021-11-03 12:28:29 +00:00
|
|
|
|
|
|
|
log := zaptest.NewLogger(t)
|
2022-12-06 13:34:38 +00:00
|
|
|
bc, err := core.NewBlockchain(st, cfg, log)
|
2022-03-22 16:54:27 +00:00
|
|
|
return bc, neotest.NewMultiSigner(multiValidatorAcc...), neotest.NewMultiSigner(multiCommitteeAcc...), err
|
2021-11-03 12:28:29 +00:00
|
|
|
}
|