consensus: verify state root in PrepareRequest

Fixes #1042.
This commit is contained in:
Evgenii Stratonikov 2020-06-10 19:00:19 +03:00
parent c13ca8d597
commit 8614867439
3 changed files with 17 additions and 3 deletions

2
go.mod
View file

@ -9,7 +9,7 @@ require (
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/gorilla/websocket v1.4.2
github.com/mr-tron/base58 v1.1.2
github.com/nspcc-dev/dbft v0.0.0-20200610070614-c07d9a2b753d
github.com/nspcc-dev/dbft v0.0.0-20200610194044-e2dbc098ab46
github.com/nspcc-dev/rfc6979 v0.2.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.2.1

4
go.sum
View file

@ -156,8 +156,8 @@ github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a h1:ajvxgEe9qY4vvoSm
github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk=
github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1 h1:yEx9WznS+rjE0jl0dLujCxuZSIb+UTjF+005TJu/nNI=
github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1/go.mod h1:O0qtn62prQSqizzoagHmuuKoz8QMkU3SzBoKdEvm3aQ=
github.com/nspcc-dev/dbft v0.0.0-20200610070614-c07d9a2b753d h1:QRGLvZneXLikvEsVEyHnSCUmOpSz2xbQzA6VZMMx6bQ=
github.com/nspcc-dev/dbft v0.0.0-20200610070614-c07d9a2b753d/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E=
github.com/nspcc-dev/dbft v0.0.0-20200610194044-e2dbc098ab46 h1:3MTsGNIWSi0FZm3hmj9//iaDRNHqBZzgfBbPEagh2eY=
github.com/nspcc-dev/dbft v0.0.0-20200610194044-e2dbc098ab46/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E=
github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg=
github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=

View file

@ -2,6 +2,7 @@ package consensus
import (
"errors"
"fmt"
"math/rand"
"sort"
"time"
@ -144,6 +145,7 @@ func NewService(cfg Config) (Service, error) {
dbft.WithNewCommit(srv.newCommit),
dbft.WithNewRecoveryRequest(func() payload.RecoveryRequest { return new(recoveryRequest) }),
dbft.WithNewRecoveryMessage(func() payload.RecoveryMessage { return new(recoveryMessage) }),
dbft.WithVerifyPrepareRequest(srv.verifyRequest),
)
if srv.dbft == nil {
@ -369,6 +371,18 @@ func (s *service) verifyBlock(b block.Block) bool {
return true
}
func (s *service) verifyRequest(p payload.ConsensusPayload) error {
r, err := s.Chain.GetStateRoot(s.dbft.BlockIndex - 1)
if err != nil {
return fmt.Errorf("can't get local state root: %v", err)
}
rb := &p.GetPrepareRequest().(*prepareRequest).proposalStateRoot
if !r.Equals(rb) {
return errors.New("state root mismatch")
}
return nil
}
func (s *service) processBlock(b block.Block) {
bb := &b.(*neoBlock).Block
bb.Script = *(s.getBlockWitness(bb))