neotest: allow to use 6-node committee

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-11-03 15:28:29 +03:00
parent bef2a6f7ae
commit 79a48a7800
4 changed files with 145 additions and 6 deletions

View file

@ -35,8 +35,8 @@ type Executor struct {
// NewExecutor creates new executor instance from provided blockchain and committee. // NewExecutor creates new executor instance from provided blockchain and committee.
func NewExecutor(t *testing.T, bc blockchainer.Blockchainer, validator, committee Signer) *Executor { func NewExecutor(t *testing.T, bc blockchainer.Blockchainer, validator, committee Signer) *Executor {
require.Equal(t, 1, len(bc.GetConfig().StandbyCommittee)) checkMultiSigner(t, validator)
require.IsType(t, multiSigner{}, committee, "committee must be a multi-signer") checkMultiSigner(t, committee)
return &Executor{ return &Executor{
Chain: bc, Chain: bc,

View file

@ -2,6 +2,7 @@ package chain
import ( import (
"encoding/hex" "encoding/hex"
"sort"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config"
@ -10,23 +11,102 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
const validatorWIF = "KxyjQ8eUa4FHt3Gvioyt1Wz29cTUrE4eTqX3yFSk1YFCsPL8uNsY" const singleValidatorWIF = "KxyjQ8eUa4FHt3Gvioyt1Wz29cTUrE4eTqX3yFSk1YFCsPL8uNsY"
// committeeAcc is an account used to sign tx as a committee. // committeeWIFs is a list of unencrypted WIFs sorted by public key.
var committeeAcc *wallet.Account var committeeWIFs = []string{
"KzfPUYDC9n2yf4fK5ro4C8KMcdeXtFuEnStycbZgX3GomiUsvX6W",
"KzgWE3u3EDp13XPXXuTKZxeJ3Gi8Bsm8f9ijY3ZsCKKRvZUo1Cdn",
singleValidatorWIF,
"L2oEXKRAAMiPEZukwR5ho2S6SMeQLhcK9mF71ZnF7GvT8dU4Kkgz",
// Provide 2 committee extra members so that committee address differs from
// the validators one.
"L1Tr1iq5oz1jaFaMXP21sHDkJYDDkuLtpvQ4wRf1cjKvJYvnvpAb",
"Kz6XTUrExy78q8f4MjDHnwz8fYYyUE8iPXwPRAkHa3qN2JcHYm7e",
}
var (
// committeeAcc is an account used to sign tx as a committee.
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
// standByCommittee contains list of committee public keys to use in config.
standByCommittee []string
)
func init() { func init() {
committeeAcc, _ = wallet.NewAccountFromWIF(validatorWIF) committeeAcc, _ = wallet.NewAccountFromWIF(singleValidatorWIF)
pubs := keys.PublicKeys{committeeAcc.PrivateKey().PublicKey()} pubs := keys.PublicKeys{committeeAcc.PrivateKey().PublicKey()}
err := committeeAcc.ConvertMultisig(1, pubs) err := committeeAcc.ConvertMultisig(1, pubs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
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])
pubs[i] = accs[i].PrivateKey().PublicKey()
}
// 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())
multiValidatorAcc = make([]*wallet.Account, mv)
sort.Sort(pubs[:4])
vloop:
for i := 0; i < mv; i++ {
for j := range accs {
if accs[j].PrivateKey().PublicKey().Equal(pubs[i]) {
multiValidatorAcc[i] = wallet.NewAccountFromPrivateKey(accs[j].PrivateKey())
err := multiValidatorAcc[i].ConvertMultisig(mv, pubs[:4])
if err != nil {
panic(err)
}
continue vloop
}
}
panic("invalid committee WIFs")
}
multiCommitteeAcc = make([]*wallet.Account, mc)
sort.Sort(pubs)
cloop:
for i := 0; i < mc; i++ {
for j := range accs {
if accs[j].PrivateKey().PublicKey().Equal(pubs[i]) {
multiCommitteeAcc[i] = wallet.NewAccountFromPrivateKey(accs[j].PrivateKey())
err := multiCommitteeAcc[i].ConvertMultisig(mc, pubs)
if err != nil {
panic(err)
}
continue cloop
}
}
panic("invalid committee WIFs")
}
} }
// NewSingle creates new blockchain instance with a single validator and // NewSingle creates new blockchain instance with a single validator and
@ -49,3 +129,24 @@ func NewSingle(t *testing.T) (*core.Blockchain, neotest.Signer) {
t.Cleanup(bc.Close) t.Cleanup(bc.Close)
return bc, neotest.NewMultiSigner(committeeAcc) return bc, neotest.NewMultiSigner(committeeAcc)
} }
// NewMulti creates new blockchain instance with 4 validators and 6 committee members.
// Second return value is for validator signer, third -- for committee.
func NewMulti(t *testing.T) (*core.Blockchain, neotest.Signer, neotest.Signer) {
protoCfg := config.ProtocolConfiguration{
Magic: netmode.UnitTestNet,
SecondsPerBlock: 1,
StandbyCommittee: standByCommittee,
ValidatorsCount: 4,
VerifyBlocks: true,
VerifyTransactions: true,
}
st := storage.NewMemoryStore()
log := zaptest.NewLogger(t)
bc, err := core.NewBlockchain(st, protoCfg, log)
require.NoError(t, err)
go bc.Run()
t.Cleanup(bc.Close)
return bc, neotest.NewMultiSigner(multiValidatorAcc...), neotest.NewMultiSigner(multiCommitteeAcc...)
}

View file

@ -0,0 +1,21 @@
package chain
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/stretchr/testify/require"
)
// TestNewMulti checks that transaction and block is signed correctly for multi-node setup.
func TestNewMulti(t *testing.T) {
bc, vAcc, cAcc := NewMulti(t)
e := neotest.NewExecutor(t, bc, vAcc, cAcc)
require.NotEqual(t, vAcc.ScriptHash(), cAcc.ScriptHash())
const amount = int64(10_0000_0000)
c := e.CommitteeInvoker(bc.UtilityTokenHash()).WithSigners(vAcc)
c.Invoke(t, true, "transfer", e.Validator.ScriptHash(), e.Committee.ScriptHash(), amount, nil)
}

View file

@ -3,6 +3,7 @@ package neotest
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"testing"
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -11,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
) )
// Signer is a generic interface which can be either simple- or multi-signature signer. // Signer is a generic interface which can be either simple- or multi-signature signer.
@ -122,3 +124,18 @@ func (m multiSigner) SignTx(magic netmode.Magic, tx *transaction.Transaction) er
}) })
return nil return nil
} }
func checkMultiSigner(t *testing.T, s Signer) {
accs, ok := s.(multiSigner)
require.True(t, ok, "expected to be a multi-signer")
require.True(t, len(accs) > 0, "empty multi-signer")
m := len(accs[0].Contract.Parameters)
require.True(t, m <= len(accs), "honest not count is too big for a multi-signer")
h := accs[0].Contract.ScriptHash()
for i := 1; i < len(accs); i++ {
require.Equal(t, m, len(accs[i].Contract.Parameters), "inconsistent multi-signer accounts")
require.Equal(t, h, accs[i].Contract.ScriptHash(), "inconsistent multi-signer accounts")
}
}