Skip to content

Commit 1291130

Browse files
authored
revenue-distribution: track debt write-offs in state (malbeclabs/doublezero-solana#93)
This change implements [RFC-0002], which outlines adding state variables to track debt write-offs in the distribution and Solana validator deposit accounts. NOTE: This change replaces the forgive-solana-validator-debt instruction with write-off-solana-validator-debt instruction. This interface change only affects the debt accountant authority. Closes #2128. [RFC-0002]: https://github.com/doublezerofoundation/doublezero-solana/blob/fc3856f334f24034b34a0df3087e75bf38d240b3/docs/rfc/0002_IMPROVED_DEBT_WRITE_OFF_TRACKING.md
1 parent fc3856f commit 1291130

14 files changed

Lines changed: 761 additions & 155 deletions

solana/programs/revenue-distribution/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- add null rewards root protection ([#86])
66
- fix swap balance in journal ([#87])
77
- allow same-distribution debt write-offs ([#91])
8+
- track debt write-offs in state ([#93])
89

910
## [v0.1.0]
1011

@@ -13,4 +14,5 @@
1314
[#86]: https://github.com/doublezerofoundation/doublezero-solana/pull/86
1415
[#87]: https://github.com/doublezerofoundation/doublezero-solana/pull/87
1516
[#91]: https://github.com/doublezerofoundation/doublezero-solana/pull/91
17+
[#93]: https://github.com/doublezerofoundation/doublezero-solana/pull/93
1618
[v0.1.0]: https://github.com/doublezerofoundation/doublezero-solana/tree/revenue-distribution/v0.1.0

solana/programs/revenue-distribution/src/instruction/account.rs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -649,42 +649,81 @@ impl From<PaySolanaValidatorDebtAccounts> for Vec<AccountMeta> {
649649
}
650650

651651
#[derive(Debug, Clone, PartialEq, Eq)]
652-
pub struct ForgiveSolanaValidatorDebtAccounts {
652+
pub struct EnableSolanaValidatorDebtWriteOffAccounts {
653+
pub program_config_key: Pubkey,
654+
pub distribution_key: Pubkey,
655+
pub payer_key: Pubkey,
656+
}
657+
658+
impl EnableSolanaValidatorDebtWriteOffAccounts {
659+
pub fn new(dz_epoch: DoubleZeroEpoch, payer_key: &Pubkey) -> Self {
660+
Self {
661+
program_config_key: ProgramConfig::find_address().0,
662+
distribution_key: Distribution::find_address(dz_epoch).0,
663+
payer_key: *payer_key,
664+
}
665+
}
666+
}
667+
668+
impl From<EnableSolanaValidatorDebtWriteOffAccounts> for Vec<AccountMeta> {
669+
fn from(accounts: EnableSolanaValidatorDebtWriteOffAccounts) -> Self {
670+
let EnableSolanaValidatorDebtWriteOffAccounts {
671+
program_config_key,
672+
distribution_key,
673+
payer_key,
674+
} = accounts;
675+
676+
vec![
677+
AccountMeta::new_readonly(program_config_key, false),
678+
AccountMeta::new(distribution_key, false),
679+
AccountMeta::new(payer_key, true),
680+
AccountMeta::new_readonly(system_program::ID, false),
681+
]
682+
}
683+
}
684+
685+
#[derive(Debug, Clone, PartialEq, Eq)]
686+
pub struct WriteOffSolanaValidatorDebtAccounts {
653687
pub program_config_key: Pubkey,
654688
pub debt_accountant_key: Pubkey,
655689
pub distribution_key: Pubkey,
656-
pub next_distribution_key: Pubkey,
690+
pub solana_validator_deposit_key: Pubkey,
691+
pub write_off_distribution_key: Pubkey,
657692
}
658693

659-
impl ForgiveSolanaValidatorDebtAccounts {
694+
impl WriteOffSolanaValidatorDebtAccounts {
660695
pub fn new(
661696
debt_accountant_key: &Pubkey,
662697
dz_epoch: DoubleZeroEpoch,
663-
next_dz_epoch: DoubleZeroEpoch,
698+
node_id: &Pubkey,
699+
write_off_dz_epoch: DoubleZeroEpoch,
664700
) -> Self {
665701
Self {
666702
program_config_key: ProgramConfig::find_address().0,
667703
debt_accountant_key: *debt_accountant_key,
668704
distribution_key: Distribution::find_address(dz_epoch).0,
669-
next_distribution_key: Distribution::find_address(next_dz_epoch).0,
705+
solana_validator_deposit_key: SolanaValidatorDeposit::find_address(node_id).0,
706+
write_off_distribution_key: Distribution::find_address(write_off_dz_epoch).0,
670707
}
671708
}
672709
}
673710

674-
impl From<ForgiveSolanaValidatorDebtAccounts> for Vec<AccountMeta> {
675-
fn from(accounts: ForgiveSolanaValidatorDebtAccounts) -> Self {
676-
let ForgiveSolanaValidatorDebtAccounts {
711+
impl From<WriteOffSolanaValidatorDebtAccounts> for Vec<AccountMeta> {
712+
fn from(accounts: WriteOffSolanaValidatorDebtAccounts) -> Self {
713+
let WriteOffSolanaValidatorDebtAccounts {
677714
program_config_key,
678715
debt_accountant_key,
679716
distribution_key,
680-
next_distribution_key,
717+
solana_validator_deposit_key,
718+
write_off_distribution_key,
681719
} = accounts;
682720

683721
vec![
684722
AccountMeta::new_readonly(program_config_key, false),
685723
AccountMeta::new_readonly(debt_accountant_key, true),
686724
AccountMeta::new(distribution_key, false),
687-
AccountMeta::new(next_distribution_key, false),
725+
AccountMeta::new(solana_validator_deposit_key, false),
726+
AccountMeta::new(write_off_distribution_key, false),
688727
]
689728
}
690729
}

solana/programs/revenue-distribution/src/instruction/mod.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ pub enum RevenueDistributionInstructionData {
9393
amount: u64,
9494
proof: MerkleProof,
9595
},
96-
ForgiveSolanaValidatorDebt {
97-
debt: SolanaValidatorDebt,
96+
EnableSolanaValidatorDebtWriteOff,
97+
WriteOffSolanaValidatorDebt {
98+
amount: u64,
9899
proof: MerkleProof,
99100
},
100101
InitializeSwapDestination,
@@ -137,8 +138,10 @@ impl RevenueDistributionInstructionData {
137138
Discriminator::new_sha2(b"dz::ix::initialize_solana_validator_deposit");
138139
pub const PAY_SOLANA_VALIDATOR_DEBT: Discriminator<DISCRIMINATOR_LEN> =
139140
Discriminator::new_sha2(b"dz::ix::pay_solana_validator_debt");
140-
pub const FORGIVE_SOLANA_VALIDATOR_DEBT: Discriminator<DISCRIMINATOR_LEN> =
141-
Discriminator::new_sha2(b"dz::ix::forgive_solana_validator_debt");
141+
pub const ENABLE_SOLANA_VALIDATOR_DEBT_WRITE_OFF: Discriminator<DISCRIMINATOR_LEN> =
142+
Discriminator::new_sha2(b"dz::ix::enable_solana_validator_debt_write_off");
143+
pub const WRITE_OFF_SOLANA_VALIDATOR_DEBT: Discriminator<DISCRIMINATOR_LEN> =
144+
Discriminator::new_sha2(b"dz::ix::write_off_solana_validator_debt");
142145
pub const INITIALIZE_SWAP_DESTINATION: Discriminator<DISCRIMINATOR_LEN> =
143146
Discriminator::new_sha2(b"dz::ix::initialize_swap_destination");
144147
pub const WITHDRAW_SOL: Discriminator<DISCRIMINATOR_LEN> =
@@ -222,11 +225,14 @@ impl BorshDeserialize for RevenueDistributionInstructionData {
222225

223226
Ok(Self::PaySolanaValidatorDebt { amount, proof })
224227
}
225-
Self::FORGIVE_SOLANA_VALIDATOR_DEBT => {
226-
let debt = BorshDeserialize::deserialize_reader(reader)?;
228+
Self::ENABLE_SOLANA_VALIDATOR_DEBT_WRITE_OFF => {
229+
Ok(Self::EnableSolanaValidatorDebtWriteOff)
230+
}
231+
Self::WRITE_OFF_SOLANA_VALIDATOR_DEBT => {
232+
let amount = BorshDeserialize::deserialize_reader(reader)?;
227233
let proof = BorshDeserialize::deserialize_reader(reader)?;
228234

229-
Ok(Self::ForgiveSolanaValidatorDebt { debt, proof })
235+
Ok(Self::WriteOffSolanaValidatorDebt { amount, proof })
230236
}
231237
Self::INITIALIZE_SWAP_DESTINATION => Ok(Self::InitializeSwapDestination),
232238
Self::SWEEP_DISTRIBUTION_TOKENS_V1 => Ok(Self::SweepDistributionTokens),
@@ -314,9 +320,12 @@ impl BorshSerialize for RevenueDistributionInstructionData {
314320
amount.serialize(writer)?;
315321
proof.serialize(writer)
316322
}
317-
Self::ForgiveSolanaValidatorDebt { debt, proof } => {
318-
Self::FORGIVE_SOLANA_VALIDATOR_DEBT.serialize(writer)?;
319-
debt.serialize(writer)?;
323+
Self::EnableSolanaValidatorDebtWriteOff => {
324+
Self::ENABLE_SOLANA_VALIDATOR_DEBT_WRITE_OFF.serialize(writer)
325+
}
326+
Self::WriteOffSolanaValidatorDebt { amount, proof } => {
327+
Self::WRITE_OFF_SOLANA_VALIDATOR_DEBT.serialize(writer)?;
328+
amount.serialize(writer)?;
320329
proof.serialize(writer)
321330
}
322331
Self::InitializeSwapDestination => Self::INITIALIZE_SWAP_DESTINATION.serialize(writer),

0 commit comments

Comments
 (0)