From 216513e14f32e58e719553d3bcd9c4a127f6d355 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 27 Mar 2021 00:31:22 +0300 Subject: [PATCH] stateroot: reject validated root if it doesn't match local one Prevent 2021-03-27T00:05:23.382+0300 WARN blockQueue: failed adding block into the blockchain {"error": "error while trying to apply MPT changes: key not found", "blockHeight": 12757, "nextIndex": 12758} after node restart (node reads "local" root hash that it doesn't have). --- pkg/core/stateroot/store.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/core/stateroot/store.go b/pkg/core/stateroot/store.go index ff3b1a6cb..9efcf7c5f 100644 --- a/pkg/core/stateroot/store.go +++ b/pkg/core/stateroot/store.go @@ -2,12 +2,20 @@ package stateroot import ( "encoding/binary" + "errors" + "fmt" "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" ) +var ( + // ErrStateMismatch means that local state root doesn't match the one + // signed by state validators. + ErrStateMismatch = errors.New("stateroot mismatch") +) + const ( prefixGC = 0x01 prefixLocal = 0x02 @@ -60,6 +68,9 @@ func (s *Module) AddStateRoot(sr *state.MPTRoot) error { if err != nil { return err } + if !local.Root.Equals(sr.Root) { + return fmt.Errorf("%w at block %d: %v vs %v", ErrStateMismatch, sr.Index, local.Root, sr.Root) + } if len(local.Witness) != 0 { return nil }