From de5828fd7f706358f90ed5956d200b570a675ba4 Mon Sep 17 00:00:00 2001 From: Silas Lenihan Date: Fri, 27 Dec 2024 14:14:06 -0500 Subject: [PATCH] Got ChainComponentsTests working with ChainWriter --- .../contract-reader-interface/src/lib.rs | 6 +- integration-tests/go.mod | 2 +- .../relayinterface/chain_components_test.go | 104 +----------------- pkg/solana/txm/txm.go | 14 --- 4 files changed, 7 insertions(+), 119 deletions(-) diff --git a/contracts/programs/contract-reader-interface/src/lib.rs b/contracts/programs/contract-reader-interface/src/lib.rs index b02b68888..d3d366c42 100644 --- a/contracts/programs/contract-reader-interface/src/lib.rs +++ b/contracts/programs/contract-reader-interface/src/lib.rs @@ -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, @@ -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>, } diff --git a/integration-tests/go.mod b/integration-tests/go.mod index c66251505..64ea60312 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -23,6 +23,7 @@ require ( github.com/smartcontractkit/chainlink/v2 v2.14.0-mercury-20240807.0.20241212202512-52c2db4bff51 github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12 github.com/stretchr/testify v1.9.0 + github.com/test-go/testify v1.1.4 github.com/testcontainers/testcontainers-go v0.34.0 golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c golang.org/x/sync v0.10.0 @@ -362,7 +363,6 @@ require ( github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect github.com/tendermint/go-amino v0.16.0 // indirect github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125 // indirect - github.com/test-go/testify v1.1.4 // indirect github.com/theodesp/go-heaps v0.0.0-20190520121037-88e35354fe0a // indirect github.com/tidwall/btree v1.6.0 // indirect github.com/tidwall/gjson v1.17.0 // indirect diff --git a/integration-tests/relayinterface/chain_components_test.go b/integration-tests/relayinterface/chain_components_test.go index 4346fa930..878245e78 100644 --- a/integration-tests/relayinterface/chain_components_test.go +++ b/integration-tests/relayinterface/chain_components_test.go @@ -6,9 +6,6 @@ package relayinterface import ( "context" "encoding/binary" - "fmt" - "io" - "log" "os" "path/filepath" "sync" @@ -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/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" @@ -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) @@ -319,8 +317,6 @@ 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) @@ -336,16 +332,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} } @@ -404,13 +390,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 } @@ -420,9 +400,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() @@ -436,82 +413,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" diff --git a/pkg/solana/txm/txm.go b/pkg/solana/txm/txm.go index 06729ca63..c87089060 100644 --- a/pkg/solana/txm/txm.go +++ b/pkg/solana/txm/txm.go @@ -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 }