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/wallet"
|
2021-03-03 09:37:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-02-02 09:34:27 +00:00
|
|
|
)
|
|
|
|
|
2021-03-03 09:37:06 +00:00
|
|
|
// Run runs service instance in a separate goroutine.
|
|
|
|
func (s *service) Run() {
|
|
|
|
s.chain.SubscribeForBlocks(s.blockCh)
|
|
|
|
go s.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) run() {
|
|
|
|
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:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the service.
|
|
|
|
func (s *service) Shutdown() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := s.getAccount()
|
|
|
|
if acc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sig := acc.PrivateKey().SignHash(r.GetSignedHash())
|
|
|
|
incRoot := s.getIncompleteRoot(r.Index)
|
|
|
|
incRoot.root = r
|
|
|
|
incRoot.addSignature(acc.PrivateKey().PublicKey(), sig)
|
|
|
|
incRoot.reverify()
|
|
|
|
|
|
|
|
s.accMtx.RLock()
|
|
|
|
myIndex := s.myIndex
|
|
|
|
s.accMtx.RUnlock()
|
2021-03-25 08:53:25 +00:00
|
|
|
msg := NewMessage(s.Network, 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
|
|
|
|
}
|
|
|
|
s.getRelayCallback()(&payload.Extensible{
|
|
|
|
Network: s.Network,
|
|
|
|
ValidBlockStart: r.Index,
|
|
|
|
ValidBlockEnd: r.Index + transaction.MaxValidUntilBlockIncrement,
|
|
|
|
Sender: s.getAccount().PrivateKey().GetScriptHash(),
|
|
|
|
Data: w.Bytes(),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) getAccount() *wallet.Account {
|
|
|
|
s.accMtx.RLock()
|
|
|
|
defer s.accMtx.RUnlock()
|
|
|
|
return s.acc
|
|
|
|
}
|