7
7
"context"
8
8
"encoding/binary"
9
9
"encoding/json"
10
- "fmt"
11
- "io"
12
- "log"
13
10
"os"
14
11
"path/filepath"
15
12
"sync"
@@ -19,11 +16,11 @@ import (
19
16
"github.com/gagliardetto/solana-go"
20
17
"github.com/gagliardetto/solana-go/rpc"
21
18
"github.com/gagliardetto/solana-go/rpc/ws"
22
- "github.com/gagliardetto/solana-go/text "
19
+ "github.com/stretchr/testify/mock "
23
20
"github.com/stretchr/testify/require"
24
- "github.com/test-go/testify/mock"
25
21
26
22
commoncodec "github.com/smartcontractkit/chainlink-common/pkg/codec"
23
+ commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
27
24
"github.com/smartcontractkit/chainlink-common/pkg/logger"
28
25
commontestutils "github.com/smartcontractkit/chainlink-common/pkg/loop/testutils"
29
26
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
@@ -207,7 +204,7 @@ func (it *SolanaChainComponentsInterfaceTester[T]) Setup(t T) {
207
204
{Static : []byte ("data" )},
208
205
{Dynamic : chainwriter.AccountLookup {
209
206
Name : "TestIDX" ,
210
- Location : "testIdx " ,
207
+ Location : "TestIdx " ,
211
208
}},
212
209
},
213
210
IsWritable : true ,
@@ -304,6 +301,7 @@ func (h *helper) Init(t *testing.T) {
304
301
solanautils .FundAccounts (t , []solana.PrivateKey {privateKey }, h .rpcClient )
305
302
306
303
cfg := config .NewDefault ()
304
+ cfg .Chain .TxRetentionTimeout = commonconfig .MustNewDuration (10 * time .Minute )
307
305
solanaClient , err := client .NewClient (h .rpcURL , cfg , 5 * time .Second , nil )
308
306
require .NoError (t , err )
309
307
@@ -313,14 +311,14 @@ func (h *helper) Init(t *testing.T) {
313
311
mkey := keyMocks .NewSimpleKeystore (t )
314
312
mkey .On ("Sign" , mock .Anything , privateKey .PublicKey ().String (), mock .Anything ).Return (func (_ context.Context , _ string , data []byte ) []byte {
315
313
sig , _ := privateKey .Sign (data )
316
- verifySignature (privateKey .PublicKey (), sig [:], data )
317
- fmt .Printf ("Signed for %s: %x\n " , privateKey .PublicKey ().String (), sig )
318
314
return sig [:]
319
315
}, nil )
320
316
lggr := logger .Test (t )
321
317
322
318
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
+
324
322
h .txm = txm
325
323
326
324
pubkey , err := solana .PublicKeyFromBase58 (programPubKey )
@@ -330,16 +328,6 @@ func (h *helper) Init(t *testing.T) {
330
328
h .programID = pubkey
331
329
}
332
330
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
-
343
331
func (h * helper ) RPCClient () * chainreader.RPCClientWrapper {
344
332
return & chainreader.RPCClientWrapper {Client : h .rpcClient }
345
333
}
@@ -398,114 +386,35 @@ func (h *helper) CreateAccount(t *testing.T, it SolanaChainComponentsInterfaceTe
398
386
pubKey , _ , err := solana .FindProgramAddress ([][]byte {[]byte ("data" ), bts }, h .programID )
399
387
require .NoError (t , err )
400
388
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 )
408
390
409
391
return pubKey
410
392
}
411
393
394
+ type InitializeArgs struct {
395
+ TestIdx uint64
396
+ Value uint64
397
+ }
398
+
412
399
func (h * helper ) runInitialize (
413
400
t * testing.T ,
414
401
it SolanaChainComponentsInterfaceTester [* testing.T ],
415
402
nonce uint64 ,
416
403
value uint64 ,
417
- data solana.PublicKey ,
418
- signerFunc func (key solana.PublicKey ) * solana.PrivateKey ,
419
- payer solana.PublicKey ,
420
404
) {
421
405
t .Helper ()
422
406
423
407
cw := it .GetContractWriter (t )
424
408
425
- args := map [ string ] interface {} {
426
- "testIdx" : nonce * value ,
427
- "value" : value ,
409
+ args := InitializeArgs {
410
+ TestIdx : nonce * value ,
411
+ Value : value ,
428
412
}
429
413
430
414
buf := make ([]byte , 8 )
431
415
binary .LittleEndian .PutUint64 (buf , nonce * value )
432
416
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
-
444
417
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
- }
509
418
}
510
419
511
420
func mustUnmarshalIDL [T TestingT [T ]](t T , rawIDL string ) codec.IDL {
0 commit comments