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
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:17:09 +00:00
|
|
|
myIndex, acc := s.getAccount()
|
2021-02-02 09:34:27 +00:00
|
|
|
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 20:29:43 +00:00
|
|
|
s.trySendRoot(incRoot, acc)
|
2021-05-06 18:53:34 +00:00
|
|
|
incRoot.Unlock()
|
2021-02-02 09:34:27 +00:00
|
|
|
|
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,
|
2021-05-06 19:17:09 +00:00
|
|
|
Sender: acc.PrivateKey().GetScriptHash(),
|
2021-02-02 09:34:27 +00:00
|
|
|
Data: w.Bytes(),
|
2021-03-25 08:55:06 +00:00
|
|
|
Witness: transaction.Witness{
|
2021-05-06 19:17:09 +00:00
|
|
|
VerificationScript: acc.GetVerificationScript(),
|
2021-03-25 08:55:06 +00:00
|
|
|
},
|
|
|
|
}
|
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-05-06 19:29:19 +00:00
|
|
|
s.relayExtensible(e)
|
2021-02-02 09:34:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:17:09 +00:00
|
|
|
// 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()
|
2021-05-06 19:17:09 +00:00
|
|
|
return s.myIndex, s.acc
|
2021-02-02 09:34:27 +00:00
|
|
|
}
|