Skip to content
Open
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
42 changes: 42 additions & 0 deletions interface/src/args/commit_finalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use wheels::variable_offset_layout;

use crate::compat::Pubkey;

#[derive(Clone, Debug, PartialEq, Eq)]
#[variable_offset_layout(buffer_offset = 1)]
pub struct CommitFinalizeArgs {
/// Next commit id for this delegated account.
pub commit_id: u64,

/// Account lamports on the ER after this commit.
pub lamports: u64,

/// Account owner committed by the ER.
pub owner: Pubkey,

/// Whether the validator requests undelegation after this commit.
pub allow_undelegation: bool,

/// Stored as `CommitPayloadKind::value()`.
pub payload_kind: u8,

/// Keeps the inline payload four-byte aligned for zero-copy diff decoding.
pub payload_alignment_padding: u8,

/// Complete account data or an encoded diff, according to `payload_kind`.
#[flexible = 4]
pub payload: Vec<u8>,
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CommitPayloadKind {
FullData = 0,
Diff = 1,
}

impl CommitPayloadKind {
pub const fn value(self) -> u8 {
self as u8
}
}
2 changes: 2 additions & 0 deletions interface/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// instruction tag, so v2 instruction args use `buffer_offset = 1`.

mod challenger_reveal;
mod commit_finalize;
mod delegate;
mod init_delegation_cluster;
mod propose_commitment;
Expand All @@ -14,6 +15,7 @@ mod update_verifier_registry;
mod write_state_buffer;

pub use challenger_reveal::*;
pub use commit_finalize::*;
pub use delegate::*;
pub use init_delegation_cluster::*;
pub use propose_commitment::*;
Expand Down
2 changes: 2 additions & 0 deletions interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum DlpV2Instruction {
ResolveDispute = 111,
/// Delegates an account to one delegation cluster.
Delegate = 112,
/// Commits and immediately applies inline state for a Direct cluster.
CommitFinalize = 113,
}

impl DlpV2Instruction {
Expand Down
37 changes: 37 additions & 0 deletions interface/src/instruction_builder/commit_finalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};
use solana_sdk_ids::system_program;
use wheels::{layout::Encodable, DataLayoutError};

use crate::{
compat::{Compatize, Modernize},
pda::delegation_state_pda,
CommitFinalizeArgs, DlpV2Instruction,
};

/// Builds a direct-cluster commit that carries its payload inline.
pub fn commit_finalize(
current_validator: Pubkey,
delegation_cluster: Pubkey,
delegated_account: Pubkey,
args: CommitFinalizeArgs,
) -> Result<Instruction, DataLayoutError> {
Ok(Instruction {
program_id: crate::id().modernize(),
accounts: vec![
AccountMeta::new(current_validator, true),
AccountMeta::new(delegated_account, false),
AccountMeta::new(
delegation_state_pda(&delegated_account.compatize())
.modernize(),
false,
),
AccountMeta::new_readonly(delegation_cluster, false),
AccountMeta::new_readonly(system_program::id(), false),
],
data: [DlpV2Instruction::CommitFinalize.to_vec(), args.encode()?]
.concat(),
})
}
2 changes: 2 additions & 0 deletions interface/src/instruction_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod approve_commitment;
mod challenger_reveal;
mod commit_finalize;
mod delegate;
mod finalize_commitment;
mod init_delegation_cluster;
Expand All @@ -14,6 +15,7 @@ mod write_state_buffer;

pub use approve_commitment::*;
pub use challenger_reveal::*;
pub use commit_finalize::*;
pub use delegate::*;
pub use finalize_commitment::*;
pub use init_delegation_cluster::*;
Expand Down
62 changes: 62 additions & 0 deletions program/src/processor/direct/commit_finalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use delegation_program_interface as dpi;
use dpi::{CommitFinalizeArgs, CommitPayloadKind};
use pinocchio::{error::ProgramError, AccountView, ProgramResult};
use wheels::{layout::Decodable, require_eq, require_n_accounts};

use crate::processor::internal::{
apply_commit_finalize, CommitFinalizeInput, CommitStateUpdate,
};

/// Commit and immediately apply one state update for a Direct cluster.
///
/// Accounts:
/// 0: `[signer, writable]` current validator and lamport settlement account
/// 1: `[writable]` delegated account
/// 2: `[writable]` DelegationState PDA
/// 3: `[]` DelegationCluster PDA
/// 4: `[]` system program, required by system CPI
#[inline(never)]
pub fn process_commit_finalize(
accounts: &[AccountView],
data: &[u8],
) -> ProgramResult {
let [
current_validator, // force multi-line
delegated_account,
delegation_state,
delegation_cluster,
_system_program,
] = require_n_accounts!(accounts, 5);

let args = CommitFinalizeArgs::decode(data)?;
require_eq!(
args.payload_alignment_padding(),
0,
ProgramError::InvalidInstructionData
);
let state_update = match args.payload_kind() {
value if value == CommitPayloadKind::FullData.value() => {
CommitStateUpdate::FullData(args.payload())
}
value if value == CommitPayloadKind::Diff.value() => {
CommitStateUpdate::Diff(dpi::diff::DiffSet::try_new(
args.payload(),
)?)
}
_ => return Err(ProgramError::InvalidInstructionData),
};

apply_commit_finalize(
current_validator,
delegated_account,
delegation_state,
delegation_cluster,
CommitFinalizeInput {
commit_id: args.commit_id(),
lamports: args.lamports(),
owner: args.owner(),
allow_undelegation: args.allow_undelegation(),
state_update,
},
)
}
5 changes: 5 additions & 0 deletions program/src/processor/direct/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Processors for clusters that finalize without fraud proofs.

mod commit_finalize;

pub use commit_finalize::*;
Loading
Loading