2021-02-01 16:00:07 +00:00
|
|
|
package stateroot
|
|
|
|
|
|
|
|
import (
|
2021-02-02 09:34:27 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
2021-05-07 09:19:48 +00:00
|
|
|
"time"
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2021-03-03 09:37:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-03-26 21:32:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/stateroot"
|
2021-02-02 09:34:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2021-02-02 09:34:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"go.uber.org/zap"
|
2021-02-01 16:00:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Service represents state root service.
|
|
|
|
Service interface {
|
|
|
|
blockchainer.StateRoot
|
|
|
|
OnPayload(p *payload.Extensible) error
|
2021-02-02 09:34:27 +00:00
|
|
|
AddSignature(height uint32, validatorIndex int32, sig []byte) error
|
|
|
|
GetConfig() config.StateRoot
|
2021-03-03 09:37:06 +00:00
|
|
|
Run()
|
|
|
|
Shutdown()
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
service struct {
|
|
|
|
blockchainer.StateRoot
|
2021-03-03 09:37:06 +00:00
|
|
|
chain blockchainer.Blockchainer
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
MainCfg config.StateRoot
|
|
|
|
Network netmode.Magic
|
|
|
|
|
2021-03-03 09:37:06 +00:00
|
|
|
log *zap.Logger
|
|
|
|
accMtx sync.RWMutex
|
|
|
|
accHeight uint32
|
|
|
|
myIndex byte
|
|
|
|
wallet *wallet.Wallet
|
|
|
|
acc *wallet.Account
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
srMtx sync.Mutex
|
|
|
|
incompleteRoots map[uint32]*incompleteRoot
|
|
|
|
|
2021-05-07 09:19:48 +00:00
|
|
|
timePerBlock time.Duration
|
|
|
|
maxRetries int
|
2021-05-06 19:29:19 +00:00
|
|
|
relayExtensible RelayCallback
|
2021-03-03 09:37:06 +00:00
|
|
|
blockCh chan *block.Block
|
|
|
|
done chan struct{}
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Category is message category for extensible payloads.
|
|
|
|
Category = "StateService"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns new state root service instance using underlying module.
|
2021-04-02 09:12:36 +00:00
|
|
|
func New(cfg config.StateRoot, log *zap.Logger, bc blockchainer.Blockchainer, cb RelayCallback) (Service, error) {
|
2021-05-07 09:19:48 +00:00
|
|
|
bcConf := bc.GetConfig()
|
2021-02-02 09:34:27 +00:00
|
|
|
s := &service{
|
2021-03-03 09:37:06 +00:00
|
|
|
StateRoot: bc.GetStateModule(),
|
2021-05-07 09:19:48 +00:00
|
|
|
Network: bcConf.Magic,
|
2021-03-03 09:37:06 +00:00
|
|
|
chain: bc,
|
2021-02-02 09:34:27 +00:00
|
|
|
log: log,
|
|
|
|
incompleteRoots: make(map[uint32]*incompleteRoot),
|
2021-03-03 09:37:06 +00:00
|
|
|
blockCh: make(chan *block.Block),
|
|
|
|
done: make(chan struct{}),
|
2021-05-07 09:19:48 +00:00
|
|
|
timePerBlock: time.Duration(bcConf.SecondsPerBlock) * time.Second,
|
2021-05-07 09:22:29 +00:00
|
|
|
maxRetries: voteValidEndInc,
|
2021-05-06 19:29:19 +00:00
|
|
|
relayExtensible: cb,
|
2021-02-02 09:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.MainCfg = cfg
|
|
|
|
if cfg.Enabled {
|
|
|
|
var err error
|
|
|
|
w := cfg.UnlockWallet
|
|
|
|
if s.wallet, err = wallet.NewWalletFromFile(w.Path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
haveAccount := false
|
|
|
|
for _, acc := range s.wallet.Accounts {
|
2021-06-04 11:27:22 +00:00
|
|
|
if err := acc.Decrypt(w.Password, s.wallet.Scrypt); err == nil {
|
2021-02-02 09:34:27 +00:00
|
|
|
haveAccount = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !haveAccount {
|
|
|
|
return nil, errors.New("no wallet account could be unlocked")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.SetUpdateValidatorsCallback(s.updateValidators)
|
|
|
|
}
|
|
|
|
return s, nil
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnPayload implements Service interface.
|
|
|
|
func (s *service) OnPayload(ep *payload.Extensible) error {
|
2021-03-25 19:11:55 +00:00
|
|
|
m := &Message{}
|
2021-02-01 16:00:07 +00:00
|
|
|
r := io.NewBinReaderFromBuf(ep.Data)
|
|
|
|
m.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
switch m.Type {
|
|
|
|
case RootT:
|
|
|
|
sr := m.Payload.(*state.MPTRoot)
|
|
|
|
if sr.Index == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-26 21:32:15 +00:00
|
|
|
err := s.AddStateRoot(sr)
|
|
|
|
if errors.Is(err, stateroot.ErrStateMismatch) {
|
|
|
|
s.log.Error("can't add SV-signed state root", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-07 10:19:40 +00:00
|
|
|
s.srMtx.Lock()
|
|
|
|
ir, ok := s.incompleteRoots[sr.Index]
|
|
|
|
s.srMtx.Unlock()
|
|
|
|
if ok {
|
|
|
|
ir.Lock()
|
|
|
|
ir.isSent = true
|
|
|
|
ir.Unlock()
|
|
|
|
}
|
2021-03-26 21:32:15 +00:00
|
|
|
return err
|
2021-02-02 09:34:27 +00:00
|
|
|
case VoteT:
|
|
|
|
v := m.Payload.(*Vote)
|
|
|
|
return s.AddSignature(v.Height, v.ValidatorIndex, v.Signature)
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-02 09:34:27 +00:00
|
|
|
|
2021-03-03 09:37:06 +00:00
|
|
|
func (s *service) updateValidators(height uint32, pubs keys.PublicKeys) {
|
2021-02-02 09:34:27 +00:00
|
|
|
s.accMtx.Lock()
|
|
|
|
defer s.accMtx.Unlock()
|
|
|
|
|
|
|
|
s.acc = nil
|
|
|
|
for i := range pubs {
|
|
|
|
if acc := s.wallet.GetAccount(pubs[i].GetScriptHash()); acc != nil {
|
2021-06-04 11:27:22 +00:00
|
|
|
err := acc.Decrypt(s.MainCfg.UnlockWallet.Password, s.wallet.Scrypt)
|
2021-02-02 09:34:27 +00:00
|
|
|
if err == nil {
|
|
|
|
s.acc = acc
|
2021-03-03 09:37:06 +00:00
|
|
|
s.accHeight = height
|
2021-02-02 09:34:27 +00:00
|
|
|
s.myIndex = byte(i)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|