Skip to content

Commit

Permalink
Completed chained lookup integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Nov 27, 2024
1 parent ed1270d commit 66d00dc
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 102 deletions.
5 changes: 3 additions & 2 deletions contracts/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test = "pnpm run test"

[programs.localnet]
access_controller = "9xi644bRR8birboDGdTiwBq3C7VEeR7VuamRYYXCubUW"
log-read-test = "J1zQwrBNBngz26jRPNWsUSZMHJwBwpkoDitXRV95LdK4"
log_read_test = "J1zQwrBNBngz26jRPNWsUSZMHJwBwpkoDitXRV95LdK4"
ocr_2 = "cjg3oHmg9uuPsP8D6g29NWvhySJkdYdAo9D25PRbKXJ" # need to rename the idl to satisfy anchor.js...
store = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny"
store = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny"
write_test = "39vbQVpEMtZtg3e6ZSE7nBSzmNZptmW45WnLkbqEe4TU"
7 changes: 7 additions & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/artifacts/localnet/write_test-keypair.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[26,39,164,161,246,97,149,0,58,187,146,162,53,35,107,2,117,242,83,171,48,7,63,240,69,221,239,45,97,55,112,106,192,228,214,205,123,71,58,23,62,229,166,213,149,122,96,145,35,150,16,156,247,199,242,108,173,80,62,231,39,196,27,192]
60 changes: 33 additions & 27 deletions contracts/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions contracts/programs/write_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "write-test"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "write_test"

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.29.0"
2 changes: 2 additions & 0 deletions contracts/programs/write_test/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
52 changes: 52 additions & 0 deletions contracts/programs/write_test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use anchor_lang::prelude::*;

declare_id!("39vbQVpEMtZtg3e6ZSE7nBSzmNZptmW45WnLkbqEe4TU");

#[program]
pub mod write_test {
use super::*;

pub fn initialize(ctx: Context<Initialize>, lookup_table: Pubkey) -> Result<()> {
let data = &mut ctx.accounts.data_account;
data.version = 1;
data.administrator = ctx.accounts.admin.key();
data.pending_administrator = Pubkey::default();
data.lookup_table = lookup_table;

Ok(())
}

}

#[derive(Accounts)]
pub struct Initialize<'info> {
/// PDA account, derived from seeds and created by the System Program in this instruction
#[account(
init, // Initialize the account
payer = admin, // Specify the payer
space = DataAccount::SIZE, // Specify the account size
seeds = [b"data"], // Define the PDA seeds
bump // Use the bump seed
)]
pub data_account: Account<'info, DataAccount>,

/// Admin account that pays for PDA creation and signs the transaction
#[account(mut)]
pub admin: Signer<'info>,

/// System Program is required for PDA creation
pub system_program: Program<'info, System>,
}

#[account]
pub struct DataAccount {
pub version: u8,
pub administrator: Pubkey,
pub pending_administrator: Pubkey,
pub lookup_table: Pubkey,
}

impl DataAccount {
/// The total size of the `DataAccount` struct, including the discriminator
pub const SIZE: usize = 8 + 1 + 32 * 3; // 8 bytes for discriminator + 1 byte for version + 32 bytes * 3 pubkeys
}
13 changes: 6 additions & 7 deletions pkg/solana/chainwriter/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ for Solana transactions. It handles constant addresses, dynamic lookups, program
*/
// GetAddresses resolves account addresses from various `Lookup` configurations to build the required `solana.AccountMeta` list
// for Solana transactions.
func GetAddresses(ctx context.Context, args any, accounts []Lookup, derivedTableMap map[string]map[string][]*solana.AccountMeta, debugID string) ([]*solana.AccountMeta, error) {
func GetAddresses(ctx context.Context, args any, accounts []Lookup, derivedTableMap map[string]map[string][]*solana.AccountMeta, reader client.Reader) ([]*solana.AccountMeta, error) {
var addresses []*solana.AccountMeta
for _, accountConfig := range accounts {
meta, err := accountConfig.Resolve(ctx, args, derivedTableMap, debugID)
meta, err := accountConfig.Resolve(ctx, args, derivedTableMap, reader)
if err != nil {
return nil, err
}
Expand All @@ -147,7 +147,6 @@ func (s *SolanaChainWriterService) FilterLookupTableAddresses(
accounts []*solana.AccountMeta,
derivedTableMap map[string]map[string][]*solana.AccountMeta,
staticTableMap map[solana.PublicKey]solana.PublicKeySlice,
debugID string,
) map[solana.PublicKey]solana.PublicKeySlice {
filteredLookupTables := make(map[solana.PublicKey]solana.PublicKeySlice)

Expand All @@ -162,7 +161,7 @@ func (s *SolanaChainWriterService) FilterLookupTableAddresses(
for innerIdentifier, metas := range innerMap {
tableKey, err := solana.PublicKeyFromBase58(innerIdentifier)
if err != nil {
errorWithDebugID(fmt.Errorf("error parsing lookup table key: %w", err), debugID)
fmt.Errorf("error parsing lookup table key: %w", err)
}

// Collect public keys that are actually used
Expand Down Expand Up @@ -212,19 +211,19 @@ func (s *SolanaChainWriterService) SubmitTransaction(ctx context.Context, contra
}

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

// Resolve account metas
accounts, err := GetAddresses(ctx, args, methodConfig.Accounts, derivedTableMap, debugID)
accounts, err := GetAddresses(ctx, args, methodConfig.Accounts, derivedTableMap, s.reader)
if err != nil {
return errorWithDebugID(fmt.Errorf("error resolving account addresses: %w", err), debugID)
}

// Filter the lookup table addresses based on which accounts are actually used
filteredLookupTableMap := s.FilterLookupTableAddresses(accounts, derivedTableMap, staticTableMap, debugID)
filteredLookupTableMap := s.FilterLookupTableAddresses(accounts, derivedTableMap, staticTableMap)

// Fetch latest blockhash
blockhash, err := s.reader.LatestBlockhash(ctx)
Expand Down
6 changes: 2 additions & 4 deletions pkg/solana/chainwriter/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
)

// GetValuesAtLocation parses through nested types and arrays to find all locations of values
func GetValuesAtLocation(args any, location string, debugID string) ([][]byte, error) {
func GetValuesAtLocation(args any, location string) ([][]byte, error) {
var vals [][]byte

path := strings.Split(location, ".")

addressList, err := traversePath(args, path)
Expand All @@ -26,7 +25,7 @@ func GetValuesAtLocation(args any, location string, debugID string) ([][]byte, e
} else if address, ok := value.(solana.PublicKey); ok {
vals = append(vals, address.Bytes())
} else {
return nil, errorWithDebugID(fmt.Errorf("invalid value format at path: %s", location), debugID)
return nil, fmt.Errorf("invalid value format at path: %s", location)
}
}

Expand Down Expand Up @@ -85,7 +84,6 @@ func traversePath(data any, path []string) ([]any, error) {
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
fmt.Printf("Current path: %v, Current value type: %v\n", path, val.Kind())

switch val.Kind() {
case reflect.Struct:
Expand Down
Loading

0 comments on commit 66d00dc

Please sign in to comment.