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.
|
2022-07-04 20:03:50 +00:00
|
|
|
// The service only starts once, subsequent calls to Start are no-op.
|
2022-01-12 01:11:21 +00:00
|
|
|
func (s *service) Start() {
|
2023-04-27 15:49:19 +00:00
|
|
|
if !s.started.CompareAndSwap(false, true) {
|
2022-07-04 07:55:53 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-02 10:13:26 +00:00
|
|
|
s.log.Info("starting state validation service")
|
2021-03-03 09:37:06 +00:00
|
|
|
go s.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) run() {
|
2024-02-22 13:50:58 +00:00
|
|
|
s.chain.SubscribeForBlocks(s.blockCh)
|
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()
|
2022-07-04 07:59:34 +00:00
|
|
|
case <-s.stopCh:
|
2021-04-02 08:38:49 +00:00
|
|
|
break runloop
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 07:59:34 +00:00
|
|
|
s.chain.UnsubscribeFromBlocks(s.blockCh)
|
2021-04-02 08:38:49 +00:00
|
|
|
drainloop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.blockCh:
|
|
|
|
default:
|
|
|
|
break drainloop
|
2021-03-03 09:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-28 15:07:22 +00:00
|
|
|
close(s.blockCh)
|
2022-07-04 07:59:34 +00:00
|
|
|
close(s.done)
|
2021-03-03 09:37:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 20:03:50 +00:00
|
|
|
// Shutdown stops the service. It can only be called once, subsequent calls
|
|
|
|
// to Shutdown on the same instance are no-op. The instance that was stopped can
|
|
|
|
// not be started again by calling Start (use a new instance if needed).
|
2021-03-03 09:37:06 +00:00
|
|
|
func (s *service) Shutdown() {
|
2023-04-27 15:49:19 +00:00
|
|
|
if !s.started.CompareAndSwap(true, false) {
|
2022-07-04 07:55:53 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-26 19:27:36 +00:00
|
|
|
s.log.Info("stopping state validation service")
|
2022-07-04 07:59:34 +00:00
|
|
|
close(s.stopCh)
|
|
|
|
<-s.done
|
stateroot: fix panic on shutdown
Stateroot service is always active, but it might have no wallet.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xc57d41]
goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/wallet.(*Wallet).Close(...)
github.com/nspcc-dev/neo-go/pkg/wallet/wallet.go:175
github.com/nspcc-dev/neo-go/pkg/services/stateroot.(*service).Shutdown(0xc000105880?)
github.com/nspcc-dev/neo-go/pkg/services/stateroot/validators.go:77 +0x81
github.com/nspcc-dev/neo-go/pkg/network.(*Server).Shutdown(0xc000105880)
github.com/nspcc-dev/neo-go/pkg/network/server.go:271 +0x205
github.com/nspcc-dev/neo-go/cli/server.startServer(0xc0002702c0)
github.com/nspcc-dev/neo-go/cli/server/server.go:641 +0x2675
github.com/urfave/cli.HandleAction({0xe456e0?, 0x1155f20?}, 0x4?)
github.com/urfave/cli@v1.22.5/app.go:524 +0x50
github.com/urfave/cli.Command.Run({{0xfca38b, 0x4}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0xfd6a46, 0x10}, {0xffebe3, ...}, ...}, ...)
github.com/urfave/cli@v1.22.5/command.go:173 +0x65b
github.com/urfave/cli.(*App).Run(0xc000272000, {0xc00003e180, 0x3, 0x3})
github.com/urfave/cli@v1.22.5/app.go:277 +0x8a7
main.main()
./main.go:21 +0x33
2022-09-13 10:18:13 +00:00
|
|
|
if s.wallet != nil {
|
|
|
|
s.wallet.Close()
|
|
|
|
}
|
2024-02-18 12:29:04 +00:00
|
|
|
_ = s.log.Sync()
|
2021-03-03 09:37:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-01 17:42:42 +00:00
|
|
|
sig := acc.SignHashable(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
|
2022-09-01 14:52:44 +00:00
|
|
|
incRoot.addSignature(acc.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,
|
2022-09-01 16:04:47 +00:00
|
|
|
Sender: acc.ScriptHash(),
|
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
|
|
|
},
|
|
|
|
}
|
2022-09-01 17:42:42 +00:00
|
|
|
sig = acc.SignHashable(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
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// sendVote attempts to send a vote if it's still valid and if stateroot message
|
|
|
|
// has not been sent yet. It must be called with the ir locked.
|
2021-05-07 09:19:48 +00:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// getAccount returns the current index and account for the node running this service.
|
2021-05-06 19:17:09 +00:00
|
|
|
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
|
|
|
}
|