neo-go/pkg/services/notary/node.go
Roman Khimov c07604fbf3 notary: simplify key presence check with slices
Signed-off-by: Roman Khimov <roman@nspcc.ru>
2024-08-27 12:51:12 +03:00

52 lines
1.2 KiB
Go

package notary
import (
"slices"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/zap"
)
// UpdateNotaryNodes implements Notary interface and updates current notary account.
func (n *Notary) UpdateNotaryNodes(notaryNodes keys.PublicKeys) {
n.accMtx.Lock()
defer n.accMtx.Unlock()
if n.currAccount != nil && slices.ContainsFunc(notaryNodes, n.currAccount.PublicKey().Equal) {
return
}
var acc *wallet.Account
for _, node := range notaryNodes {
acc = n.wallet.GetAccount(node.GetScriptHash())
if acc != nil {
if acc.CanSign() {
break
}
err := acc.Decrypt(n.Config.MainCfg.UnlockWallet.Password, n.wallet.Scrypt)
if err != nil {
n.Config.Log.Warn("can't unlock notary node account",
zap.String("address", address.Uint160ToString(acc.Contract.ScriptHash())),
zap.Error(err))
acc = nil
}
break
}
}
n.currAccount = acc
if acc == nil {
n.reqMtx.Lock()
n.requests = make(map[util.Uint256]*request)
n.reqMtx.Unlock()
}
}
func (n *Notary) getAccount() *wallet.Account {
n.accMtx.RLock()
defer n.accMtx.RUnlock()
return n.currAccount
}