stateroot: simplify account locking, deduplicate code

This commit is contained in:
Roman Khimov 2021-05-06 22:17:09 +03:00
parent fc800dcbc1
commit 222b4dc920
2 changed files with 12 additions and 13 deletions

View file

@ -7,10 +7,10 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/zap"
)
@ -22,7 +22,7 @@ func (s *service) AddSignature(height uint32, validatorIndex int32, sig []byte)
if !s.MainCfg.Enabled {
return nil
}
acc := s.getAccount()
_, acc := s.getAccount()
if acc == nil {
return nil
}
@ -55,7 +55,7 @@ func (s *service) AddSignature(height uint32, validatorIndex int32, sig []byte)
if err != nil {
s.log.Error("can't add validated state root", zap.Error(err))
}
s.sendValidatedRoot(sr, acc.PrivateKey())
s.sendValidatedRoot(sr, acc)
}
return nil
}
@ -76,7 +76,8 @@ func (s *service) getIncompleteRoot(height uint32) *incompleteRoot {
return incRoot
}
func (s *service) sendValidatedRoot(r *state.MPTRoot, priv *keys.PrivateKey) {
func (s *service) sendValidatedRoot(r *state.MPTRoot, acc *wallet.Account) {
priv := acc.PrivateKey()
w := io.NewBufBinWriter()
m := NewMessage(RootT, r)
m.EncodeBinary(w.BinWriter)
@ -87,7 +88,7 @@ func (s *service) sendValidatedRoot(r *state.MPTRoot, priv *keys.PrivateKey) {
Sender: priv.GetScriptHash(),
Data: w.Bytes(),
Witness: transaction.Witness{
VerificationScript: s.getAccount().GetVerificationScript(),
VerificationScript: acc.GetVerificationScript(),
},
}
sig := priv.SignHashable(uint32(s.Network), ep)

View file

@ -53,7 +53,7 @@ func (s *service) signAndSend(r *state.MPTRoot) error {
return nil
}
acc := s.getAccount()
myIndex, acc := s.getAccount()
if acc == nil {
return nil
}
@ -66,9 +66,6 @@ func (s *service) signAndSend(r *state.MPTRoot) error {
incRoot.reverify(s.Network)
incRoot.Unlock()
s.accMtx.RLock()
myIndex := s.myIndex
s.accMtx.RUnlock()
msg := NewMessage(VoteT, &Vote{
ValidatorIndex: int32(myIndex),
Height: r.Index,
@ -84,10 +81,10 @@ func (s *service) signAndSend(r *state.MPTRoot) error {
Category: Category,
ValidBlockStart: r.Index,
ValidBlockEnd: r.Index + transaction.MaxValidUntilBlockIncrement,
Sender: s.getAccount().PrivateKey().GetScriptHash(),
Sender: acc.PrivateKey().GetScriptHash(),
Data: w.Bytes(),
Witness: transaction.Witness{
VerificationScript: s.getAccount().GetVerificationScript(),
VerificationScript: acc.GetVerificationScript(),
},
}
sig = acc.PrivateKey().SignHashable(uint32(s.Network), e)
@ -98,8 +95,9 @@ func (s *service) signAndSend(r *state.MPTRoot) error {
return nil
}
func (s *service) getAccount() *wallet.Account {
// getAccount returns current index and account for the node running this service.
func (s *service) getAccount() (byte, *wallet.Account) {
s.accMtx.RLock()
defer s.accMtx.RUnlock()
return s.acc
return s.myIndex, s.acc
}