-
Notifications
You must be signed in to change notification settings - Fork 115
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
[OTE-823] Fix FNS onchain events staging + retrieval logic #2318
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,10 +163,6 @@ func (f *Flags) Validate() error { | |
|
||
// Grpc streaming | ||
if f.GrpcStreamingEnabled { | ||
if f.OptimisticExecutionEnabled { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously added to prevent both flags from being accidentally set true |
||
// TODO(OTE-456): Finish gRPC streaming x OE integration. | ||
return fmt.Errorf("grpc streaming cannot be enabled together with optimistic execution") | ||
} | ||
if !f.GrpcEnable { | ||
return fmt.Errorf("grpc.enable must be set to true - grpc streaming requires gRPC server") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,14 +113,17 @@ func TestValidate(t *testing.T) { | |
OptimisticExecutionEnabled: true, | ||
}, | ||
}, | ||
"failure - optimistic execution cannot be enabled with gRPC streaming": { | ||
"success - optimistic execution canbe enabled with gRPC streaming": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
flags: flags.Flags{ | ||
NonValidatingFullNode: false, | ||
GrpcEnable: true, | ||
GrpcStreamingEnabled: true, | ||
OptimisticExecutionEnabled: true, | ||
NonValidatingFullNode: false, | ||
GrpcEnable: true, | ||
GrpcStreamingEnabled: true, | ||
OptimisticExecutionEnabled: true, | ||
GrpcStreamingMaxBatchSize: 2000, | ||
GrpcStreamingFlushIntervalMs: 100, | ||
GrpcStreamingMaxChannelBufferSize: 2000, | ||
WebsocketStreamingPort: 8989, | ||
}, | ||
expectedErr: fmt.Errorf("grpc streaming cannot be enabled together with optimistic execution"), | ||
}, | ||
"failure - gRPC disabled": { | ||
flags: flags.Flags{ | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |||||||||||
"cosmossdk.io/log" | ||||||||||||
"cosmossdk.io/store/prefix" | ||||||||||||
storetypes "cosmossdk.io/store/types" | ||||||||||||
"github.com/cosmos/cosmos-sdk/codec" | ||||||||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||||||||
ante_types "github.com/dydxprotocol/v4-chain/protocol/app/ante/types" | ||||||||||||
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics" | ||||||||||||
|
@@ -27,6 +28,7 @@ var _ types.FullNodeStreamingManager = (*FullNodeStreamingManagerImpl)(nil) | |||||||||||
type FullNodeStreamingManagerImpl struct { | ||||||||||||
sync.Mutex | ||||||||||||
|
||||||||||||
cdc codec.BinaryCodec | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Update all calls to
Ensure that the 🔗 Analysis chainUpdate all usages to accommodate the new 'cdc' parameter. The
Run the following script to verify that all calls to Also applies to: 100-100, 119-119 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify all calls to 'NewFullNodeStreamingManager' include the 'cdc' parameter.
# Search for calls to 'NewFullNodeStreamingManager' lacking the 'cdc' parameter
rg --type go 'NewFullNodeStreamingManager\(' -A 5 | grep -v 'cdc'
# Alternatively, list all calls to inspect them manually
rg --type go -A 5 'NewFullNodeStreamingManager\('
Length of output: 1246 |
||||||||||||
logger log.Logger | ||||||||||||
|
||||||||||||
// orderbookSubscriptions maps subscription IDs to their respective orderbook subscriptions. | ||||||||||||
|
@@ -95,6 +97,7 @@ func NewFullNodeStreamingManager( | |||||||||||
maxSubscriptionChannelSize uint32, | ||||||||||||
snapshotBlockInterval uint32, | ||||||||||||
streamingManagerTransientStoreKey storetypes.StoreKey, | ||||||||||||
cdc codec.BinaryCodec, | ||||||||||||
) *FullNodeStreamingManagerImpl { | ||||||||||||
fullNodeStreamingManager := &FullNodeStreamingManagerImpl{ | ||||||||||||
logger: logger, | ||||||||||||
|
@@ -113,6 +116,7 @@ func NewFullNodeStreamingManager( | |||||||||||
snapshotBlockInterval: snapshotBlockInterval, | ||||||||||||
|
||||||||||||
streamingManagerTransientStoreKey: streamingManagerTransientStoreKey, | ||||||||||||
cdc: cdc, | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Start the goroutine for pushing order updates through. | ||||||||||||
|
@@ -391,14 +395,15 @@ func (sm *FullNodeStreamingManagerImpl) StageFinalizeBlockSubaccountUpdate( | |||||||||||
ctx sdk.Context, | ||||||||||||
subaccountUpdate satypes.StreamSubaccountUpdate, | ||||||||||||
) { | ||||||||||||
lib.AssertDeliverTxMode(ctx) | ||||||||||||
stagedEvent := clobtypes.StagedFinalizeBlockEvent{ | ||||||||||||
Event: &clobtypes.StagedFinalizeBlockEvent_SubaccountUpdate{ | ||||||||||||
SubaccountUpdate: &subaccountUpdate, | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
sm.stageFinalizeBlockEvent( | ||||||||||||
ctx, | ||||||||||||
clobtypes.Amino.MustMarshal(stagedEvent), | ||||||||||||
sm.cdc.MustMarshal(&stagedEvent), | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace 'MustMarshal' with error-handling 'Marshal' to prevent panics. In Apply this change to handle marshaling errors gracefully: - sm.cdc.MustMarshal(&stagedEvent)
+ marshaledEvent, err := sm.cdc.Marshal(&stagedEvent)
+ if err != nil {
+ sm.logger.Error("Failed to marshal staged event", "error", err)
+ return // Handle the error appropriately
+ }
+ sm.stageFinalizeBlockEvent(ctx, marshaledEvent) Also applies to: 428-428 |
||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -411,25 +416,30 @@ func (sm *FullNodeStreamingManagerImpl) StageFinalizeBlockFill( | |||||||||||
ctx sdk.Context, | ||||||||||||
fill clobtypes.StreamOrderbookFill, | ||||||||||||
) { | ||||||||||||
lib.AssertDeliverTxMode(ctx) | ||||||||||||
stagedEvent := clobtypes.StagedFinalizeBlockEvent{ | ||||||||||||
Event: &clobtypes.StagedFinalizeBlockEvent_OrderFill{ | ||||||||||||
OrderFill: &fill, | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
|
||||||||||||
sm.stageFinalizeBlockEvent( | ||||||||||||
ctx, | ||||||||||||
clobtypes.Amino.MustMarshal(stagedEvent), | ||||||||||||
sm.cdc.MustMarshal(&stagedEvent), | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func getStagedFinalizeBlockEvents(store storetypes.KVStore) []clobtypes.StagedFinalizeBlockEvent { | ||||||||||||
func getStagedFinalizeBlockEventsFromStore( | ||||||||||||
store storetypes.KVStore, | ||||||||||||
cdc codec.BinaryCodec, | ||||||||||||
) []clobtypes.StagedFinalizeBlockEvent { | ||||||||||||
count := getStagedEventsCount(store) | ||||||||||||
events := make([]clobtypes.StagedFinalizeBlockEvent, count) | ||||||||||||
store = prefix.NewStore(store, []byte(StagedEventsKeyPrefix)) | ||||||||||||
for i := uint32(0); i < count; i++ { | ||||||||||||
var event clobtypes.StagedFinalizeBlockEvent | ||||||||||||
bytes := store.Get(lib.Uint32ToKey(i)) | ||||||||||||
clobtypes.Amino.MustUnmarshal(bytes, &event) | ||||||||||||
cdc.MustUnmarshal(bytes, &event) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle errors from 'Unmarshal' instead of using 'MustUnmarshal'. At line 442, Modify the code to handle unmarshaling errors: - cdc.MustUnmarshal(bytes, &event)
+ if err := cdc.Unmarshal(bytes, &event); err != nil {
+ sm.logger.Error("Failed to unmarshal staged event", "error", err)
+ continue // or handle the error as appropriate
+ } 📝 Committable suggestion
Suggested change
|
||||||||||||
events[i] = event | ||||||||||||
} | ||||||||||||
return events | ||||||||||||
|
@@ -441,7 +451,7 @@ func (sm *FullNodeStreamingManagerImpl) GetStagedFinalizeBlockEvents( | |||||||||||
) []clobtypes.StagedFinalizeBlockEvent { | ||||||||||||
noGasCtx := ctx.WithGasMeter(ante_types.NewFreeInfiniteGasMeter()) | ||||||||||||
store := noGasCtx.TransientStore(sm.streamingManagerTransientStoreKey) | ||||||||||||
return getStagedFinalizeBlockEvents(store) | ||||||||||||
return getStagedFinalizeBlockEventsFromStore(store, sm.cdc) | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (sm *FullNodeStreamingManagerImpl) stageFinalizeBlockEvent( | ||||||||||||
|
@@ -889,6 +899,9 @@ func (sm *FullNodeStreamingManagerImpl) StreamBatchUpdatesAfterFinalizeBlock( | |||||||||||
orderBookUpdatesToSyncLocalOpsQueue *clobtypes.OffchainUpdates, | ||||||||||||
perpetualIdToClobPairId map[uint32][]clobtypes.ClobPairId, | ||||||||||||
) { | ||||||||||||
// Prevent gas metering from state read. | ||||||||||||
ctx = ctx.WithGasMeter(ante_types.NewFreeInfiniteGasMeter()) | ||||||||||||
|
||||||||||||
finalizedFills, finalizedSubaccountUpdates := sm.getStagedEventsFromFinalizeBlock(ctx) | ||||||||||||
|
||||||||||||
orderbookStreamUpdates, orderbookClobPairIds := getStreamUpdatesFromOffchainUpdates( | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,16 @@ func (am AppModule) PreBlock(ctx context.Context) (appmodule.ResponsePreBlock, e | |
}, nil | ||
} | ||
|
||
// BeginBlock executes all ABCI BeginBlock logic respective to the clob module. | ||
func (am AppModule) Precommit(ctx context.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Callback was not previously implemented, so |
||
defer telemetry.ModuleMeasureSince(am.Name(), time.Now(), telemetry.MetricKeyPrecommiter) | ||
Precommit( | ||
lib.UnwrapSDKContext(ctx, types.ModuleName), | ||
*am.keeper, | ||
) | ||
return nil | ||
} | ||
|
||
// BeginBlock executes all ABCI BeginBlock logic respective to the clob module. | ||
func (am AppModule) BeginBlock(ctx context.Context) error { | ||
defer telemetry.ModuleMeasureSince(am.Name(), time.Now(), telemetry.MetricKeyBeginBlocker) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Remove unused
cdc
parameter from NewFullNodeStreamingManager.The
cdc
parameter is currently not utilized within theNewFullNodeStreamingManager
function. If it's not required, please remove it to simplify the function signature and maintain code clarity.🔗 Analysis chain
LGTM. Verify usage of new parameter in NewFullNodeStreamingManager.
The addition of the
cdc
parameter toNewFullNodeStreamingManager
looks good. This change likely introduces codec functionality for serialization/deserialization in the streaming manager.Please ensure that the
NewFullNodeStreamingManager
function properly utilizes the newcdc
parameter. Run the following script to verify:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 502
Script:
Length of output: 91461