Skip to content

Commit

Permalink
update costs of unknown conditions, part of the hard fork
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jun 20, 2023
1 parent b47d922 commit 9aed943
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
44 changes: 24 additions & 20 deletions src/gen/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub fn parse_args(
}
}
}
256..=511 => {
256..=65535 => {
// All of these conditions are unknown
// but they have costs (when ENABLE_SOFTFORK_CONDITION is enabled)
if (flags & NO_UNKNOWN_CONDS) != 0 {
Expand Down Expand Up @@ -3831,25 +3831,29 @@ fn test_relative_condition_on_ephemeral(
// additional arguments are ignored
#[case("((90 (1 ( 42 ( 1337 )", 10000)]
// reserved opcodes with fixed cost
#[case("((256 )", 1000000)]
#[case("((257 )", 1200000)]
#[case("((258 )", 1400000)]
#[case("((259 )", 1600000)]
#[case("((260 )", 1800000)]
#[case("((261 )", 2000000)]
#[case("((262 )", 2200000)]
#[case("((263 )", 2400000)]
#[case("((264 )", 1000000)]
#[case("((265 )", 1200000)]
#[case("((266 )", 1400000)]
#[case("((504 )", 1000000)]
#[case("((505 )", 1200000)]
#[case("((506 )", 1400000)]
#[case("((507 )", 1600000)]
#[case("((508 )", 1800000)]
#[case("((509 )", 2000000)]
#[case("((510 )", 2200000)]
#[case("((511 )", 2400000)]
#[case("((256 )", 100)]
#[case("((257 )", 106)]
#[case("((258 )", 112)]
#[case("((259 )", 119)]
#[case("((260 )", 127)]
#[case("((261 )", 135)]
#[case("((262 )", 143)]
#[case("((263 )", 152)]
#[case("((264 )", 162)]
#[case("((265 )", 172)]
#[case("((266 )", 183)]
#[case("((504 )", 338000000)]
#[case("((505 )", 359000000)]
#[case("((506 )", 382000000)]
#[case("((507 )", 406000000)]
#[case("((508 )", 431000000)]
#[case("((509 )", 458000000)]
#[case("((510 )", 487000000)]
#[case("((511 )", 517000000)]
#[case("((512 )", 100)]
#[case("((513 )", 106)]
#[case("((0xff00 )", 100)]
#[case("((0xff01 )", 106)]
fn test_softfork_condition(#[case] conditions: &str, #[case] expected_cost: Cost) {
// SOFTFORK (90)
let (_, spends) = cond_test_flag(
Expand Down
41 changes: 33 additions & 8 deletions src/gen/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,39 @@ pub const CREATE_COIN_COST: Cost = 1800000;
pub const AGG_SIG_COST: Cost = 1200000;

// when ENABLE_SOFTFORK_CONDITION is enabled
// 2-byte condition opcodes whose first byte is 1 have costs, despite being unknown:
// 2-byte condition opcodes have costs according to this table:

// cost = 1000000 + (byte[1] & 0x7) * 200000
const COSTS: [Cost; 256] = [
100, 106, 112, 119, 127, 135, 143, 152, 162, 172, 183, 194, 206, 219, 233, 248, 263, 280, 297,
316, 336, 357, 379, 403, 428, 455, 483, 513, 546, 580, 616, 654, 695, 739, 785, 834, 886, 942,
1000, 1060, 1130, 1200, 1270, 1350, 1440, 1530, 1620, 1720, 1830, 1950, 2070, 2200, 2330, 2480,
2640, 2800, 2980, 3160, 3360, 3570, 3790, 4030, 4280, 4550, 4840, 5140, 5460, 5800, 6170, 6550,
6960, 7400, 7860, 8350, 8870, 9430, 10000, 10600, 11300, 12000, 12700, 13500, 14400, 15300,
16200, 17200, 18300, 19500, 20700, 22000, 23400, 24800, 26400, 28000, 29800, 31700, 33600,
35800, 38000, 40400, 42900, 45600, 48400, 51500, 54700, 58100, 61700, 65600, 69700, 74100,
78700, 83600, 88800, 94400, 100000, 106000, 113000, 120000, 127000, 135000, 144000, 153000,
162000, 173000, 183000, 195000, 207000, 220000, 234000, 249000, 264000, 281000, 298000, 317000,
337000, 358000, 380000, 404000, 429000, 456000, 485000, 515000, 547000, 582000, 618000, 657000,
698000, 741000, 788000, 837000, 889000, 945000, 1000000, 1060000, 1130000, 1200000, 1280000,
1360000, 1440000, 1530000, 1630000, 1730000, 1840000, 1950000, 2070000, 2200000, 2340000,
2490000, 2650000, 2810000, 2990000, 3170000, 3370000, 3580000, 3810000, 4050000, 4300000,
4570000, 4850000, 5160000, 5480000, 5820000, 6190000, 6570000, 6990000, 7420000, 7890000,
8380000, 8900000, 9460000, 10000000, 10600000, 11300000, 12000000, 12800000, 13600000,
14400000, 15300000, 16300000, 17300000, 18400000, 19500000, 20800000, 22100000, 23500000,
24900000, 26500000, 28100000, 29900000, 31800000, 33800000, 35900000, 38100000, 40500000,
43000000, 45700000, 48600000, 51600000, 54900000, 58300000, 61900000, 65800000, 69900000,
74300000, 79000000, 83900000, 89100000, 94700000, 100000000, 106000000, 113000000, 120000000,
128000000, 136000000, 144000000, 153000000, 163000000, 173000000, 184000000, 196000000,
208000000, 221000000, 235000000, 249000000, 265000000, 282000000, 299000000, 318000000,
338000000, 359000000, 382000000, 406000000, 431000000, 458000000, 487000000, 517000000,
];

pub fn compute_unknown_condition_cost(op: ConditionOpcode) -> Cost {
match op {
256..=511 => 1000000 + ((op & 0x7) as Cost) * 200000,
_ => 0,
if op < 256 {
println!("op: {op}");
0
} else {
COSTS[(op & 0xff) as usize]
}
}

Expand All @@ -78,8 +103,8 @@ pub fn parse_opcode(a: &Allocator, op: NodePtr, flags: u32) -> Option<ConditionO
_ => return None,
};
if buf.len() == 2 && (flags & ENABLE_SOFTFORK_CONDITION) != 0 {
let b0 = buf[0];
if b0 != 1 {
if buf[0] == 0 {
// no redundant leading zeroes
None
} else {
// These are 2-byte condition codes whose first byte is 1
Expand Down Expand Up @@ -143,7 +168,7 @@ use rstest::rstest;
#[cfg(test)]
#[rstest]
// leading zeros are not allowed, it makes it a different value
#[case(&[ASSERT_HEIGHT_ABSOLUTE as u8, 0], None, None)]
#[case(&[ASSERT_HEIGHT_ABSOLUTE as u8, 0, 0], None, None)]
#[case(&[0, ASSERT_HEIGHT_ABSOLUTE as u8], None, None)]
#[case(&[0], None, None)]
// all condition codes
Expand Down

0 comments on commit 9aed943

Please sign in to comment.