Skip to content

Commit d07e7f0

Browse files
committed
Got ChainComponentsTests working with ChainWriter
1 parent 52c4ef4 commit d07e7f0

File tree

4 files changed

+37
-142
lines changed

4 files changed

+37
-142
lines changed

contracts/generated/contract_reader_interface/Initialize.go

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/programs/contract-reader-interface/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub mod contract_reader_interface {
3535
#[derive(Accounts)]
3636
#[instruction(test_idx: u64)]
3737
pub struct Initialize<'info> {
38+
#[account(mut)]
39+
pub signer: Signer<'info>,
40+
3841
// derived test PDA
3942
#[account(
4043
init,
@@ -44,9 +47,6 @@ pub struct Initialize<'info> {
4447
bump)]
4548
pub data: Account<'info, DataAccount>,
4649

47-
#[account(mut)]
48-
pub signer: Signer<'info>,
49-
5050
pub system_program: Program<'info, System>,
5151
}
5252

integration-tests/relayinterface/chain_components_test.go

+16-107
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"context"
88
"encoding/binary"
99
"encoding/json"
10-
"fmt"
11-
"io"
12-
"log"
1310
"os"
1411
"path/filepath"
1512
"sync"
@@ -19,11 +16,11 @@ import (
1916
"github.com/gagliardetto/solana-go"
2017
"github.com/gagliardetto/solana-go/rpc"
2118
"github.com/gagliardetto/solana-go/rpc/ws"
22-
"github.com/gagliardetto/solana-go/text"
19+
"github.com/stretchr/testify/mock"
2320
"github.com/stretchr/testify/require"
24-
"github.com/test-go/testify/mock"
2521

2622
commoncodec "github.com/smartcontractkit/chainlink-common/pkg/codec"
23+
commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
2724
"github.com/smartcontractkit/chainlink-common/pkg/logger"
2825
commontestutils "github.com/smartcontractkit/chainlink-common/pkg/loop/testutils"
2926
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
@@ -207,7 +204,7 @@ func (it *SolanaChainComponentsInterfaceTester[T]) Setup(t T) {
207204
{Static: []byte("data")},
208205
{Dynamic: chainwriter.AccountLookup{
209206
Name: "TestIDX",
210-
Location: "testIdx",
207+
Location: "TestIdx",
211208
}},
212209
},
213210
IsWritable: true,
@@ -304,6 +301,7 @@ func (h *helper) Init(t *testing.T) {
304301
solanautils.FundAccounts(t, []solana.PrivateKey{privateKey}, h.rpcClient)
305302

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

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

322318
txm := txm.NewTxm("localnet", loader, nil, cfg, mkey, lggr)
323-
txm.Start(tests.Context(t))
319+
err = txm.Start(tests.Context(t))
320+
require.NoError(t, err)
321+
324322
h.txm = txm
325323

326324
pubkey, err := solana.PublicKeyFromBase58(programPubKey)
@@ -330,16 +328,6 @@ func (h *helper) Init(t *testing.T) {
330328
h.programID = pubkey
331329
}
332330

333-
func verifySignature(publicKey solana.PublicKey, signature []byte, message []byte) bool {
334-
valid := publicKey.Verify(message, solana.SignatureFromBytes(signature))
335-
if valid {
336-
log.Printf("Signature is valid for public key: %s\n", publicKey.String())
337-
} else {
338-
log.Printf("Signature is invalid for public key: %s\n", publicKey.String())
339-
}
340-
return valid
341-
}
342-
343331
func (h *helper) RPCClient() *chainreader.RPCClientWrapper {
344332
return &chainreader.RPCClientWrapper{Client: h.rpcClient}
345333
}
@@ -398,114 +386,35 @@ func (h *helper) CreateAccount(t *testing.T, it SolanaChainComponentsInterfaceTe
398386
pubKey, _, err := solana.FindProgramAddress([][]byte{[]byte("data"), bts}, h.programID)
399387
require.NoError(t, err)
400388

401-
// Getting the default localnet private key
402-
privateKey, err := solana.PrivateKeyFromBase58(solclient.DefaultPrivateKeysSolValidator[1])
403-
require.NoError(t, err)
404-
405-
h.runInitialize(t, it, nonce, value, pubKey, func(key solana.PublicKey) *solana.PrivateKey {
406-
return &privateKey
407-
}, privateKey.PublicKey())
389+
h.runInitialize(t, it, nonce, value)
408390

409391
return pubKey
410392
}
411393

394+
type InitializeArgs struct {
395+
TestIdx uint64
396+
Value uint64
397+
}
398+
412399
func (h *helper) runInitialize(
413400
t *testing.T,
414401
it SolanaChainComponentsInterfaceTester[*testing.T],
415402
nonce uint64,
416403
value uint64,
417-
data solana.PublicKey,
418-
signerFunc func(key solana.PublicKey) *solana.PrivateKey,
419-
payer solana.PublicKey,
420404
) {
421405
t.Helper()
422406

423407
cw := it.GetContractWriter(t)
424408

425-
args := map[string]interface{}{
426-
"testIdx": nonce * value,
427-
"value": value,
409+
args := InitializeArgs{
410+
TestIdx: nonce * value,
411+
Value: value,
428412
}
429413

430414
buf := make([]byte, 8)
431415
binary.LittleEndian.PutUint64(buf, nonce*value)
432416

433-
data, _, err := solana.FindProgramAddress(
434-
[][]byte{
435-
[]byte("data"), // Seed 1
436-
buf, // Seed 2 (test_idx)
437-
},
438-
solana.MustPublicKeyFromBase58(programPubKey), // The program ID
439-
)
440-
require.NoError(t, err)
441-
442-
fmt.Printf("Derived PDA in test: %s\n", data.String())
443-
444417
SubmitTransactionToCW(t, &it, cw, "initialize", args, types.BoundContract{Name: AnyContractName, Address: h.programID.String()}, types.Finalized)
445-
446-
// inst, err := contract.NewInitializeInstruction(nonce*value, value, data, payer, solana.SystemProgramID).ValidateAndBuild()
447-
// require.NoError(t, err)
448-
449-
// h.sendInstruction(t, inst, signerFunc, payer)
450-
}
451-
452-
func (h *helper) sendInstruction(
453-
t *testing.T,
454-
inst *contract.Instruction,
455-
signerFunc func(key solana.PublicKey) *solana.PrivateKey,
456-
payer solana.PublicKey,
457-
) {
458-
t.Helper()
459-
460-
ctx := tests.Context(t)
461-
462-
recent, err := h.rpcClient.GetLatestBlockhash(ctx, rpc.CommitmentFinalized)
463-
require.NoError(t, err)
464-
465-
tx, err := solana.NewTransaction(
466-
[]solana.Instruction{
467-
inst,
468-
},
469-
recent.Value.Blockhash,
470-
solana.TransactionPayer(payer),
471-
)
472-
require.NoError(t, err)
473-
474-
_, err = tx.EncodeTree(text.NewTreeEncoder(io.Discard, "Initialize"))
475-
require.NoError(t, err)
476-
477-
_, err = tx.Sign(signerFunc)
478-
require.NoError(t, err)
479-
480-
sig, err := h.rpcClient.SendTransactionWithOpts(
481-
ctx, tx,
482-
rpc.TransactionOpts{
483-
PreflightCommitment: rpc.CommitmentConfirmed,
484-
},
485-
)
486-
require.NoError(t, err)
487-
488-
h.waitForTX(t, sig, rpc.CommitmentFinalized)
489-
}
490-
491-
func (h *helper) waitForTX(t *testing.T, sig solana.Signature, commitment rpc.CommitmentType) {
492-
t.Helper()
493-
494-
sub, err := h.wsClient.SignatureSubscribe(
495-
sig,
496-
commitment,
497-
)
498-
require.NoError(t, err)
499-
500-
defer sub.Unsubscribe()
501-
502-
res, err := sub.Recv()
503-
require.NoError(t, err)
504-
505-
if res.Value.Err != nil {
506-
t.Logf("transaction confirmation failed: %v", res.Value.Err)
507-
t.FailNow()
508-
}
509418
}
510419

511420
func mustUnmarshalIDL[T TestingT[T]](t T, rawIDL string) codec.IDL {

pkg/solana/txm/txm.go

-14
Original file line numberDiff line numberDiff line change
@@ -253,24 +253,10 @@ func (txm *Txm) buildTx(ctx context.Context, msg pendingTx, retryCount int) (sol
253253
if err != nil {
254254
return solanaGo.Transaction{}, fmt.Errorf("error in Sign: %w", err)
255255
}
256-
fmt.Printf("Transaction Message (hex): %x\n", txMsg)
257-
258256
var finalSig [64]byte
259257
copy(finalSig[:], sigBytes)
260258
newTx.Signatures = append(newTx.Signatures, finalSig)
261259

262-
for i, sig := range newTx.Signatures {
263-
fmt.Printf("Signature[%d]: %x\n", i, sig)
264-
}
265-
266-
for i, account := range newTx.Message.AccountKeys {
267-
writable, err := newTx.Message.IsWritable(account)
268-
if err != nil {
269-
return solanaGo.Transaction{}, fmt.Errorf("error in IsWritable: %w", err)
270-
}
271-
fmt.Printf("Account[%d]: %s (Signer: %v, Writable: %v)\n", i, account, newTx.Message.IsSigner(account), writable)
272-
}
273-
274260
return newTx, nil
275261
}
276262

0 commit comments

Comments
 (0)