Skip to content

Commit

Permalink
updated comments and slight tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Dec 23, 2024
1 parent 94ee3fc commit b852c76
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
6 changes: 3 additions & 3 deletions integration-tests/relayinterface/lookups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestAccountLookups(t *testing.T) {
}

func TestPDALookups(t *testing.T) {
programID := solana.SystemProgramID
programID := chainwriter.GetRandomPubKey(t)

t.Run("PDALookup resolves valid PDA with constant address seeds", func(t *testing.T) {
seed := chainwriter.GetRandomPubKey(t)
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestLookupTables(t *testing.T) {
table := chainwriter.CreateTestLookupTable(ctx, t, rpcClient, sender, pubKeys)
lookupConfig := chainwriter.LookupTables{
DerivedLookupTables: nil,
StaticLookupTables: []string{table.String()},
StaticLookupTables: []solana.PublicKey{table},
}
_, staticTableMap, resolveErr := cw.ResolveLookupTables(ctx, nil, lookupConfig)
require.NoError(t, resolveErr)
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestLookupTables(t *testing.T) {

lookupConfig := chainwriter.LookupTables{
DerivedLookupTables: nil,
StaticLookupTables: []string{invalidTable.String()},
StaticLookupTables: []solana.PublicKey{invalidTable},
}

_, _, err = cw.ResolveLookupTables(ctx, nil, lookupConfig)
Expand Down
12 changes: 6 additions & 6 deletions pkg/solana/chainwriter/ccip_example_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package chainwriter

import (
"fmt"
"github.com/gagliardetto/solana-go"
)

func TestConfig() {
Expand All @@ -13,8 +13,8 @@ func TestConfig() {
systemProgramAddress := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6E"
computeBudgetProgramAddress := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6F"
sysvarProgramAddress := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6G"
commonAddressesLookupTable := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6H"
routerLookupTable := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6I"
commonAddressesLookupTable := solana.MustPublicKeyFromBase58("4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6H")
routerLookupTable := solana.MustPublicKeyFromBase58("4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6I")
userAddress := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6J"

executionReportSingleChainIDL := `{"name":"ExecutionReportSingleChain","type":{"kind":"struct","fields":[{"name":"source_chain_selector","type":"u64"},{"name":"message","type":{"defined":"Any2SolanaRampMessage"}},{"name":"root","type":{"array":["u8",32]}},{"name":"proofs","type":{"vec":{"array":["u8",32]}}}]}},{"name":"Any2SolanaRampMessage","type":{"kind":"struct","fields":[{"name":"header","type":{"defined":"RampMessageHeader"}},{"name":"sender","type":{"vec":"u8"}},{"name":"data","type":{"vec":"u8"}},{"name":"receiver","type":{"array":["u8",32]}},{"name":"extra_args","type":{"defined":"SolanaExtraArgs"}}]}},{"name":"RampMessageHeader","type":{"kind":"struct","fields":[{"name":"message_id","type":{"array":["u8",32]}},{"name":"source_chain_selector","type":"u64"},{"name":"dest_chain_selector","type":"u64"},{"name":"sequence_number","type":"u64"},{"name":"nonce","type":"u64"}]}},{"name":"SolanaExtraArgs","type":{"kind":"struct","fields":[{"name":"compute_units","type":"u32"},{"name":"allow_out_of_order_execution","type":"bool"}]}}`
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestConfig() {
// Static lookup tables are the traditional use case (point 2 above) of Lookup tables. These are lookup
// tables which contain commonly used addresses in all CCIP execute transactions. The ChainWriter reads
// these lookup tables and appends them to the transaction to reduce the size of the transaction.
StaticLookupTables: []string{
StaticLookupTables: []solana.PublicKey{
commonAddressesLookupTable,
routerLookupTable,
},
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestConfig() {
InputModifications: nil,
ChainSpecificName: "commit",
LookupTables: LookupTables{
StaticLookupTables: []string{
StaticLookupTables: []solana.PublicKey{
commonAddressesLookupTable,
routerLookupTable,
},
Expand Down Expand Up @@ -329,5 +329,5 @@ func TestConfig() {
},
},
}
fmt.Println(chainWriterConfig)
_ = chainWriterConfig
}
6 changes: 3 additions & 3 deletions pkg/solana/chainwriter/chain_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestChainWriter_FilterLookupTableAddresses(t *testing.T) {
Name: "DataAccountPDA",
PublicKey: chainwriter.AccountConstant{Name: "WriteTest", Address: programID.String()},
Seeds: []chainwriter.Lookup{
// extract seed2 for PDA lookup
// extract seed1 for PDA lookup
chainwriter.AccountLookup{Name: "seed1", Location: "seed1"},
},
IsSigner: true,
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestChainWriter_FilterLookupTableAddresses(t *testing.T) {
},
},
},
StaticLookupTables: []string{staticLookupTablePubkey1.String(), staticLookupTablePubkey2.String()},
StaticLookupTables: []solana.PublicKey{staticLookupTablePubkey1, staticLookupTablePubkey2},
}

args := map[string]interface{}{
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestChainWriter_SubmitTransaction(t *testing.T) {
},
},
},
StaticLookupTables: []string{staticLookupTablePubkey.String()},
StaticLookupTables: []solana.PublicKey{staticLookupTablePubkey},
},
Accounts: []chainwriter.Lookup{
chainwriter.AccountConstant{
Expand Down
28 changes: 10 additions & 18 deletions pkg/solana/chainwriter/lookups.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ type InternalField struct {
Location string
}

type ValueLookup struct {
Location string
}

// LookupTables represents a list of lookup tables that are used to derive addresses for a program.
type LookupTables struct {
DerivedLookupTables []DerivedLookupTable
StaticLookupTables []string
StaticLookupTables []solana.PublicKey
}

// DerivedLookupTable represents a lookup table that is used to derive addresses for a program.
Expand Down Expand Up @@ -212,7 +208,6 @@ func decodeBorshIntoType(data []byte, typ reflect.Type) (interface{}, error) {
// It handles both AddressSeeds (which are public keys) and ValueSeeds (which are byte arrays from input args).
func getSeedBytes(ctx context.Context, lookup PDALookups, args any, derivedTableMap map[string]map[string][]*solana.AccountMeta, reader client.Reader) ([][]byte, error) {
var seedBytes [][]byte
maxSeedLength := 32

for _, seed := range lookup.Seeds {
if lookupSeed, ok := seed.(AccountLookup); ok {
Expand All @@ -223,8 +218,8 @@ func getSeedBytes(ctx context.Context, lookup PDALookups, args any, derivedTable
}
// validate seed length
for _, b := range bytes {
if len(b) > maxSeedLength {
return nil, fmt.Errorf("seed byte array exceeds maximum length of %d: got %d bytes", maxSeedLength, len(b))
if len(b) > solana.MaxSeedLength {
return nil, fmt.Errorf("seed byte array exceeds maximum length of %d: got %d bytes", solana.MaxSeedLength, len(b))
}
seedBytes = append(seedBytes, b)
}
Expand All @@ -247,7 +242,7 @@ func getSeedBytes(ctx context.Context, lookup PDALookups, args any, derivedTable

// generatePDAs generates program-derived addresses (PDAs) from public keys and seeds.
func generatePDAs(publicKeys []*solana.AccountMeta, seeds [][]byte, lookup PDALookups) ([]*solana.AccountMeta, error) {
if len(seeds) > 16 {
if len(seeds) > solana.MaxSeeds {
return nil, fmt.Errorf("seed maximum exceeded: %d", len(seeds))
}
var addresses []*solana.AccountMeta
Expand All @@ -271,6 +266,8 @@ func (s *SolanaChainWriterService) ResolveLookupTables(ctx context.Context, args

// Read derived lookup tables
for _, derivedLookup := range lookupTables.DerivedLookupTables {
// Load the lookup table - note: This could be multiple tables if the lookup is a PDALookups that resovles to more
// than one address
lookupTableMap, _, err := s.LoadTable(ctx, args, derivedLookup, s.reader, derivedTableMap)
if err != nil {
return nil, nil, fmt.Errorf("error loading derived lookup table: %w", err)
Expand All @@ -289,17 +286,11 @@ func (s *SolanaChainWriterService) ResolveLookupTables(ctx context.Context, args

// Read static lookup tables
for _, staticTable := range lookupTables.StaticLookupTables {
// Parse the static table address
tableAddress, err := solana.PublicKeyFromBase58(staticTable)
if err != nil {
return nil, nil, fmt.Errorf("invalid static lookup table address: %s, error: %w", staticTable, err)
}

addressses, err := getLookupTableAddresses(ctx, s.reader, tableAddress)
addressses, err := getLookupTableAddresses(ctx, s.reader, staticTable)
if err != nil {
return nil, nil, fmt.Errorf("error fetching static lookup table address: %w", err)
}
staticTableMap[tableAddress] = addressses
staticTableMap[staticTable] = addressses
}

return derivedTableMap, staticTableMap, nil
Expand All @@ -312,12 +303,13 @@ func (s *SolanaChainWriterService) LoadTable(ctx context.Context, args any, rlt
return nil, nil, fmt.Errorf("error resolving addresses for lookup table: %w", err)
}

// Nested map in case the lookup table resolves to multiple addresses
resultMap := make(map[string]map[string][]*solana.AccountMeta)
var lookupTableMetas []*solana.AccountMeta

// Iterate over each address of the lookup table
for _, addressMeta := range lookupTableAddresses {
// Fetch account info
// Read the full list of addresses from the lookup table
addresses, err := getLookupTableAddresses(ctx, reader, addressMeta.PublicKey)
if err != nil {
return nil, nil, fmt.Errorf("error fetching lookup table address: %w", err)
Expand Down

0 comments on commit b852c76

Please sign in to comment.