github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/services/notary/node_test.go (about)

     1  package notary
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nspcc-dev/neo-go/internal/fakechain"
     7  	"github.com/nspcc-dev/neo-go/pkg/config"
     8  	"github.com/nspcc-dev/neo-go/pkg/config/netmode"
     9  	"github.com/nspcc-dev/neo-go/pkg/core/mempool"
    10  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
    11  	"github.com/nspcc-dev/neo-go/pkg/wallet"
    12  	"github.com/stretchr/testify/require"
    13  	"go.uber.org/zap/zaptest"
    14  )
    15  
    16  func getTestNotary(t *testing.T, bc Ledger, walletPath, pass string) (*wallet.Account, *Notary, *mempool.Pool) {
    17  	mainCfg := config.P2PNotary{
    18  		Enabled: true,
    19  		UnlockWallet: config.Wallet{
    20  			Path:     walletPath,
    21  			Password: pass,
    22  		},
    23  	}
    24  	mp := mempool.New(10, 1, true, nil)
    25  	cfg := Config{
    26  		MainCfg: mainCfg,
    27  		Chain:   bc,
    28  		Log:     zaptest.NewLogger(t),
    29  	}
    30  	ntr, err := NewNotary(cfg, netmode.UnitTestNet, mp, nil)
    31  	require.NoError(t, err)
    32  
    33  	w, err := wallet.NewWalletFromFile(walletPath)
    34  	require.NoError(t, err)
    35  	require.NoError(t, w.Accounts[0].Decrypt(pass, w.Scrypt))
    36  	return w.Accounts[0], ntr, mp
    37  }
    38  
    39  func TestUpdateNotaryNodes(t *testing.T) {
    40  	bc := fakechain.NewFakeChain()
    41  	acc, ntr, _ := getTestNotary(t, bc, "./testdata/notary1.json", "one")
    42  	randomKey, err := keys.NewPrivateKey()
    43  	require.NoError(t, err)
    44  	// currAcc is nil before UpdateNotaryNodes call
    45  	require.Nil(t, ntr.currAccount)
    46  	// set account for the first time
    47  	ntr.UpdateNotaryNodes(keys.PublicKeys{acc.PublicKey()})
    48  	require.Equal(t, acc, ntr.currAccount)
    49  
    50  	t.Run("account is already set", func(t *testing.T) {
    51  		ntr.UpdateNotaryNodes(keys.PublicKeys{acc.PublicKey(), randomKey.PublicKey()})
    52  		require.Equal(t, acc, ntr.currAccount)
    53  	})
    54  
    55  	t.Run("another account from the same wallet", func(t *testing.T) {
    56  		t.Run("good config password", func(t *testing.T) {
    57  			w, err := wallet.NewWalletFromFile("./testdata/notary1.json")
    58  			require.NoError(t, err)
    59  			require.NoError(t, w.Accounts[1].Decrypt("one", w.Scrypt))
    60  			ntr.UpdateNotaryNodes(keys.PublicKeys{w.Accounts[1].PublicKey()})
    61  			require.Equal(t, w.Accounts[1], ntr.currAccount)
    62  		})
    63  		t.Run("bad config password", func(t *testing.T) {
    64  			w, err := wallet.NewWalletFromFile("./testdata/notary1.json")
    65  			require.NoError(t, err)
    66  			require.NoError(t, w.Accounts[2].Decrypt("four", w.Scrypt))
    67  			ntr.UpdateNotaryNodes(keys.PublicKeys{w.Accounts[2].PublicKey()})
    68  			require.Nil(t, ntr.currAccount)
    69  		})
    70  	})
    71  
    72  	t.Run("unknown account", func(t *testing.T) {
    73  		ntr.UpdateNotaryNodes(keys.PublicKeys{randomKey.PublicKey()})
    74  		require.Nil(t, ntr.currAccount)
    75  	})
    76  }