diff --git a/pkg/systems/smr/system.go b/pkg/systems/smr/system.go index a4ba5c57f..91e596fb3 100644 --- a/pkg/systems/smr/system.go +++ b/pkg/systems/smr/system.go @@ -4,18 +4,17 @@ import ( "github.com/libp2p/go-libp2p-core/host" "github.com/pkg/errors" - "github.com/filecoin-project/mir/pkg/net" - - "github.com/filecoin-project/mir/pkg/checkpoint" - "github.com/filecoin-project/mir/pkg/availability/batchdb/fakebatchdb" "github.com/filecoin-project/mir/pkg/availability/multisigcollector" "github.com/filecoin-project/mir/pkg/batchfetcher" + "github.com/filecoin-project/mir/pkg/checkpoint" mircrypto "github.com/filecoin-project/mir/pkg/crypto" + "github.com/filecoin-project/mir/pkg/eventmangler" "github.com/filecoin-project/mir/pkg/iss" "github.com/filecoin-project/mir/pkg/logging" "github.com/filecoin-project/mir/pkg/mempool/simplemempool" "github.com/filecoin-project/mir/pkg/modules" + "github.com/filecoin-project/mir/pkg/net" libp2pnet "github.com/filecoin-project/mir/pkg/net/libp2p" t "github.com/filecoin-project/mir/pkg/types" ) @@ -50,6 +49,28 @@ func (sys *System) WithModule(moduleID t.ModuleID, module modules.Module) *Syste return sys } +// PerturbMessages configures the SMR system to randomly drop and delay some of the messages sent over the network. +// Useful for debugging and stress-testing. +// The params argument defines parameters of the perturbation, such as how many messages should be dropped +// and how the remaining messages should be delayed. +func (sys *System) PerturbMessages(params *eventmangler.ModuleParams) error { + + // Create event mangler perturbing (dropping and delaying) events. + messageMangler, err := eventmangler.NewModule( + &eventmangler.ModuleConfig{Self: "net", Dest: "truenet", Timer: "timer"}, + params, + ) + if err != nil { + return err + } + + // Intercept all events (in this case SendMessage events) directed to the "net" module by the mangler + // And change the actual transport module ID to "truenet", where the mangler forwards the surviving messages. + sys.modules[iss.DefaultModuleConfig().Net] = messageMangler + sys.modules["truenet"] = sys.transport + return nil +} + // Start starts the operation of the modules of the SMR system. // It starts the network transport and connects to the initial members of the system. func (sys *System) Start() error {