consensus: fetch dbft pre-0.2.0 version

Try out updated dBFT interface.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2024-03-21 22:49:39 +03:00
parent b028c772f2
commit 3e6dfff503
18 changed files with 176 additions and 152 deletions

3
go.mod
View file

@ -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.20240311071455-88b2f18461e5
github.com/nspcc-dev/go-ordered-json v0.0.0-20240301084351-0246b013f8b2
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231127165613-b35f351f0ba0
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11
@ -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

5
go.sum
View file

@ -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.20240311071455-88b2f18461e5 h1:XK88zeGFqO/43ZTwHbNSisgxuvExYp8DB/TMbjfw1yk=
github.com/nspcc-dev/dbft v0.1.1-0.20240311071455-88b2f18461e5/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=
@ -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=

View file

@ -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"
@ -20,10 +19,10 @@ 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
@ -31,7 +30,7 @@ func (n *neoBlock) Sign(key crypto.PrivateKey) error {
}
// 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
@ -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
}
@ -50,7 +49,7 @@ 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)

View file

@ -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"
@ -41,7 +41,7 @@ func TestNeoBlock_Setters(t *testing.T) {
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())
}

View file

@ -3,7 +3,7 @@ package consensus
import (
"testing"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/stretchr/testify/require"
)
@ -53,7 +53,7 @@ func getDifferentPayloads(t *testing.T, n int) (payloads []Payload) {
random.Fill(sign[:])
payloads[i].SetValidatorIndex(uint16(i))
payloads[i].SetType(payload.MessageType(commitType))
payloads[i].SetType(dbft.MessageType(commitType))
payloads[i].payload = &commit{
signature: sign,
}

View file

@ -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"
)
@ -9,10 +9,10 @@ import (
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) {
@ -23,7 +23,7 @@ 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.
@ -39,7 +39,7 @@ func (c changeView) Timestamp() uint64 { return c.timestamp * nsInMs }
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 }
func (c changeView) Reason() dbft.ChangeViewReason { return c.reason }
// SetReason implements the payload.ChangeView interface.
func (c *changeView) SetReason(reason payload.ChangeViewReason) { c.reason = reason }
func (c *changeView) SetReason(reason dbft.ChangeViewReason) { c.reason = reason }

View file

@ -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"
)
@ -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) {

View file

@ -8,9 +8,6 @@ import (
"time"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
coreb "github.com/nspcc-dev/neo-go/pkg/core/block"
@ -88,7 +85,7 @@ type service struct {
log *zap.Logger
// txx is a fifo cache which stores miner transactions.
txx *relayCache
dbft *dbft.DBFT
dbft *dbft.DBFT[util.Uint256]
// messages and transactions are channels needed to process
// everything in single thread.
messages chan Payload
@ -181,36 +178,33 @@ func NewService(cfg Config) (Service, error) {
}
}
srv.dbft = dbft.New(
dbft.WithLogger(srv.log),
dbft.WithSecondsPerBlock(cfg.TimePerBlock),
dbft.WithGetKeyPair(srv.getKeyPair),
srv.dbft = dbft.New[util.Uint256](
dbft.WithLogger[util.Uint256](srv.log),
dbft.WithSecondsPerBlock[util.Uint256](cfg.TimePerBlock),
dbft.WithGetKeyPair[util.Uint256](srv.getKeyPair),
dbft.WithRequestTx(cfg.RequestTx),
dbft.WithStopTxFlow(cfg.StopTxFlow),
dbft.WithGetTx(srv.getTx),
dbft.WithGetVerified(srv.getVerifiedTx),
dbft.WithBroadcast(srv.broadcast),
dbft.WithProcessBlock(srv.processBlock),
dbft.WithVerifyBlock(srv.verifyBlock),
dbft.WithGetBlock(srv.getBlock),
dbft.WithWatchOnly(func() bool { return false }),
dbft.WithNewBlockFromContext(srv.newBlockFromContext),
dbft.WithCurrentHeight(cfg.Chain.BlockHeight),
dbft.WithStopTxFlow[util.Uint256](cfg.StopTxFlow),
dbft.WithGetTx[util.Uint256](srv.getTx),
dbft.WithGetVerified[util.Uint256](srv.getVerifiedTx),
dbft.WithBroadcast[util.Uint256](srv.broadcast),
dbft.WithProcessBlock[util.Uint256](srv.processBlock),
dbft.WithVerifyBlock[util.Uint256](srv.verifyBlock),
dbft.WithGetBlock[util.Uint256](srv.getBlock),
dbft.WithWatchOnly[util.Uint256](func() bool { return false }),
dbft.WithNewBlockFromContext[util.Uint256](srv.newBlockFromContext),
dbft.WithCurrentHeight[util.Uint256](cfg.Chain.BlockHeight),
dbft.WithCurrentBlockHash(cfg.Chain.CurrentBlockHash),
dbft.WithGetValidators(srv.getValidators),
dbft.WithGetConsensusAddress(srv.getConsensusAddress),
dbft.WithGetValidators[util.Uint256](srv.getValidators),
dbft.WithNewConsensusPayload(srv.newPayload),
dbft.WithNewPrepareRequest(srv.newPrepareRequest),
dbft.WithNewPrepareResponse(func() payload.PrepareResponse { return new(prepareResponse) }),
dbft.WithNewChangeView(func() payload.ChangeView { return new(changeView) }),
dbft.WithNewCommit(func() payload.Commit { return new(commit) }),
dbft.WithNewRecoveryRequest(func() payload.RecoveryRequest { return new(recoveryRequest) }),
dbft.WithNewRecoveryMessage(func() payload.RecoveryMessage {
return &recoveryMessage{stateRootEnabled: srv.ProtocolConfiguration.StateRootInHeader}
}),
dbft.WithVerifyPrepareRequest(srv.verifyRequest),
dbft.WithVerifyPrepareResponse(func(_ payload.ConsensusPayload) error { return nil }),
dbft.WithNewConsensusPayload[util.Uint256](srv.newPayload),
dbft.WithNewPrepareRequest[util.Uint256](srv.newPrepareRequest),
dbft.WithNewPrepareResponse[util.Uint256](srv.newPrepareResponse),
dbft.WithNewChangeView[util.Uint256](srv.newChangeView),
dbft.WithNewCommit[util.Uint256](srv.newCommit),
dbft.WithNewRecoveryRequest[util.Uint256](srv.newRecoveryRequest),
dbft.WithNewRecoveryMessage[util.Uint256](srv.newRecoveryMessage),
dbft.WithVerifyPrepareRequest[util.Uint256](srv.verifyRequest),
dbft.WithVerifyPrepareResponse[util.Uint256](srv.verifyResponse),
)
if srv.dbft == nil {
@ -221,8 +215,8 @@ func NewService(cfg Config) (Service, error) {
}
var (
_ block.Transaction = (*transaction.Transaction)(nil)
_ block.Block = (*neoBlock)(nil)
_ dbft.Transaction[util.Uint256] = (*transaction.Transaction)(nil)
_ dbft.Block[util.Uint256] = (*neoBlock)(nil)
)
// NewPayload creates a new consensus payload for the provided network.
@ -238,7 +232,7 @@ func NewPayload(m netmode.Magic, stateRootEnabled bool) *Payload {
}
}
func (s *service) newPayload(c *dbft.Context, t payload.MessageType, msg any) payload.ConsensusPayload {
func (s *service) newPayload(c *dbft.Context[util.Uint256], t dbft.MessageType, msg any) dbft.ConsensusPayload[util.Uint256] {
cp := NewPayload(s.ProtocolConfiguration.Magic, s.ProtocolConfiguration.StateRootInHeader)
cp.SetHeight(c.BlockIndex)
cp.SetValidatorIndex(uint16(c.MyIndex))
@ -246,7 +240,7 @@ func (s *service) newPayload(c *dbft.Context, t payload.MessageType, msg any) pa
cp.SetType(t)
if pr, ok := msg.(*prepareRequest); ok {
pr.SetPrevHash(s.dbft.PrevHash)
pr.SetVersion(s.dbft.Version)
pr.SetVersion(coreb.VersionInitial)
}
cp.SetPayload(msg)
@ -257,8 +251,12 @@ func (s *service) newPayload(c *dbft.Context, t payload.MessageType, msg any) pa
return cp
}
func (s *service) newPrepareRequest() payload.PrepareRequest {
r := new(prepareRequest)
func (s *service) newPrepareRequest(ts uint64, nonce uint64, transactionsHashes []util.Uint256) dbft.PrepareRequest[util.Uint256] {
r := &prepareRequest{
timestamp: ts / nsInMs,
nonce: nonce,
transactionHashes: transactionsHashes,
}
if s.ProtocolConfiguration.StateRootInHeader {
r.stateRootEnabled = true
if sr, err := s.Chain.GetStateRoot(s.dbft.BlockIndex - 1); err == nil {
@ -270,6 +268,38 @@ func (s *service) newPrepareRequest() payload.PrepareRequest {
return r
}
func (s *service) newPrepareResponse(preparationHash util.Uint256) dbft.PrepareResponse[util.Uint256] {
return &prepareResponse{
preparationHash: preparationHash,
}
}
func (s *service) newChangeView(newViewNumber byte, reason dbft.ChangeViewReason, ts uint64) dbft.ChangeView {
return &changeView{
newViewNumber: newViewNumber,
timestamp: ts / nsInMs,
reason: reason,
}
}
func (s *service) newCommit(signature []byte) dbft.Commit {
c := new(commit)
copy(c.signature[:], signature)
return c
}
func (s *service) newRecoveryRequest(ts uint64) dbft.RecoveryRequest {
return &recoveryRequest{
timestamp: ts / nsInMs,
}
}
func (s *service) newRecoveryMessage() dbft.RecoveryMessage[util.Uint256] {
return &recoveryMessage{
stateRootEnabled: s.ProtocolConfiguration.StateRootInHeader,
}
}
// Name returns service name.
func (s *service) Name() string {
return "consensus"
@ -315,18 +345,18 @@ events:
s.Chain.UnsubscribeFromBlocks(s.blockEvents)
break events
case <-s.dbft.Timer.C():
hv := s.dbft.Timer.HV()
h, v := s.dbft.Timer.Height(), s.dbft.Timer.View()
s.log.Debug("timer fired",
zap.Uint32("height", hv.Height),
zap.Uint("view", uint(hv.View)))
s.dbft.OnTimeout(hv)
zap.Uint32("height", h),
zap.Uint("view", uint(v)))
s.dbft.OnTimeout(h, v)
case msg := <-s.messages:
fields := []zap.Field{
zap.Uint8("from", msg.message.ValidatorIndex),
zap.Stringer("type", msg.Type()),
}
if msg.Type() == payload.RecoveryMessageType {
if msg.Type() == dbft.RecoveryMessageType {
rec := msg.GetRecoveryMessage().(*recoveryMessage)
if rec.preparationHash == nil {
req := rec.GetPrepareRequest(&msg, s.dbft.Validators, uint16(s.dbft.PrimaryIndex))
@ -389,7 +419,7 @@ func (s *service) handleChainBlock(b *coreb.Block) {
zap.Uint32("dbft index", s.dbft.BlockIndex),
zap.Uint32("chain index", s.Chain.BlockHeight()))
s.postBlock(b)
s.dbft.InitializeConsensus(0, b.Timestamp*nsInMs)
s.dbft.Reset(b.Timestamp * nsInMs)
}
}
@ -404,7 +434,7 @@ func (s *service) validatePayload(p *Payload) bool {
return p.Sender == h
}
func (s *service) getKeyPair(pubs []crypto.PublicKey) (int, crypto.PrivateKey, crypto.PublicKey) {
func (s *service) getKeyPair(pubs []dbft.PublicKey) (int, dbft.PrivateKey, dbft.PublicKey) {
if s.wallet != nil {
for i := range pubs {
sh := pubs[i].(*publicKey).GetScriptHash()
@ -466,7 +496,7 @@ func (s *service) OnTransaction(tx *transaction.Transaction) {
}
}
func (s *service) broadcast(p payload.ConsensusPayload) {
func (s *service) broadcast(p dbft.ConsensusPayload[util.Uint256]) {
if err := p.(*Payload).Sign(s.dbft.Priv.(*privateKey)); err != nil {
s.log.Warn("can't sign consensus payload", zap.Error(err))
}
@ -475,7 +505,7 @@ func (s *service) broadcast(p payload.ConsensusPayload) {
s.Config.Broadcast(ep)
}
func (s *service) getTx(h util.Uint256) block.Transaction {
func (s *service) getTx(h util.Uint256) dbft.Transaction[util.Uint256] {
if tx := s.txx.Get(h); tx != nil {
return tx.(*transaction.Transaction)
}
@ -491,7 +521,7 @@ func (s *service) getTx(h util.Uint256) block.Transaction {
return nil
}
func (s *service) verifyBlock(b block.Block) bool {
func (s *service) verifyBlock(b dbft.Block[util.Uint256]) bool {
coreb := &b.(*neoBlock).Block
if s.Chain.BlockHeight() >= coreb.Index {
@ -558,12 +588,12 @@ var (
errInvalidTransactionsCount = errors.New("invalid transactions count")
)
func (s *service) verifyRequest(p payload.ConsensusPayload) error {
func (s *service) verifyRequest(p dbft.ConsensusPayload[util.Uint256]) error {
req := p.GetPrepareRequest().(*prepareRequest)
if req.prevHash != s.dbft.PrevHash {
return errInvalidPrevHash
}
if req.version != s.dbft.Version {
if req.version != coreb.VersionInitial {
return errInvalidVersion
}
if s.ProtocolConfiguration.StateRootInHeader {
@ -583,7 +613,11 @@ func (s *service) verifyRequest(p payload.ConsensusPayload) error {
return nil
}
func (s *service) processBlock(b block.Block) {
func (s *service) verifyResponse(p dbft.ConsensusPayload[util.Uint256]) error {
return nil
}
func (s *service) processBlock(b dbft.Block[util.Uint256]) {
bb := &b.(*neoBlock).Block
bb.Script = *(s.getBlockWitness(bb))
@ -638,7 +672,7 @@ func (s *service) getBlockWitness(b *coreb.Block) *transaction.Witness {
}
}
func (s *service) getBlock(h util.Uint256) block.Block {
func (s *service) getBlock(h util.Uint256) dbft.Block[util.Uint256] {
b, err := s.Chain.GetBlock(h)
if err != nil {
return nil
@ -647,7 +681,7 @@ func (s *service) getBlock(h util.Uint256) block.Block {
return &neoBlock{network: s.ProtocolConfiguration.Magic, Block: *b}
}
func (s *service) getVerifiedTx() []block.Transaction {
func (s *service) getVerifiedTx() []dbft.Transaction[util.Uint256] {
pool := s.Config.Chain.GetMemPool()
var txx []*transaction.Transaction
@ -671,7 +705,7 @@ func (s *service) getVerifiedTx() []block.Transaction {
txx = s.Config.Chain.ApplyPolicyToTxSet(txx)
}
res := make([]block.Transaction, len(txx))
res := make([]dbft.Transaction[util.Uint256], len(txx))
for i := range txx {
res[i] = txx[i]
}
@ -679,7 +713,7 @@ func (s *service) getVerifiedTx() []block.Transaction {
return res
}
func (s *service) getValidators(txes ...block.Transaction) []crypto.PublicKey {
func (s *service) getValidators(txes ...dbft.Transaction[util.Uint256]) []dbft.PublicKey {
var (
pKeys []*keys.PublicKey
err error
@ -699,7 +733,7 @@ func (s *service) getValidators(txes ...block.Transaction) []crypto.PublicKey {
s.log.Error("error while trying to get validators", zap.Error(err))
}
pubs := make([]crypto.PublicKey, len(pKeys))
pubs := make([]dbft.PublicKey, len(pKeys))
for i := range pKeys {
pubs[i] = &publicKey{PublicKey: pKeys[i]}
}
@ -707,11 +741,7 @@ func (s *service) getValidators(txes ...block.Transaction) []crypto.PublicKey {
return pubs
}
func (s *service) getConsensusAddress(validators ...crypto.PublicKey) util.Uint160 {
return util.Uint160{}
}
func convertKeys(validators []crypto.PublicKey) (pubs []*keys.PublicKey) {
func convertKeys(validators []dbft.PublicKey) (pubs []*keys.PublicKey) {
pubs = make([]*keys.PublicKey, len(validators))
for i, k := range validators {
pubs[i] = k.(*publicKey).PublicKey
@ -720,7 +750,7 @@ func convertKeys(validators []crypto.PublicKey) (pubs []*keys.PublicKey) {
return
}
func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
func (s *service) newBlockFromContext(ctx *dbft.Context[util.Uint256]) dbft.Block[util.Uint256] {
block := &neoBlock{network: s.ProtocolConfiguration.Magic}
block.Block.Timestamp = ctx.Timestamp / nsInMs
@ -750,9 +780,9 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
if err != nil {
s.log.Fatal(fmt.Sprintf("failed to create multisignature script: %s", err.Error()))
}
block.Block.NextConsensus = crypto.Hash160(script)
block.Block.NextConsensus = hash.Hash160(script)
block.Block.PrevHash = ctx.PrevHash
block.Block.Version = ctx.Version
block.Block.Version = coreb.VersionInitial
primaryIndex := byte(ctx.PrimaryIndex)
block.Block.PrimaryIndex = primaryIndex

View file

@ -4,9 +4,7 @@ import (
"testing"
"time"
"github.com/nspcc-dev/dbft/block"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft/timer"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/config"
@ -38,7 +36,7 @@ func TestNewService(t *testing.T) {
signTx(t, srv.Chain, tx)
require.NoError(t, srv.Chain.PoolTx(tx))
var txx []block.Transaction
var txx []dbft.Transaction[util.Uint256]
require.NotPanics(t, func() { txx = srv.getVerifiedTx() })
require.Len(t, txx, 1)
require.Equal(t, tx, txx[0])
@ -65,10 +63,10 @@ func TestNewWatchingService(t *testing.T) {
func collectBlock(t *testing.T, bc *core.Blockchain, srv *service) {
h := bc.BlockHeight()
srv.dbft.OnTimeout(timer.HV{Height: srv.dbft.Context.BlockIndex}) // Collect and add block to the chain.
srv.dbft.OnTimeout(srv.dbft.Context.BlockIndex, 0) // Collect and add block to the chain.
header, err := bc.GetHeader(bc.GetHeaderHash(h + 1))
require.NoError(t, err)
srv.dbft.InitializeConsensus(0, header.Timestamp*nsInMs) // Init consensus manually at the next height, as we don't run the consensus service.
srv.dbft.Reset(header.Timestamp * nsInMs) // Init consensus manually at the next height, as we don't run the consensus service.
}
func initServiceNextConsensus(t *testing.T, newAcc *wallet.Account, offset uint32) (*service, *wallet.Account) {
@ -102,7 +100,7 @@ func initServiceNextConsensus(t *testing.T, newAcc *wallet.Account, offset uint3
srv.dbft.Start(0)
header, err := bc.GetHeader(bc.GetHeaderHash(h + 1))
require.NoError(t, err)
srv.dbft.InitializeConsensus(0, header.Timestamp*nsInMs) // Init consensus manually at the next height, as we don't run the consensus service.
srv.dbft.Reset(header.Timestamp * nsInMs) // Init consensus manually at the next height, as we don't run the consensus service.
// Register new candidate.
b.Reset()
@ -214,10 +212,10 @@ func TestService_GetVerified(t *testing.T) {
p := new(Payload)
// One PrepareRequest and three ChangeViews.
if i == 1 {
p.SetType(payload.PrepareRequestType)
p.SetType(dbft.PrepareRequestType)
p.SetPayload(&prepareRequest{prevHash: srv.Chain.CurrentBlockHash(), transactionHashes: hashes})
} else {
p.SetType(payload.ChangeViewType)
p.SetType(dbft.ChangeViewType)
p.SetPayload(&changeView{newViewNumber: 1, timestamp: uint64(time.Now().UnixNano() / nsInMs)})
}
p.SetHeight(1)

View file

@ -3,7 +3,7 @@ package consensus
import (
"fmt"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/io"
npayload "github.com/nspcc-dev/neo-go/pkg/network/payload"
@ -55,12 +55,12 @@ func (p *Payload) SetViewNumber(view byte) {
}
// Type implements the payload.ConsensusPayload interface.
func (p Payload) Type() payload.MessageType {
return payload.MessageType(p.message.Type)
func (p Payload) Type() dbft.MessageType {
return dbft.MessageType(p.message.Type)
}
// SetType implements the payload.ConsensusPayload interface.
func (p *Payload) SetType(t payload.MessageType) {
func (p *Payload) SetType(t dbft.MessageType) {
p.message.Type = messageType(t)
}
@ -75,29 +75,29 @@ func (p *Payload) SetPayload(pl any) {
}
// GetChangeView implements the payload.ConsensusPayload interface.
func (p Payload) GetChangeView() payload.ChangeView { return p.payload.(payload.ChangeView) }
func (p Payload) GetChangeView() dbft.ChangeView { return p.payload.(dbft.ChangeView) }
// GetPrepareRequest implements the payload.ConsensusPayload interface.
func (p Payload) GetPrepareRequest() payload.PrepareRequest {
return p.payload.(payload.PrepareRequest)
func (p Payload) GetPrepareRequest() dbft.PrepareRequest[util.Uint256] {
return p.payload.(dbft.PrepareRequest[util.Uint256])
}
// GetPrepareResponse implements the payload.ConsensusPayload interface.
func (p Payload) GetPrepareResponse() payload.PrepareResponse {
return p.payload.(payload.PrepareResponse)
func (p Payload) GetPrepareResponse() dbft.PrepareResponse[util.Uint256] {
return p.payload.(dbft.PrepareResponse[util.Uint256])
}
// GetCommit implements the payload.ConsensusPayload interface.
func (p Payload) GetCommit() payload.Commit { return p.payload.(payload.Commit) }
func (p Payload) GetCommit() dbft.Commit { return p.payload.(dbft.Commit) }
// GetRecoveryRequest implements the payload.ConsensusPayload interface.
func (p Payload) GetRecoveryRequest() payload.RecoveryRequest {
return p.payload.(payload.RecoveryRequest)
func (p Payload) GetRecoveryRequest() dbft.RecoveryRequest {
return p.payload.(dbft.RecoveryRequest)
}
// GetRecoveryMessage implements the payload.ConsensusPayload interface.
func (p Payload) GetRecoveryMessage() payload.RecoveryMessage {
return p.payload.(payload.RecoveryMessage)
func (p Payload) GetRecoveryMessage() dbft.RecoveryMessage[util.Uint256] {
return p.payload.(dbft.RecoveryMessage[util.Uint256])
}
// ValidatorIndex implements the payload.ConsensusPayload interface.

View file

@ -6,7 +6,7 @@ import (
"math/rand"
"testing"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
@ -47,8 +47,8 @@ func TestConsensusPayload_Setters(t *testing.T) {
p.SetViewNumber(2)
assert.EqualValues(t, 2, p.ViewNumber())
p.SetType(payload.PrepareRequestType)
assert.Equal(t, payload.PrepareRequestType, p.Type())
p.SetType(dbft.PrepareRequestType)
assert.Equal(t, dbft.PrepareRequestType, p.Type())
pl := randomMessage(t, prepareRequestType)
p.SetPayload(pl)
@ -290,7 +290,7 @@ func TestPayload_DecodeFromPrivnet(t *testing.T) {
p := NewPayload(netmode.PrivNet, false)
p.DecodeBinary(buf)
require.NoError(t, buf.Err)
require.Equal(t, payload.CommitType, p.Type())
require.Equal(t, dbft.CommitType, p.Type())
require.Equal(t, uint32(2), p.Height())
require.Equal(t, uint16(3), p.ValidatorIndex())
require.Equal(t, byte(0), p.ViewNumber())

View file

@ -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/core/block"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -18,7 +18,7 @@ type prepareRequest struct {
stateRoot util.Uint256
}
var _ payload.PrepareRequest = (*prepareRequest)(nil)
var _ dbft.PrepareRequest[util.Uint256] = (*prepareRequest)(nil)
// EncodeBinary implements the io.Serializable interface.
func (p *prepareRequest) EncodeBinary(w *io.BinWriter) {

View file

@ -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"
"github.com/nspcc-dev/neo-go/pkg/util"
)
@ -11,7 +11,7 @@ type prepareResponse struct {
preparationHash util.Uint256
}
var _ payload.PrepareResponse = (*prepareResponse)(nil)
var _ dbft.PrepareResponse[util.Uint256] = (*prepareResponse)(nil)
// EncodeBinary implements the io.Serializable interface.
func (p *prepareResponse) EncodeBinary(w *io.BinWriter) {
@ -25,6 +25,3 @@ func (p *prepareResponse) DecodeBinary(r *io.BinReader) {
// PreparationHash implements the payload.PrepareResponse interface.
func (p *prepareResponse) PreparationHash() util.Uint256 { return p.preparationHash }
// SetPreparationHash implements the payload.PrepareResponse interface.
func (p *prepareResponse) SetPreparationHash(h util.Uint256) { p.preparationHash = h }

View file

@ -8,8 +8,9 @@ import (
)
func TestPrepareResponse_Setters(t *testing.T) {
var p prepareResponse
var p = prepareResponse{
preparationHash: util.Uint256{1, 2, 3},
}
p.SetPreparationHash(util.Uint256{1, 2, 3})
require.Equal(t, util.Uint256{1, 2, 3}, p.PreparationHash())
}

View file

@ -3,8 +3,7 @@ package consensus
import (
"errors"
"github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/io"
npayload "github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -41,7 +40,7 @@ type (
}
)
var _ payload.RecoveryMessage = (*recoveryMessage)(nil)
var _ dbft.RecoveryMessage[util.Uint256] = (*recoveryMessage)(nil)
// DecodeBinary implements the io.Serializable interface.
func (m *recoveryMessage) DecodeBinary(r *io.BinReader) {
@ -139,11 +138,11 @@ func (p *preparationCompact) EncodeBinary(w *io.BinWriter) {
}
// AddPayload implements the payload.RecoveryMessage interface.
func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
func (m *recoveryMessage) AddPayload(p dbft.ConsensusPayload[util.Uint256]) {
validator := uint8(p.ValidatorIndex())
switch p.Type() {
case payload.PrepareRequestType:
case dbft.PrepareRequestType:
m.prepareRequest = &message{
Type: prepareRequestType,
ViewNumber: p.ViewNumber(),
@ -156,24 +155,24 @@ func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
ValidatorIndex: validator,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
case payload.PrepareResponseType:
case dbft.PrepareResponseType:
m.preparationPayloads = append(m.preparationPayloads, &preparationCompact{
ValidatorIndex: validator,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
if m.preparationHash == nil {
h := p.GetPrepareResponse().PreparationHash()
h := p.GetPrepareResponse().(*prepareResponse).preparationHash
m.preparationHash = &h
}
case payload.ChangeViewType:
case dbft.ChangeViewType:
m.changeViewPayloads = append(m.changeViewPayloads, &changeViewCompact{
ValidatorIndex: validator,
OriginalViewNumber: p.ViewNumber(),
Timestamp: p.GetChangeView().Timestamp() / nsInMs,
Timestamp: p.GetChangeView().(*changeView).timestamp,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
case payload.CommitType:
case dbft.CommitType:
m.commitPayloads = append(m.commitPayloads, &commitCompact{
ValidatorIndex: validator,
ViewNumber: p.ViewNumber(),
@ -184,7 +183,7 @@ func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
}
// GetPrepareRequest implements the payload.RecoveryMessage interface.
func (m *recoveryMessage) GetPrepareRequest(p payload.ConsensusPayload, validators []crypto.PublicKey, primary uint16) payload.ConsensusPayload {
func (m *recoveryMessage) GetPrepareRequest(p dbft.ConsensusPayload[util.Uint256], validators []dbft.PublicKey, primary uint16) dbft.ConsensusPayload[util.Uint256] {
if m.prepareRequest == nil {
return nil
}
@ -211,12 +210,12 @@ func (m *recoveryMessage) GetPrepareRequest(p payload.ConsensusPayload, validato
}
// GetPrepareResponses implements the payload.RecoveryMessage interface.
func (m *recoveryMessage) GetPrepareResponses(p payload.ConsensusPayload, validators []crypto.PublicKey) []payload.ConsensusPayload {
func (m *recoveryMessage) GetPrepareResponses(p dbft.ConsensusPayload[util.Uint256], validators []dbft.PublicKey) []dbft.ConsensusPayload[util.Uint256] {
if m.preparationHash == nil {
return nil
}
ps := make([]payload.ConsensusPayload, len(m.preparationPayloads))
ps := make([]dbft.ConsensusPayload[util.Uint256], len(m.preparationPayloads))
for i, resp := range m.preparationPayloads {
r := fromPayload(prepareResponseType, p.(*Payload), &prepareResponse{
@ -234,8 +233,8 @@ func (m *recoveryMessage) GetPrepareResponses(p payload.ConsensusPayload, valida
}
// GetChangeViews implements the payload.RecoveryMessage interface.
func (m *recoveryMessage) GetChangeViews(p payload.ConsensusPayload, validators []crypto.PublicKey) []payload.ConsensusPayload {
ps := make([]payload.ConsensusPayload, len(m.changeViewPayloads))
func (m *recoveryMessage) GetChangeViews(p dbft.ConsensusPayload[util.Uint256], validators []dbft.PublicKey) []dbft.ConsensusPayload[util.Uint256] {
ps := make([]dbft.ConsensusPayload[util.Uint256], len(m.changeViewPayloads))
for i, cv := range m.changeViewPayloads {
c := fromPayload(changeViewType, p.(*Payload), &changeView{
@ -255,8 +254,8 @@ func (m *recoveryMessage) GetChangeViews(p payload.ConsensusPayload, validators
}
// GetCommits implements the payload.RecoveryMessage interface.
func (m *recoveryMessage) GetCommits(p payload.ConsensusPayload, validators []crypto.PublicKey) []payload.ConsensusPayload {
ps := make([]payload.ConsensusPayload, len(m.commitPayloads))
func (m *recoveryMessage) GetCommits(p dbft.ConsensusPayload[util.Uint256], validators []dbft.PublicKey) []dbft.ConsensusPayload[util.Uint256] {
ps := make([]dbft.ConsensusPayload[util.Uint256], len(m.commitPayloads))
for i, c := range m.commitPayloads {
cc := fromPayload(commitType, p.(*Payload), &commit{signature: c.Signature})
@ -281,7 +280,7 @@ func (m *recoveryMessage) SetPreparationHash(h *util.Uint256) {
m.preparationHash = h
}
func getVerificationScript(i uint8, validators []crypto.PublicKey) []byte {
func getVerificationScript(i uint8, validators []dbft.PublicKey) []byte {
if int(i) >= len(validators) {
return nil
}

View file

@ -3,8 +3,7 @@ package consensus
import (
"testing"
"github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -23,7 +22,7 @@ func TestRecoveryMessageSetters(t *testing.T) {
func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
srv := newTestServiceWithState(t, enableStateRoot)
privs := make([]*privateKey, testchain.Size())
pubs := make([]crypto.PublicKey, testchain.Size())
pubs := make([]dbft.PublicKey, testchain.Size())
for i := 0; i < testchain.Size(); i++ {
privs[i], pubs[i] = getTestValidator(i)
}
@ -32,7 +31,7 @@ func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
r := &recoveryMessage{stateRootEnabled: enableStateRoot}
p := NewPayload(netmode.UnitTestNet, enableStateRoot)
p.SetType(payload.RecoveryMessageType)
p.SetType(dbft.RecoveryMessageType)
p.SetHeight(msgHeight)
p.SetPayload(r)
// sign payload to have verification script
@ -44,7 +43,7 @@ func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
stateRootEnabled: enableStateRoot,
}
p1 := NewPayload(netmode.UnitTestNet, enableStateRoot)
p1.SetType(payload.PrepareRequestType)
p1.SetType(dbft.PrepareRequestType)
p1.SetHeight(msgHeight)
p1.SetPayload(req)
p1.SetValidatorIndex(0)
@ -53,7 +52,7 @@ func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
t.Run("prepare response is added", func(t *testing.T) {
p2 := NewPayload(netmode.UnitTestNet, enableStateRoot)
p2.SetType(payload.PrepareResponseType)
p2.SetType(dbft.PrepareResponseType)
p2.SetHeight(msgHeight)
p2.SetPayload(&prepareResponse{
preparationHash: p1.Hash(),
@ -91,7 +90,7 @@ func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
t.Run("change view is added", func(t *testing.T) {
p3 := NewPayload(netmode.UnitTestNet, enableStateRoot)
p3.SetType(payload.ChangeViewType)
p3.SetType(dbft.ChangeViewType)
p3.SetHeight(msgHeight)
p3.SetPayload(&changeView{
newViewNumber: 1,
@ -115,7 +114,7 @@ func testRecoveryMessageSetters(t *testing.T, enableStateRoot bool) {
t.Run("commit is added", func(t *testing.T) {
p4 := NewPayload(netmode.UnitTestNet, enableStateRoot)
p4.SetType(payload.CommitType)
p4.SetType(dbft.CommitType)
p4.SetHeight(msgHeight)
p4.SetPayload(randomMessage(t, commitType))
p4.SetValidatorIndex(3)

View file

@ -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"
)
@ -10,7 +10,7 @@ type recoveryRequest struct {
timestamp uint64
}
var _ payload.RecoveryRequest = (*recoveryRequest)(nil)
var _ dbft.RecoveryRequest = (*recoveryRequest)(nil)
// DecodeBinary implements the io.Serializable interface.
func (m *recoveryRequest) DecodeBinary(r *io.BinReader) {

View file

@ -13,6 +13,9 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
)
// VersionInitial is the default Neo block version.
const VersionInitial uint32 = 0
// Header holds the base info of a block.
type Header struct {
// Version of the block.