Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solana elections #5637

Draft
wants to merge 1 commit into
base: feat/solana-versioned-transaction
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/src/elections/voter_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ macro_rules! generate_voter_api_tuple_impls {
}
}

generate_voter_api_tuple_impls!(tuple_6_impls: ((A, A0), (B, B0), (C, C0), (D, D0), (EE, E0), (FF, F0)));
generate_voter_api_tuple_impls!(tuple_7_impls: ((A, A0), (B, B0), (C, C0), (D, D0), (EE, E0), (FF, F0), (GG, G0)));
26 changes: 22 additions & 4 deletions engine/src/witness/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use pallet_cf_elections::{
};
use state_chain_runtime::{
chainflip::solana_elections::{
SolanaBlockHeightTracking, SolanaEgressWitnessing, SolanaElectoralSystemRunner,
SolanaIngressTracking, SolanaLiveness, SolanaNonceTracking, SolanaVaultSwapTracking,
TransactionSuccessDetails,
SolanaAltWitnessing, SolanaBlockHeightTracking, SolanaEgressWitnessing,
SolanaElectoralSystemRunner, SolanaIngressTracking, SolanaLiveness, SolanaNonceTracking,
SolanaVaultSwapTracking, TransactionSuccessDetails,
},
SolanaInstance,
};
Expand Down Expand Up @@ -200,6 +200,23 @@ impl VoterApi<SolanaVaultSwapTracking> for SolanaVaultSwapsVoter {
}
}

#[allow(dead_code)]
#[derive(Clone)]
struct SolanaAltWitnessingVoter {
client: SolRetryRpcClient,
}

#[async_trait::async_trait]
impl VoterApi<SolanaAltWitnessing> for SolanaAltWitnessingVoter {
async fn vote(
&self,
_settings: <SolanaAltWitnessing as ElectoralSystemTypes>::ElectoralSettings,
_properties: <SolanaAltWitnessing as ElectoralSystemTypes>::ElectionProperties,
) -> Result<Option<VoteOf<SolanaAltWitnessing>>, anyhow::Error> {
todo!()
}
}

pub async fn start<StateChainClient>(
scope: &Scope<'_, anyhow::Error>,
client: SolRetryRpcClient,
Expand All @@ -226,7 +243,8 @@ where
SolanaNonceTrackingVoter { client: client.clone() },
SolanaEgressWitnessingVoter { client: client.clone() },
SolanaLivenessVoter { client: client.clone() },
SolanaVaultSwapsVoter { client },
SolanaVaultSwapsVoter { client: client.clone() },
SolanaAltWitnessingVoter { client },
)),
)
.continuously_vote()
Expand Down
15 changes: 14 additions & 1 deletion foreign-chains/solana/sol-prim/src/alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ pub struct MessageAddressTableLookup {
/// The definition of address lookup table accounts.
///
/// As used by the `crate::message::v0` message format.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(
Serialize,
Deserialize,
Default,
Debug,
PartialEq,
Eq,
Clone,
Encode,
Decode,
TypeInfo,
Ord,
PartialOrd,
)]
pub struct AddressLookupTableAccount {
pub key: Pubkey,
pub addresses: Vec<Pubkey>,
Expand Down
4 changes: 3 additions & 1 deletion state-chain/cf-integration-tests/src/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ impl ExtBuilder {
(),
(),
Default::default(),
(),
),
unsynchronised_settings: ((), (), (), (), (), ()),
unsynchronised_settings: ((), (), (), (), (), (), ()),
settings: (
(),
SolanaIngressSettings {
Expand All @@ -323,6 +324,7 @@ impl ExtBuilder {
sol_test_values::SWAP_ENDPOINT_DATA_ACCOUNT_ADDRESS,
usdc_token_mint_pubkey: sol_test_values::USDC_TOKEN_MINT_PUB_KEY,
},
(),
),
}),
},
Expand Down
2 changes: 1 addition & 1 deletion state-chain/cf-integration-tests/src/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::{
traits::{OnFinalize, UnfilteredDispatchable},
};
use pallet_cf_elections::{
vote_storage::{composite::tuple_6_impls::CompositeVote, AuthorityVote},
vote_storage::{composite::tuple_7_impls::CompositeVote, AuthorityVote},
AuthorityVoteOf, ElectionIdentifierOf, MAXIMUM_VOTES_PER_EXTRINSIC,
};
use pallet_cf_ingress_egress::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,4 @@ macro_rules! generate_electoral_system_tuple_impls {
};
}

generate_electoral_system_tuple_impls!(tuple_6_impls: ((A, A0), (B, B0), (C, C0), (D, D0), (EE, E0), (FF, F0)));
generate_electoral_system_tuple_impls!(tuple_7_impls: ((A, A0), (B, B0), (C, C0), (D, D0), (EE, E0), (FF, F0), (GG, G0)));
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,4 @@ macro_rules! generate_vote_storage_tuple_impls {
}
}

generate_vote_storage_tuple_impls!(tuple_6_impls: (A, B, C, D, EE, FF));
generate_vote_storage_tuple_impls!(tuple_7_impls: (A, B, C, D, EE, FF, GG));
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ macro_rules! generate_individual_vote_storage_tuple_impls {
}
#[cfg(test)]
generate_individual_vote_storage_tuple_impls!(tuple_2_impls: (A, B));
generate_individual_vote_storage_tuple_impls!(tuple_6_impls: (A, B, C, D, EE, FF));
generate_individual_vote_storage_tuple_impls!(tuple_7_impls: (A, B, C, D, EE, FF, GG));
20 changes: 18 additions & 2 deletions state-chain/pallets/cf-environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use cf_chains::{
eth::Address as EvmAddress,
sol::{
api::{DurableNonceAndAccount, SolanaApi, SolanaEnvironment, SolanaGovCall},
SolAddress, SolApiEnvironment, SolHash, Solana, NONCE_NUMBER_CRITICAL_NONCES,
SolAddress, SolAddressLookupTableAccount, SolApiEnvironment, SolHash, Solana,
NONCE_NUMBER_CRITICAL_NONCES,
},
Chain,
};
use cf_primitives::{
chains::assets::{arb::Asset as ArbAsset, eth::Asset as EthAsset},
BroadcastId, NetworkEnvironment, SemVer,
BroadcastId, NetworkEnvironment, SemVer, SwapRequestId,
};
use cf_traits::{
Broadcaster, CompatibleCfeVersions, GetBitcoinFeeInfo, KeyProvider, NetworkEnvironmentProvider,
Expand Down Expand Up @@ -241,6 +242,11 @@ pub mod pallet {
#[pallet::getter(fn solana_api_environment)]
pub type SolanaApiEnvironment<T> = StorageValue<_, SolApiEnvironment, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn solana_ccm_swap_alts)]
pub type SolanaCcmSwapAlts<T> =
StorageMap<_, Blake2_128Concat, SwapRequestId, SolAddressLookupTableAccount>;

// OTHER ENVIRONMENT ITEMS
#[pallet::storage]
#[pallet::getter(fn safe_mode)]
Expand Down Expand Up @@ -801,6 +807,16 @@ impl<T: Config> Pallet<T> {
log::error!("Nonce account {nonce_account} not found in unavailable nonce accounts");
}
}

pub fn add_sol_ccm_swap_alt(swap_request_id: SwapRequestId, alt: SolAddressLookupTableAccount) {
SolanaCcmSwapAlts::<T>::insert(swap_request_id, alt);
}

pub fn take_sol_ccm_swap_alt(
swap_request_id: SwapRequestId,
) -> Option<SolAddressLookupTableAccount> {
SolanaCcmSwapAlts::<T>::take(swap_request_id)
}
}

impl<T: Config> CompatibleCfeVersions for Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion state-chain/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub type BroadcastId = u32;

define_wrapper_type!(SwapId, u64, extra_derives: Serialize, Deserialize);

define_wrapper_type!(SwapRequestId, u64, extra_derives: Serialize, Deserialize);
define_wrapper_type!(SwapRequestId, u64, extra_derives: Serialize, Deserialize, Ord, PartialOrd);

pub type PrewitnessedDepositId = u64;

Expand Down
68 changes: 63 additions & 5 deletions state-chain/runtime/src/chainflip/solana_elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use cf_chains::{
},
compute_units_costs::MIN_COMPUTE_PRICE,
sol_tx_core::SlotNumber,
SolAddress, SolAmount, SolHash, SolSignature, SolTrackedData, SolanaCrypto,
SolAddress, SolAddressLookupTableAccount, SolAmount, SolHash, SolSignature, SolTrackedData,
SolanaCrypto,
},
CcmDepositMetadata, Chain, ChannelRefundParameters, FeeEstimationApi,
FetchAndCloseSolanaVaultSwapAccounts, ForeignChain, Solana,
};
use cf_primitives::{AffiliateShortId, Affiliates, Beneficiary, DcaParameters};
use cf_primitives::{AffiliateShortId, Affiliates, Beneficiary, DcaParameters, SwapRequestId};
use cf_runtime_utilities::log_or_panic;
use cf_traits::{
offence_reporting::OffenceReporter, AdjustedFeeEstimationApi, Broadcaster, Chainflip,
Expand All @@ -30,7 +31,7 @@ use pallet_cf_elections::{
electoral_system::{ElectoralReadAccess, ElectoralSystem, ElectoralSystemTypes},
electoral_systems::{
self,
composite::{tuple_6_impls::Hooks, CompositeRunner},
composite::{tuple_7_impls::Hooks, CompositeRunner},
egress_success::OnEgressSuccess,
liveness::OnCheckComplete,
monotonic_change::OnChangeHook,
Expand Down Expand Up @@ -60,6 +61,7 @@ pub type SolanaElectoralSystemRunner = CompositeRunner<
SolanaEgressWitnessing,
SolanaLiveness,
SolanaVaultSwapTracking,
SolanaAltWitnessing,
),
<Runtime as Chainflip>::ValidatorId,
RunnerStorageAccess<Runtime, SolanaInstance>,
Expand All @@ -84,15 +86,17 @@ pub fn initial_state(
(),
(),
0u32,
(),
),
unsynchronised_settings: ((), (), (), (), (), ()),
unsynchronised_settings: ((), (), (), (), (), (), ()),
settings: (
(),
SolanaIngressSettings { vault_program, usdc_token_mint_pubkey },
(),
(),
LIVENESS_CHECK_DURATION,
SolanaVaultSwapsSettings { swap_endpoint_data_account_address, usdc_token_mint_pubkey },
(),
),
}
}
Expand Down Expand Up @@ -154,6 +158,46 @@ pub type SolanaVaultSwapTracking =
SolanaTransactionBuildingError,
>;

#[derive(
Serialize,
Deserialize,
Default,
Debug,
PartialEq,
Eq,
Clone,
Encode,
Decode,
TypeInfo,
Ord,
PartialOrd,
)]
pub struct SolanaAltWitnessingIdentifier {
pub swap_request_id: SwapRequestId,
pub alt_address: SolAddress,
}

pub type SolanaAltWitnessing = electoral_systems::egress_success::EgressSuccess<
SolanaAltWitnessingIdentifier,
SolAddressLookupTableAccount,
(),
SolanaAltWitnessingHook,
<Runtime as Chainflip>::ValidatorId,
>;

pub struct SolanaAltWitnessingHook;

impl OnEgressSuccess<SolanaAltWitnessingIdentifier, SolAddressLookupTableAccount>
for SolanaAltWitnessingHook
{
fn on_egress_success(
alt_identifier: SolanaAltWitnessingIdentifier,
alt: SolAddressLookupTableAccount,
) {
Environment::add_sol_ccm_swap_alt(alt_identifier.swap_request_id, alt);
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, TypeInfo)]
pub struct TransactionSuccessDetails {
pub tx_fee: u64,
Expand Down Expand Up @@ -235,6 +279,7 @@ impl
SolanaEgressWitnessing,
SolanaLiveness,
SolanaVaultSwapTracking,
SolanaAltWitnessing,
> for SolanaElectionHooks
{
fn on_finalize(
Expand All @@ -245,6 +290,7 @@ impl
egress_witnessing_identifiers,
liveness_identifiers,
vault_swap_identifiers,
alt_witnessing_identifiers,
): (
Vec<
ElectionIdentifier<
Expand Down Expand Up @@ -276,6 +322,11 @@ impl
<SolanaVaultSwapTracking as ElectoralSystemTypes>::ElectionIdentifierExtra,
>,
>,
Vec<
ElectionIdentifier<
<SolanaAltWitnessing as ElectoralSystemTypes>::ElectionIdentifierExtra,
>,
>,
),
) -> Result<(), CorruptStorageError> {
let current_sc_block_number = crate::System::block_number();
Expand Down Expand Up @@ -317,6 +368,13 @@ impl
RunnerStorageAccess<Runtime, SolanaInstance>,
>,
>(vault_swap_identifiers, &current_sc_block_number)?;
SolanaAltWitnessing::on_finalize::<
DerivedElectoralAccess<
_,
SolanaAltWitnessing,
RunnerStorageAccess<Runtime, SolanaInstance>,
>,
>(alt_witnessing_identifiers, &())?;
Ok(())
}
}
Expand All @@ -337,7 +395,7 @@ impl BenchmarkValue for SolanaIngressSettings {
}
}

use pallet_cf_elections::electoral_systems::composite::tuple_6_impls::DerivedElectoralAccess;
use pallet_cf_elections::electoral_systems::composite::tuple_7_impls::DerivedElectoralAccess;

pub struct SolanaChainTrackingProvider;
impl GetBlockHeight<Solana> for SolanaChainTrackingProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::*;
use frame_support::{pallet_prelude::Weight, traits::OnRuntimeUpgrade};

use pallet_cf_elections::{
electoral_systems::composite::tuple_6_impls::CompositeElectoralUnsynchronisedStateMapKey,
electoral_systems::composite::tuple_7_impls::CompositeElectoralUnsynchronisedStateMapKey,
ElectoralUnsynchronisedStateMap,
};
use sp_core::bounded::alloc::collections::BTreeSet;
Expand Down
Loading