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"
|
2021-03-25 08:55:06 +00:00
|
|
|
"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"
|
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() {
|
2021-04-02 10:13:26 +00:00
|
|
|
s.log.Info("starting state validation service")
|
2021-03-03 09:37:06 +00:00
|
|
|
s.chain.SubscribeForBlocks(s.blockCh)
|
|
|
|
go s.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) run() {
|
2021-04-02 08:38:49 +00:00
|
|
|
runloop:
|
2021-03-03 09:37:06 +00:00
|
|
|
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:
|
2021-04-02 08:38:49 +00:00
|
|
|
break runloop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drainloop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.blockCh:
|
|
|
|
default:
|
|
|
|
break drainloop
|
2021-03-03 09:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the service.
|
|
|
|
func (s *service) Shutdown() {
|
2021-04-02 08:38:49 +00:00
|
|
|
s.chain.UnsubscribeFromBlocks(s.blockCh)
|
2021-03-03 09:37:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:11:55 +00:00
|
|
|
sig := acc.PrivateKey().SignHashable(uint32(s.Network), r)
|
2021-02-02 09:34:27 +00:00
|
|
|
incRoot := s.getIncompleteRoot(r.Index)
|
2021-05-06 18:53:34 +00:00
|
|
|
incRoot.Lock()
|
2021-02-02 09:34:27 +00:00
|
|
|
incRoot.root = r
|
|
|
|
incRoot.addSignature(acc.PrivateKey().PublicKey(), sig)
|
2021-03-25 19:11:55 +00:00
|
|
|
incRoot.reverify(s.Network)
|
2021-05-06 18:53:34 +00:00
|
|
|
incRoot.Unlock()
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
s.accMtx.RLock()
|
|
|
|
myIndex := s.myIndex
|
|
|
|
s.accMtx.RUnlock()
|
2021-03-25 19:11:55 +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
|
|
|
|
}
|
2021-03-25 08:55:06 +00:00
|
|
|
e := &payload.Extensible{
|
2021-03-26 19:16:46 +00:00
|
|
|
Category: Category,
|
2021-02-02 09:34:27 +00:00
|
|
|
ValidBlockStart: r.Index,
|
|
|
|
ValidBlockEnd: r.Index + transaction.MaxValidUntilBlockIncrement,
|
|
|
|
Sender: s.getAccount().PrivateKey().GetScriptHash(),
|
|
|
|
Data: w.Bytes(),
|
2021-03-25 08:55:06 +00:00
|
|
|
Witness: transaction.Witness{
|
|
|
|
VerificationScript: s.getAccount().GetVerificationScript(),
|
|
|
|
},
|
|
|
|
}
|
2021-03-25 18:59:54 +00:00
|
|
|
sig = acc.PrivateKey().SignHashable(uint32(s.Network), e)
|
2021-03-25 08:55:06 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Bytes(buf.BinWriter, sig)
|
|
|
|
e.Witness.InvocationScript = buf.Bytes()
|
2021-04-02 09:12:36 +00:00
|
|
|
s.onValidatedRoot(e)
|
2021-02-02 09:34:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) getAccount() *wallet.Account {
|
|
|
|
s.accMtx.RLock()
|
|
|
|
defer s.accMtx.RUnlock()
|
|
|
|
return s.acc
|
|
|
|
}
|