Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.mod: update dbft version (2.x) #1675

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-redis/redis v6.10.2+incompatible
github.com/gorilla/websocket v1.4.2
github.com/mr-tron/base58 v1.1.2
github.com/nspcc-dev/dbft v0.0.0-20200925163137-8f3b9ab3b720
github.com/nspcc-dev/dbft v0.0.0-20210122071512-d9a728094f0d
github.com/nspcc-dev/rfc6979 v0.2.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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-20200925163137-8f3b9ab3b720 h1:e/lFQUIPnWErf2yiHjp2HyPhf0nyo3lp4hMm8IlQX1U=
github.com/nspcc-dev/dbft v0.0.0-20200925163137-8f3b9ab3b720/go.mod h1:I5D0W3tu3epdt2RMCTxS//HDr4S+OHRqajouQTOAHI8=
github.com/nspcc-dev/dbft v0.0.0-20210122071512-d9a728094f0d h1:uUaRysqa/9VtHETVARUlteqfbXAgwxR2nvUc4DzK4pI=
github.com/nspcc-dev/dbft v0.0.0-20210122071512-d9a728094f0d/go.mod h1:I5D0W3tu3epdt2RMCTxS//HDr4S+OHRqajouQTOAHI8=
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=
Expand Down
29 changes: 26 additions & 3 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,21 @@ func (s *service) eventLoop() {
}
}

func (s *service) newPayload() payload.ConsensusPayload {
return &Payload{
func (s *service) newPayload(c *dbft.Context, t payload.MessageType, msg interface{}) payload.ConsensusPayload {
cp := &Payload{
message: &message{
stateRootEnabled: s.stateRootEnabled(),
},
}
cp.SetHeight(c.BlockIndex)
cp.SetValidatorIndex(uint16(c.MyIndex))
cp.SetViewNumber(c.ViewNumber)
cp.SetType(t)
cp.SetPrevHash(c.PrevHash)
cp.SetVersion(c.Version)
cp.SetPayload(msg)

return cp
}

// stateRootEnabled checks if state root feature is enabled on current height.
Expand Down Expand Up @@ -453,12 +462,26 @@ func (s *service) verifyStateRootSig(index int, sig []byte) error {

pub := validators[index]
if pub.Verify(r.GetSignedPart(), sig) != nil {
return errors.New("bad state root signature")
return errInvalidStateRoot
}
return nil
}

var (
errInvalidPrevHash = errors.New("invalid PrevHash")
errInvalidVersion = errors.New("invalid Version")
errInvalidStateRoot = errors.New("invalid state root signature")
)

func (s *service) verifyRequest(p payload.ConsensusPayload) error {
pl := p.(*Payload)
if pl.prevHash != s.dbft.PrevHash {
return errInvalidPrevHash
}
if pl.version != s.dbft.Version {
return errInvalidVersion
}

req := p.GetPrepareRequest().(*prepareRequest)
if s.stateRootEnabled() {
err := s.verifyStateRootSig(int(p.ValidatorIndex()), req.stateRootSig[:])
Expand Down
73 changes: 63 additions & 10 deletions pkg/consensus/consensus_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package consensus

import (
"errors"
"testing"
"time"

Expand All @@ -12,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -49,23 +51,33 @@ func TestService_GetVerified(t *testing.T) {

// Everyone sends a message.
for i := 0; i < 4; i++ {
p := srv.newPayload().(*Payload)
p.SetHeight(1)
p.SetValidatorIndex(uint16(i))
var p *Payload
priv, _ := getTestValidator(i)
// To properly sign stateroot in prepare request.
srv.dbft.Priv = priv
// One PrepareRequest and three ChangeViews.
if i == 1 {
p.SetType(payload.PrepareRequestType)
preq := srv.newPrepareRequest().(*prepareRequest)
preq.transactionHashes = hashes
preq.minerTx = *newMinerTx(999)
p.SetPayload(preq)
req := &prepareRequest{
transactionHashes: hashes,
minerTx: *newMinerTx(999),
}
if srv.stateRootEnabled() {
req.stateRootEnabled = srv.stateRootEnabled()
sr, err := srv.Chain.GetStateRoot(srv.Chain.BlockHeight())
require.NoError(t, err)
sig, err := priv.Sign(sr.GetSignedPart())
require.NoError(t, err)
copy(req.stateRootSig[:], sig)
}
p = srv.newPayload(&srv.dbft.Context, payload.PrepareRequestType, req).(*Payload)
} else {
p.SetType(payload.ChangeViewType)
p.SetPayload(&changeView{newViewNumber: 1, timestamp: uint32(time.Now().Unix())})
p = srv.newPayload(&srv.dbft.Context, payload.ChangeViewType, &changeView{
newViewNumber: 1,
timestamp: uint32(time.Now().Unix()),
}).(*Payload)
}
p.SetHeight(1)
p.SetValidatorIndex(uint16(i))

require.NoError(t, p.Sign(priv))

Expand Down Expand Up @@ -125,6 +137,47 @@ func TestService_ValidatePayload(t *testing.T) {
srv.Chain.Close()
}

func TestService_PrepareRequest(t *testing.T) {
srv := newTestService(t)
defer srv.Chain.Close()

srv.dbft.Start()
defer srv.dbft.Timer.Stop()

priv, _ := getTestValidator(1)
prevHash := srv.Chain.CurrentBlockHash()

checkRequest := func(t *testing.T, expectedErr error, version uint32, prevHash util.Uint256, sig []byte) {
req := &prepareRequest{
stateRootEnabled: true,
minerTx: *newMinerTx(123),
}
req.transactionHashes = []util.Uint256{req.minerTx.Hash()}
copy(req.stateRootSig[:], sig)
p := srv.newPayload(&srv.dbft.Context, payload.PrepareRequestType, req)
p.SetValidatorIndex(1)
p.(*Payload).SetVersion(version)
p.(*Payload).SetPrevHash(prevHash)
require.NoError(t, p.(*Payload).Sign(priv))
err := srv.verifyRequest(p)
if expectedErr == nil {
require.NoError(t, err)
return
}
require.True(t, errors.Is(err, expectedErr), "got: %v", err)
}

sr, err := srv.Chain.GetStateRoot(srv.dbft.BlockIndex - 1)
require.NoError(t, err)
sig, err := priv.Sign(sr.GetSignedPart())
require.NoError(t, err)

checkRequest(t, errInvalidVersion, 0xFF, prevHash, sig)
checkRequest(t, errInvalidPrevHash, 0, random.Uint256(), sig)
checkRequest(t, errInvalidStateRoot, 0, prevHash, []byte{})
checkRequest(t, nil, 0, prevHash, sig)
}

func TestService_getTx(t *testing.T) {
srv := newTestService(t)

Expand Down