2022-06-23 15:16:11 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2022-10-24 12:49:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-06-23 15:16:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2022-10-24 12:41:55 +00:00
|
|
|
const (
|
|
|
|
contractsPath = "../../../../../../neofs-contract/neofs-contract-v0.16.0.tar.gz"
|
|
|
|
protoFileName = "proto.yml"
|
|
|
|
)
|
2022-10-24 11:38:55 +00:00
|
|
|
|
2022-06-23 15:16:11 +00:00
|
|
|
func TestInitialize(t *testing.T) {
|
|
|
|
// This test needs neofs-contract tarball, so it is skipped by default.
|
|
|
|
// It is here for performing local testing after the changes.
|
|
|
|
t.Skip()
|
2022-10-24 12:41:55 +00:00
|
|
|
|
2022-10-24 11:38:55 +00:00
|
|
|
t.Run("4 nodes", func(t *testing.T) {
|
|
|
|
testInitialize(t, 4)
|
|
|
|
})
|
|
|
|
t.Run("7 nodes", func(t *testing.T) {
|
|
|
|
testInitialize(t, 7)
|
|
|
|
})
|
|
|
|
}
|
2022-06-23 15:16:11 +00:00
|
|
|
|
2022-10-24 11:38:55 +00:00
|
|
|
func testInitialize(t *testing.T, committeeSize int) {
|
2022-10-24 12:45:13 +00:00
|
|
|
testdataDir := t.TempDir()
|
2022-06-23 15:16:11 +00:00
|
|
|
v := viper.GetViper()
|
|
|
|
|
2022-10-24 12:41:55 +00:00
|
|
|
generateTestData(t, testdataDir, committeeSize)
|
|
|
|
v.Set(protoConfigPath, filepath.Join(testdataDir, protoFileName))
|
|
|
|
|
|
|
|
// Set to the path or remove the next statement to download from the network.
|
|
|
|
require.NoError(t, initCmd.Flags().Set(contractsInitFlag, contractsPath))
|
|
|
|
v.Set(localDumpFlag, filepath.Join(testdataDir, "out"))
|
|
|
|
v.Set(alphabetWalletsFlag, testdataDir)
|
|
|
|
v.Set(epochDurationInitFlag, 1)
|
|
|
|
v.Set(maxObjectSizeInitFlag, 1024)
|
|
|
|
|
|
|
|
setTestCredentials(v, committeeSize)
|
|
|
|
require.NoError(t, initializeSideChainCmd(initCmd, nil))
|
|
|
|
|
|
|
|
t.Run("force-new-epoch", func(t *testing.T) {
|
|
|
|
require.NoError(t, forceNewEpochCmd(forceNewEpoch, nil))
|
|
|
|
})
|
|
|
|
t.Run("set-config", func(t *testing.T) {
|
|
|
|
require.NoError(t, setConfigCmd(setConfig, []string{"MaintenanceModeAllowed=true"}))
|
|
|
|
})
|
2022-10-24 12:49:13 +00:00
|
|
|
t.Run("set-policy", func(t *testing.T) {
|
|
|
|
require.NoError(t, setPolicyCmd(setPolicy, []string{"ExecFeeFactor=1"}))
|
|
|
|
})
|
|
|
|
t.Run("remove-node", func(t *testing.T) {
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pub := hex.EncodeToString(pk.PublicKey().Bytes())
|
|
|
|
require.NoError(t, removeNodesCmd(removeNodes, []string{pub}))
|
|
|
|
})
|
2022-10-24 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestData(t *testing.T, dir string, size int) {
|
|
|
|
v := viper.GetViper()
|
|
|
|
v.Set(alphabetWalletsFlag, dir)
|
2022-10-24 11:38:55 +00:00
|
|
|
|
2022-10-24 12:41:55 +00:00
|
|
|
sizeStr := strconv.FormatUint(uint64(size), 10)
|
2022-10-24 11:38:55 +00:00
|
|
|
require.NoError(t, generateAlphabetCmd.Flags().Set(alphabetSizeFlag, sizeStr))
|
|
|
|
|
2022-10-24 12:41:55 +00:00
|
|
|
setTestCredentials(v, size)
|
2022-10-24 11:38:55 +00:00
|
|
|
require.NoError(t, generateAlphabetCreds(generateAlphabetCmd, nil))
|
2022-06-23 15:16:11 +00:00
|
|
|
|
|
|
|
var pubs []string
|
2022-10-24 12:41:55 +00:00
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
p := filepath.Join(dir, innerring.GlagoliticLetter(i).String()+".json")
|
2022-06-23 15:16:11 +00:00
|
|
|
w, err := wallet.NewWalletFromFile(p)
|
|
|
|
require.NoError(t, err, "wallet doesn't exist")
|
|
|
|
for _, acc := range w.Accounts {
|
|
|
|
if acc.Label == singleAccountName {
|
|
|
|
pub, ok := vm.ParseSignatureContract(acc.Contract.Script)
|
|
|
|
require.True(t, ok)
|
|
|
|
pubs = append(pubs, hex.EncodeToString(pub))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := config.Config{}
|
2022-10-24 12:41:55 +00:00
|
|
|
cfg.ProtocolConfiguration.ValidatorsCount = size
|
2022-06-23 15:16:11 +00:00
|
|
|
cfg.ProtocolConfiguration.SecondsPerBlock = 1
|
|
|
|
cfg.ProtocolConfiguration.StandbyCommittee = pubs // sorted by glagolic letters
|
|
|
|
cfg.ProtocolConfiguration.P2PSigExtensions = true
|
2022-10-24 10:56:39 +00:00
|
|
|
cfg.ProtocolConfiguration.VerifyTransactions = true
|
2022-06-23 15:16:11 +00:00
|
|
|
data, err := yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-24 12:41:55 +00:00
|
|
|
protoPath := filepath.Join(dir, protoFileName)
|
2022-08-15 07:29:30 +00:00
|
|
|
require.NoError(t, os.WriteFile(protoPath, data, os.ModePerm))
|
2022-10-24 12:41:55 +00:00
|
|
|
}
|
2022-06-23 15:16:11 +00:00
|
|
|
|
2022-10-24 12:41:55 +00:00
|
|
|
func setTestCredentials(v *viper.Viper, size int) {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
v.Set("credentials."+innerring.GlagoliticLetter(i).String(), strconv.FormatUint(uint64(i), 10))
|
|
|
|
}
|
|
|
|
v.Set("credentials.contract", testContractPassword)
|
2022-06-23 15:16:11 +00:00
|
|
|
}
|