Skip to content

Commit 31bfac3

Browse files
authored
Merge pull request #487 from filecoin-project/asr/fip-0032-externs
Add a base and per-byte cost for getting randomness
2 parents 391a3fa + f310937 commit 31bfac3

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

fvm/src/gas/price_list.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ lazy_static! {
119119
.collect(),
120120

121121
gas_per_exec_unit: 0,
122-
extern_traversal_cost: 0,
122+
get_randomness_base: 0,
123+
get_randomness_per_byte: 0,
123124

124125
block_memcpy_per_byte_cost: 0,
125126
block_io_per_byte_cost: 0,
@@ -204,6 +205,7 @@ lazy_static! {
204205
.cloned()
205206
.collect(),
206207

208+
// TODO: PARAM_FINISH: this may need to be increased to account for the cost of an extern
207209
verify_consensus_fault: 495422,
208210
verify_replica_update: 36316136,
209211
verify_post_lookup: [
@@ -241,7 +243,10 @@ lazy_static! {
241243
// TODO: PARAM_FINISH
242244
gas_per_exec_unit: 2,
243245
// TODO: PARAM_FINISH
244-
extern_traversal_cost: 1,
246+
get_randomness_base: 1,
247+
// TODO: PARAM_FINISH
248+
get_randomness_per_byte: 1,
249+
245250
// TODO: PARAM_FINIuiSH
246251
block_open_base: 1,
247252
// TODO: PARAM_FINISH
@@ -364,9 +369,8 @@ pub struct PriceList {
364369
// 1 Exec Unit = gas_per_exec_unit * 1 Gas
365370
pub(crate) gas_per_exec_unit: i64,
366371

367-
// A special cost for traversing the boundary between the FVM and the client node
368-
// See https://github.com/filecoin-project/FIPs/blob/master/FIPS/fip-0032.md#extern-traversing-syscall-fee-revision for more
369-
pub(crate) extern_traversal_cost: i64,
372+
pub(crate) get_randomness_base: i64,
373+
pub(crate) get_randomness_per_byte: i64,
370374

371375
pub(crate) block_memcpy_per_byte_cost: i64,
372376
pub(crate) block_io_per_byte_cost: i64,
@@ -560,10 +564,21 @@ impl PriceList {
560564
}
561565
}
562566

563-
/// Returns the gas required for traversing an extern boundary into the client.
567+
/// Returns the base cost of the gas required for getting randomness from the client.
568+
#[inline]
569+
pub fn on_get_randomness_base(&self) -> GasCharge<'static> {
570+
GasCharge::new("OnGetRandomnessBase", self.get_randomness_base, 0)
571+
}
572+
573+
/// Returns the gas required for getting randomness from the client based on the number of bytes of randomness.
564574
#[inline]
565-
pub fn on_extern_traversal(&self) -> GasCharge<'static> {
566-
GasCharge::new("OnExternTraversal", self.extern_traversal_cost, 0)
575+
pub fn on_get_randomness_per_byte(&self, randomness_size: usize) -> GasCharge<'static> {
576+
GasCharge::new(
577+
"OnGetRandomnessPerByte",
578+
self.get_randomness_per_byte
579+
.saturating_mul(randomness_size as i64),
580+
0,
581+
)
567582
}
568583

569584
/// Returns the base gas required for loading an object, independent of the object's size.

fvm/src/kernel/default.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,6 @@ where
589589
h2: &[u8],
590590
extra: &[u8],
591591
) -> Result<Option<ConsensusFault>> {
592-
self.call_manager
593-
.charge_gas(self.call_manager.price_list().on_extern_traversal())?;
594592
self.call_manager
595593
.charge_gas(self.call_manager.price_list().on_verify_consensus_fault())?;
596594

@@ -796,12 +794,20 @@ where
796794
entropy: &[u8],
797795
) -> Result<[u8; RANDOMNESS_LENGTH]> {
798796
self.call_manager
799-
.charge_gas(self.call_manager.price_list().on_extern_traversal())?;
797+
.charge_gas(self.call_manager.price_list().on_get_randomness_base())?;
800798
// TODO: Check error code
801-
self.call_manager
799+
let rand = self
800+
.call_manager
802801
.externs()
803802
.get_chain_randomness(personalization, rand_epoch, entropy)
804-
.or_illegal_argument()
803+
.or_illegal_argument()?;
804+
self.call_manager
805+
.charge_gas(
806+
self.call_manager
807+
.price_list()
808+
.on_get_randomness_per_byte(rand.len()),
809+
)
810+
.map(|_| rand)
805811
}
806812

807813
#[allow(unused)]
@@ -812,13 +818,21 @@ where
812818
entropy: &[u8],
813819
) -> Result<[u8; RANDOMNESS_LENGTH]> {
814820
self.call_manager
815-
.charge_gas(self.call_manager.price_list().on_extern_traversal())?;
821+
.charge_gas(self.call_manager.price_list().on_get_randomness_base())?;
816822
// TODO: Check error code
817-
// Hyperdrive and above only.
818-
self.call_manager
823+
let rand = self
824+
.call_manager
819825
.externs()
820826
.get_beacon_randomness(personalization, rand_epoch, entropy)
821-
.or_illegal_argument()
827+
.or_illegal_argument()?;
828+
829+
self.call_manager
830+
.charge_gas(
831+
self.call_manager
832+
.price_list()
833+
.on_get_randomness_per_byte(rand.len()),
834+
)
835+
.map(|_| rand)
822836
}
823837
}
824838

0 commit comments

Comments
 (0)