2021-02-02 09:34:27 +00:00
|
|
|
package stateroot
|
|
|
|
|
|
|
|
import (
|
2021-05-07 09:19:48 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-02 09:34:27 +00:00
|
|
|
"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-05-07 09:22:29 +00:00
|
|
|
const (
|
|
|
|
voteValidEndInc = 10
|
|
|
|
firstVoteResendDelay = 3 * time.Second
|
|
|
|
)
|
2021-05-07 09:19:48 +00:00
|
|
|
|
2022-04-22 08:33:56 +00:00
|
|
|
// Name returns service name.
|
|
|
|
func (s *service) Name() string {
|
|
|
|
return "stateroot"
|
|
|
|
}
|
|
|
|
|
2022-01-12 01:11:21 +00:00
|
|
|
// Start runs service instance in a separate goroutine.
|
|
|
|
func (s *service) Start() {
|
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))
|
|
|
|
}
|
2021-05-07 10:23:49 +00:00
|
|
|
s.srMtx.Lock()
|
|
|
|
delete(s.incompleteRoots, b.Index-voteValidEndInc)
|
|
|
|
s.srMtx.Unlock()
|
2021-03-03 09:37:06 +00:00
|
|
|
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-05-07 10:19:40 +00:00
|
|
|
incRoot := s.getIncompleteRoot(r.Index, myIndex)
|
2021-05-06 18:53:34 +00:00
|
|
|
incRoot.Lock()
|
2021-05-07 09:19:48 +00:00
|
|
|
defer incRoot.Unlock()
|
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-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,
|
2021-05-07 09:22:29 +00:00
|
|
|
ValidBlockEnd: r.Index + voteValidEndInc,
|
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-07 09:19:48 +00:00
|
|
|
incRoot.myVote = e
|
|
|
|
incRoot.retries = -1
|
|
|
|
s.sendVote(incRoot)
|
2021-02-02 09:34:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:19:48 +00:00
|
|
|
// sendVote attempts to send vote if it's still valid and if stateroot message
|
|
|
|
// was not sent yet. It must be called with ir locked.
|
|
|
|
func (s *service) sendVote(ir *incompleteRoot) {
|
|
|
|
if ir.isSent || ir.retries >= s.maxRetries ||
|
|
|
|
s.chain.HeaderHeight() >= ir.myVote.ValidBlockEnd {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.relayExtensible(ir.myVote)
|
|
|
|
delay := firstVoteResendDelay
|
|
|
|
if ir.retries > 0 {
|
|
|
|
delay = s.timePerBlock << ir.retries
|
|
|
|
}
|
|
|
|
_ = time.AfterFunc(delay, func() {
|
|
|
|
ir.Lock()
|
|
|
|
s.sendVote(ir)
|
|
|
|
ir.Unlock()
|
|
|
|
})
|
|
|
|
ir.retries++
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|