Skip to content

Commit 8602308

Browse files
committed
updated comments and slight tweaks
1 parent d656123 commit 8602308

File tree

8 files changed

+250
-107
lines changed

8 files changed

+250
-107
lines changed

integration-tests/relayinterface/chain_components_test.go

+123-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package relayinterface
66
import (
77
"context"
88
"encoding/binary"
9+
"fmt"
910
"io"
11+
"log"
1012
"os"
1113
"path/filepath"
1214
"sync"
@@ -18,20 +20,27 @@ import (
1820
"github.com/gagliardetto/solana-go/rpc/ws"
1921
"github.com/gagliardetto/solana-go/text"
2022
"github.com/stretchr/testify/require"
23+
"github.com/test-go/testify/mock"
2124

2225
"github.com/smartcontractkit/chainlink-common/pkg/codec"
2326
"github.com/smartcontractkit/chainlink-common/pkg/logger"
2427
commontestutils "github.com/smartcontractkit/chainlink-common/pkg/loop/testutils"
28+
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
2529
"github.com/smartcontractkit/chainlink-common/pkg/types"
2630
. "github.com/smartcontractkit/chainlink-common/pkg/types/interfacetests" //nolint common practice to import test mods with .
2731
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
32+
commonutils "github.com/smartcontractkit/chainlink-common/pkg/utils"
2833
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
2934

3035
contract "github.com/smartcontractkit/chainlink-solana/contracts/generated/contract_reader_interface"
3136
"github.com/smartcontractkit/chainlink-solana/integration-tests/solclient"
3237
"github.com/smartcontractkit/chainlink-solana/integration-tests/utils"
3338
"github.com/smartcontractkit/chainlink-solana/pkg/solana/chainreader"
39+
"github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter"
40+
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
3441
"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
42+
"github.com/smartcontractkit/chainlink-solana/pkg/solana/txm"
43+
keyMocks "github.com/smartcontractkit/chainlink-solana/pkg/solana/txm/mocks"
3544
solanautils "github.com/smartcontractkit/chainlink-solana/pkg/solana/utils"
3645
)
3746

@@ -124,14 +133,17 @@ type SolanaChainComponentsInterfaceTesterHelper[T TestingT[T]] interface {
124133
Context(t T) context.Context
125134
Logger(t T) logger.Logger
126135
GetJSONEncodedIDL(t T) []byte
127-
CreateAccount(t T, value uint64) solana.PublicKey
136+
CreateAccount(t T, it SolanaChainComponentsInterfaceTester[T], value uint64) solana.PublicKey
137+
TXM() *txm.TxManager
138+
SolanaClient() *client.Client
128139
}
129140

130141
type SolanaChainComponentsInterfaceTester[T TestingT[T]] struct {
131142
TestSelectionSupport
132143
Helper SolanaChainComponentsInterfaceTesterHelper[T]
133144
cr *chainreader.SolanaChainReaderService
134145
chainReaderConfig config.ChainReader
146+
chainWriterConfig chainwriter.ChainWriterConfig
135147
}
136148

137149
func (it *SolanaChainComponentsInterfaceTester[T]) Setup(t T) {
@@ -179,6 +191,41 @@ func (it *SolanaChainComponentsInterfaceTester[T]) Setup(t T) {
179191
},
180192
},
181193
}
194+
195+
it.chainWriterConfig = chainwriter.ChainWriterConfig{
196+
Programs: map[string]chainwriter.ProgramConfig{
197+
AnyContractName: {
198+
IDL: string(it.Helper.GetJSONEncodedIDL(t)),
199+
Methods: map[string]chainwriter.MethodConfig{
200+
"initialize": {
201+
FromAddress: solana.MustPrivateKeyFromBase58(solclient.DefaultPrivateKeysSolValidator[1]).PublicKey().String(),
202+
InputModifications: nil,
203+
ChainSpecificName: "initialize",
204+
LookupTables: chainwriter.LookupTables{},
205+
Accounts: []chainwriter.Lookup{
206+
chainwriter.PDALookups{
207+
Name: "Account",
208+
PublicKey: chainwriter.AccountConstant{
209+
Name: "ProgramID",
210+
Address: programPubKey,
211+
},
212+
Seeds: []chainwriter.Seed{
213+
{Static: []byte("data")},
214+
{Dynamic: chainwriter.AccountLookup{
215+
Name: "TestIDX",
216+
Location: "testIdx",
217+
}},
218+
},
219+
IsWritable: true,
220+
IsSigner: false,
221+
},
222+
},
223+
DebugIDLocation: "",
224+
},
225+
},
226+
},
227+
},
228+
}
182229
}
183230

184231
func (it *SolanaChainComponentsInterfaceTester[T]) Name() string {
@@ -210,14 +257,18 @@ func (it *SolanaChainComponentsInterfaceTester[T]) GetContractReader(t T) types.
210257
}
211258

212259
func (it *SolanaChainComponentsInterfaceTester[T]) GetContractWriter(t T) types.ContractWriter {
213-
return nil
260+
cw, err := chainwriter.NewSolanaChainWriterService(it.Helper.Logger(t), it.Helper.SolanaClient(), *it.Helper.TXM(), nil, it.chainWriterConfig)
261+
require.NoError(t, err)
262+
263+
servicetest.Run(t, cw)
264+
return cw
214265
}
215266

216267
func (it *SolanaChainComponentsInterfaceTester[T]) GetBindings(t T) []types.BoundContract {
217268
// Create a new account with fresh state for each test
218269
return []types.BoundContract{
219-
{Name: AnyContractName, Address: it.Helper.CreateAccount(t, AnyValueToReadWithoutAnArgument).String()},
220-
{Name: AnySecondContractName, Address: it.Helper.CreateAccount(t, AnyDifferentValueToReadWithoutAnArgument).String()},
270+
{Name: AnyContractName, Address: it.Helper.CreateAccount(t, *it, AnyValueToReadWithoutAnArgument).String()},
271+
{Name: AnySecondContractName, Address: it.Helper.CreateAccount(t, *it, AnyDifferentValueToReadWithoutAnArgument).String()},
221272
}
222273
}
223274

@@ -240,6 +291,8 @@ type helper struct {
240291
idlBts []byte
241292
nonce uint64
242293
nonceMu sync.Mutex
294+
txm txm.TxManager
295+
sc *client.Client
243296
}
244297

245298
func (h *helper) Init(t *testing.T) {
@@ -256,17 +309,55 @@ func (h *helper) Init(t *testing.T) {
256309

257310
solanautils.FundAccounts(t, []solana.PrivateKey{privateKey}, h.rpcClient)
258311

312+
cfg := config.NewDefault()
313+
solanaClient, err := client.NewClient(h.rpcURL, cfg, 5*time.Second, nil)
314+
require.NoError(t, err)
315+
316+
h.sc = solanaClient
317+
318+
loader := commonutils.NewLazyLoad(func() (client.ReaderWriter, error) { return solanaClient, nil })
319+
mkey := keyMocks.NewSimpleKeystore(t)
320+
mkey.On("Sign", mock.Anything, privateKey.PublicKey().String(), mock.Anything).Return(func(_ context.Context, _ string, data []byte) []byte {
321+
sig, _ := privateKey.Sign(data)
322+
verifySignature(privateKey.PublicKey(), sig[:], data)
323+
fmt.Printf("Signed for %s: %x\n", privateKey.PublicKey().String(), sig)
324+
return sig[:]
325+
}, nil)
326+
lggr := logger.Test(t)
327+
328+
txm := txm.NewTxm("localnet", loader, nil, cfg, mkey, lggr)
329+
txm.Start(tests.Context(t))
330+
h.txm = txm
331+
259332
pubkey, err := solana.PublicKeyFromBase58(programPubKey)
260333
require.NoError(t, err)
261334

262335
contract.SetProgramID(pubkey)
263336
h.programID = pubkey
264337
}
265338

339+
func verifySignature(publicKey solana.PublicKey, signature []byte, message []byte) bool {
340+
valid := publicKey.Verify(message, solana.SignatureFromBytes(signature))
341+
if valid {
342+
log.Printf("Signature is valid for public key: %s\n", publicKey.String())
343+
} else {
344+
log.Printf("Signature is invalid for public key: %s\n", publicKey.String())
345+
}
346+
return valid
347+
}
348+
266349
func (h *helper) RPCClient() *chainreader.RPCClientWrapper {
267350
return &chainreader.RPCClientWrapper{Client: h.rpcClient}
268351
}
269352

353+
func (h *helper) TXM() *txm.TxManager {
354+
return &h.txm
355+
}
356+
357+
func (h *helper) SolanaClient() *client.Client {
358+
return h.sc
359+
}
360+
270361
func (h *helper) Context(t *testing.T) context.Context {
271362
return tests.Context(t)
272363
}
@@ -298,7 +389,7 @@ func (h *helper) GetJSONEncodedIDL(t *testing.T) []byte {
298389
return h.idlBts
299390
}
300391

301-
func (h *helper) CreateAccount(t *testing.T, value uint64) solana.PublicKey {
392+
func (h *helper) CreateAccount(t *testing.T, it SolanaChainComponentsInterfaceTester[*testing.T], value uint64) solana.PublicKey {
302393
t.Helper()
303394

304395
// avoid collisions in parallel tests
@@ -317,7 +408,7 @@ func (h *helper) CreateAccount(t *testing.T, value uint64) solana.PublicKey {
317408
privateKey, err := solana.PrivateKeyFromBase58(solclient.DefaultPrivateKeysSolValidator[1])
318409
require.NoError(t, err)
319410

320-
h.runInitialize(t, nonce, value, pubKey, func(key solana.PublicKey) *solana.PrivateKey {
411+
h.runInitialize(t, it, nonce, value, pubKey, func(key solana.PublicKey) *solana.PrivateKey {
321412
return &privateKey
322413
}, privateKey.PublicKey())
323414

@@ -326,6 +417,7 @@ func (h *helper) CreateAccount(t *testing.T, value uint64) solana.PublicKey {
326417

327418
func (h *helper) runInitialize(
328419
t *testing.T,
420+
it SolanaChainComponentsInterfaceTester[*testing.T],
329421
nonce uint64,
330422
value uint64,
331423
data solana.PublicKey,
@@ -334,10 +426,33 @@ func (h *helper) runInitialize(
334426
) {
335427
t.Helper()
336428

337-
inst, err := contract.NewInitializeInstruction(nonce*value, value, data, payer, solana.SystemProgramID).ValidateAndBuild()
429+
cw := it.GetContractWriter(t)
430+
431+
args := map[string]interface{}{
432+
"testIdx": nonce * value,
433+
"value": value,
434+
}
435+
436+
buf := make([]byte, 8)
437+
binary.LittleEndian.PutUint64(buf, nonce*value)
438+
439+
data, _, err := solana.FindProgramAddress(
440+
[][]byte{
441+
[]byte("data"), // Seed 1
442+
buf, // Seed 2 (test_idx)
443+
},
444+
solana.MustPublicKeyFromBase58(programPubKey), // The program ID
445+
)
338446
require.NoError(t, err)
339447

340-
h.sendInstruction(t, inst, signerFunc, payer)
448+
fmt.Printf("Derived PDA in test: %s\n", data.String())
449+
450+
SubmitTransactionToCW(t, &it, cw, "initialize", args, types.BoundContract{Name: AnyContractName, Address: h.programID.String()}, types.Finalized)
451+
452+
// inst, err := contract.NewInitializeInstruction(nonce*value, value, data, payer, solana.SystemProgramID).ValidateAndBuild()
453+
// require.NoError(t, err)
454+
455+
// h.sendInstruction(t, inst, signerFunc, payer)
341456
}
342457

343458
func (h *helper) sendInstruction(

integration-tests/relayinterface/lookups_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestAccountLookups(t *testing.T) {
126126
}
127127

128128
func TestPDALookups(t *testing.T) {
129-
programID := solana.SystemProgramID
129+
programID := chainwriter.GetRandomPubKey(t)
130130

131131
t.Run("PDALookup resolves valid PDA with constant address seeds", func(t *testing.T) {
132132
seed := chainwriter.GetRandomPubKey(t)
@@ -145,8 +145,8 @@ func TestPDALookups(t *testing.T) {
145145
pdaLookup := chainwriter.PDALookups{
146146
Name: "TestPDA",
147147
PublicKey: chainwriter.AccountConstant{Name: "ProgramID", Address: programID.String()},
148-
Seeds: []chainwriter.Lookup{
149-
chainwriter.AccountConstant{Name: "seed", Address: seed.String()},
148+
Seeds: []chainwriter.Seed{
149+
{Dynamic: chainwriter.AccountConstant{Name: "seed", Address: seed.String()}},
150150
},
151151
IsSigner: false,
152152
IsWritable: true,
@@ -175,9 +175,9 @@ func TestPDALookups(t *testing.T) {
175175
pdaLookup := chainwriter.PDALookups{
176176
Name: "TestPDA",
177177
PublicKey: chainwriter.AccountConstant{Name: "ProgramID", Address: programID.String()},
178-
Seeds: []chainwriter.Lookup{
179-
chainwriter.AccountLookup{Name: "seed1", Location: "test_seed"},
180-
chainwriter.AccountLookup{Name: "seed2", Location: "another_seed"},
178+
Seeds: []chainwriter.Seed{
179+
{Dynamic: chainwriter.AccountLookup{Name: "seed1", Location: "test_seed"}},
180+
{Dynamic: chainwriter.AccountLookup{Name: "seed2", Location: "another_seed"}},
181181
},
182182
IsSigner: false,
183183
IsWritable: true,
@@ -198,8 +198,8 @@ func TestPDALookups(t *testing.T) {
198198
pdaLookup := chainwriter.PDALookups{
199199
Name: "TestPDA",
200200
PublicKey: chainwriter.AccountConstant{Name: "ProgramID", Address: programID.String()},
201-
Seeds: []chainwriter.Lookup{
202-
chainwriter.AccountLookup{Name: "seed1", Location: "MissingSeed"},
201+
Seeds: []chainwriter.Seed{
202+
{Dynamic: chainwriter.AccountLookup{Name: "seed1", Location: "MissingSeed"}},
203203
},
204204
IsSigner: false,
205205
IsWritable: true,
@@ -233,9 +233,9 @@ func TestPDALookups(t *testing.T) {
233233
pdaLookup := chainwriter.PDALookups{
234234
Name: "TestPDA",
235235
PublicKey: chainwriter.AccountConstant{Name: "ProgramID", Address: programID.String()},
236-
Seeds: []chainwriter.Lookup{
237-
chainwriter.AccountLookup{Name: "seed1", Location: "test_seed"},
238-
chainwriter.AccountLookup{Name: "seed2", Location: "another_seed"},
236+
Seeds: []chainwriter.Seed{
237+
{Dynamic: chainwriter.AccountLookup{Name: "seed1", Location: "test_seed"}},
238+
{Dynamic: chainwriter.AccountLookup{Name: "seed2", Location: "another_seed"}},
239239
},
240240
IsSigner: false,
241241
IsWritable: true,
@@ -281,7 +281,7 @@ func TestLookupTables(t *testing.T) {
281281
table := chainwriter.CreateTestLookupTable(ctx, t, rpcClient, sender, pubKeys)
282282
lookupConfig := chainwriter.LookupTables{
283283
DerivedLookupTables: nil,
284-
StaticLookupTables: []string{table.String()},
284+
StaticLookupTables: []solana.PublicKey{table},
285285
}
286286
_, staticTableMap, resolveErr := cw.ResolveLookupTables(ctx, nil, lookupConfig)
287287
require.NoError(t, resolveErr)
@@ -342,7 +342,7 @@ func TestLookupTables(t *testing.T) {
342342

343343
lookupConfig := chainwriter.LookupTables{
344344
DerivedLookupTables: nil,
345-
StaticLookupTables: []string{invalidTable.String()},
345+
StaticLookupTables: []solana.PublicKey{invalidTable},
346346
}
347347

348348
_, _, err = cw.ResolveLookupTables(ctx, nil, lookupConfig)
@@ -403,8 +403,8 @@ func TestLookupTables(t *testing.T) {
403403
Accounts: chainwriter.PDALookups{
404404
Name: "DataAccountPDA",
405405
PublicKey: chainwriter.AccountConstant{Name: "WriteTest", Address: programID.String()},
406-
Seeds: []chainwriter.Lookup{
407-
chainwriter.AccountLookup{Name: "seed1", Location: "seed1"},
406+
Seeds: []chainwriter.Seed{
407+
{Dynamic: chainwriter.AccountLookup{Name: "seed1", Location: "seed1"}},
408408
},
409409
IsSigner: false,
410410
IsWritable: false,

0 commit comments

Comments
 (0)