Skip to content

Commit

Permalink
Merge branch 'develop' into array-vec-v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
akuzni2 authored Dec 19, 2024
2 parents cd02b61 + 150f744 commit 9dcd519
Show file tree
Hide file tree
Showing 25 changed files with 1,644 additions and 479 deletions.
9 changes: 9 additions & 0 deletions contracts/Cargo.lock

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

25 changes: 25 additions & 0 deletions contracts/crates/chainlink-solana-data-streams/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "chainlink_solana_data_streams"
description = "Chainlink Data Streams Uility for Solana. Can be used on-chain/off-chain to get `verify` transaction instructions."
version = "1.0.0"
edition = "2018"
license = "MIT"

[lib]
crate-type = ["cdylib", "lib"]
name = "chainlink_solana_data_streams"

[features]
default = []

[dependencies]
borsh = "0.10.3"

[target.'cfg(target_os = "solana")'.dependencies]
solana-program = "1.17"

[target.'cfg(not(target_os = "solana"))'.dependencies]
solana-sdk = "1.17"

[dev-dependencies]
solana-sdk = "1.17"
21 changes: 21 additions & 0 deletions contracts/crates/chainlink-solana-data-streams/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 SmartContract ChainLink, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions contracts/crates/chainlink-solana-data-streams/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# chainlink-solana-data-streams

This tool is provided under an MIT license and is for convenience and illustration purposes only.
121 changes: 121 additions & 0 deletions contracts/crates/chainlink-solana-data-streams/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//! Chainlink Data Streams Client for Solana
mod solana {
#[cfg(not(target_os = "solana"))]
pub use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};

#[cfg(target_os = "solana")]
pub use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};
}

use crate::solana::{AccountMeta, Instruction, Pubkey};
use borsh::{BorshDeserialize, BorshSerialize};

/// Program function name discriminators
pub mod discriminator {
pub const VERIFY: [u8; 8] = [133, 161, 141, 48, 120, 198, 88, 150];
}

#[derive(BorshSerialize, BorshDeserialize)]
struct VerifyParams {
signed_report: Vec<u8>,
}

/// A helper struct for creating Verifier program instructions
pub struct VerifierInstructions;

impl VerifierInstructions {
/// Creates a verify instruction.
///
/// # Parameters:
///
/// * `program_id` - The public key of the verifier program.
/// * `verifier_account` - The public key of the verifier account. The function [`Self::get_verifier_config_pda`] can be used to calculate this.
/// * `access_controller_account` - The public key of the access controller account.
/// * `user` - The public key of the user - this account must be a signer
/// * `report_config_account` - The public key of the report configuration account. The function [`Self::get_config_pda`] can be used to calculate this.
/// * `signed_report` - The signed report data as a vector of bytes. Returned from data streams API/WS
///
/// # Returns
///
/// Returns an `Instruction` object that can be sent to the Solana runtime.
pub fn verify(
program_id: &Pubkey,
verifier_account: &Pubkey,
access_controller_account: &Pubkey,
user: &Pubkey,
report_config_account: &Pubkey,
signed_report: Vec<u8>,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*verifier_account, false),
AccountMeta::new_readonly(*access_controller_account, false),
AccountMeta::new_readonly(*user, true),
AccountMeta::new_readonly(*report_config_account, false),
];

// 8 bytes for discriminator
// 4 bytes size of the length prefix for the signed_report vector
let mut instruction_data = Vec::with_capacity(8 + 4 + signed_report.len());
instruction_data.extend_from_slice(&discriminator::VERIFY);

let params = VerifyParams { signed_report };
let param_data = params.try_to_vec().unwrap();
instruction_data.extend_from_slice(&param_data);

Instruction {
program_id: *program_id,
accounts,
data: instruction_data,
}
}

/// Helper to compute the verifier config PDA account.
pub fn get_verifier_config_pda(program_id: &Pubkey) -> Pubkey {
Pubkey::find_program_address(&[b"verifier"], program_id).0
}

/// Helper to compute the report config PDA account. This uses the first 32 bytes of the
/// uncompressed report as the seed. This is validated within the verifier program
pub fn get_config_pda(report: &[u8], program_id: &Pubkey) -> Pubkey {
Pubkey::find_program_address(&[&report[..32]], program_id).0
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_create_verify_instruction() {
let program_id = Pubkey::new_unique();
let verifier = Pubkey::new_unique();
let controller = Pubkey::new_unique();
let user = Pubkey::new_unique();
let report = vec![1u8; 64];

// Calculate expected PDA before moving report
let expected_config = VerifierInstructions::get_config_pda(&report, &program_id);

let ix = VerifierInstructions::verify(
&program_id,
&verifier,
&controller,
&user,
&expected_config,
report,
);

assert!(ix.data.starts_with(&discriminator::VERIFY));
assert_eq!(ix.program_id, program_id);
assert_eq!(ix.accounts.len(), 4);
assert!(ix.accounts[2].is_signer);
assert_eq!(ix.accounts[3].pubkey, expected_config);
}
}
5 changes: 3 additions & 2 deletions docs/relay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ chainlink nodes solana create --name=<node-name> --chain-id=<chain-id> --url=<ur
| `OCR2CacheTTL` | stale OCR2 cache deadline | 1m | |
| `TxTimeout` | timeout to send tx to rpc endpoint | 1m | |
| `TxRetryTimeout` | duration for tx to be rebroadcast to rpc, txm stops rebroadcast after timeout | 10s | |
| `TxConfirmTimeout` | duration when confirming a tx signature before signature is discarded as unconfirmed | 30s | |
| `SkipPreflight` | enable or disable preflight checks when sending tx | `true` | `true`, `false` |
| `TxConfirmTimeout` | duration when confirming a tx signature before signature is discarded as unconfirmed | 30s |
| `TxExpirationRebroadcast` | enables or disables transaction rebroadcast if expired. Expiration check is performed every `ConfirmPollPeriod`. A transaction is considered expired if the blockhash it was sent with is 150 blocks older than the latest blockhash. | `false` | `true`, `false` |
| `SkipPreflight` | enables or disables preflight checks when sending tx | `true` | `true`, `false` |
| `Commitment` | Confirmation level for solana state and transactions. ([documentation](https://docs.solana.com/developing/clients/jsonrpc-api#configuring-state-commitment)) | `confirmed` | `processed`, `confirmed`, `finalized` |
| `MaxRetries` | Parameter when sending transactions, how many times the RPC node will automatically rebroadcast a tx, default = `0` for custom txm rebroadcasting method, set to `-1` to use the RPC node's default retry strategy | `0` | |
2 changes: 1 addition & 1 deletion integration-tests/testconfig/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ inside_k8 = false
network = "localnet"
user = "default"
stateful_db = false
devnet_image = "anzaxyz/agave:v2.0.18"
devnet_image = "anzaxyz/agave:v2.0.20"

[OCR2]
node_count = 6
Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func (c *chain) sendTx(ctx context.Context, from, to string, amount *big.Int, ba
}

chainTxm := c.TxManager()
err = chainTxm.Enqueue(ctx, "", tx, nil,
err = chainTxm.Enqueue(ctx, "", tx, nil, blockhash.Value.LastValidBlockHeight,
txm.SetComputeUnitLimit(500), // reduce from default 200K limit - should only take 450 compute units
// no fee bumping and no additional fee - makes validating balance accurate
txm.SetComputeUnitPriceMax(0),
Expand Down
13 changes: 8 additions & 5 deletions pkg/solana/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func TestSolanaChain_MultiNode_Txm(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(0), receiverBal)

createTx := func(signer solana.PublicKey, sender solana.PublicKey, receiver solana.PublicKey, amt uint64) *solana.Transaction {
createTx := func(signer solana.PublicKey, sender solana.PublicKey, receiver solana.PublicKey, amt uint64) (*solana.Transaction, uint64) {
selectedClient, err = testChain.getClient()
assert.NoError(t, err)
hash, hashErr := selectedClient.LatestBlockhash(tests.Context(t))
Expand All @@ -553,11 +553,12 @@ func TestSolanaChain_MultiNode_Txm(t *testing.T) {
solana.TransactionPayer(signer),
)
require.NoError(t, txErr)
return tx
return tx, hash.Value.LastValidBlockHeight
}

// Send funds twice, along with an invalid transaction
require.NoError(t, testChain.txm.Enqueue(tests.Context(t), "test_success", createTx(pubKey, pubKey, pubKeyReceiver, solana.LAMPORTS_PER_SOL), nil))
tx, lastValidBlockHeight := createTx(pubKey, pubKey, pubKeyReceiver, solana.LAMPORTS_PER_SOL)
require.NoError(t, testChain.txm.Enqueue(tests.Context(t), "test_success", tx, nil, lastValidBlockHeight))

// Wait for new block hash
currentBh, err := selectedClient.LatestBlockhash(tests.Context(t))
Expand All @@ -578,8 +579,10 @@ NewBlockHash:
}
}

require.NoError(t, testChain.txm.Enqueue(tests.Context(t), "test_success_2", createTx(pubKey, pubKey, pubKeyReceiver, solana.LAMPORTS_PER_SOL), nil))
require.Error(t, testChain.txm.Enqueue(tests.Context(t), "test_invalidSigner", createTx(pubKeyReceiver, pubKey, pubKeyReceiver, solana.LAMPORTS_PER_SOL), nil)) // cannot sign tx before enqueuing
tx2, lastValidBlockHeight2 := createTx(pubKey, pubKey, pubKeyReceiver, solana.LAMPORTS_PER_SOL)
require.NoError(t, testChain.txm.Enqueue(tests.Context(t), "test_success_2", tx2, nil, lastValidBlockHeight2))
tx3, lastValidBlockHeight3 := createTx(pubKeyReceiver, pubKey, pubKeyReceiver, solana.LAMPORTS_PER_SOL)
require.Error(t, testChain.txm.Enqueue(tests.Context(t), "test_invalidSigner", tx3, nil, lastValidBlockHeight3)) // cannot sign tx before enqueuing

// wait for all txes to finish
ctx, cancel := context.WithCancel(tests.Context(t))
Expand Down
28 changes: 17 additions & 11 deletions pkg/solana/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (

// Global solana defaults.
var defaultConfigSet = Chain{
BalancePollPeriod: config.MustNewDuration(5 * time.Second), // poll period for balance monitoring
ConfirmPollPeriod: config.MustNewDuration(500 * time.Millisecond), // polling for tx confirmation
OCR2CachePollPeriod: config.MustNewDuration(time.Second), // cache polling rate
OCR2CacheTTL: config.MustNewDuration(time.Minute), // stale cache deadline
TxTimeout: config.MustNewDuration(time.Minute), // timeout for send tx method in client
TxRetryTimeout: config.MustNewDuration(10 * time.Second), // duration for tx rebroadcasting to RPC node
TxConfirmTimeout: config.MustNewDuration(30 * time.Second), // duration before discarding tx as unconfirmed. Set to 0 to disable discarding tx.
TxRetentionTimeout: config.MustNewDuration(0 * time.Second), // duration to retain transactions after being marked as finalized or errored. Set to 0 to immediately drop transactions.
SkipPreflight: ptr(true), // to enable or disable preflight checks
Commitment: ptr(string(rpc.CommitmentConfirmed)),
MaxRetries: ptr(int64(0)), // max number of retries (default = 0). when config.MaxRetries < 0), interpreted as MaxRetries = nil and rpc node will do a reasonable number of retries
BalancePollPeriod: config.MustNewDuration(5 * time.Second), // poll period for balance monitoring
ConfirmPollPeriod: config.MustNewDuration(500 * time.Millisecond), // polling for tx confirmation
OCR2CachePollPeriod: config.MustNewDuration(time.Second), // cache polling rate
OCR2CacheTTL: config.MustNewDuration(time.Minute), // stale cache deadline
TxTimeout: config.MustNewDuration(time.Minute), // timeout for send tx method in client
TxRetryTimeout: config.MustNewDuration(10 * time.Second), // duration for tx rebroadcasting to RPC node
TxConfirmTimeout: config.MustNewDuration(30 * time.Second), // duration before discarding tx as unconfirmed. Set to 0 to disable discarding tx.
TxExpirationRebroadcast: ptr(false), // to enable rebroadcasting of expired transactions
TxRetentionTimeout: config.MustNewDuration(0 * time.Second), // duration to retain transactions after being marked as finalized or errored. Set to 0 to immediately drop transactions.
SkipPreflight: ptr(true), // to enable or disable preflight checks
Commitment: ptr(string(rpc.CommitmentConfirmed)),
MaxRetries: ptr(int64(0)), // max number of retries (default = 0). when config.MaxRetries < 0), interpreted as MaxRetries = nil and rpc node will do a reasonable number of retries

// fee estimator
FeeEstimatorMode: ptr("fixed"),
Expand All @@ -43,6 +44,7 @@ type Config interface {
TxTimeout() time.Duration
TxRetryTimeout() time.Duration
TxConfirmTimeout() time.Duration
TxExpirationRebroadcast() bool
TxRetentionTimeout() time.Duration
SkipPreflight() bool
Commitment() rpc.CommitmentType
Expand All @@ -68,6 +70,7 @@ type Chain struct {
TxTimeout *config.Duration
TxRetryTimeout *config.Duration
TxConfirmTimeout *config.Duration
TxExpirationRebroadcast *bool
TxRetentionTimeout *config.Duration
SkipPreflight *bool
Commitment *string
Expand Down Expand Up @@ -105,6 +108,9 @@ func (c *Chain) SetDefaults() {
if c.TxConfirmTimeout == nil {
c.TxConfirmTimeout = defaultConfigSet.TxConfirmTimeout
}
if c.TxExpirationRebroadcast == nil {
c.TxExpirationRebroadcast = defaultConfigSet.TxExpirationRebroadcast
}
if c.TxRetentionTimeout == nil {
c.TxRetentionTimeout = defaultConfigSet.TxRetentionTimeout
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/solana/config/mocks/config.go

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

7 changes: 7 additions & 0 deletions pkg/solana/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ func setFromChain(c, f *Chain) {
if f.TxConfirmTimeout != nil {
c.TxConfirmTimeout = f.TxConfirmTimeout
}
if f.TxExpirationRebroadcast != nil {
c.TxExpirationRebroadcast = f.TxExpirationRebroadcast
}
if f.TxRetentionTimeout != nil {
c.TxRetentionTimeout = f.TxRetentionTimeout
}
Expand Down Expand Up @@ -241,6 +244,10 @@ func (c *TOMLConfig) TxConfirmTimeout() time.Duration {
return c.Chain.TxConfirmTimeout.Duration()
}

func (c *TOMLConfig) TxExpirationRebroadcast() bool {
return *c.Chain.TxExpirationRebroadcast
}

func (c *TOMLConfig) TxRetentionTimeout() time.Duration {
return c.Chain.TxRetentionTimeout.Duration()
}
Expand Down
Loading

0 comments on commit 9dcd519

Please sign in to comment.