Skip to content

Commit a539468

Browse files
bjartekjribbinkCopilot
authored
Update the latest Scheduled Transactions API changes (#894)
* trying to expose system transactions * added some todos * fixed a very basic implementation * fixing some tests * fixed tests * Update to remaining scheduled transaction API changes * remove comment * format * fix build * cleanup index & add test * Update types/result.go * Update storage/store.go Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Jordan Ribbink <[email protected]> Co-authored-by: Jordan Ribbink <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 629faf5 commit a539468

File tree

19 files changed

+981
-160
lines changed

19 files changed

+981
-160
lines changed

adapters/access.go

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,56 @@ func (a *AccessAdapter) GetExecutionResultByID(_ context.Context, _ flowgo.Ident
434434
return nil, nil
435435
}
436436

437-
func (a *AccessAdapter) GetSystemTransaction(_ context.Context, _ flowgo.Identifier, _ flowgo.Identifier) (*flowgo.TransactionBody, error) {
438-
return nil, nil
437+
func (a *AccessAdapter) GetSystemTransaction(_ context.Context, txID flowgo.Identifier, blockID flowgo.Identifier) (*flowgo.TransactionBody, error) {
438+
tx, err := a.emulator.GetSystemTransaction(txID, blockID)
439+
if err != nil {
440+
return nil, convertError(err, codes.NotFound)
441+
}
442+
443+
return tx, nil
439444
}
440445

441-
func (a *AccessAdapter) GetSystemTransactionResult(_ context.Context, _ flowgo.Identifier, _ flowgo.Identifier, _ entities.EventEncodingVersion) (*accessmodel.TransactionResult, error) {
442-
return nil, nil
446+
func (a *AccessAdapter) GetSystemTransactionResult(_ context.Context, txID flowgo.Identifier, blockID flowgo.Identifier, encodingVersion entities.EventEncodingVersion) (*accessmodel.TransactionResult, error) {
447+
result, err := a.emulator.GetSystemTransactionResult(txID, blockID)
448+
if err != nil {
449+
return nil, convertError(err, codes.NotFound)
450+
}
451+
452+
// Convert CCF events to JSON events, else return CCF encoded version
453+
if encodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
454+
result.Events, err = ConvertCCFEventsToJsonEvents(result.Events)
455+
if err != nil {
456+
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to convert events: %v", err))
457+
}
458+
}
459+
460+
return result, nil
461+
}
462+
463+
func (a *AccessAdapter) GetScheduledTransaction(_ context.Context, scheduledTxID uint64) (*flowgo.TransactionBody, error) {
464+
tx, err := a.emulator.GetScheduledTransaction(scheduledTxID)
465+
if err != nil {
466+
return nil, convertError(err, codes.NotFound)
467+
}
468+
469+
return tx, nil
470+
}
471+
472+
func (a *AccessAdapter) GetScheduledTransactionResult(_ context.Context, scheduledTxID uint64, encodingVersion entities.EventEncodingVersion) (*accessmodel.TransactionResult, error) {
473+
result, err := a.emulator.GetScheduledTransactionResult(scheduledTxID)
474+
if err != nil {
475+
return nil, convertError(err, codes.NotFound)
476+
}
477+
478+
// Convert CCF events to JSON events, else return CCF encoded version
479+
if encodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
480+
result.Events, err = ConvertCCFEventsToJsonEvents(result.Events)
481+
if err != nil {
482+
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to convert events: %v", err))
483+
}
484+
}
485+
486+
return result, nil
443487
}
444488

445489
func (a *AccessAdapter) GetAccountBalanceAtLatestBlock(_ context.Context, address flowgo.Address) (uint64, error) {
@@ -654,7 +698,7 @@ func (a *AccessAdapter) subscribeBlocksFromStartBlockID(ctx context.Context, sta
654698
if err != nil {
655699
return subscription.NewFailedSubscription(err, "could not get block by ID")
656700
}
657-
701+
658702
emulatorBlockchain, ok := a.emulator.(*emulator.Blockchain)
659703
if !ok {
660704
return subscription.NewFailedSubscription(fmt.Errorf("emulator is not a Blockchain"), "invalid emulator type")
@@ -681,7 +725,7 @@ func (a *AccessAdapter) subscribeBlocksFromLatest(ctx context.Context, getData s
681725
if err != nil {
682726
return subscription.NewFailedSubscription(err, "could not get latest block")
683727
}
684-
728+
685729
emulatorBlockchain, ok := a.emulator.(*emulator.Blockchain)
686730
if !ok {
687731
return subscription.NewFailedSubscription(fmt.Errorf("emulator is not a Blockchain"), "invalid emulator type")
@@ -787,7 +831,7 @@ func (a *AccessAdapter) SubscribeTransactionStatuses(ctx context.Context, txID f
787831
if err != nil {
788832
return subscription.NewFailedSubscription(err, "failed to lookup latest block")
789833
}
790-
834+
791835
return a.createTransactionSubscription(ctx, txID, latestBlock.ID(), flowgo.ZeroID, requiredEventEncodingVersion)
792836
}
793837

@@ -800,7 +844,7 @@ func (a *AccessAdapter) SubscribeTransactionStatusesFromStartHeight(ctx context.
800844
if err != nil {
801845
return subscription.NewFailedSubscription(err, "failed to get start block")
802846
}
803-
847+
804848
return a.createTransactionSubscription(ctx, txID, block.ID(), flowgo.ZeroID, requiredEventEncodingVersion)
805849
}
806850

@@ -809,7 +853,7 @@ func (a *AccessAdapter) SubscribeTransactionStatusesFromLatest(ctx context.Conte
809853
if err != nil {
810854
return subscription.NewFailedSubscription(err, "failed to lookup latest block")
811855
}
812-
856+
813857
return a.createTransactionSubscription(ctx, txID, latestBlock.ID(), flowgo.ZeroID, requiredEventEncodingVersion)
814858
}
815859

@@ -857,7 +901,7 @@ func (a *AccessAdapter) getTransactionStatusResponse(
857901
requiredEventEncodingVersion entities.EventEncodingVersion,
858902
) subscription.GetDataByHeightFunc {
859903
lastStatus := flowgo.TransactionStatusUnknown
860-
904+
861905
return func(ctx context.Context, height uint64) (interface{}, error) {
862906
// Check if block is ready
863907
if err := a.validateHeight(height, flowgo.BlockStatusSealed); err != nil {
@@ -880,7 +924,7 @@ func (a *AccessAdapter) getTransactionStatusResponse(
880924
TransactionID: txID,
881925
}}, nil
882926
}
883-
927+
884928
// Otherwise, transaction is still pending/unknown
885929
if lastStatus == flowgo.TransactionStatusUnknown {
886930
return nil, nil // Don't send duplicate unknown status
@@ -928,14 +972,14 @@ func (a *AccessAdapter) generateTransactionStatusUpdates(
928972
Status: status,
929973
TransactionID: txResult.TransactionID,
930974
}
931-
975+
932976
// Add block info for finalized and later statuses
933977
if status >= flowgo.TransactionStatusFinalized {
934978
result.BlockID = txResult.BlockID
935979
result.BlockHeight = txResult.BlockHeight
936980
result.CollectionID = txResult.CollectionID
937981
}
938-
982+
939983
// Add execution details for executed and sealed statuses
940984
if status >= flowgo.TransactionStatusExecuted {
941985
result.Events = txResult.Events
@@ -961,10 +1005,3 @@ func ConvertCCFEventsToJsonEvents(events []flowgo.Event) ([]flowgo.Event, error)
9611005

9621006
return converted, nil
9631007
}
964-
965-
func (a *AccessAdapter) GetScheduledTransaction(_ context.Context, _ uint64) (*flowgo.TransactionBody, error) {
966-
return nil, nil
967-
}
968-
func (a *AccessAdapter) GetScheduledTransactionResult(_ context.Context, _ uint64, _ entities.EventEncodingVersion) (*accessmodel.TransactionResult, error) {
969-
return nil, nil
970-
}

adapters/streaming_integration_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func TestStreamingTransactionStatuses_Integration(t *testing.T) {
186186
SetProposalKey(serviceAddress, serviceKey.Index, serviceKey.SequenceNumber).
187187
SetPayer(serviceAddress).
188188
AddAuthorizer(serviceAddress)
189-
189+
190190
tx, err := txBuilder.Build()
191191
require.NoError(t, err)
192192

@@ -240,7 +240,7 @@ func TestStreamingTransactionStatuses_Integration(t *testing.T) {
240240
}
241241

242242
done:
243-
assert.True(t, statusesReceived[flowgo.TransactionStatusSealed],
243+
assert.True(t, statusesReceived[flowgo.TransactionStatusSealed],
244244
"should receive sealed status. Got: %v", statusesReceived)
245245
}
246246

@@ -287,4 +287,3 @@ func TestStreamingMultipleBlocks_Integration(t *testing.T) {
287287

288288
assert.Equal(t, expectedBlocks, blocksReceived)
289289
}
290-

convert/emu.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func ToStorableResult(
2929
output fvm.ProcedureOutput,
3030
blockID flowgo.Identifier,
3131
blockHeight uint64,
32+
collectionID flowgo.Identifier,
3233
) (
3334
types.StorableTransactionResult,
3435
error,
@@ -48,5 +49,6 @@ func ToStorableResult(
4849
ErrorMessage: errorMessage,
4950
Logs: output.Logs,
5051
Events: output.Events,
52+
CollectionID: collectionID,
5153
}, nil
5254
}

0 commit comments

Comments
 (0)