Skip to content

Commit 17379b5

Browse files
committed
unit tests for lookups
1 parent a7b8b8b commit 17379b5

File tree

6 files changed

+467
-43
lines changed

6 files changed

+467
-43
lines changed

.mockery.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ packages:
3636
SimpleKeystore:
3737
config:
3838
filename: simple_keystore.go
39-
case: underscore
39+
case: underscore
40+
Txm:
41+
config:
42+
filename: txm.go

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241112140826-0e2daed34ef6
2121
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
2222
github.com/stretchr/testify v1.9.0
23+
github.com/test-go/testify v1.1.4
2324
go.uber.org/zap v1.27.0
2425
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
2526
golang.org/x/sync v0.8.0

gotest.log

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
📦 github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter
2+
exit status 1
3+
❌ TestLookupTables (30.07s)
4+
ports.go:37: found open port: 54520
5+
ports.go:37: found open port: 8536
6+
test_helpers.go:50: API server not ready yet (attempt 1)
7+
test_helpers.go:50: API server not ready yet (attempt 2)
8+
test_helpers.go:50: API server not ready yet (attempt 3)
9+
test_helpers.go:50: API server not ready yet (attempt 4)
10+
test_helpers.go:50: API server not ready yet (attempt 5)
11+
test_helpers.go:50: API server not ready yet (attempt 6)
12+
test_helpers.go:50: API server not ready yet (attempt 7)
13+
test_helpers.go:50: API server not ready yet (attempt 8)
14+
test_helpers.go:50: API server not ready yet (attempt 9)
15+
test_helpers.go:50: API server not ready yet (attempt 10)
16+
test_helpers.go:50: API server not ready yet (attempt 11)
17+
test_helpers.go:50: API server not ready yet (attempt 12)
18+
test_helpers.go:50: API server not ready yet (attempt 13)
19+
test_helpers.go:50: API server not ready yet (attempt 14)
20+
test_helpers.go:50: API server not ready yet (attempt 15)
21+
test_helpers.go:50: API server not ready yet (attempt 16)
22+
test_helpers.go:50: API server not ready yet (attempt 17)
23+
test_helpers.go:50: API server not ready yet (attempt 18)
24+
test_helpers.go:50: API server not ready yet (attempt 19)
25+
test_helpers.go:50: API server not ready yet (attempt 20)
26+
test_helpers.go:50: API server not ready yet (attempt 21)
27+
test_helpers.go:50: API server not ready yet (attempt 22)
28+
test_helpers.go:50: API server not ready yet (attempt 23)
29+
test_helpers.go:50: API server not ready yet (attempt 24)
30+
test_helpers.go:50: API server not ready yet (attempt 25)
31+
test_helpers.go:50: API server not ready yet (attempt 26)
32+
test_helpers.go:50: API server not ready yet (attempt 27)
33+
test_helpers.go:50: API server not ready yet (attempt 28)
34+
test_helpers.go:50: API server not ready yet (attempt 29)
35+
test_helpers.go:50: API server not ready yet (attempt 30)
36+
test_helpers.go:57: Cmd output:
37+
Notice! No wallet available. `solana airdrop` localnet SOL after creating one
38+
39+
Ledger location: /var/folders/p4/jlx3pf896blgl6tcj0xbkhvm0000gn/T/TestLookupTables940285115/001
40+
Log: /var/folders/p4/jlx3pf896blgl6tcj0xbkhvm0000gn/T/TestLookupTables940285115/001/validator.log
41+
Initializing...
42+
Error: failed to start validator: Failed to create ledger at /var/folders/p4/jlx3pf896blgl6tcj0xbkhvm0000gn/T/TestLookupTables940285115/001: blockstore error
43+
44+
Cmd error:
45+
test_helpers.go:59:
46+
Error Trace: /Users/silaslenihan/Desktop/repos/chainlink-solana/pkg/solana/client/test_helpers.go:59
47+
/Users/silaslenihan/Desktop/repos/chainlink-solana/pkg/solana/chainwriter/lookups_test.go:148
48+
Error: Should be true
49+
Test: TestLookupTables
50+
test_helpers.go:37:
51+
Error Trace: /Users/silaslenihan/Desktop/repos/chainlink-solana/pkg/solana/client/test_helpers.go:37
52+
/Users/silaslenihan/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1176
53+
/Users/silaslenihan/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1354
54+
/Users/silaslenihan/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1684
55+
/Users/silaslenihan/go/pkg/mod/golang.org/[email protected]/src/runtime/panic.go:629
56+
/Users/silaslenihan/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1006
57+
/Users/silaslenihan/Desktop/repos/chainlink-solana/pkg/solana/client/test_helpers.go:59
58+
/Users/silaslenihan/Desktop/repos/chainlink-solana/pkg/solana/chainwriter/lookups_test.go:148
59+
Error: "exit status 1" does not contain "signal: killed"
60+
Test: TestLookupTables
61+
Messages: exit status 1
62+
test_helpers.go:38: solana-test-validator
63+
stdout:
64+
Notice! No wallet available. `solana airdrop` localnet SOL after creating one
65+
66+
Ledger location: /var/folders/p4/jlx3pf896blgl6tcj0xbkhvm0000gn/T/TestLookupTables940285115/001
67+
Log: /var/folders/p4/jlx3pf896blgl6tcj0xbkhvm0000gn/T/TestLookupTables940285115/001/validator.log
68+
Initializing...
69+
Error: failed to start validator: Failed to create ledger at /var/folders/p4/jlx3pf896blgl6tcj0xbkhvm0000gn/T/TestLookupTables940285115/001: blockstore error
70+
71+
stderr:
72+

pkg/solana/chainwriter/chain_writer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (s *SolanaChainWriterService) SubmitTransaction(ctx context.Context, contra
212212
}
213213

214214
// Fetch derived and static table maps
215-
derivedTableMap, staticTableMap, err := s.getDerivedTableMap(ctx, methodConfig.LookupTables, debugID)
215+
derivedTableMap, staticTableMap, err := s.ResolveLookupTables(ctx, methodConfig.LookupTables, debugID)
216216
if err != nil {
217217
return errorWithDebugID(fmt.Errorf("error getting lookup tables: %w", err), debugID)
218218
}

pkg/solana/chainwriter/lookups.go

+19-41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/gagliardetto/solana-go"
8+
addresslookuptable "github.com/gagliardetto/solana-go/programs/address-lookup-table"
89
"github.com/gagliardetto/solana-go/rpc"
910
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
1011
)
@@ -181,7 +182,7 @@ func generatePDAs(publicKeys []*solana.AccountMeta, seeds [][]byte, lookup PDALo
181182
return addresses, nil
182183
}
183184

184-
func (s *SolanaChainWriterService) getDerivedTableMap(ctx context.Context, lookupTables LookupTables, debugID string) (map[string]map[string][]*solana.AccountMeta, map[solana.PublicKey]solana.PublicKeySlice, error) {
185+
func (s *SolanaChainWriterService) ResolveLookupTables(ctx context.Context, lookupTables LookupTables, debugID string) (map[string]map[string][]*solana.AccountMeta, map[solana.PublicKey]solana.PublicKeySlice, error) {
185186
derivedTableMap := make(map[string]map[string][]*solana.AccountMeta)
186187
staticTableMap := make(map[solana.PublicKey]solana.PublicKeySlice)
187188

@@ -211,23 +212,8 @@ func (s *SolanaChainWriterService) getDerivedTableMap(ctx context.Context, looku
211212
return nil, nil, errorWithDebugID(fmt.Errorf("invalid static lookup table address: %s, error: %w", staticTable, err), debugID)
212213
}
213214

214-
// Fetch the account info for the static table
215-
accountInfo, err := s.reader.GetAccountInfoWithOpts(ctx, tableAddress, &rpc.GetAccountInfoOpts{
216-
Encoding: "base64",
217-
Commitment: rpc.CommitmentConfirmed,
218-
})
219-
if err != nil || accountInfo == nil || accountInfo.Value == nil {
220-
return nil, nil, errorWithDebugID(fmt.Errorf("error fetching account info for static table: %s, error: %w", staticTable, err), debugID)
221-
}
222-
223-
// Decode the account data into an array of public keys
224-
addresses, err := decodeLookupTable(accountInfo.Value.Data.GetBinary())
225-
if err != nil {
226-
return nil, nil, errorWithDebugID(fmt.Errorf("error decoding static lookup table data for %s: %w", staticTable, err), debugID)
227-
}
228-
229-
// Add the static lookup table to the map
230-
staticTableMap[tableAddress] = addresses
215+
addressses, err := getLookupTableAddress(ctx, s.reader, tableAddress, debugID)
216+
staticTableMap[tableAddress] = addressses
231217
}
232218

233219
return derivedTableMap, staticTableMap, nil
@@ -246,18 +232,9 @@ func (s *SolanaChainWriterService) LoadTable(rlt DerivedLookupTable, ctx context
246232
// Iterate over each address of the lookup table
247233
for _, addressMeta := range lookupTableAddresses {
248234
// Fetch account info
249-
accountInfo, err := reader.GetAccountInfoWithOpts(ctx, addressMeta.PublicKey, &rpc.GetAccountInfoOpts{
250-
Encoding: "base64",
251-
Commitment: rpc.CommitmentConfirmed,
252-
})
253-
if err != nil || accountInfo == nil || accountInfo.Value == nil {
254-
return nil, nil, errorWithDebugID(fmt.Errorf("error fetching account info for address %s: %w", addressMeta.PublicKey.String(), err), debugID)
255-
}
256-
257-
// Decode the account data into an array of public keys
258-
addresses, err := decodeLookupTable(accountInfo.Value.Data.GetBinary())
235+
addresses, err := getLookupTableAddress(ctx, reader, addressMeta.PublicKey, debugID)
259236
if err != nil {
260-
return nil, nil, errorWithDebugID(fmt.Errorf("error decoding lookup table data for address %s: %w", addressMeta.PublicKey.String(), err), debugID)
237+
return nil, nil, errorWithDebugID(fmt.Errorf("error fetching lookup table address: %w", err), debugID)
261238
}
262239

263240
// Create the inner map for this lookup table
@@ -281,18 +258,19 @@ func (s *SolanaChainWriterService) LoadTable(rlt DerivedLookupTable, ctx context
281258
return resultMap, lookupTableMetas, nil
282259
}
283260

284-
func decodeLookupTable(data []byte) (solana.PublicKeySlice, error) {
285-
// Example logic to decode lookup table data; you may need to adjust based on the actual format of the data.
286-
var addresses solana.PublicKeySlice
261+
func getLookupTableAddress(ctx context.Context, reader client.Reader, tableAddress solana.PublicKey, debugID string) (solana.PublicKeySlice, error) {
262+
// Fetch the account info for the static table
263+
accountInfo, err := reader.GetAccountInfoWithOpts(ctx, tableAddress, &rpc.GetAccountInfoOpts{
264+
Encoding: "base64",
265+
Commitment: rpc.CommitmentConfirmed,
266+
})
287267

288-
// Assuming the data is a list of 32-byte public keys in binary format:
289-
for i := 0; i < len(data); i += solana.PublicKeyLength {
290-
if i+solana.PublicKeyLength > len(data) {
291-
return nil, fmt.Errorf("invalid lookup table data length")
292-
}
293-
address := solana.PublicKeyFromBytes(data[i : i+solana.PublicKeyLength])
294-
addresses = append(addresses, address)
268+
if err != nil || accountInfo == nil || accountInfo.Value == nil {
269+
return nil, errorWithDebugID(fmt.Errorf("error fetching account info for table: %s, error: %w", tableAddress.String(), err), debugID)
295270
}
296-
297-
return addresses, nil
271+
alt, err := addresslookuptable.DecodeAddressLookupTableState(accountInfo.GetBinary())
272+
if err != nil {
273+
return nil, errorWithDebugID(fmt.Errorf("error decoding address lookup table state: %w", err), debugID)
274+
}
275+
return alt.Addresses, nil
298276
}

0 commit comments

Comments
 (0)