Skip to content

Commit

Permalink
internal: split simulation from consensus service implementation
Browse files Browse the repository at this point in the history
All dBFT payloads implementations should be kept in the same package or
have exported fields. It's inevitable since dBFT user have to refer to
payload fields via converting dBFT interfaces to user-defined local
structures.

dBFT test (`dbft_test` package) imports custom payloads implementations
from `internal`. Since all payloads will be moved to the same package,
we need to split this package from `main` package of simulation.

A consequence of #84.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Mar 6, 2024
1 parent f2ac5ce commit 76d0832
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 40 deletions.
61 changes: 61 additions & 0 deletions internal/consensus/consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package consensus

import (
"time"

"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/dbft/internal/block"
"github.com/nspcc-dev/dbft/internal/crypto"
"github.com/nspcc-dev/dbft/internal/payload"
"go.uber.org/zap"
)

func New(logger *zap.Logger, key dbft.PrivateKey, pub dbft.PublicKey,
getTx func(uint256 crypto.Uint256) dbft.Transaction[crypto.Uint256],
getVerified func() []dbft.Transaction[crypto.Uint256],
broadcast func(dbft.ConsensusPayload[crypto.Uint256]),
processBlock func(dbft.Block[crypto.Uint256]),
currentHeight func() uint32,
currentBlockHash func() crypto.Uint256,
getValidators func(...dbft.Transaction[crypto.Uint256]) []dbft.PublicKey,
verifyPayload func(consensusPayload dbft.ConsensusPayload[crypto.Uint256]) error) *dbft.DBFT[crypto.Uint256] {
return dbft.New[crypto.Uint256](
dbft.WithLogger[crypto.Uint256](logger),
dbft.WithSecondsPerBlock[crypto.Uint256](time.Second*5),
dbft.WithKeyPair[crypto.Uint256](key, pub),
dbft.WithGetTx[crypto.Uint256](getTx),
dbft.WithGetVerified[crypto.Uint256](getVerified),
dbft.WithBroadcast[crypto.Uint256](broadcast),
dbft.WithProcessBlock[crypto.Uint256](processBlock),
dbft.WithCurrentHeight[crypto.Uint256](currentHeight),
dbft.WithCurrentBlockHash[crypto.Uint256](currentBlockHash),
dbft.WithGetValidators[crypto.Uint256](getValidators),
dbft.WithVerifyPrepareRequest[crypto.Uint256](verifyPayload),
dbft.WithVerifyPrepareResponse[crypto.Uint256](verifyPayload),

dbft.WithNewBlockFromContext[crypto.Uint256](newBlockFromContext),
dbft.WithNewConsensusPayload[crypto.Uint256](defaultNewConsensusPayload),
dbft.WithNewPrepareRequest[crypto.Uint256](payload.NewPrepareRequest),
dbft.WithNewPrepareResponse[crypto.Uint256](payload.NewPrepareResponse),
dbft.WithNewChangeView[crypto.Uint256](payload.NewChangeView),
dbft.WithNewCommit[crypto.Uint256](payload.NewCommit),
dbft.WithNewRecoveryMessage[crypto.Uint256](func() dbft.RecoveryMessage[crypto.Uint256] {
return payload.NewRecoveryMessage(nil)
}),
dbft.WithNewRecoveryRequest[crypto.Uint256](payload.NewRecoveryRequest),
)
}

func newBlockFromContext(ctx *dbft.Context[crypto.Uint256]) dbft.Block[crypto.Uint256] {
if ctx.TransactionHashes == nil {
return nil
}
block := block.NewBlock(ctx.Timestamp, ctx.BlockIndex, ctx.PrevHash, ctx.Nonce, ctx.TransactionHashes)
return block
}

// defaultNewConsensusPayload is default function for creating
// consensus payload of specific type.
func defaultNewConsensusPayload(c *dbft.Context[crypto.Uint256], t dbft.MessageType, msg any) dbft.ConsensusPayload[crypto.Uint256] {
return payload.NewConsensusPayload(t, c.BlockIndex, uint16(c.MyIndex), c.ViewNumber, msg)
}
49 changes: 9 additions & 40 deletions internal/simulation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import (
"time"

"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/dbft/internal/block"
"github.com/nspcc-dev/dbft/internal/consensus"
"github.com/nspcc-dev/dbft/internal/crypto"
"github.com/nspcc-dev/dbft/internal/payload"
"github.com/twmb/murmur3"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -110,20 +109,6 @@ func initNodes(nodes []*simNode, log *zap.Logger) {
}
}

func newBlockFromContext(ctx *dbft.Context[crypto.Uint256]) dbft.Block[crypto.Uint256] {
if ctx.TransactionHashes == nil {
return nil
}
block := block.NewBlock(ctx.Timestamp, ctx.BlockIndex, ctx.PrevHash, ctx.Nonce, ctx.TransactionHashes)
return block
}

// defaultNewConsensusPayload is default function for creating
// consensus payload of specific type.
func defaultNewConsensusPayload(c *dbft.Context[crypto.Uint256], t dbft.MessageType, msg any) dbft.ConsensusPayload[crypto.Uint256] {
return payload.NewConsensusPayload(t, c.BlockIndex, uint16(c.MyIndex), c.ViewNumber, msg)
}

func initSimNode(nodes []*simNode, i int, log *zap.Logger) error {
key, pub := crypto.Generate(rand.Reader)
nodes[i] = &simNode{
Expand All @@ -136,30 +121,14 @@ func initSimNode(nodes []*simNode, i int, log *zap.Logger) error {
cluster: nodes,
}

nodes[i].d = dbft.New[crypto.Uint256](
dbft.WithLogger[crypto.Uint256](nodes[i].log),
dbft.WithSecondsPerBlock[crypto.Uint256](time.Second*5),
dbft.WithKeyPair[crypto.Uint256](key, pub),
dbft.WithGetTx[crypto.Uint256](nodes[i].pool.Get),
dbft.WithGetVerified[crypto.Uint256](nodes[i].pool.GetVerified),
dbft.WithBroadcast[crypto.Uint256](nodes[i].Broadcast),
dbft.WithProcessBlock[crypto.Uint256](nodes[i].ProcessBlock),
dbft.WithCurrentHeight[crypto.Uint256](nodes[i].CurrentHeight),
dbft.WithCurrentBlockHash[crypto.Uint256](nodes[i].CurrentBlockHash),
dbft.WithGetValidators[crypto.Uint256](nodes[i].GetValidators),
dbft.WithVerifyPrepareRequest[crypto.Uint256](nodes[i].VerifyPayload),
dbft.WithVerifyPrepareResponse[crypto.Uint256](nodes[i].VerifyPayload),

dbft.WithNewBlockFromContext[crypto.Uint256](newBlockFromContext),
dbft.WithNewConsensusPayload[crypto.Uint256](defaultNewConsensusPayload),
dbft.WithNewPrepareRequest[crypto.Uint256](payload.NewPrepareRequest),
dbft.WithNewPrepareResponse[crypto.Uint256](payload.NewPrepareResponse),
dbft.WithNewChangeView[crypto.Uint256](payload.NewChangeView),
dbft.WithNewCommit[crypto.Uint256](payload.NewCommit),
dbft.WithNewRecoveryMessage[crypto.Uint256](func() dbft.RecoveryMessage[crypto.Uint256] {
return payload.NewRecoveryMessage(nil)
}),
dbft.WithNewRecoveryRequest[crypto.Uint256](payload.NewRecoveryRequest),
nodes[i].d = consensus.New(nodes[i].log, key, pub, nodes[i].pool.Get,
nodes[i].pool.GetVerified,
nodes[i].Broadcast,
nodes[i].ProcessBlock,
nodes[i].CurrentHeight,
nodes[i].CurrentBlockHash,
nodes[i].GetValidators,
nodes[i].VerifyPayload,
)

if nodes[i].d == nil {
Expand Down

0 comments on commit 76d0832

Please sign in to comment.