Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use protobuf structs for serialization #512

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmd/bench/cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/filecoin-project/mir/pkg/logging"
"github.com/filecoin-project/mir/pkg/membership"
libp2p2 "github.com/filecoin-project/mir/pkg/net/libp2p"
"github.com/filecoin-project/mir/pkg/pb/batchfetcherpb"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
"github.com/filecoin-project/mir/pkg/pb/mempoolpb"
batchfetcherpbtypes "github.com/filecoin-project/mir/pkg/pb/batchfetcherpb/types"
eventpbtypes "github.com/filecoin-project/mir/pkg/pb/eventpb/types"
mempoolpbtypes "github.com/filecoin-project/mir/pkg/pb/mempoolpb/types"
"github.com/filecoin-project/mir/pkg/transactionreceiver"
"github.com/filecoin-project/mir/pkg/trantor"
t "github.com/filecoin-project/mir/pkg/types"
Expand Down Expand Up @@ -141,16 +141,16 @@ func runNode() error {
ownID,
"bench-output",
logging.Decorate(logger, "EVTLOG: "),
eventlog.EventFilterOpt(func(e *eventpb.Event) bool {
eventlog.EventFilterOpt(func(e *eventpbtypes.Event) bool {
switch e := e.Type.(type) {
case *eventpb.Event_Mempool:
case *eventpbtypes.Event_Mempool:
switch e.Mempool.Type.(type) {
case *mempoolpb.Event_NewTransactions:
case *mempoolpbtypes.Event_NewTransactions:
return true
}
case *eventpb.Event_BatchFetcher:
case *eventpbtypes.Event_BatchFetcher:
switch e.BatchFetcher.Type.(type) {
case *batchfetcherpb.Event_NewOrderedBatch:
case *batchfetcherpbtypes.Event_NewOrderedBatch:
return true
}
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/bench/stats/stat-interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package stats

import (
"github.com/filecoin-project/mir/pkg/events"
bfpb "github.com/filecoin-project/mir/pkg/pb/batchfetcherpb"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
"github.com/filecoin-project/mir/pkg/pb/mempoolpb"
batchfetcherpbtypes "github.com/filecoin-project/mir/pkg/pb/batchfetcherpb/types"
eventpbtypes "github.com/filecoin-project/mir/pkg/pb/eventpb/types"
mempoolpbtypes "github.com/filecoin-project/mir/pkg/pb/mempoolpb/types"
t "github.com/filecoin-project/mir/pkg/types"
)

Expand Down Expand Up @@ -39,22 +39,22 @@ func (i *StatInterceptor) Intercept(events *events.EventList) error {
for evt := it.Next(); evt != nil; evt = it.Next() {

switch e := evt.Type.(type) {
case *eventpb.Event_Mempool:
case *eventpbtypes.Event_Mempool:
switch e := e.Mempool.Type.(type) {
case *mempoolpb.Event_NewTransactions:
case *mempoolpbtypes.Event_NewTransactions:
for _, tx := range e.NewTransactions.Transactions {
i.Stats.NewTX(tx)
}
}
case *eventpb.Event_BatchFetcher:
case *eventpbtypes.Event_BatchFetcher:

// Skip events destined to other modules than the one consuming the transactions.
if t.ModuleID(evt.DestModule) != i.txConsumerModule {
if evt.DestModule != i.txConsumerModule {
continue
}

switch e := e.BatchFetcher.Type.(type) {
case *bfpb.Event_NewOrderedBatch:
case *batchfetcherpbtypes.Event_NewOrderedBatch:
for _, tx := range e.NewOrderedBatch.Txs {
i.Stats.Delivered(tx)
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/bench/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"sync"
"time"

"github.com/filecoin-project/mir/pkg/pb/trantorpb"
trantorpbtypes "github.com/filecoin-project/mir/pkg/pb/trantorpb/types"

tt "github.com/filecoin-project/mir/pkg/trantor/types"
)

type Stats struct {
Expand All @@ -23,8 +25,8 @@ type Stats struct {
}

type txKey struct {
ClientID string
TxNo uint64
ClientID tt.ClientID
TxNo tt.TxNo
}

func NewStats() *Stats {
Expand All @@ -33,14 +35,14 @@ func NewStats() *Stats {
}
}

func (s *Stats) NewTX(tx *trantorpb.Transaction) {
func (s *Stats) NewTX(tx *trantorpbtypes.Transaction) {
s.lock.Lock()
k := txKey{tx.ClientId, tx.TxNo}
s.txTimestamps[k] = time.Now()
s.lock.Unlock()
}

func (s *Stats) Delivered(tx *trantorpb.Transaction) {
func (s *Stats) Delivered(tx *trantorpbtypes.Transaction) {
s.lock.Lock()
s.deliveredTransactions++
k := txKey{tx.ClientId, tx.TxNo}
Expand Down
6 changes: 4 additions & 2 deletions cmd/mircat/customtransform.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import "github.com/filecoin-project/mir/pkg/pb/eventpb"
import (
eventpbtypes "github.com/filecoin-project/mir/pkg/pb/eventpb/types"
)

// This function is applied to every event loaded from the event log
// and its return value is used instead of the original event.
// If customEventFilter returns nil, the event is ignored (this can be used for additional event filtering).
// It is meant for ad-hoc editing while debugging, to be able to select events in a fine-grained way.
func customTransform(e *eventpb.Event) *eventpb.Event {
func customTransform(e *eventpbtypes.Event) *eventpbtypes.Event {

//moduleID := ""
//if e.DestModule == moduleID {
Expand Down
9 changes: 5 additions & 4 deletions cmd/mircat/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
issconfig "github.com/filecoin-project/mir/pkg/iss/config"
"github.com/filecoin-project/mir/pkg/logging"
"github.com/filecoin-project/mir/pkg/modules"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
eventpbtypes "github.com/filecoin-project/mir/pkg/pb/eventpb/types"
"github.com/filecoin-project/mir/pkg/pb/recordingpb"
trantorpbtypes "github.com/filecoin-project/mir/pkg/pb/trantorpb/types"
t "github.com/filecoin-project/mir/pkg/types"
Expand Down Expand Up @@ -111,7 +111,8 @@ func debug(args *arguments) error {
}

// Process each event in the entry.
for _, event := range entry.Events {
for _, eventPb := range entry.Events {
event := eventpbtypes.EventFromPb(eventPb)

// Set the index of the event in the event log.
metadata.index = uint64(index)
Expand Down Expand Up @@ -217,7 +218,7 @@ func debuggerNode(id t.NodeID, membership *trantorpbtypes.Membership) (*mir.Node

// stopBeforeNext waits for two confirmations of the user, a confirmation being a new line on the standard input.
// After the first one, event is displayed and after the second one, the function returns.
func stopBeforeNext(event *eventpb.Event, metadata eventMetadata) {
func stopBeforeNext(event *eventpbtypes.Event, metadata eventMetadata) {
bufio.NewScanner(os.Stdin).Scan()
fmt.Printf("========================================\n")
fmt.Printf("Next step (%d):\n", metadata.index)
Expand All @@ -233,7 +234,7 @@ func printNodeOutput(eventChan chan *events.EventList) {
fmt.Printf("========================================\n")
fmt.Printf("Node produced the following events:\n\n")
for _, event := range receivedEvents.Slice() {
fmt.Println(protojson.Format(event))
fmt.Println(protojson.Format(event.Pb()))
}
}
}
16 changes: 9 additions & 7 deletions cmd/mircat/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/filecoin-project/mir/pkg/eventlog"
"github.com/filecoin-project/mir/pkg/events"
"github.com/filecoin-project/mir/pkg/modules"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
eventpbtypes "github.com/filecoin-project/mir/pkg/pb/eventpb/types"
"github.com/filecoin-project/mir/pkg/pb/recordingpb"
t "github.com/filecoin-project/mir/pkg/types"
)
Expand Down Expand Up @@ -68,7 +68,9 @@ func displayEvents(args *arguments) error { //nolint:gocognit
time: entry.Time,
}
// getting events from entry
for _, event := range entry.Events {
for _, eventPb := range entry.Events {
event := eventpbtypes.EventFromPb(eventPb)

metadata.index = uint64(index)

_, validEvent := args.selectedEventNames[eventName(event)]
Expand All @@ -79,7 +81,7 @@ func displayEvents(args *arguments) error { //nolint:gocognit
// If event type has been selected for displaying

switch e := event.Type.(type) {
case *eventpb.Event_Iss:
case *eventpbtypes.Event_Iss:
// Only display selected sub-types of the ISS Event
if _, validIssEvent := args.selectedIssEventNames[issEventName(e.Iss)]; validIssEvent {
displayEvent(event, metadata)
Expand Down Expand Up @@ -111,13 +113,13 @@ func displayEvents(args *arguments) error { //nolint:gocognit
}

// Displays one event according to its type.
func displayEvent(event *eventpb.Event, metadata eventMetadata) {
func displayEvent(event *eventpbtypes.Event, metadata eventMetadata) {

switch e := event.Type.(type) {
case *eventpb.Event_Iss:
display(fmt.Sprintf("%s : %s", eventName(event), issEventName(e.Iss)), protojson.Format(event), metadata)
case *eventpbtypes.Event_Iss:
display(fmt.Sprintf("%s : %s", eventName(event), issEventName(e.Iss)), protojson.Format(event.Pb()), metadata)
default:
display(eventName(event), protojson.Format(event), metadata)
display(eventName(event), protojson.Format(event.Pb()), metadata)
}
}

Expand Down
35 changes: 12 additions & 23 deletions cmd/mircat/eventloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"io"
"os"
"reflect"
"sort"
"strings"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/filecoin-project/mir/pkg/eventlog"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
"github.com/filecoin-project/mir/pkg/pb/isspb"
eventpbtypes "github.com/filecoin-project/mir/pkg/pb/eventpb/types"
isspbtypes "github.com/filecoin-project/mir/pkg/pb/isspb/types"
"github.com/filecoin-project/mir/pkg/pb/recordingpb"
t "github.com/filecoin-project/mir/pkg/types"
)
Expand All @@ -26,10 +25,10 @@ type eventMetadata struct {

// Returns the list of event names and destinations present in the given eventlog file,
// along with the total number of events present in the file.
func getEventList(filenames *[]string) (map[string]struct{}, map[string]struct{}, map[string]struct{}, int, error) {
func getEventList(filenames *[]string) (map[string]struct{}, map[string]struct{}, map[t.ModuleID]struct{}, int, error) {
events := make(map[string]struct{})
issEvents := make(map[string]struct{})
eventDests := make(map[string]struct{})
eventDests := make(map[t.ModuleID]struct{})

totalCount := 0
for _, filename := range *filenames {
Expand Down Expand Up @@ -60,15 +59,17 @@ func getEventList(filenames *[]string) (map[string]struct{}, map[string]struct{}
for entry, err = reader.ReadEntry(); err == nil; entry, err = reader.ReadEntry() {
// For each entry of the event log

for _, event := range entry.Events {
for _, eventPb := range entry.Events {
event := eventpbtypes.EventFromPb(eventPb)

// For each Event in the entry
cnt++

// Add the Event name to the set of known Events.
events[eventName(event)] = struct{}{}
eventDests[event.DestModule] = struct{}{}
switch e := event.Type.(type) {
case *eventpb.Event_Iss:
case *eventpbtypes.Event_Iss:
// For ISS Events, also add the type of the ISS event to a set of known ISS events.
issEvents[issEventName(e.Iss)] = struct{}{}
}
Expand All @@ -85,21 +86,21 @@ func getEventList(filenames *[]string) (map[string]struct{}, map[string]struct{}
}

// eventName returns a string name of an Event.
func eventName(event *eventpb.Event) string {
func eventName(event *eventpbtypes.Event) string {
return strings.ReplaceAll(
reflect.TypeOf(event.Type).Elem().Name(), // gets the type's name i.e. Event_Tick , Event_Iss,etc
"Event_", "")
}

// issEventName returns a string name of an ISS event.
func issEventName(issEvent *isspb.Event) string {
func issEventName(issEvent *isspbtypes.Event) string {
return strings.ReplaceAll(
reflect.TypeOf(issEvent.Type).Elem().Name(), // gets the type's name i.e. ISSEvent_sb , ISSEvent_PersistCheckpoint,etc
"ISSEvent_", "") // replaces the given substring from the name
}

// selected returns true if the given event has been selected by the user according to the given criteria.
func selected(event *eventpb.Event, selectedEvents map[string]struct{}, selectedIssEvents map[string]struct{}) bool {
func selected(event *eventpbtypes.Event, selectedEvents map[string]struct{}, selectedIssEvents map[string]struct{}) bool {
if _, ok := selectedEvents[eventName(event)]; !ok {
// If the basic type of the event has not been selected, return false.
return false
Expand All @@ -108,26 +109,14 @@ func selected(event *eventpb.Event, selectedEvents map[string]struct{}, selected
// If the basic type of the event has been selected,
// check whether the sub-type has been selected as well for ISS events.
switch e := event.Type.(type) {
case *eventpb.Event_Iss:
case *eventpbtypes.Event_Iss:
_, ok := selectedIssEvents[issEventName(e.Iss)]
return ok
default:
return true
}
}

// Converts a set of strings (represented as a map) to a list.
// Returns a slice containing all the keys present in the given set.
// toList is used to convert sets to a format used by the survey library.
func toList(set map[string]struct{}) []string {
list := make([]string, 0, len(set))
for item := range set {
list = append(list, item)
}
sort.Strings(list)
return list
}

// Converts a list of strings to a set (represented as a map).
// Returns a map of empty structs with one entry for each unique item of the given list (the item being the map key).
// toSet is used to convert lists produced by the survey library to sets for easier lookup.
Expand Down
26 changes: 17 additions & 9 deletions cmd/mircat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/alecthomas/kingpin.v2"

t "github.com/filecoin-project/mir/pkg/types"
"github.com/filecoin-project/mir/pkg/util/maputil"
)

// mircat is a tool for reviewing Mir state machine recordings.
Expand Down Expand Up @@ -38,7 +39,7 @@ type arguments struct {
selectedIssEventNames map[string]struct{}

// Events with specific destination modules selected by the user for displaying
selectedEventDests map[string]struct{}
selectedEventDests map[t.ModuleID]struct{}

// If set to true, start a Node in debug mode with the given event log.
debug bool
Expand Down Expand Up @@ -99,8 +100,12 @@ func main() {
// // have the user interactively select the event destinations' to include in the output.
if len(args.selectedEventDests) == 0 {

allDestsStr := maputil.Transform(allDests, func(k t.ModuleID, v struct{}) (string, struct{}) { return string(k), v })
// Select top-level events
args.selectedEventDests = checkboxes("Please select the event destinations", allDests)
args.selectedEventDests = maputil.Transform(
checkboxes("Please select the event destinations", allDestsStr),
func(k string, v struct{}) (t.ModuleID, struct{}) { return t.ModuleID(k), v },
)

}

Expand Down Expand Up @@ -161,7 +166,10 @@ func parseArgs(args []string) (*arguments, error) {
limit: *limit,
selectedEventNames: toSet(*events),
selectedIssEventNames: toSet(*issEvents),
selectedEventDests: toSet(*eventDests),
selectedEventDests: maputil.Transform(
toSet(*eventDests),
func(k string, v struct{}) (t.ModuleID, struct{}) { return t.ModuleID(k), v },
),
}, nil
}

Expand All @@ -173,7 +181,7 @@ func checkboxes(label string, opts map[string]struct{}) map[string]struct{} {
selected := make([]string, 0)
prompt := &survey.MultiSelect{
Message: label,
Options: toList(opts),
Options: maputil.GetSortedKeys(opts),
}
if err := survey.AskOne(prompt, &selected); err != nil {
fmt.Printf("Error selecting event types: %v", err)
Expand All @@ -182,19 +190,19 @@ func checkboxes(label string, opts map[string]struct{}) map[string]struct{} {
return toSet(selected)
}

func selectionArgs(events map[string]struct{}, issEvents map[string]struct{}, dests map[string]struct{}) string {
func selectionArgs(events map[string]struct{}, issEvents map[string]struct{}, dests map[t.ModuleID]struct{}) string {
argStr := ""

for _, eventName := range toList(events) {
for _, eventName := range maputil.GetSortedKeys(events) {
argStr += " --event " + eventName
}

for _, issEventName := range toList(issEvents) {
for _, issEventName := range maputil.GetSortedKeys(issEvents) {
argStr += " --iss-event " + issEventName
}

for _, dest := range toList(dests) {
argStr += " --event-dest " + dest
for _, dest := range maputil.GetSortedKeys(dests) {
argStr += " --event-dest " + string(dest)
}

return argStr
Expand Down
Loading