Skip to content

Commit

Permalink
transaction-status, storage-proto: add compute_units_consumed (solana…
Browse files Browse the repository at this point in the history
…-labs#26528)

* transaction-status, storage-proto: add compute_units_consumed

* fix bpf test

Co-authored-by: Justin Starry <[email protected]>
  • Loading branch information
2 people authored and apfitzge committed Aug 9, 2022
1 parent 01c6bab commit 7f4335d
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cli-output/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ fn write_transaction<W: io::Write>(
write_status(w, &transaction_status.status, prefix)?;
write_fees(w, transaction_status.fee, prefix)?;
write_balances(w, transaction_status, prefix)?;
write_compute_units_consumed(w, transaction_status.compute_units_consumed, prefix)?;
write_log_messages(w, transaction_status.log_messages.as_ref(), prefix)?;
write_return_data(w, transaction_status.return_data.as_ref(), prefix)?;
write_rewards(w, transaction_status.rewards.as_ref(), prefix)?;
Expand Down Expand Up @@ -613,6 +614,17 @@ fn write_return_data<W: io::Write>(
Ok(())
}

fn write_compute_units_consumed<W: io::Write>(
w: &mut W,
compute_units_consumed: Option<u64>,
prefix: &str,
) -> io::Result<()> {
if let Some(cus) = compute_units_consumed {
writeln!(w, "{}Compute Units Consumed: {}", prefix, cus)?;
}
Ok(())
}

fn write_log_messages<W: io::Write>(
w: &mut W,
log_messages: Option<&Vec<String>>,
Expand Down Expand Up @@ -794,6 +806,7 @@ mod test {
program_id: Pubkey::new_from_array([2u8; 32]),
data: vec![1, 2, 3],
}),
compute_units_consumed: Some(1234u64),
};

let output = {
Expand Down Expand Up @@ -828,6 +841,7 @@ Status: Ok
Fee: ◎0.000005
Account 0 balance: ◎0.000005 -> ◎0
Account 1 balance: ◎0.00001 -> ◎0.0000099
Compute Units Consumed: 1234
Log Messages:
Test message
Return Data from Program 8qbHbw2BbbTHBW1sbeqakYXVKRQM8Ne7pLK7m6CVfeR:
Expand Down Expand Up @@ -871,6 +885,7 @@ Rewards:
program_id: Pubkey::new_from_array([2u8; 32]),
data: vec![1, 2, 3],
}),
compute_units_consumed: Some(2345u64),
};

let output = {
Expand Down Expand Up @@ -914,6 +929,7 @@ Status: Ok
Account 1 balance: ◎0.00001
Account 2 balance: ◎0.000015 -> ◎0.0000149
Account 3 balance: ◎0.00002
Compute Units Consumed: 2345
Log Messages:
Test message
Return Data from Program 8qbHbw2BbbTHBW1sbeqakYXVKRQM8Ne7pLK7m6CVfeR:
Expand Down
1 change: 1 addition & 0 deletions client/src/mock_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl RpcSender for MockSender {
rewards: None,
loaded_addresses: None,
return_data: None,
compute_units_consumed: None,
}),
},
block_time: Some(1628633791),
Expand Down
1 change: 1 addition & 0 deletions core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3883,6 +3883,7 @@ mod tests {
post_token_balances: Some(vec![]),
rewards: Some(vec![]),
loaded_addresses: sanitized_tx.get_loaded_addresses(),
compute_units_consumed: Some(0),
..TransactionStatusMeta::default()
}
);
Expand Down
21 changes: 21 additions & 0 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6485,6 +6485,7 @@ pub mod tests {
pre_balances.push(i as u64 * 10);
post_balances.push(i as u64 * 11);
}
let compute_units_consumed = Some(12345);
let signature = transaction.signatures[0];
let status = TransactionStatusMeta {
status: Ok(()),
Expand All @@ -6498,6 +6499,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed,
}
.into();
blockstore
Expand All @@ -6516,6 +6518,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed,
}
.into();
blockstore
Expand All @@ -6534,6 +6537,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed,
}
.into();
blockstore
Expand All @@ -6554,6 +6558,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed,
},
}
})
Expand Down Expand Up @@ -6670,6 +6675,8 @@ pub mod tests {
program_id: Pubkey::new_unique(),
data: vec![1, 2, 3],
};
let compute_units_consumed_1 = Some(3812649u64);
let compute_units_consumed_2 = Some(42u64);

// result not found
assert!(transaction_status_cf
Expand All @@ -6690,6 +6697,7 @@ pub mod tests {
rewards: Some(rewards_vec.clone()),
loaded_addresses: test_loaded_addresses.clone(),
return_data: Some(test_return_data.clone()),
compute_units_consumed: compute_units_consumed_1,
}
.into();
assert!(transaction_status_cf
Expand All @@ -6709,6 +6717,7 @@ pub mod tests {
rewards,
loaded_addresses,
return_data,
compute_units_consumed,
} = transaction_status_cf
.get_protobuf_or_bincode::<StoredTransactionStatusMeta>((0, Signature::default(), 0))
.unwrap()
Expand All @@ -6726,6 +6735,7 @@ pub mod tests {
assert_eq!(rewards.unwrap(), rewards_vec);
assert_eq!(loaded_addresses, test_loaded_addresses);
assert_eq!(return_data.unwrap(), test_return_data);
assert_eq!(compute_units_consumed, compute_units_consumed_1);

// insert value
let status = TransactionStatusMeta {
Expand All @@ -6740,6 +6750,7 @@ pub mod tests {
rewards: Some(rewards_vec.clone()),
loaded_addresses: test_loaded_addresses.clone(),
return_data: Some(test_return_data.clone()),
compute_units_consumed: compute_units_consumed_2,
}
.into();
assert!(transaction_status_cf
Expand All @@ -6759,6 +6770,7 @@ pub mod tests {
rewards,
loaded_addresses,
return_data,
compute_units_consumed,
} = transaction_status_cf
.get_protobuf_or_bincode::<StoredTransactionStatusMeta>((
0,
Expand All @@ -6782,6 +6794,7 @@ pub mod tests {
assert_eq!(rewards.unwrap(), rewards_vec);
assert_eq!(loaded_addresses, test_loaded_addresses);
assert_eq!(return_data.unwrap(), test_return_data);
assert_eq!(compute_units_consumed, compute_units_consumed_2);
}

#[test]
Expand Down Expand Up @@ -7011,6 +7024,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed: Some(42u64),
}
.into();

Expand Down Expand Up @@ -7207,6 +7221,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed: Some(42u64),
}
.into();

Expand Down Expand Up @@ -7394,6 +7409,7 @@ pub mod tests {
rewards: rewards.clone(),
loaded_addresses: LoadedAddresses::default(),
return_data: return_data.clone(),
compute_units_consumed: Some(42),
}
.into();
blockstore
Expand All @@ -7414,6 +7430,7 @@ pub mod tests {
rewards,
loaded_addresses: LoadedAddresses::default(),
return_data,
compute_units_consumed: Some(42),
},
}
})
Expand Down Expand Up @@ -7502,6 +7519,7 @@ pub mod tests {
rewards: rewards.clone(),
loaded_addresses: LoadedAddresses::default(),
return_data: return_data.clone(),
compute_units_consumed: Some(42u64),
}
.into();
blockstore
Expand All @@ -7522,6 +7540,7 @@ pub mod tests {
rewards,
loaded_addresses: LoadedAddresses::default(),
return_data,
compute_units_consumed: Some(42u64),
},
}
})
Expand Down Expand Up @@ -8282,6 +8301,7 @@ pub mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed: None,
}
.into();
transaction_status_cf
Expand Down Expand Up @@ -8889,6 +8909,7 @@ pub mod tests {
program_id: Pubkey::new_unique(),
data: vec![1, 2, 3],
}),
compute_units_consumed: Some(23456),
};
let deprecated_status: StoredTransactionStatusMeta = status.clone().try_into().unwrap();
let protobuf_status: generated::TransactionStatusMeta = status.into();
Expand Down
2 changes: 2 additions & 0 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ fn execute_transactions(
inner_instructions,
durable_nonce_fee,
return_data,
executed_units,
..
} = details;

Expand Down Expand Up @@ -482,6 +483,7 @@ fn execute_transactions(
rewards: None,
loaded_addresses: LoadedAddresses::default(),
return_data,
compute_units_consumed: Some(executed_units),
};

Ok(ConfirmedTransactionWithStatusMeta {
Expand Down
2 changes: 2 additions & 0 deletions rpc/src/transaction_status_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl TransactionStatusService {
inner_instructions,
durable_nonce_fee,
return_data,
executed_units,
..
} = details;
let lamports_per_signature = match durable_nonce_fee {
Expand Down Expand Up @@ -160,6 +161,7 @@ impl TransactionStatusService {
rewards,
loaded_addresses,
return_data,
compute_units_consumed: Some(executed_units),
};

if let Some(transaction_notifier) = transaction_notifier.as_ref() {
Expand Down
2 changes: 2 additions & 0 deletions storage-bigtable/src/bigtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ mod tests {
rewards: Some(vec![]),
loaded_addresses: LoadedAddresses::default(),
return_data: Some(TransactionReturnData::default()),
compute_units_consumed: Some(1234),
},
});
let expected_block = ConfirmedBlock {
Expand Down Expand Up @@ -955,6 +956,7 @@ mod tests {
meta.post_token_balances = None; // Legacy bincode implementation does not support token balances
meta.rewards = None; // Legacy bincode implementation does not support rewards
meta.return_data = None; // Legacy bincode implementation does not support return data
meta.compute_units_consumed = None; // Legacy bincode implementation does not support CU consumed
}
assert_eq!(block, bincode_block.into());
} else {
Expand Down
1 change: 1 addition & 0 deletions storage-bigtable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl From<StoredConfirmedBlockTransactionStatusMeta> for TransactionStatusMeta {
rewards: None,
loaded_addresses: LoadedAddresses::default(),
return_data: None,
compute_units_consumed: None,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions storage-proto/proto/confirmed_block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ message TransactionStatusMeta {
repeated bytes loaded_readonly_addresses = 13;
ReturnData return_data = 14;
bool return_data_none = 15;

// Sum of compute units consumed by all instructions.
// Available since Solana v1.10.35 / v1.11.6.
// Set to `None` for txs executed on earlier versions.
optional uint64 compute_units_consumed = 16;
}

message TransactionError {
Expand Down
4 changes: 4 additions & 0 deletions storage-proto/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ impl From<TransactionStatusMeta> for generated::TransactionStatusMeta {
rewards,
loaded_addresses,
return_data,
compute_units_consumed,
} = value;
let err = match status {
Ok(()) => None,
Expand Down Expand Up @@ -424,6 +425,7 @@ impl From<TransactionStatusMeta> for generated::TransactionStatusMeta {
loaded_readonly_addresses,
return_data,
return_data_none,
compute_units_consumed,
}
}
}
Expand Down Expand Up @@ -455,6 +457,7 @@ impl TryFrom<generated::TransactionStatusMeta> for TransactionStatusMeta {
loaded_readonly_addresses,
return_data,
return_data_none,
compute_units_consumed,
} = value;
let status = match &err {
None => Ok(()),
Expand Down Expand Up @@ -515,6 +518,7 @@ impl TryFrom<generated::TransactionStatusMeta> for TransactionStatusMeta {
rewards,
loaded_addresses,
return_data,
compute_units_consumed,
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions storage-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ pub struct StoredTransactionStatusMeta {
pub rewards: Option<Vec<StoredExtendedReward>>,
#[serde(deserialize_with = "default_on_eof")]
pub return_data: Option<TransactionReturnData>,
#[serde(deserialize_with = "default_on_eof")]
pub compute_units_consumed: Option<u64>,
}

impl From<StoredTransactionStatusMeta> for TransactionStatusMeta {
Expand All @@ -191,6 +193,7 @@ impl From<StoredTransactionStatusMeta> for TransactionStatusMeta {
post_token_balances,
rewards,
return_data,
compute_units_consumed,
} = value;
Self {
status,
Expand All @@ -207,6 +210,7 @@ impl From<StoredTransactionStatusMeta> for TransactionStatusMeta {
.map(|rewards| rewards.into_iter().map(|reward| reward.into()).collect()),
loaded_addresses: LoadedAddresses::default(),
return_data,
compute_units_consumed,
}
}
}
Expand All @@ -226,6 +230,7 @@ impl TryFrom<TransactionStatusMeta> for StoredTransactionStatusMeta {
rewards,
loaded_addresses,
return_data,
compute_units_consumed,
} = value;

if !loaded_addresses.is_empty() {
Expand All @@ -250,6 +255,7 @@ impl TryFrom<TransactionStatusMeta> for StoredTransactionStatusMeta {
rewards: rewards
.map(|rewards| rewards.into_iter().map(|reward| reward.into()).collect()),
return_data,
compute_units_consumed,
})
}
}
Loading

0 comments on commit 7f4335d

Please sign in to comment.