Skip to content

Commit

Permalink
Add unique_gas_payment_pubkey to GasPaymentData (#2583)
Browse files Browse the repository at this point in the history
### Description

Followup to #2574, we need this to verify gas payment PDAs

Also fixes DiscriminatorPrefixed

### Drive-by changes

n/a

### Related issues

n/a

### Backward compatibility

Different gas payment data account format, but hasn't been deployed so
all is good

### Testing

unit / functional tests
  • Loading branch information
tkporter authored Aug 2, 2023
1 parent 9ed3a24 commit 2fc4903
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
8 changes: 6 additions & 2 deletions rust/sealevel/libraries/account-utils/src/discriminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
T: DiscriminatorData + borsh::BorshSerialize,
{
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
PROGRAM_INSTRUCTION_DISCRIMINATOR.serialize(writer)?;
T::DISCRIMINATOR.serialize(writer)?;
self.data.serialize(writer)
}
}
Expand All @@ -35,7 +35,7 @@ where
{
fn deserialize(buf: &mut &[u8]) -> std::io::Result<Self> {
let (discriminator, rest) = buf.split_at(Discriminator::LENGTH);
if discriminator != PROGRAM_INSTRUCTION_DISCRIMINATOR {
if discriminator != T::DISCRIMINATOR {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid discriminator",
Expand Down Expand Up @@ -134,5 +134,9 @@ mod test {
let serialized_prefixed_foo = prefixed_foo.try_to_vec().unwrap();

assert_eq!(serialized_prefixed_foo.len(), prefixed_foo.size());
assert_eq!(
serialized_prefixed_foo[0..Discriminator::LENGTH],
Foo::DISCRIMINATOR
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ pub struct GasPaymentData {
pub message_id: H256,
/// The amount of gas paid for.
pub gas_amount: u64,
/// The unique gas payment pubkey.
pub unique_gas_payment_pubkey: Pubkey,
/// The slot of the gas payment.
pub slot: Slot,
}
Expand All @@ -243,8 +245,9 @@ impl SizedData for GasPaymentData {
// 4 for destination_domain
// 32 for message_id
// 8 for gas_amount
// 32 for unique_gas_payment_pubkey
// 8 for slot
8 + 32 + 4 + 32 + 8 + 8
8 + 32 + 4 + 32 + 8 + 32 + 8
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,15 @@ fn pay_for_gas(program_id: &Pubkey, accounts: &[AccountInfo], payment: PayForGas
// Account 4: The unique gas payment account.
// Uniqueness is enforced by making sure the message storage PDA based on
// this unique message account is empty, which is done next.
let unique_message_account_info = next_account_info(accounts_iter)?;
if !unique_message_account_info.is_signer {
let unique_gas_payment_account_info = next_account_info(accounts_iter)?;
if !unique_gas_payment_account_info.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}

// Account 5: Gas payment PDA.
let gas_payment_account_info = next_account_info(accounts_iter)?;
let (gas_payment_key, gas_payment_bump) = Pubkey::find_program_address(
igp_gas_payment_pda_seeds!(unique_message_account_info.key),
igp_gas_payment_pda_seeds!(unique_gas_payment_account_info.key),
program_id,
);
if gas_payment_account_info.key != &gas_payment_key {
Expand Down Expand Up @@ -357,6 +357,7 @@ fn pay_for_gas(program_id: &Pubkey, accounts: &[AccountInfo], payment: PayForGas
destination_domain: payment.destination_domain,
message_id: payment.message_id,
gas_amount,
unique_gas_payment_pubkey: *unique_gas_payment_account_info.key,
slot: Clock::get()?.slot,
}
.into(),
Expand All @@ -372,7 +373,7 @@ fn pay_for_gas(program_id: &Pubkey, accounts: &[AccountInfo], payment: PayForGas
program_id,
system_program_info,
gas_payment_account_info,
igp_gas_payment_pda_seeds!(unique_message_account_info.key, gas_payment_bump),
igp_gas_payment_pda_seeds!(unique_gas_payment_account_info.key, gas_payment_bump),
)?;

gas_payment_account.store(gas_payment_account_info, false)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ async fn assert_gas_payment(
banks_client: &mut BanksClient,
igp_key: Pubkey,
payment_tx_signature: Signature,
_payment_unique_account_pubkey: Pubkey,
unique_gas_payment_pubkey: Pubkey,
gas_payment_account_key: Pubkey,
destination_domain: u32,
gas_amount: u64,
Expand Down Expand Up @@ -1059,6 +1059,7 @@ async fn assert_gas_payment(
destination_domain,
message_id,
gas_amount,
unique_gas_payment_pubkey,
slot,
}
.into(),
Expand Down

0 comments on commit 2fc4903

Please sign in to comment.