Skip to content

Commit

Permalink
Got ChainComponentsTests working with ChainWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Dec 27, 2024
1 parent 86a5ad3 commit 99babd0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 138 deletions.
36 changes: 18 additions & 18 deletions contracts/generated/contract_reader_interface/Initialize.go

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

6 changes: 3 additions & 3 deletions contracts/programs/contract-reader-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub mod contract_reader_interface {
#[derive(Accounts)]
#[instruction(test_idx: u64)]
pub struct Initialize<'info> {
#[account(mut)]
pub signer: Signer<'info>,

// derived test PDA
#[account(
init,
Expand All @@ -44,9 +47,6 @@ pub struct Initialize<'info> {
bump)]
pub data: Account<'info, DataAccount>,

#[account(mut)]
pub signer: Signer<'info>,

pub system_program: Program<'info, System>,
}

Expand Down
110 changes: 7 additions & 103 deletions integration-tests/relayinterface/chain_components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package relayinterface
import (
"context"
"encoding/binary"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
Expand All @@ -18,11 +15,11 @@ import (
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/gagliardetto/solana-go/rpc/ws"
"github.com/gagliardetto/solana-go/text"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/test-go/testify/mock"

"github.com/smartcontractkit/chainlink-common/pkg/codec"
commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
commontestutils "github.com/smartcontractkit/chainlink-common/pkg/loop/testutils"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
Expand Down Expand Up @@ -310,6 +307,7 @@ func (h *helper) Init(t *testing.T) {
solanautils.FundAccounts(t, []solana.PrivateKey{privateKey}, h.rpcClient)

cfg := config.NewDefault()
cfg.Chain.TxRetentionTimeout = commonconfig.MustNewDuration(10 * time.Minute)
solanaClient, err := client.NewClient(h.rpcURL, cfg, 5*time.Second, nil)
require.NoError(t, err)

Expand All @@ -319,14 +317,14 @@ func (h *helper) Init(t *testing.T) {
mkey := keyMocks.NewSimpleKeystore(t)
mkey.On("Sign", mock.Anything, privateKey.PublicKey().String(), mock.Anything).Return(func(_ context.Context, _ string, data []byte) []byte {
sig, _ := privateKey.Sign(data)
verifySignature(privateKey.PublicKey(), sig[:], data)
fmt.Printf("Signed for %s: %x\n", privateKey.PublicKey().String(), sig)
return sig[:]
}, nil)
lggr := logger.Test(t)

txm := txm.NewTxm("localnet", loader, nil, cfg, mkey, lggr)
txm.Start(tests.Context(t))
err = txm.Start(tests.Context(t))
require.NoError(t, err)

h.txm = txm

pubkey, err := solana.PublicKeyFromBase58(programPubKey)
Expand All @@ -336,16 +334,6 @@ func (h *helper) Init(t *testing.T) {
h.programID = pubkey
}

func verifySignature(publicKey solana.PublicKey, signature []byte, message []byte) bool {
valid := publicKey.Verify(message, solana.SignatureFromBytes(signature))
if valid {
log.Printf("Signature is valid for public key: %s\n", publicKey.String())
} else {
log.Printf("Signature is invalid for public key: %s\n", publicKey.String())
}
return valid
}

func (h *helper) RPCClient() *chainreader.RPCClientWrapper {
return &chainreader.RPCClientWrapper{Client: h.rpcClient}
}
Expand Down Expand Up @@ -404,13 +392,7 @@ func (h *helper) CreateAccount(t *testing.T, it SolanaChainComponentsInterfaceTe
pubKey, _, err := solana.FindProgramAddress([][]byte{[]byte("data"), bts}, h.programID)
require.NoError(t, err)

// Getting the default localnet private key
privateKey, err := solana.PrivateKeyFromBase58(solclient.DefaultPrivateKeysSolValidator[1])
require.NoError(t, err)

h.runInitialize(t, it, nonce, value, pubKey, func(key solana.PublicKey) *solana.PrivateKey {
return &privateKey
}, privateKey.PublicKey())
h.runInitialize(t, it, nonce, value)

return pubKey
}
Expand All @@ -420,9 +402,6 @@ func (h *helper) runInitialize(
it SolanaChainComponentsInterfaceTester[*testing.T],
nonce uint64,
value uint64,
data solana.PublicKey,
signerFunc func(key solana.PublicKey) *solana.PrivateKey,
payer solana.PublicKey,
) {
t.Helper()

Expand All @@ -436,82 +415,7 @@ func (h *helper) runInitialize(
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, nonce*value)

data, _, err := solana.FindProgramAddress(
[][]byte{
[]byte("data"), // Seed 1
buf, // Seed 2 (test_idx)
},
solana.MustPublicKeyFromBase58(programPubKey), // The program ID
)
require.NoError(t, err)

fmt.Printf("Derived PDA in test: %s\n", data.String())

SubmitTransactionToCW(t, &it, cw, "initialize", args, types.BoundContract{Name: AnyContractName, Address: h.programID.String()}, types.Finalized)

// inst, err := contract.NewInitializeInstruction(nonce*value, value, data, payer, solana.SystemProgramID).ValidateAndBuild()
// require.NoError(t, err)

// h.sendInstruction(t, inst, signerFunc, payer)
}

func (h *helper) sendInstruction(
t *testing.T,
inst *contract.Instruction,
signerFunc func(key solana.PublicKey) *solana.PrivateKey,
payer solana.PublicKey,
) {
t.Helper()

ctx := tests.Context(t)

recent, err := h.rpcClient.GetLatestBlockhash(ctx, rpc.CommitmentFinalized)
require.NoError(t, err)

tx, err := solana.NewTransaction(
[]solana.Instruction{
inst,
},
recent.Value.Blockhash,
solana.TransactionPayer(payer),
)
require.NoError(t, err)

_, err = tx.EncodeTree(text.NewTreeEncoder(io.Discard, "Initialize"))
require.NoError(t, err)

_, err = tx.Sign(signerFunc)
require.NoError(t, err)

sig, err := h.rpcClient.SendTransactionWithOpts(
ctx, tx,
rpc.TransactionOpts{
PreflightCommitment: rpc.CommitmentConfirmed,
},
)
require.NoError(t, err)

h.waitForTX(t, sig, rpc.CommitmentFinalized)
}

func (h *helper) waitForTX(t *testing.T, sig solana.Signature, commitment rpc.CommitmentType) {
t.Helper()

sub, err := h.wsClient.SignatureSubscribe(
sig,
commitment,
)
require.NoError(t, err)

defer sub.Unsubscribe()

res, err := sub.Recv()
require.NoError(t, err)

if res.Value.Err != nil {
t.Logf("transaction confirmation failed: %v", res.Value.Err)
t.FailNow()
}
}

const programPubKey = "6AfuXF6HapDUhQfE4nQG9C1SGtA1YjP3icaJyRfU4RyE"
14 changes: 0 additions & 14 deletions pkg/solana/txm/txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,24 +253,10 @@ func (txm *Txm) buildTx(ctx context.Context, msg pendingTx, retryCount int) (sol
if err != nil {
return solanaGo.Transaction{}, fmt.Errorf("error in Sign: %w", err)
}
fmt.Printf("Transaction Message (hex): %x\n", txMsg)

var finalSig [64]byte
copy(finalSig[:], sigBytes)
newTx.Signatures = append(newTx.Signatures, finalSig)

for i, sig := range newTx.Signatures {
fmt.Printf("Signature[%d]: %x\n", i, sig)
}

for i, account := range newTx.Message.AccountKeys {
writable, err := newTx.Message.IsWritable(account)
if err != nil {
return solanaGo.Transaction{}, fmt.Errorf("error in IsWritable: %w", err)
}
fmt.Printf("Account[%d]: %s (Signer: %v, Writable: %v)\n", i, account, newTx.Message.IsSigner(account), writable)
}

return newTx, nil
}

Expand Down

0 comments on commit 99babd0

Please sign in to comment.