2021-01-29 14:33:24 +00:00
|
|
|
package stateroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2021-03-26 21:31:22 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-01-29 14:33:24 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
)
|
|
|
|
|
2021-03-26 21:31:22 +00:00
|
|
|
var (
|
|
|
|
// ErrStateMismatch means that local state root doesn't match the one
|
|
|
|
// signed by state validators.
|
|
|
|
ErrStateMismatch = errors.New("stateroot mismatch")
|
|
|
|
)
|
|
|
|
|
2021-01-29 14:33:24 +00:00
|
|
|
const (
|
2021-02-01 16:00:07 +00:00
|
|
|
prefixLocal = 0x02
|
|
|
|
prefixValidated = 0x03
|
2021-01-29 14:33:24 +00:00
|
|
|
)
|
|
|
|
|
2022-02-16 14:48:15 +00:00
|
|
|
func (s *Module) addLocalStateRoot(store *storage.MemCachedStore, sr *state.MPTRoot) {
|
2021-01-29 14:33:24 +00:00
|
|
|
key := makeStateRootKey(sr.Index)
|
2022-02-16 14:48:15 +00:00
|
|
|
putStateRoot(store, key, sr)
|
2021-01-29 14:33:24 +00:00
|
|
|
|
|
|
|
data := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(data, sr.Index)
|
2022-02-16 14:48:15 +00:00
|
|
|
store.Put([]byte{byte(storage.DataMPTAux), prefixLocal}, data)
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:48:15 +00:00
|
|
|
func putStateRoot(store *storage.MemCachedStore, key []byte, sr *state.MPTRoot) {
|
2021-01-29 14:33:24 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
sr.EncodeBinary(w.BinWriter)
|
2022-02-16 14:48:15 +00:00
|
|
|
store.Put(key, w.Bytes())
|
2021-01-29 14:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Module) getStateRoot(key []byte) (*state.MPTRoot, error) {
|
|
|
|
data, err := s.Store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:11:55 +00:00
|
|
|
sr := &state.MPTRoot{}
|
2021-01-29 14:33:24 +00:00
|
|
|
r := io.NewBinReaderFromBuf(data)
|
|
|
|
sr.DecodeBinary(r)
|
|
|
|
return sr, r.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStateRootKey(index uint32) []byte {
|
|
|
|
key := make([]byte, 5)
|
2022-02-12 19:48:16 +00:00
|
|
|
key[0] = byte(storage.DataMPTAux)
|
2022-04-22 15:23:38 +00:00
|
|
|
binary.BigEndian.PutUint32(key[1:], index)
|
2021-01-29 14:33:24 +00:00
|
|
|
return key
|
|
|
|
}
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
// AddStateRoot adds validated state root provided by network.
|
|
|
|
func (s *Module) AddStateRoot(sr *state.MPTRoot) error {
|
|
|
|
if err := s.VerifyStateRoot(sr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := makeStateRootKey(sr.Index)
|
|
|
|
local, err := s.getStateRoot(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-26 21:31:22 +00:00
|
|
|
if !local.Root.Equals(sr.Root) {
|
|
|
|
return fmt.Errorf("%w at block %d: %v vs %v", ErrStateMismatch, sr.Index, local.Root, sr.Root)
|
|
|
|
}
|
2021-03-26 10:39:37 +00:00
|
|
|
if len(local.Witness) != 0 {
|
2021-02-01 16:00:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-16 14:48:15 +00:00
|
|
|
putStateRoot(s.Store, key, sr)
|
2021-02-01 16:00:07 +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-02-01 16:00:07 +00:00
|
|
|
s.validatedHeight.Store(sr.Index)
|
2022-01-13 01:34:14 +00:00
|
|
|
if !s.srInHead {
|
2021-02-01 16:00:07 +00:00
|
|
|
updateStateHeightMetric(sr.Index)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|