neo-go/pkg/services/stateroot/validators.go

105 lines
2.4 KiB
Go
Raw Normal View History

2021-02-02 09:34:27 +00:00
package stateroot
import (
"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/io"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
2021-02-02 09:34:27 +00:00
"github.com/nspcc-dev/neo-go/pkg/wallet"
"go.uber.org/zap"
2021-02-02 09:34:27 +00:00
)
// Run runs service instance in a separate goroutine.
func (s *service) Run() {
s.log.Info("starting state validation service")
s.chain.SubscribeForBlocks(s.blockCh)
go s.run()
}
func (s *service) run() {
runloop:
for {
select {
case b := <-s.blockCh:
r, err := s.GetStateRoot(b.Index)
if err != nil {
s.log.Error("can't get state root for new block", zap.Error(err))
} else if err := s.signAndSend(r); err != nil {
s.log.Error("can't sign or send state root", zap.Error(err))
}
case <-s.done:
break runloop
}
}
drainloop:
for {
select {
case <-s.blockCh:
default:
break drainloop
}
}
}
// Shutdown stops the service.
func (s *service) Shutdown() {
s.chain.UnsubscribeFromBlocks(s.blockCh)
close(s.done)
}
2021-02-02 09:34:27 +00:00
func (s *service) signAndSend(r *state.MPTRoot) error {
if !s.MainCfg.Enabled {
return nil
}
myIndex, acc := s.getAccount()
2021-02-02 09:34:27 +00:00
if acc == nil {
return nil
}
sig := acc.PrivateKey().SignHashable(uint32(s.Network), r)
2021-02-02 09:34:27 +00:00
incRoot := s.getIncompleteRoot(r.Index)
incRoot.Lock()
2021-02-02 09:34:27 +00:00
incRoot.root = r
incRoot.addSignature(acc.PrivateKey().PublicKey(), sig)
incRoot.reverify(s.Network)
s.trySendRoot(incRoot, acc)
incRoot.Unlock()
2021-02-02 09:34:27 +00:00
msg := NewMessage(VoteT, &Vote{
2021-02-02 09:34:27 +00:00
ValidatorIndex: int32(myIndex),
Height: r.Index,
Signature: sig,
})
w := io.NewBufBinWriter()
msg.EncodeBinary(w.BinWriter)
if w.Err != nil {
return w.Err
}
e := &payload.Extensible{
Category: Category,
2021-02-02 09:34:27 +00:00
ValidBlockStart: r.Index,
ValidBlockEnd: r.Index + transaction.MaxValidUntilBlockIncrement,
Sender: acc.PrivateKey().GetScriptHash(),
2021-02-02 09:34:27 +00:00
Data: w.Bytes(),
Witness: transaction.Witness{
VerificationScript: acc.GetVerificationScript(),
},
}
sig = acc.PrivateKey().SignHashable(uint32(s.Network), e)
buf := io.NewBufBinWriter()
emit.Bytes(buf.BinWriter, sig)
e.Witness.InvocationScript = buf.Bytes()
s.relayExtensible(e)
2021-02-02 09:34:27 +00:00
return nil
}
// getAccount returns current index and account for the node running this service.
func (s *service) getAccount() (byte, *wallet.Account) {
2021-02-02 09:34:27 +00:00
s.accMtx.RLock()
defer s.accMtx.RUnlock()
return s.myIndex, s.acc
2021-02-02 09:34:27 +00:00
}