1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
//! Tools for gas instrumentalization
use std::ops::Deref;
use std::sync::Arc;
use fuel_types::Word;
#[allow(dead_code)]
/// Default gas costs are generated from the
/// `fuel-core` repo using the `collect` bin
/// in the `fuel-core-benches` crate.
/// The git sha is included in the file to
/// show what version of `fuel-core` was used
/// to generate the costs.
mod default_gas_costs;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Gas unit cost that embeds a unit price and operations count.
///
/// The operations count will be the argument of every variant except
/// `Accumulated`, that will hold the total acumulated gas.
pub enum GasUnit {
/// Atomic operation.
Atom(Word),
/// Arithmetic operation.
Arithmetic(Word),
/// Expensive arithmetic operation.
ArithmeticExpensive(Word),
/// Write to a register.
RegisterWrite(Word),
/// Branching cost.
Branching(Word),
/// Hash crypto operation.
Hash(Word),
/// Memory ownership test cost.
MemoryOwnership(Word),
/// Cost of memory read, per byte.
MemoryRead(Word),
/// Cost of memory write, per byte.
MemoryWrite(Word),
/// Crypto public key recover.
Recover(Word),
/// Cost to read bytes from a storage tree
StorageReadTree(Word),
/// Cost to write bytes to a storage tree
StorageWriteTree(Word),
/// Cost to write a word to the storage
StorageWriteWord(Word),
/// Accumulated cost of several operations.
Accumulated(Word),
}
impl GasUnit {
/// Return the `cost := price · N`.
pub const fn cost(&self) -> Word {
use GasUnit::*;
match self {
Atom(1) => self.unit_price(),
Arithmetic(1) => self.unit_price(),
ArithmeticExpensive(1) => self.unit_price(),
RegisterWrite(1) => self.unit_price(),
Branching(1) => self.unit_price(),
Hash(1) => self.unit_price(),
MemoryOwnership(1) => self.unit_price(),
MemoryRead(1) => self.unit_price(),
MemoryWrite(1) => self.unit_price(),
Recover(1) => self.unit_price(),
StorageReadTree(1) => self.unit_price(),
StorageWriteTree(1) => self.unit_price(),
StorageWriteWord(1) => self.unit_price(),
Atom(n) => *n * Atom(1).cost(),
Arithmetic(n) => *n * Arithmetic(1).cost(),
ArithmeticExpensive(n) => *n * ArithmeticExpensive(1).cost(),
RegisterWrite(n) => *n * RegisterWrite(1).cost(),
Branching(n) => *n * Branching(1).cost(),
Hash(n) => *n * Hash(1).cost(),
MemoryOwnership(n) => *n * MemoryOwnership(1).cost(),
MemoryRead(n) => *n * MemoryRead(1).cost(),
MemoryWrite(n) => *n * MemoryWrite(1).cost(),
Recover(n) => *n * Recover(1).cost(),
StorageReadTree(n) => *n * StorageReadTree(1).cost(),
StorageWriteTree(n) => *n * StorageWriteTree(1).cost(),
StorageWriteWord(n) => *n * StorageWriteWord(1).cost(),
Accumulated(c) => *c,
}
}
/// Return the price per unit.
pub const fn unit_price(&self) -> Word {
use GasUnit::*;
// the values are defined empirically from tests performed in fuel-core-benches.
//
// the worst case scenario of execution is a memory write for chunks larger than the OS
// page size, that is commonly set to `4096` bytes.
//
// the storage, as expected from a production-ready implementation, didn't present alarming
// computing power demand from increased operations because tree-seek should be, in worst
// case scenario, logarithmic.
match self {
// base price for pc inc
Atom(_) => 10,
// arithmetic operations
Arithmetic(_) => 15,
// expensive arith operations
ArithmeticExpensive(_) => 100,
// write a register with reserved branching check
RegisterWrite(_) => 20,
// branching different than reserved reg
Branching(_) => 20,
// native hash operation
Hash(_) => 300,
// memory ownership branching check
MemoryOwnership(_) => 20,
// memory read per page. should increase exponentially to the number of used pages
MemoryRead(_) => 15,
// memory write per page. should increase exponentially to the number of used pages
MemoryWrite(_) => 20,
// native ecrecover operation
Recover(_) => 950,
// storage read. the storage backend should offer logarithmic worst case scenarios
StorageReadTree(_) => 75,
// storage write. the storage backend should offer logarithmic worst case scenarios
StorageWriteTree(_) => 150,
// storage write word. the storage backend should offer logarithmic worst case scenarios
StorageWriteWord(_) => 130,
// accumulated cost for different operations
Accumulated(c) => *c,
}
}
/// Combine two gas computations, accumulating their cost.
pub const fn join(self, other: Self) -> Self {
Self::Accumulated(self.cost() + other.cost())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Gas costings for every op.
/// The inner values are wrapped in an [`Arc`]
/// so this is cheap to clone.
pub struct GasCosts(Arc<GasCostsValues>);
impl GasCosts {
/// Create new cost values wrapped in an [`Arc`].
pub fn new(costs: GasCostsValues) -> Self {
Self(Arc::new(costs))
}
}
impl Default for GasCosts {
fn default() -> Self {
Self(Arc::new(GasCostsValues::default()))
}
}
impl Default for GasCostsValues {
fn default() -> Self {
// The default values for gas costs
// are generated from fuel-core-benches.
default_gas_costs::default_gas_costs()
}
}
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default = "GasCostsValues::unit"))]
/// Gas costs for every op.
pub struct GasCostsValues {
pub add: Word,
pub addi: Word,
pub aloc: Word,
pub and: Word,
pub andi: Word,
pub bal: Word,
pub bhei: Word,
pub bhsh: Word,
pub burn: Word,
pub cb: Word,
pub cfei: Word,
pub cfsi: Word,
pub croo: Word,
pub div: Word,
pub divi: Word,
pub ecr: Word,
pub eq: Word,
pub exp: Word,
pub expi: Word,
pub flag: Word,
pub gm: Word,
pub gt: Word,
pub gtf: Word,
pub ji: Word,
pub jmp: Word,
pub jne: Word,
pub jnei: Word,
pub jnzi: Word,
pub k256: Word,
pub lb: Word,
pub log: Word,
pub lt: Word,
pub lw: Word,
pub mcpi: Word,
pub mint: Word,
pub mlog: Word,
#[cfg_attr(feature = "serde", serde(rename = "mod"))]
pub mod_op: Word,
pub modi: Word,
#[cfg_attr(feature = "serde", serde(rename = "move"))]
pub move_op: Word,
pub movi: Word,
pub mroo: Word,
pub mul: Word,
pub muli: Word,
pub noop: Word,
pub not: Word,
pub or: Word,
pub ori: Word,
#[cfg_attr(feature = "serde", serde(rename = "ret_contract"))]
pub ret: Word,
#[cfg_attr(feature = "serde", serde(rename = "rvrt_contract"))]
pub rvrt: Word,
pub s256: Word,
pub sb: Word,
pub scwq: Word,
pub sll: Word,
pub slli: Word,
pub srl: Word,
pub srli: Word,
pub srw: Word,
pub sub: Word,
pub subi: Word,
pub sw: Word,
pub sww: Word,
pub swwq: Word,
pub time: Word,
pub tr: Word,
pub tro: Word,
pub xor: Word,
pub xori: Word,
// Dependent
pub call: DependentCost,
pub ccp: DependentCost,
pub csiz: DependentCost,
pub ldc: DependentCost,
pub logd: DependentCost,
pub mcl: DependentCost,
pub mcli: DependentCost,
pub mcp: DependentCost,
pub meq: DependentCost,
#[cfg_attr(feature = "serde", serde(rename = "retd_contract"))]
pub retd: DependentCost,
pub smo: DependentCost,
pub srwq: DependentCost,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Dependent cost is a cost that depends on the number of units.
/// The cost starts at the base and grows by `dep_per_unit` for every unit.
///
/// For example, if the base is 10 and the `dep_per_unit` is 2,
/// then the cost for 0 units is 10, 1 unit is 12, 2 units is 14, etc.
pub struct DependentCost {
/// The minimum that this operation can cost.
pub base: Word,
/// The amount that this operation costs per
/// increase in unit.
pub dep_per_unit: Word,
}
impl GasCosts {
/// Create costs that are all set to zero.
pub fn free() -> Self {
Self(Arc::new(GasCostsValues::free()))
}
/// Create costs that are all set to one.
pub fn unit() -> Self {
Self(Arc::new(GasCostsValues::unit()))
}
}
impl GasCostsValues {
/// Create costs that are all set to zero.
pub fn free() -> Self {
Self {
add: 0,
addi: 0,
aloc: 0,
and: 0,
andi: 0,
bal: 0,
bhei: 0,
bhsh: 0,
burn: 0,
cb: 0,
cfei: 0,
cfsi: 0,
croo: 0,
div: 0,
divi: 0,
ecr: 0,
eq: 0,
exp: 0,
expi: 0,
flag: 0,
gm: 0,
gt: 0,
gtf: 0,
ji: 0,
jmp: 0,
jne: 0,
jnei: 0,
jnzi: 0,
k256: 0,
lb: 0,
log: 0,
lt: 0,
lw: 0,
mcpi: 0,
mint: 0,
mlog: 0,
mod_op: 0,
modi: 0,
move_op: 0,
movi: 0,
mroo: 0,
mul: 0,
muli: 0,
noop: 0,
not: 0,
or: 0,
ori: 0,
ret: 0,
rvrt: 0,
s256: 0,
sb: 0,
scwq: 0,
sll: 0,
slli: 0,
srl: 0,
srli: 0,
srw: 0,
sub: 0,
subi: 0,
sw: 0,
sww: 0,
swwq: 0,
time: 0,
tr: 0,
tro: 0,
xor: 0,
xori: 0,
call: DependentCost::free(),
ccp: DependentCost::free(),
csiz: DependentCost::free(),
ldc: DependentCost::free(),
logd: DependentCost::free(),
mcl: DependentCost::free(),
mcli: DependentCost::free(),
mcp: DependentCost::free(),
meq: DependentCost::free(),
retd: DependentCost::free(),
smo: DependentCost::free(),
srwq: DependentCost::free(),
}
}
/// Create costs that are all set to one.
pub fn unit() -> Self {
Self {
add: 1,
addi: 1,
aloc: 1,
and: 1,
andi: 1,
bal: 1,
bhei: 1,
bhsh: 1,
burn: 1,
cb: 1,
cfei: 1,
cfsi: 1,
croo: 1,
div: 1,
divi: 1,
ecr: 1,
eq: 1,
exp: 1,
expi: 1,
flag: 1,
gm: 1,
gt: 1,
gtf: 1,
ji: 1,
jmp: 1,
jne: 1,
jnei: 1,
jnzi: 1,
k256: 1,
lb: 1,
log: 1,
lt: 1,
lw: 1,
mcpi: 1,
mint: 1,
mlog: 1,
mod_op: 1,
modi: 1,
move_op: 1,
movi: 1,
mroo: 1,
mul: 1,
muli: 1,
noop: 1,
not: 1,
or: 1,
ori: 1,
ret: 1,
rvrt: 1,
s256: 1,
sb: 1,
scwq: 1,
sll: 1,
slli: 1,
srl: 1,
srli: 1,
srw: 1,
sub: 1,
subi: 1,
sw: 1,
sww: 1,
swwq: 1,
time: 1,
tr: 1,
tro: 1,
xor: 1,
xori: 1,
call: DependentCost::unit(),
ccp: DependentCost::unit(),
csiz: DependentCost::unit(),
ldc: DependentCost::unit(),
logd: DependentCost::unit(),
mcl: DependentCost::unit(),
mcli: DependentCost::unit(),
mcp: DependentCost::unit(),
meq: DependentCost::unit(),
retd: DependentCost::unit(),
smo: DependentCost::unit(),
srwq: DependentCost::unit(),
}
}
}
impl DependentCost {
/// Create costs that are all set to zero.
pub fn free() -> Self {
Self {
base: 0,
dep_per_unit: 0,
}
}
/// Create costs that are all set to one.
pub fn unit() -> Self {
Self {
base: 1,
dep_per_unit: 0,
}
}
}
impl Deref for GasCosts {
type Target = GasCostsValues;
fn deref(&self) -> &Self::Target {
&(self.0)
}
}
impl From<GasCostsValues> for GasCosts {
fn from(i: GasCostsValues) -> Self {
Self(Arc::new(i))
}
}
impl From<GasCosts> for GasCostsValues {
fn from(i: GasCosts) -> Self {
(*i.0).clone()
}
}