-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move QuantSpec into the miner actor (#1521)
Unfortunately, I had to duplicate a little bit of code in the market actor, but I think it's better to do that than to put this mostly miner specific logic in, e.g., the runtime.
- Loading branch information
Showing
22 changed files
with
120 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2021-2023 Protocol Labs | ||
// Copyright 2019-2022 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use fvm_shared::clock::ChainEpoch; | ||
|
||
/// Constant defining the [QuantSpec] which performs no quantization. | ||
pub const NO_QUANTIZATION: QuantSpec = QuantSpec { unit: 1, offset: 0 }; | ||
|
||
/// A spec for epoch quantization. | ||
#[derive(Copy, Clone)] | ||
pub struct QuantSpec { | ||
/// The unit of quantization | ||
pub unit: ChainEpoch, | ||
/// The offset from zero from which to base the modulus | ||
pub offset: ChainEpoch, | ||
} | ||
|
||
impl QuantSpec { | ||
/// Rounds `epoch` to the nearest exact multiple of the quantization unit offset by | ||
/// `offset % unit`, rounding up. | ||
/// | ||
/// This function is equivalent to `unit * ceil(epoch - (offset % unit) / unit) + (offsetSeed % unit)` | ||
/// with the variables/operations over real numbers instead of ints. | ||
/// | ||
/// Precondition: `unit >= 0` | ||
pub fn quantize_up(&self, epoch: ChainEpoch) -> ChainEpoch { | ||
let offset = self.offset % self.unit; | ||
|
||
let remainder = (epoch - offset) % self.unit; | ||
let quotient = (epoch - offset) / self.unit; | ||
|
||
// Don't round if epoch falls on a quantization epoch | ||
if remainder == 0 | ||
// Negative truncating division rounds up | ||
|| epoch - offset < 0 | ||
{ | ||
self.unit * quotient + offset | ||
} else { | ||
self.unit * (quotient + 1) + offset | ||
} | ||
} | ||
|
||
pub fn quantize_down(&self, epoch: ChainEpoch) -> ChainEpoch { | ||
let next = self.quantize_up(epoch); | ||
// QuantizeDown == QuantizeUp iff epoch is a fixed point of QuantizeUp | ||
if epoch == next { | ||
next | ||
} else { | ||
next - self.unit | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.