2021-01-29 14:33:24 +00:00
|
|
|
package stateroot
|
|
|
|
|
|
|
|
import (
|
2021-02-01 16:00:07 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2021-01-29 14:33:24 +00:00
|
|
|
"fmt"
|
2021-02-01 16:00:07 +00:00
|
|
|
"sync"
|
2022-01-29 08:28:29 +00:00
|
|
|
"time"
|
2021-01-29 14:33:24 +00:00
|
|
|
|
2022-01-13 01:34:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-03-25 08:53:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2021-01-29 14:33:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mpt"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2022-01-13 01:34:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-01-29 14:33:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-01-13 01:34:14 +00:00
|
|
|
// VerifierFunc is a function that allows to check witness of account
|
|
|
|
// for Hashable item with GAS limit.
|
|
|
|
VerifierFunc func(util.Uint160, hash.Hashable, *transaction.Witness, int64) (int64, error)
|
2021-01-29 14:33:24 +00:00
|
|
|
// Module represents module for local processing of state roots.
|
|
|
|
Module struct {
|
2022-01-13 01:34:14 +00:00
|
|
|
Store *storage.MemCachedStore
|
|
|
|
network netmode.Magic
|
|
|
|
srInHead bool
|
2022-01-28 08:56:33 +00:00
|
|
|
mode mpt.TrieMode
|
2022-01-13 01:34:14 +00:00
|
|
|
mpt *mpt.Trie
|
|
|
|
verifier VerifierFunc
|
|
|
|
log *zap.Logger
|
2021-01-29 14:33:24 +00:00
|
|
|
|
2021-02-01 16:00:07 +00:00
|
|
|
currentLocal atomic.Value
|
2021-01-29 14:33:24 +00:00
|
|
|
localHeight atomic.Uint32
|
|
|
|
validatedHeight atomic.Uint32
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
keys []keyCache
|
2021-02-02 09:34:27 +00:00
|
|
|
|
2021-03-03 09:37:06 +00:00
|
|
|
updateValidatorsCb func(height uint32, publicKeys keys.PublicKeys)
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyCache struct {
|
|
|
|
height uint32
|
|
|
|
validatorsKeys keys.PublicKeys
|
|
|
|
validatorsHash util.Uint160
|
|
|
|
validatorsScript []byte
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewModule returns new instance of stateroot module.
|
2022-01-13 01:34:14 +00:00
|
|
|
func NewModule(cfg config.ProtocolConfiguration, verif VerifierFunc, log *zap.Logger, s *storage.MemCachedStore) *Module {
|
2022-01-28 08:56:33 +00:00
|
|
|
var mode mpt.TrieMode
|
|
|
|
if cfg.KeepOnlyLatestState {
|
|
|
|
mode |= mpt.ModeLatest
|
|
|
|
}
|
2022-01-29 08:28:29 +00:00
|
|
|
if cfg.RemoveUntraceableBlocks {
|
|
|
|
mode |= mpt.ModeGC
|
|
|
|
}
|
2021-01-29 14:33:24 +00:00
|
|
|
return &Module{
|
2022-01-13 01:34:14 +00:00
|
|
|
network: cfg.Magic,
|
|
|
|
srInHead: cfg.StateRootInHeader,
|
2022-01-28 08:56:33 +00:00
|
|
|
mode: mode,
|
2022-01-13 01:34:14 +00:00
|
|
|
verifier: verif,
|
|
|
|
log: log,
|
|
|
|
Store: s,
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:03:37 +00:00
|
|
|
// GetState returns value at the specified key fom the MPT with the specified root.
|
|
|
|
func (s *Module) GetState(root util.Uint256, key []byte) ([]byte, error) {
|
2022-01-28 12:05:13 +00:00
|
|
|
// Allow accessing old values, it's RO thing.
|
|
|
|
tr := mpt.NewTrie(mpt.NewHashNode(root), s.mode&^mpt.ModeGCFlag, storage.NewMemCachedStore(s.Store))
|
2021-10-07 09:03:37 +00:00
|
|
|
return tr.Get(key)
|
|
|
|
}
|
|
|
|
|
2021-10-07 13:56:27 +00:00
|
|
|
// FindStates returns set of key-value pairs with key matching the prefix starting
|
|
|
|
// from the `prefix`+`start` path from MPT trie with the specified root. `max` is
|
|
|
|
// the maximum number of elements to be returned. If nil `start` specified, then
|
|
|
|
// item with key equals to prefix is included into result; if empty `start` specified,
|
|
|
|
// then item with key equals to prefix is not included into result.
|
|
|
|
func (s *Module) FindStates(root util.Uint256, prefix, start []byte, max int) ([]storage.KeyValue, error) {
|
2022-01-28 12:05:13 +00:00
|
|
|
// Allow accessing old values, it's RO thing.
|
|
|
|
tr := mpt.NewTrie(mpt.NewHashNode(root), s.mode&^mpt.ModeGCFlag, storage.NewMemCachedStore(s.Store))
|
2021-10-07 13:56:27 +00:00
|
|
|
return tr.Find(prefix, start, max)
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:33:24 +00:00
|
|
|
// GetStateProof returns proof of having key in the MPT with the specified root.
|
|
|
|
func (s *Module) GetStateProof(root util.Uint256, key []byte) ([][]byte, error) {
|
2022-01-28 12:05:13 +00:00
|
|
|
// Allow accessing old values, it's RO thing.
|
|
|
|
tr := mpt.NewTrie(mpt.NewHashNode(root), s.mode&^mpt.ModeGCFlag, storage.NewMemCachedStore(s.Store))
|
2021-01-29 14:33:24 +00:00
|
|
|
return tr.GetProof(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStateRoot returns state root for a given height.
|
|
|
|
func (s *Module) GetStateRoot(height uint32) (*state.MPTRoot, error) {
|
|
|
|
return s.getStateRoot(makeStateRootKey(height))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentLocalStateRoot returns hash of the local state root.
|
|
|
|
func (s *Module) CurrentLocalStateRoot() util.Uint256 {
|
|
|
|
return s.currentLocal.Load().(util.Uint256)
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:28:44 +00:00
|
|
|
// CurrentLocalHeight returns height of the local state root.
|
|
|
|
func (s *Module) CurrentLocalHeight() uint32 {
|
|
|
|
return s.localHeight.Load()
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:00:07 +00:00
|
|
|
// CurrentValidatedHeight returns current state root validated height.
|
|
|
|
func (s *Module) CurrentValidatedHeight() uint32 {
|
|
|
|
return s.validatedHeight.Load()
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:33:24 +00:00
|
|
|
// Init initializes state root module at the given height.
|
2022-01-28 08:56:33 +00:00
|
|
|
func (s *Module) Init(height uint32) error {
|
2022-02-12 19:48:16 +00:00
|
|
|
data, err := s.Store.Get([]byte{byte(storage.DataMPTAux), prefixValidated})
|
2021-02-01 16:00:07 +00:00
|
|
|
if err == nil {
|
|
|
|
s.validatedHeight.Store(binary.LittleEndian.Uint32(data))
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:33:24 +00:00
|
|
|
if height == 0 {
|
2022-01-28 08:56:33 +00:00
|
|
|
s.mpt = mpt.NewTrie(nil, s.mode, s.Store)
|
2021-01-29 14:33:24 +00:00
|
|
|
s.currentLocal.Store(util.Uint256{})
|
2021-10-22 08:09:47 +00:00
|
|
|
return nil
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
|
|
|
r, err := s.getStateRoot(makeStateRootKey(height))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.currentLocal.Store(r.Root)
|
|
|
|
s.localHeight.Store(r.Index)
|
2022-01-28 08:56:33 +00:00
|
|
|
s.mpt = mpt.NewTrie(mpt.NewHashNode(r.Root), s.mode, s.Store)
|
2021-01-29 14:33:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 15:16:10 +00:00
|
|
|
// CleanStorage removes all MPT-related data from the storage (MPT nodes, validated stateroots)
|
|
|
|
// except local stateroot for the current height and GC flag. This method is aimed to clean
|
|
|
|
// outdated MPT data before state sync process can be started.
|
|
|
|
// Note: this method is aimed to be called for genesis block only, an error is returned otherwice.
|
|
|
|
func (s *Module) CleanStorage() error {
|
|
|
|
if s.localHeight.Load() != 0 {
|
|
|
|
return fmt.Errorf("can't clean MPT data for non-genesis block: expected local stateroot height 0, got %d", s.localHeight.Load())
|
|
|
|
}
|
2022-02-16 13:13:12 +00:00
|
|
|
b := storage.NewMemCachedStore(s.Store)
|
2022-01-17 17:41:51 +00:00
|
|
|
s.Store.Seek(storage.SeekRange{Prefix: []byte{byte(storage.DataMPT)}}, func(k, _ []byte) bool {
|
2021-09-24 15:44:42 +00:00
|
|
|
// #1468, but don't need to copy here, because it is done by Store.
|
2022-02-16 14:48:15 +00:00
|
|
|
b.Delete(k)
|
2022-01-17 17:41:51 +00:00
|
|
|
return true
|
2021-08-17 15:16:10 +00:00
|
|
|
})
|
2022-02-16 13:13:12 +00:00
|
|
|
_, err := b.Persist()
|
2021-08-17 15:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to remove outdated MPT-reated items: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 13:57:42 +00:00
|
|
|
// JumpToState performs jump to the state specified by given stateroot index.
|
2022-02-16 14:48:15 +00:00
|
|
|
func (s *Module) JumpToState(sr *state.MPTRoot) {
|
|
|
|
s.addLocalStateRoot(s.Store, sr)
|
2021-07-30 13:57:42 +00:00
|
|
|
|
|
|
|
data := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(data, sr.Index)
|
2022-02-16 14:48:15 +00:00
|
|
|
s.Store.Put([]byte{byte(storage.DataMPTAux), prefixValidated}, data)
|
2021-07-30 13:57:42 +00:00
|
|
|
s.validatedHeight.Store(sr.Index)
|
|
|
|
|
|
|
|
s.currentLocal.Store(sr.Root)
|
|
|
|
s.localHeight.Store(sr.Index)
|
2022-01-28 08:56:33 +00:00
|
|
|
s.mpt = mpt.NewTrie(mpt.NewHashNode(sr.Root), s.mode, s.Store)
|
2021-07-30 13:57:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 08:28:29 +00:00
|
|
|
// GC performs garbage collection.
|
|
|
|
func (s *Module) GC(index uint32, store storage.Store) time.Duration {
|
|
|
|
if !s.mode.GC() {
|
|
|
|
panic("stateroot: GC invoked, but not enabled")
|
|
|
|
}
|
|
|
|
var removed int
|
|
|
|
var stored int64
|
|
|
|
s.log.Info("starting MPT garbage collection", zap.Uint32("index", index))
|
|
|
|
start := time.Now()
|
2022-02-11 17:35:45 +00:00
|
|
|
err := store.SeekGC(storage.SeekRange{
|
2022-01-29 08:28:29 +00:00
|
|
|
Prefix: []byte{byte(storage.DataMPT)},
|
|
|
|
}, func(k, v []byte) bool {
|
|
|
|
stored++
|
|
|
|
if !mpt.IsActiveValue(v) {
|
|
|
|
h := binary.LittleEndian.Uint32(v[len(v)-4:])
|
2022-02-11 17:35:45 +00:00
|
|
|
if h <= index {
|
|
|
|
removed++
|
|
|
|
stored--
|
|
|
|
return false
|
2022-01-29 08:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
dur := time.Since(start)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("failed to flush MPT GC changeset", zap.Duration("time", dur), zap.Error(err))
|
|
|
|
} else {
|
|
|
|
s.log.Info("finished MPT garbage collection",
|
|
|
|
zap.Int("removed", removed),
|
2022-02-12 19:50:34 +00:00
|
|
|
zap.Int64("kept", stored),
|
2022-01-29 08:28:29 +00:00
|
|
|
zap.Duration("time", dur))
|
|
|
|
}
|
|
|
|
return dur
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:33:24 +00:00
|
|
|
// AddMPTBatch updates using provided batch.
|
2021-03-26 21:13:19 +00:00
|
|
|
func (s *Module) AddMPTBatch(index uint32, b mpt.Batch, cache *storage.MemCachedStore) (*mpt.Trie, *state.MPTRoot, error) {
|
|
|
|
mpt := *s.mpt
|
|
|
|
mpt.Store = cache
|
|
|
|
if _, err := mpt.PutBatch(b); err != nil {
|
|
|
|
return nil, nil, err
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
2022-01-28 12:05:13 +00:00
|
|
|
mpt.Flush(index)
|
2021-03-26 21:13:19 +00:00
|
|
|
sr := &state.MPTRoot{
|
2021-03-25 19:11:55 +00:00
|
|
|
Index: index,
|
2021-03-26 21:13:19 +00:00
|
|
|
Root: mpt.StateRoot(),
|
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
s.addLocalStateRoot(cache, sr)
|
|
|
|
return &mpt, sr, nil
|
2021-03-26 21:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCurrentLocal updates local caches using provided state root.
|
|
|
|
func (s *Module) UpdateCurrentLocal(mpt *mpt.Trie, sr *state.MPTRoot) {
|
|
|
|
s.mpt = mpt
|
|
|
|
s.currentLocal.Store(sr.Root)
|
|
|
|
s.localHeight.Store(sr.Index)
|
2022-01-13 01:34:14 +00:00
|
|
|
if s.srInHead {
|
2021-03-26 21:13:19 +00:00
|
|
|
s.validatedHeight.Store(sr.Index)
|
|
|
|
updateStateHeightMetric(sr.Index)
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
// VerifyStateRoot checks if state root is valid.
|
|
|
|
func (s *Module) VerifyStateRoot(r *state.MPTRoot) error {
|
|
|
|
_, err := s.getStateRoot(makeStateRootKey(r.Index - 1))
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("can't get previous state root")
|
|
|
|
}
|
2021-03-26 10:39:37 +00:00
|
|
|
if len(r.Witness) != 1 {
|
|
|
|
return errors.New("no witness")
|
|
|
|
}
|
2021-02-01 16:00:07 +00:00
|
|
|
return s.verifyWitness(r)
|
|
|
|
}
|
|
|
|
|
2021-07-07 13:14:49 +00:00
|
|
|
const maxVerificationGAS = 2_00000000
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
// verifyWitness verifies state root witness.
|
|
|
|
func (s *Module) verifyWitness(r *state.MPTRoot) error {
|
|
|
|
s.mtx.Lock()
|
|
|
|
h := s.getKeyCacheForHeight(r.Index).validatorsHash
|
|
|
|
s.mtx.Unlock()
|
2022-01-13 01:34:14 +00:00
|
|
|
_, err := s.verifier(h, r, &r.Witness[0], maxVerificationGAS)
|
2021-10-25 14:42:20 +00:00
|
|
|
return err
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|