-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: split simulation from consensus service implementation
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
1 parent
02deb6d
commit d9c1b95
Showing
2 changed files
with
70 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, crypto.Uint160]), | ||
processBlock func(dbft.Block[crypto.Uint256, crypto.Uint160]), | ||
currentHeight func() uint32, | ||
currentBlockHash func() crypto.Uint256, | ||
getValidators func(...dbft.Transaction[crypto.Uint256]) []dbft.PublicKey, | ||
verifyPayload func(consensusPayload dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160]) error) *dbft.DBFT[crypto.Uint256, crypto.Uint160] { | ||
return dbft.New[crypto.Uint256, crypto.Uint160]( | ||
dbft.WithLogger[crypto.Uint256, crypto.Uint160](logger), | ||
dbft.WithSecondsPerBlock[crypto.Uint256, crypto.Uint160](time.Second*5), | ||
dbft.WithKeyPair[crypto.Uint256, crypto.Uint160](key, pub), | ||
dbft.WithGetTx[crypto.Uint256, crypto.Uint160](getTx), | ||
dbft.WithGetVerified[crypto.Uint256, crypto.Uint160](getVerified), | ||
dbft.WithBroadcast[crypto.Uint256, crypto.Uint160](broadcast), | ||
dbft.WithProcessBlock[crypto.Uint256, crypto.Uint160](processBlock), | ||
dbft.WithCurrentHeight[crypto.Uint256, crypto.Uint160](currentHeight), | ||
dbft.WithCurrentBlockHash[crypto.Uint256, crypto.Uint160](currentBlockHash), | ||
dbft.WithGetValidators[crypto.Uint256, crypto.Uint160](getValidators), | ||
dbft.WithVerifyPrepareRequest[crypto.Uint256, crypto.Uint160](verifyPayload), | ||
dbft.WithVerifyPrepareResponse[crypto.Uint256, crypto.Uint160](verifyPayload), | ||
|
||
dbft.WithNewBlockFromContext[crypto.Uint256, crypto.Uint160](newBlockFromContext), | ||
dbft.WithNewConsensusPayload[crypto.Uint256, crypto.Uint160](defaultNewConsensusPayload), | ||
dbft.WithNewPrepareRequest[crypto.Uint256, crypto.Uint160](payload.NewPrepareRequest), | ||
dbft.WithNewPrepareResponse[crypto.Uint256, crypto.Uint160](payload.NewPrepareResponse), | ||
dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](payload.NewChangeView), | ||
dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](payload.NewCommit), | ||
dbft.WithNewRecoveryMessage[crypto.Uint256, crypto.Uint160](func() dbft.RecoveryMessage[crypto.Uint256, crypto.Uint160] { | ||
return payload.NewRecoveryMessage(nil) | ||
}), | ||
dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](payload.NewRecoveryRequest), | ||
) | ||
} | ||
|
||
func newBlockFromContext(ctx *dbft.Context[crypto.Uint256, crypto.Uint160]) dbft.Block[crypto.Uint256, crypto.Uint160] { | ||
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, crypto.Uint160], t dbft.MessageType, msg any) dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160] { | ||
return payload.NewConsensusPayload(t, c.BlockIndex, uint16(c.MyIndex), c.ViewNumber, msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters