From 033697f2debca7dd168adcc330fbc7f927ab1689 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Thu, 17 Jul 2025 09:59:50 -0700 Subject: [PATCH] Switch to latest finalized reference block --- flowkit.go | 19 ++++++++++++------- gateway/emulator.go | 4 ++-- gateway/gateway.go | 2 +- gateway/grpc.go | 4 ++-- gateway/mocks/Gateway.go | 24 ++++++++++++------------ gateway/mocks/gateway_mock.go | 2 +- mocks/Services.go | 8 ++++---- 7 files changed, 34 insertions(+), 29 deletions(-) diff --git a/flowkit.go b/flowkit.go index d7953b73..3942712a 100644 --- a/flowkit.go +++ b/flowkit.go @@ -49,13 +49,14 @@ import ( // BlockQuery defines possible queries for block. type BlockQuery struct { - ID *flow.Identifier - Height uint64 - Latest bool + ID *flow.Identifier + Height uint64 + Latest bool + IsSealed bool } // LatestBlockQuery specifies the latest block. -var LatestBlockQuery = BlockQuery{Latest: true} +var LatestBlockQuery = BlockQuery{Latest: true, IsSealed: true} // NewBlockQuery creates block query based on the passed query value. // @@ -226,7 +227,7 @@ func (f *Flowkit) prepareTransaction( tx *transactions.Transaction, account *accounts.Account, ) (*transactions.Transaction, error) { - block, err := f.gateway.GetLatestBlock(ctx) + block, err := f.gateway.GetLatestBlock(ctx, false) if err != nil { return nil, err } @@ -455,7 +456,11 @@ func (f *Flowkit) GetBlock(ctx context.Context, query BlockQuery) (*flow.Block, var err error var block *flow.Block if query.Latest { - block, err = f.gateway.GetLatestBlock(ctx) + if query.IsSealed { + block, err = f.gateway.GetLatestBlock(ctx, true) + } else { + block, err = f.gateway.GetLatestBlock(ctx, false) + } } else if query.ID != nil { block, err = f.gateway.GetBlockByID(ctx, *query.ID) } else { @@ -929,7 +934,7 @@ func (f *Flowkit) BuildTransaction( return nil, err } - latestBlock, err := f.gateway.GetLatestBlock(ctx) + latestBlock, err := f.gateway.GetLatestBlock(ctx, false) if err != nil { return nil, fmt.Errorf("failed to get latest sealed block: %w", err) } diff --git a/gateway/emulator.go b/gateway/emulator.go index c38f2eaf..d2ba5aea 100644 --- a/gateway/emulator.go +++ b/gateway/emulator.go @@ -236,8 +236,8 @@ func (g *EmulatorGateway) ExecuteScriptAtID( return g.executeScriptQuery(ctx, script, arguments, scriptQuery{id: id}) } -func (g *EmulatorGateway) GetLatestBlock(ctx context.Context) (*flow.Block, error) { - block, _, err := g.adapter.GetLatestBlock(ctx, true) +func (g *EmulatorGateway) GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, error) { + block, _, err := g.adapter.GetLatestBlock(ctx, isSealed) if err != nil { return nil, UnwrapStatusError(err) } diff --git a/gateway/gateway.go b/gateway/gateway.go index 8863ac29..6befcec6 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -40,7 +40,7 @@ type Gateway interface { ExecuteScript(context.Context, []byte, []cadence.Value) (cadence.Value, error) ExecuteScriptAtHeight(context.Context, []byte, []cadence.Value, uint64) (cadence.Value, error) ExecuteScriptAtID(context.Context, []byte, []cadence.Value, flow.Identifier) (cadence.Value, error) - GetLatestBlock(context.Context) (*flow.Block, error) + GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, error) GetBlockByHeight(context.Context, uint64) (*flow.Block, error) GetBlockByID(context.Context, flow.Identifier) (*flow.Block, error) GetEvents(context.Context, string, uint64, uint64) ([]flow.BlockEvents, error) diff --git a/gateway/grpc.go b/gateway/grpc.go index 3790571b..cf84319a 100644 --- a/gateway/grpc.go +++ b/gateway/grpc.go @@ -171,8 +171,8 @@ func (g *GrpcGateway) ExecuteScriptAtID(ctx context.Context, script []byte, argu } // GetLatestBlock gets the latest block on Flow through the Access API. -func (g *GrpcGateway) GetLatestBlock(ctx context.Context) (*flow.Block, error) { - return g.client.GetLatestBlock(ctx, true) +func (g *GrpcGateway) GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, error) { + return g.client.GetLatestBlock(ctx, isSealed) } // GetBlockByID get block by ID from the Flow Access API. diff --git a/gateway/mocks/Gateway.go b/gateway/mocks/Gateway.go index 2bc61b2b..697e6158 100644 --- a/gateway/mocks/Gateway.go +++ b/gateway/mocks/Gateway.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.53.3. DO NOT EDIT. +// Code generated by mockery v2.40.3. DO NOT EDIT. package mocks @@ -257,9 +257,9 @@ func (_m *Gateway) GetEvents(_a0 context.Context, _a1 string, _a2 uint64, _a3 ui return r0, r1 } -// GetLatestBlock provides a mock function with given fields: _a0 -func (_m *Gateway) GetLatestBlock(_a0 context.Context) (*flow.Block, error) { - ret := _m.Called(_a0) +// GetLatestBlock provides a mock function with given fields: ctx, isSealed +func (_m *Gateway) GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, error) { + ret := _m.Called(ctx, isSealed) if len(ret) == 0 { panic("no return value specified for GetLatestBlock") @@ -267,19 +267,19 @@ func (_m *Gateway) GetLatestBlock(_a0 context.Context) (*flow.Block, error) { var r0 *flow.Block var r1 error - if rf, ok := ret.Get(0).(func(context.Context) (*flow.Block, error)); ok { - return rf(_a0) + if rf, ok := ret.Get(0).(func(context.Context, bool) (*flow.Block, error)); ok { + return rf(ctx, isSealed) } - if rf, ok := ret.Get(0).(func(context.Context) *flow.Block); ok { - r0 = rf(_a0) + if rf, ok := ret.Get(0).(func(context.Context, bool) *flow.Block); ok { + r0 = rf(ctx, isSealed) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*flow.Block) } } - if rf, ok := ret.Get(1).(func(context.Context) error); ok { - r1 = rf(_a0) + if rf, ok := ret.Get(1).(func(context.Context, bool) error); ok { + r1 = rf(ctx, isSealed) } else { r1 = ret.Error(1) } @@ -497,7 +497,7 @@ func (_m *Gateway) GetTransactionsByBlockID(_a0 context.Context, _a1 flow.Identi return r0, r1 } -// Ping provides a mock function with no fields +// Ping provides a mock function with given fields: func (_m *Gateway) Ping() error { ret := _m.Called() @@ -515,7 +515,7 @@ func (_m *Gateway) Ping() error { return r0 } -// SecureConnection provides a mock function with no fields +// SecureConnection provides a mock function with given fields: func (_m *Gateway) SecureConnection() bool { ret := _m.Called() diff --git a/gateway/mocks/gateway_mock.go b/gateway/mocks/gateway_mock.go index 25f694a5..1de2bdf2 100644 --- a/gateway/mocks/gateway_mock.go +++ b/gateway/mocks/gateway_mock.go @@ -110,7 +110,7 @@ func DefaultMockGateway() *TestGateway { ), GetBlockByHeight: m.On(GetBlockByHeightFunc, ctxMock, mock.Anything), GetBlockByID: m.On(GetBlockByIDFunc, ctxMock, mock.Anything), - GetLatestBlock: m.On(GetLatestBlockFunc, ctxMock), + GetLatestBlock: m.On(GetLatestBlockFunc, ctxMock, mock.AnythingOfType("bool")), GetSystemTransaction: m.On( GetSystemTransactionFunc, ctxMock, diff --git a/mocks/Services.go b/mocks/Services.go index eac7a0af..0cfafca9 100644 --- a/mocks/Services.go +++ b/mocks/Services.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.53.3. DO NOT EDIT. +// Code generated by mockery v2.40.3. DO NOT EDIT. package mocks @@ -228,7 +228,7 @@ func (_m *Services) ExecuteScript(_a0 context.Context, _a1 flowkit.Script, _a2 f return r0, r1 } -// Gateway provides a mock function with no fields +// Gateway provides a mock function with given fields: func (_m *Services) Gateway() gateway.Gateway { ret := _m.Called() @@ -552,7 +552,7 @@ func (_m *Services) GetTransactionsByBlockID(_a0 context.Context, _a1 flow.Ident return r0, r1, r2 } -// Network provides a mock function with no fields +// Network provides a mock function with given fields: func (_m *Services) Network() config.Network { ret := _m.Called() @@ -570,7 +570,7 @@ func (_m *Services) Network() config.Network { return r0 } -// Ping provides a mock function with no fields +// Ping provides a mock function with given fields: func (_m *Services) Ping() error { ret := _m.Called()