2021-01-26 16:37:19 +00:00
|
|
|
package notary
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/internal/fakechain"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-03-25 16:18:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2021-01-26 16:37:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mempool"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
2022-01-13 00:19:10 +00:00
|
|
|
func getTestNotary(t *testing.T, bc Ledger, walletPath, pass string) (*wallet.Account, *Notary, *mempool.Pool) {
|
2021-02-16 10:49:56 +00:00
|
|
|
mainCfg := config.P2PNotary{
|
2021-01-26 16:37:19 +00:00
|
|
|
Enabled: true,
|
|
|
|
UnlockWallet: config.Wallet{
|
|
|
|
Path: walletPath,
|
|
|
|
Password: pass,
|
|
|
|
},
|
|
|
|
}
|
2023-04-13 11:03:02 +00:00
|
|
|
mp := mempool.New(10, 1, true, nil)
|
2021-02-16 10:49:56 +00:00
|
|
|
cfg := Config{
|
|
|
|
MainCfg: mainCfg,
|
|
|
|
Chain: bc,
|
|
|
|
Log: zaptest.NewLogger(t),
|
|
|
|
}
|
2021-03-25 16:18:01 +00:00
|
|
|
ntr, err := NewNotary(cfg, netmode.UnitTestNet, mp, nil)
|
2021-01-26 16:37:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w, err := wallet.NewWalletFromFile(walletPath)
|
|
|
|
require.NoError(t, err)
|
2021-06-04 11:27:22 +00:00
|
|
|
require.NoError(t, w.Accounts[0].Decrypt(pass, w.Scrypt))
|
2021-01-26 16:37:19 +00:00
|
|
|
return w.Accounts[0], ntr, mp
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateNotaryNodes(t *testing.T) {
|
|
|
|
bc := fakechain.NewFakeChain()
|
|
|
|
acc, ntr, _ := getTestNotary(t, bc, "./testdata/notary1.json", "one")
|
|
|
|
randomKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
// currAcc is nil before UpdateNotaryNodes call
|
|
|
|
require.Nil(t, ntr.currAccount)
|
|
|
|
// set account for the first time
|
2022-09-01 14:52:44 +00:00
|
|
|
ntr.UpdateNotaryNodes(keys.PublicKeys{acc.PublicKey()})
|
2021-01-26 16:37:19 +00:00
|
|
|
require.Equal(t, acc, ntr.currAccount)
|
|
|
|
|
|
|
|
t.Run("account is already set", func(t *testing.T) {
|
2022-09-01 14:52:44 +00:00
|
|
|
ntr.UpdateNotaryNodes(keys.PublicKeys{acc.PublicKey(), randomKey.PublicKey()})
|
2021-01-26 16:37:19 +00:00
|
|
|
require.Equal(t, acc, ntr.currAccount)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("another account from the same wallet", func(t *testing.T) {
|
|
|
|
t.Run("good config password", func(t *testing.T) {
|
|
|
|
w, err := wallet.NewWalletFromFile("./testdata/notary1.json")
|
|
|
|
require.NoError(t, err)
|
2021-06-04 11:27:22 +00:00
|
|
|
require.NoError(t, w.Accounts[1].Decrypt("one", w.Scrypt))
|
2022-09-01 14:52:44 +00:00
|
|
|
ntr.UpdateNotaryNodes(keys.PublicKeys{w.Accounts[1].PublicKey()})
|
2021-01-26 16:37:19 +00:00
|
|
|
require.Equal(t, w.Accounts[1], ntr.currAccount)
|
|
|
|
})
|
|
|
|
t.Run("bad config password", func(t *testing.T) {
|
|
|
|
w, err := wallet.NewWalletFromFile("./testdata/notary1.json")
|
|
|
|
require.NoError(t, err)
|
2021-06-04 11:27:22 +00:00
|
|
|
require.NoError(t, w.Accounts[2].Decrypt("four", w.Scrypt))
|
2022-09-01 14:52:44 +00:00
|
|
|
ntr.UpdateNotaryNodes(keys.PublicKeys{w.Accounts[2].PublicKey()})
|
2021-01-26 16:37:19 +00:00
|
|
|
require.Nil(t, ntr.currAccount)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown account", func(t *testing.T) {
|
|
|
|
ntr.UpdateNotaryNodes(keys.PublicKeys{randomKey.PublicKey()})
|
|
|
|
require.Nil(t, ntr.currAccount)
|
|
|
|
})
|
|
|
|
}
|