Skip to content

Commit

Permalink
use rolling sum of gas costs in checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
emmazzz committed Nov 17, 2022
1 parent 9bffa79 commit 7878cd3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
42 changes: 36 additions & 6 deletions crates/sui-core/src/checkpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,32 @@ impl CheckpointBuilder {
let last_checkpoint = self.tables.checkpoint_summary.iter().skip_to_last().next();
let previous_digest = last_checkpoint.as_ref().map(|(_, c)| c.digest());
let sequence_number = last_checkpoint
.as_ref()
.map(|(_, c)| c.sequence_number + 1)
.unwrap_or_default();
let contents = CheckpointContents::new_with_causally_ordered_transactions(
l.iter().map(TransactionEffects::execution_digests),
);
let gas_cost_summary = GasCostSummary::new_from_txn_effects(l.iter());
let (previous_epoch, previous_gas_costs) = last_checkpoint
.map(|(_, c)| (c.epoch, c.epoch_rolling_gas_cost_summary))
.unwrap_or_default();
let current_gas_costs = GasCostSummary::new_from_txn_effects(l.iter());
let epoch_rolling_gas_cost_summary = if previous_epoch == self.epoch {
// sum only when we are within the same epoch
GasCostSummary::new(
previous_gas_costs.computation_cost + current_gas_costs.computation_cost,
previous_gas_costs.storage_cost + current_gas_costs.storage_cost,
previous_gas_costs.storage_rebate + current_gas_costs.storage_rebate,
)
} else {
current_gas_costs
};
let summary = CheckpointSummary::new(
self.epoch, // todo - need to figure out how this is updated
sequence_number,
&contents,
previous_digest,
gas_cost_summary,
epoch_rolling_gas_cost_summary,
None, //todo
);

Expand Down Expand Up @@ -660,10 +674,16 @@ mod tests {
let _guard = setup_tracing();
let tempdir = tempdir().unwrap();
let mut store: HashMap<TransactionDigest, TransactionEffects> = HashMap::new();
store.insert(d(1), e(d(1), vec![d(2), d(3)]));
store.insert(d(2), e(d(2), vec![d(3), d(4)]));
store.insert(d(3), e(d(3), vec![]));
store.insert(d(4), e(d(4), vec![]));
store.insert(
d(1),
e(d(1), vec![d(2), d(3)], GasCostSummary::new(11, 12, 13)),
);
store.insert(
d(2),
e(d(2), vec![d(3), d(4)], GasCostSummary::new(21, 22, 23)),
);
store.insert(d(3), e(d(3), vec![], GasCostSummary::new(31, 32, 33)));
store.insert(d(4), e(d(4), vec![], GasCostSummary::new(41, 42, 43)));
let (output, mut result) =
mpsc::channel::<(CheckpointContents, CheckpointSummary, bool)>(10);
let (certified_output, mut certified_result) =
Expand Down Expand Up @@ -700,10 +720,18 @@ mod tests {
assert_eq!(c1t, vec![d(4)]);
assert_eq!(c1s.previous_digest, None);
assert_eq!(c1s.sequence_number, 0);
assert_eq!(
c1s.epoch_rolling_gas_cost_summary,
GasCostSummary::new(41, 42, 43)
);

assert_eq!(c2t, vec![d(3), d(2), d(1)]);
assert_eq!(c2s.previous_digest, Some(c1s.digest()));
assert_eq!(c2s.sequence_number, 1);
assert_eq!(
c2s.epoch_rolling_gas_cost_summary,
GasCostSummary::new(104, 108, 112)
);

let c1ss =
SignedCheckpointSummary::new_from_summary(c1s, keypair.public().into(), &keypair);
Expand Down Expand Up @@ -785,10 +813,12 @@ mod tests {
fn e(
transaction_digest: TransactionDigest,
dependencies: Vec<TransactionDigest>,
gas_used: GasCostSummary,
) -> TransactionEffects {
TransactionEffects {
transaction_digest,
dependencies,
gas_used,
..Default::default()
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-network/src/state_sync/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl CommitteeFixture {
sequence_number: 0,
content_digest: empty_contents().digest(),
previous_digest: None,
gas_cost_summary: Default::default(),
epoch_rolling_gas_cost_summary: Default::default(),
next_epoch_committee: None,
};

Expand Down Expand Up @@ -447,7 +447,7 @@ impl CommitteeFixture {
sequence_number: prev.summary.sequence_number + 1,
content_digest: empty_contents().digest(),
previous_digest: Some(prev.summary.digest()),
gas_cost_summary: Default::default(),
epoch_rolling_gas_cost_summary: Default::default(),
next_epoch_committee: None,
};

Expand Down
8 changes: 8 additions & 0 deletions crates/sui-types/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ pub struct GasCostSummary {
}

impl GasCostSummary {
pub fn new(computation_cost: u64, storage_cost: u64, storage_rebate: u64) -> GasCostSummary {
GasCostSummary {
computation_cost,
storage_cost,
storage_rebate,
}
}

pub fn gas_used(&self) -> u64 {
self.computation_cost + self.storage_cost
}
Expand Down
17 changes: 9 additions & 8 deletions crates/sui-types/src/messages_checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ pub struct CheckpointSummary {
pub sequence_number: CheckpointSequenceNumber,
pub content_digest: CheckpointContentsDigest,
pub previous_digest: Option<CheckpointDigest>,
/// The total gas costs of all transactions included in this checkpoint.
pub gas_cost_summary: GasCostSummary,
/// The running total gas costs of all transactions included in the current epoch so far
/// until this checkpoint.
pub epoch_rolling_gas_cost_summary: GasCostSummary,
/// If this checkpoint is the last checkpoint of the epoch, we also include the committee
/// of the next epoch. This allows anyone receiving this checkpoint know that the epoch
/// will change after this checkpoint, as well as what the new committee is.
Expand All @@ -126,7 +127,7 @@ impl CheckpointSummary {
sequence_number: CheckpointSequenceNumber,
transactions: &CheckpointContents,
previous_digest: Option<CheckpointDigest>,
gas_cost_summary: GasCostSummary,
epoch_rolling_gas_cost_summary: GasCostSummary,
next_epoch_committee: Option<Committee>,
) -> CheckpointSummary {
let mut waypoint = Box::new(Waypoint::default());
Expand All @@ -141,7 +142,7 @@ impl CheckpointSummary {
sequence_number,
content_digest,
previous_digest,
gas_cost_summary,
epoch_rolling_gas_cost_summary,
next_epoch_committee: next_epoch_committee.map(|c| c.voting_rights),
}
}
Expand All @@ -160,11 +161,11 @@ impl Display for CheckpointSummary {
write!(
f,
"CheckpointSummary {{ epoch: {:?}, seq: {:?}, content_digest: {},
gas_cost_summary: {:?}}}",
epoch_rolling_gas_cost_summary: {:?}}}",
self.epoch,
self.sequence_number,
Hex::encode(self.content_digest),
self.gas_cost_summary,
self.epoch_rolling_gas_cost_summary,
)
}
}
Expand Down Expand Up @@ -224,15 +225,15 @@ impl SignedCheckpointSummary {
signer: &dyn signature::Signer<AuthoritySignature>,
transactions: &CheckpointContents,
previous_digest: Option<CheckpointDigest>,
gas_cost_summary: GasCostSummary,
epoch_rolling_gas_cost_summary: GasCostSummary,
next_epoch_committee: Option<Committee>,
) -> SignedCheckpointSummary {
let checkpoint = CheckpointSummary::new(
epoch,
sequence_number,
transactions,
previous_digest,
gas_cost_summary,
epoch_rolling_gas_cost_summary,
next_epoch_committee,
);
SignedCheckpointSummary::new_from_summary(checkpoint, authority, signer)
Expand Down

0 comments on commit 7878cd3

Please sign in to comment.