Skip to content

Commit 741fc94

Browse files
fix: use GetSlot+GetBlockTime to retrieve the current time in Solana (#346)
The previous implementation, which used chainlink-ccip's `GetBlockTime()` helper function -- which, on its turn, used `rpc.GetBlockHeight` -- was returning an error in devnet: ``` failed to get block time: (*jsonrpc.RPCError)(0xc0017784b0)({ Code: (int) -32001, Message: (string) (len=84) "Block 357750972 cleaned up, does not exist on node. First available block: 362548764", Data: (interface {}) <nil> }) ``` This PR simply replaces the `GetBlockTime()` with direct calls to `rpc.GetSlot` and `rpc.GetBlockTime(slot)`. [DPA-1636](https://smartcontract-it.atlassian.net/browse/DPA-1636) [DPA-1636]: https://smartcontract-it.atlassian.net/browse/DPA-1636?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 58c1379 commit 741fc94

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

.changeset/light-bags-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smartcontractkit/mcms": patch
3+
---
4+
5+
fix: use GetLatestBlockhash to retrive curr. block time in Solana

sdk/solana/timelock_inspector.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ func (t TimelockInspector) IsOperationReady(ctx context.Context, address string,
9696
return false, err
9797
}
9898

99-
blockTime, err := solanaCommon.GetBlockTime(ctx, t.client, rpc.CommitmentConfirmed)
99+
slot, err := t.client.GetSlot(ctx, rpc.CommitmentConfirmed)
100100
if err != nil {
101-
return false, err
101+
return false, fmt.Errorf("failed to get slot: %w", err)
102+
}
103+
104+
blockTime, err := t.client.GetBlockTime(ctx, slot)
105+
if err != nil {
106+
return false, fmt.Errorf("failed to get block time: %w", err)
102107
}
103108

104109
ts, err := safecast.Uint64ToInt64(op.Timestamp)

sdk/solana/timelock_inspector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func TestTimelockInspector_IsOperationReady(t *testing.T) {
419419
mockGetAccountInfo(t, mockJSONRPCClient, operationPDA, operation, nil)
420420
mockGetBlockTime(t, mockJSONRPCClient, 1, &blockTime, errors.New("rpc error"), nil)
421421
},
422-
wantErr: "failed to get block height: rpc error",
422+
wantErr: "failed to get slot: rpc error",
423423
},
424424
{
425425
name: "error: GetBlockTime rpc error",

sdk/solana/utils_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ func mockGetAccountInfo(
4646
}
4747

4848
func mockGetBlockTime(
49-
t *testing.T, client *mocks.JSONRPCClient, block uint64,
49+
t *testing.T, client *mocks.JSONRPCClient, slot uint64,
5050
blockTime *solana.UnixTimeSeconds, mockBlockHeightError error,
5151
mockBlockTimeError error,
5252
) {
5353
t.Helper()
5454

5555
// mock getBlockHeight
56-
client.EXPECT().CallForInto(anyContext, mock.Anything, "getBlockHeight",
56+
client.EXPECT().CallForInto(anyContext, mock.Anything, "getSlot",
5757
[]any{rpc.M{"commitment": rpc.CommitmentConfirmed}},
5858
).RunAndReturn(func(_ context.Context, output any, _ string, _ []any) error {
5959
result, ok := output.(*uint64)
6060
require.True(t, ok)
6161

62-
*result = block // set block height as 1
62+
*result = slot
6363

6464
return mockBlockHeightError
6565
}).Once()
@@ -69,7 +69,7 @@ func mockGetBlockTime(
6969
}
7070

7171
client.EXPECT().CallForInto(
72-
anyContext, mock.Anything, "getBlockTime", []any{block},
72+
anyContext, mock.Anything, "getBlockTime", []any{slot},
7373
).RunAndReturn(func(_ context.Context, output any, _ string, _ []any) error {
7474
result, ok := output.(**solana.UnixTimeSeconds)
7575
require.True(t, ok)

0 commit comments

Comments
 (0)