Skip to content

Commit

Permalink
use ptr in service event conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanschalm committed Mar 7, 2024
1 parent b5cafcb commit ec6e878
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions model/convert/service_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,15 +905,15 @@ func convertServiceEventProtocolStateVersionUpgrade(event flow.Event) (*flow.Ser
}

versionUpgrade, err := DecodeCadenceValue("ProtocolStateVersionUpgrade payload", payload,
func(cdcEvent cadence.Event) (flow.ProtocolStateVersionUpgrade, error) {
func(cdcEvent cadence.Event) (*flow.ProtocolStateVersionUpgrade, error) {
const expectedFieldCount = 2
if len(cdcEvent.Fields) < expectedFieldCount {
return flow.ProtocolStateVersionUpgrade{}, fmt.Errorf("unexpected number of fields in ProtocolStateVersionUpgrade (%d < %d)",
return nil, fmt.Errorf("unexpected number of fields in ProtocolStateVersionUpgrade (%d < %d)",
len(cdcEvent.Fields), expectedFieldCount)
}

if cdcEvent.Type() == nil {
return flow.ProtocolStateVersionUpgrade{}, fmt.Errorf("ProtocolStateVersionUpgrade event doesn't have type")
return nil, fmt.Errorf("ProtocolStateVersionUpgrade event doesn't have type")
}

var newProtocolVersionValue cadence.Value
Expand All @@ -933,7 +933,7 @@ func convertServiceEventProtocolStateVersionUpgrade(event flow.Event) (*flow.Ser
}

if foundFieldCount != expectedFieldCount {
return flow.ProtocolStateVersionUpgrade{}, fmt.Errorf(
return nil, fmt.Errorf(
"ProtocolStateVersionUpgrade event required fields not found (%d != %d)",
foundFieldCount,
expectedFieldCount,
Expand All @@ -946,18 +946,18 @@ func convertServiceEventProtocolStateVersionUpgrade(event flow.Event) (*flow.Ser
},
)
if err != nil {
return flow.ProtocolStateVersionUpgrade{}, err
return nil, err
}
activeView, err := DecodeCadenceValue(
".activeView", activeViewValue, func(cadenceVal cadence.UInt64) (uint64, error) {
return uint64(cadenceVal), err
},
)
if err != nil {
return flow.ProtocolStateVersionUpgrade{}, err
return nil, err
}

return flow.ProtocolStateVersionUpgrade{
return &flow.ProtocolStateVersionUpgrade{
NewProtocolStateVersion: newProtocolVersion,
ActiveView: activeView,
}, nil
Expand All @@ -969,7 +969,7 @@ func convertServiceEventProtocolStateVersionUpgrade(event flow.Event) (*flow.Ser
// create the service event
serviceEvent := &flow.ServiceEvent{
Type: flow.ServiceEventProtocolStateVersionUpgrade,
Event: &versionUpgrade,
Event: versionUpgrade,
}
return serviceEvent, nil
}
Expand All @@ -986,21 +986,18 @@ func convertServiceEventVersionBeacon(event flow.Event) (*flow.ServiceEvent, err
}

versionBeacon, err := DecodeCadenceValue(
"VersionBeacon payload", payload, func(cdcEvent cadence.Event) (
flow.VersionBeacon,
error,
) {
"VersionBeacon payload", payload, func(cdcEvent cadence.Event) (*flow.VersionBeacon, error) {
const expectedFieldCount = 2
if len(cdcEvent.Fields) != expectedFieldCount {
return flow.VersionBeacon{}, fmt.Errorf(
return nil, fmt.Errorf(
"unexpected number of fields in VersionBeacon event (%d != %d)",
len(cdcEvent.Fields),
expectedFieldCount,
)
}

if cdcEvent.Type() == nil {
return flow.VersionBeacon{}, fmt.Errorf("VersionBeacon event doesn't have type")
return nil, fmt.Errorf("VersionBeacon event doesn't have type")
}

var versionBoundariesValue, sequenceValue cadence.Value
Expand All @@ -1021,7 +1018,7 @@ func convertServiceEventVersionBeacon(event flow.Event) (*flow.ServiceEvent, err
}

if foundFieldCount != expectedFieldCount {
return flow.VersionBeacon{}, fmt.Errorf(
return nil, fmt.Errorf(
"VersionBeacon event required fields not found (%d != %d)",
foundFieldCount,
expectedFieldCount,
Expand All @@ -1032,7 +1029,7 @@ func convertServiceEventVersionBeacon(event flow.Event) (*flow.ServiceEvent, err
".versionBoundaries", versionBoundariesValue, convertVersionBoundaries,
)
if err != nil {
return flow.VersionBeacon{}, err
return nil, err
}

sequence, err := DecodeCadenceValue(
Expand All @@ -1044,10 +1041,10 @@ func convertServiceEventVersionBeacon(event flow.Event) (*flow.ServiceEvent, err
},
)
if err != nil {
return flow.VersionBeacon{}, err
return nil, err
}

return flow.VersionBeacon{
return &flow.VersionBeacon{
VersionBoundaries: versionBoundaries,
Sequence: sequence,
}, err
Expand All @@ -1065,7 +1062,7 @@ func convertServiceEventVersionBeacon(event flow.Event) (*flow.ServiceEvent, err
// create the service event
serviceEvent := &flow.ServiceEvent{
Type: flow.ServiceEventVersionBeacon,
Event: &versionBeacon,
Event: versionBeacon,
}

return serviceEvent, nil
Expand Down

0 comments on commit ec6e878

Please sign in to comment.