Skip to content

Commit

Permalink
Merge pull request #3371 from nspcc-dev/upd-dbft
Browse files Browse the repository at this point in the history
consensus: integrate dBFT pre-0.2.0
  • Loading branch information
AnnaShaleva authored Mar 25, 2024
2 parents f8ca51d + beee260 commit 627a0ab
Show file tree
Hide file tree
Showing 23 changed files with 268 additions and 335 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/holiman/uint256 v1.2.4
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mr-tron/base58 v1.2.0
github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c
github.com/nspcc-dev/dbft v0.1.1-0.20240321205542-332ff86ba4c6
github.com/nspcc-dev/go-ordered-json v0.0.0-20240301084351-0246b013f8b2
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20240322141543-1840c057bdd7
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11
Expand Down Expand Up @@ -55,7 +55,6 @@ require (
github.com/nspcc-dev/neofs-api-go/v2 v2.14.0 // indirect
github.com/nspcc-dev/neofs-crypto v0.4.0 // indirect
github.com/nspcc-dev/tzhash v1.7.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqky
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c h1:uyK5aLbAhrnZtnvobJLN24gGUrlxIJAAFqiWl+liZuo=
github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c/go.mod h1:kjBC9F8L25GR+kIHy/1KgG/KfcoGnVwIiyovgq1uszk=
github.com/nspcc-dev/dbft v0.1.1-0.20240321205542-332ff86ba4c6 h1:6rpKY0AvXQpL2ZpLTxGDJAM54ak6ziXghi4uLGUrSdg=
github.com/nspcc-dev/dbft v0.1.1-0.20240321205542-332ff86ba4c6/go.mod h1:oFE6paSC/yfFh9mcNU6MheMGOYXK9+sPiRk3YMoz49o=
github.com/nspcc-dev/go-ordered-json v0.0.0-20240301084351-0246b013f8b2 h1:mD9hU3v+zJcnHAVmHnZKt3I++tvn30gBj2rP2PocZMk=
github.com/nspcc-dev/go-ordered-json v0.0.0-20240301084351-0246b013f8b2/go.mod h1:U5VfmPNM88P4RORFb6KSUVBdJBDhlqggJZYGXGPxOcc=
github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y=
Expand Down Expand Up @@ -119,7 +119,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM=
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
24 changes: 7 additions & 17 deletions pkg/consensus/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package consensus
import (
"errors"

"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
coreb "github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
Expand All @@ -20,18 +19,18 @@ type neoBlock struct {
signature []byte
}

var _ block.Block = (*neoBlock)(nil)
var _ dbft.Block[util.Uint256] = (*neoBlock)(nil)

// Sign implements the block.Block interface.
func (n *neoBlock) Sign(key crypto.PrivateKey) error {
func (n *neoBlock) Sign(key dbft.PrivateKey) error {
k := key.(*privateKey)
sig := k.PrivateKey.SignHashable(uint32(n.network), &n.Block)
n.signature = sig
return nil
}

// Verify implements the block.Block interface.
func (n *neoBlock) Verify(key crypto.PublicKey, sign []byte) error {
func (n *neoBlock) Verify(key dbft.PublicKey, sign []byte) error {
k := key.(*publicKey)
if k.PublicKey.VerifyHashable(sign, uint32(n.network), &n.Block) {
return nil
Expand All @@ -40,8 +39,8 @@ func (n *neoBlock) Verify(key crypto.PublicKey, sign []byte) error {
}

// Transactions implements the block.Block interface.
func (n *neoBlock) Transactions() []block.Transaction {
txes := make([]block.Transaction, len(n.Block.Transactions))
func (n *neoBlock) Transactions() []dbft.Transaction[util.Uint256] {
txes := make([]dbft.Transaction[util.Uint256], len(n.Block.Transactions))
for i, tx := range n.Block.Transactions {
txes[i] = tx
}
Expand All @@ -50,16 +49,13 @@ func (n *neoBlock) Transactions() []block.Transaction {
}

// SetTransactions implements the block.Block interface.
func (n *neoBlock) SetTransactions(txes []block.Transaction) {
func (n *neoBlock) SetTransactions(txes []dbft.Transaction[util.Uint256]) {
n.Block.Transactions = make([]*transaction.Transaction, len(txes))
for i, tx := range txes {
n.Block.Transactions[i] = tx.(*transaction.Transaction)
}
}

// Version implements the block.Block interface.
func (n *neoBlock) Version() uint32 { return n.Block.Version }

// PrevHash implements the block.Block interface.
func (n *neoBlock) PrevHash() util.Uint256 { return n.Block.PrevHash }

Expand All @@ -72,11 +68,5 @@ func (n *neoBlock) Timestamp() uint64 { return n.Block.Timestamp * nsInMs }
// Index implements the block.Block interface.
func (n *neoBlock) Index() uint32 { return n.Block.Index }

// ConsensusData implements the block.Block interface.
func (n *neoBlock) ConsensusData() uint64 { return n.Block.Nonce }

// NextConsensus implements the block.Block interface.
func (n *neoBlock) NextConsensus() util.Uint160 { return n.Block.NextConsensus }

// Signature implements the block.Block interface.
func (n *neoBlock) Signature() []byte { return n.signature }
10 changes: 2 additions & 8 deletions pkg/consensus/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package consensus
import (
"testing"

"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft"
"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/util"
Expand All @@ -22,9 +22,6 @@ func TestNeoBlock_Sign(t *testing.T) {
func TestNeoBlock_Setters(t *testing.T) {
b := new(neoBlock)

b.Block.Version = 1
require.EqualValues(t, 1, b.Version())

b.Block.Index = 12
require.EqualValues(t, 12, b.Index())

Expand All @@ -35,13 +32,10 @@ func TestNeoBlock_Setters(t *testing.T) {
b.Block.MerkleRoot = util.Uint256{1, 2, 3, 4}
require.Equal(t, util.Uint256{1, 2, 3, 4}, b.MerkleRoot())

b.Block.NextConsensus = util.Uint160{9, 2}
require.Equal(t, util.Uint160{9, 2}, b.NextConsensus())

b.Block.PrevHash = util.Uint256{9, 8, 7}
require.Equal(t, util.Uint256{9, 8, 7}, b.PrevHash())

txx := []block.Transaction{transaction.New([]byte{byte(opcode.PUSH1)}, 1)}
txx := []dbft.Transaction[util.Uint256]{transaction.New([]byte{byte(opcode.PUSH1)}, 1)}
b.SetTransactions(txx)
require.Equal(t, txx, b.Transactions())
}
5 changes: 2 additions & 3 deletions pkg/consensus/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consensus
import (
"testing"

"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -52,8 +51,8 @@ func getDifferentPayloads(t *testing.T, n int) (payloads []Payload) {
var sign [signatureSize]byte
random.Fill(sign[:])

payloads[i].SetValidatorIndex(uint16(i))
payloads[i].SetType(payload.MessageType(commitType))
payloads[i].message.ValidatorIndex = byte(i)
payloads[i].message.Type = commitType
payloads[i].payload = &commit{
signature: sign,
}
Expand Down
22 changes: 5 additions & 17 deletions pkg/consensus/change_view.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package consensus

import (
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/io"
)

// changeView represents dBFT ChangeView message.
type changeView struct {
newViewNumber byte
timestamp uint64
reason payload.ChangeViewReason
reason dbft.ChangeViewReason
}

var _ payload.ChangeView = (*changeView)(nil)
var _ dbft.ChangeView = (*changeView)(nil)

// EncodeBinary implements the io.Serializable interface.
func (c *changeView) EncodeBinary(w *io.BinWriter) {
Expand All @@ -23,23 +23,11 @@ func (c *changeView) EncodeBinary(w *io.BinWriter) {
// DecodeBinary implements the io.Serializable interface.
func (c *changeView) DecodeBinary(r *io.BinReader) {
c.timestamp = r.ReadU64LE()
c.reason = payload.ChangeViewReason(r.ReadB())
c.reason = dbft.ChangeViewReason(r.ReadB())
}

// NewViewNumber implements the payload.ChangeView interface.
func (c changeView) NewViewNumber() byte { return c.newViewNumber }

// SetNewViewNumber implements the payload.ChangeView interface.
func (c *changeView) SetNewViewNumber(view byte) { c.newViewNumber = view }

// Timestamp implements the payload.ChangeView interface.
func (c changeView) Timestamp() uint64 { return c.timestamp * nsInMs }

// SetTimestamp implements the payload.ChangeView interface.
func (c *changeView) SetTimestamp(ts uint64) { c.timestamp = ts / nsInMs }

// Reason implements the payload.ChangeView interface.
func (c changeView) Reason() payload.ChangeViewReason { return c.reason }

// SetReason implements the payload.ChangeView interface.
func (c *changeView) SetReason(reason payload.ChangeViewReason) { c.reason = reason }
func (c changeView) Reason() dbft.ChangeViewReason { return c.reason }
13 changes: 7 additions & 6 deletions pkg/consensus/change_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package consensus
import (
"testing"

"github.com/nspcc-dev/dbft"
"github.com/stretchr/testify/require"
)

func TestChangeView_Setters(t *testing.T) {
var c changeView
func TestChangeView_Getters(t *testing.T) {
var c = &changeView{
newViewNumber: 2,
reason: dbft.CVTimeout,
}

c.SetTimestamp(123 * nsInMs)
require.EqualValues(t, 123*nsInMs, c.Timestamp())

c.SetNewViewNumber(2)
require.EqualValues(t, 2, c.NewViewNumber())
require.EqualValues(t, dbft.CVTimeout, c.Reason())
}
9 changes: 2 additions & 7 deletions pkg/consensus/commit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package consensus

import (
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/io"
)

Expand All @@ -14,7 +14,7 @@ type commit struct {
// without a leading byte (0x04, uncompressed).
const signatureSize = 64

var _ payload.Commit = (*commit)(nil)
var _ dbft.Commit = (*commit)(nil)

// EncodeBinary implements the io.Serializable interface.
func (c *commit) EncodeBinary(w *io.BinWriter) {
Expand All @@ -28,8 +28,3 @@ func (c *commit) DecodeBinary(r *io.BinReader) {

// Signature implements the payload.Commit interface.
func (c commit) Signature() []byte { return c.signature[:] }

// SetSignature implements the payload.Commit interface.
func (c *commit) SetSignature(signature []byte) {
copy(c.signature[:], signature)
}
7 changes: 4 additions & 3 deletions pkg/consensus/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/stretchr/testify/require"
)

func TestCommit_Setters(t *testing.T) {
func TestCommit_Getters(t *testing.T) {
var sign [signatureSize]byte
random.Fill(sign[:])

var c commit
c.SetSignature(sign[:])
var c = &commit{
signature: sign,
}
require.Equal(t, sign[:], c.Signature())
}
Loading

0 comments on commit 627a0ab

Please sign in to comment.