Skip to content

Commit

Permalink
Merge pull request #867 from Chia-Network/remove-allow-backrefs
Browse files Browse the repository at this point in the history
remove `ALLOW_BACKREFS` flag, as it's always enabled now
  • Loading branch information
arvidn authored Jan 13, 2025
2 parents 10bc9f2 + f432c60 commit 435fece
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 110 deletions.
6 changes: 3 additions & 3 deletions crates/chia-consensus/benches/run-generator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chia_bls::Signature;
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::additions_and_removals::additions_and_removals;
use chia_consensus::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE};
use chia_consensus::gen::flags::DONT_VALIDATE_SIGNATURE;
use chia_consensus::gen::run_block_generator::{run_block_generator, run_block_generator2};
use clvmr::serde::{node_from_bytes, node_to_bytes_backrefs};
use clvmr::Allocator;
Expand Down Expand Up @@ -57,7 +57,7 @@ fn run(c: &mut Criterion) {
gen,
&block_refs,
11_000_000_000,
ALLOW_BACKREFS | DONT_VALIDATE_SIGNATURE,
DONT_VALIDATE_SIGNATURE,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand All @@ -77,7 +77,7 @@ fn run(c: &mut Criterion) {
gen,
&block_refs,
11_000_000_000,
ALLOW_BACKREFS,
0,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use chia_bls::Signature;
use chia_consensus::allocator::make_allocator;
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::additions_and_removals::additions_and_removals;
use chia_consensus::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE};
use chia_consensus::gen::flags::DONT_VALIDATE_SIGNATURE;
use chia_consensus::gen::run_block_generator::run_block_generator2;
use chia_protocol::{Bytes, Coin};
use libfuzzer_sys::fuzz_target;
Expand All @@ -12,15 +12,15 @@ use std::collections::HashSet;
fuzz_target!(|data: &[u8]| {
// additions_and_removals only work on trusted blocks, so if
// run_block_generator2() fails, we can call additions_and_removals() on it.
let results = additions_and_removals::<&[u8], _>(data, [], ALLOW_BACKREFS, &TEST_CONSTANTS);
let results = additions_and_removals::<&[u8], _>(data, [], 0, &TEST_CONSTANTS);

let mut a1 = make_allocator(0);
let Ok(r1) = run_block_generator2::<&[u8], _>(
&mut a1,
data,
[],
110_000_000,
ALLOW_BACKREFS | DONT_VALIDATE_SIGNATURE,
DONT_VALIDATE_SIGNATURE,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand Down
5 changes: 2 additions & 3 deletions crates/chia-consensus/fuzz/fuzz_targets/run-generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use chia_bls::Signature;
use chia_consensus::allocator::make_allocator;
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::flags::ALLOW_BACKREFS;
use chia_consensus::gen::run_block_generator::{run_block_generator, run_block_generator2};
use chia_consensus::gen::validation_error::{ErrorCode, ValidationErr};
use clvmr::chia_dialect::LIMIT_HEAP;
Expand All @@ -15,7 +14,7 @@ fuzz_target!(|data: &[u8]| {
data,
[],
110_000_000,
ALLOW_BACKREFS,
0,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand All @@ -28,7 +27,7 @@ fuzz_target!(|data: &[u8]| {
data,
[],
110_000_000,
ALLOW_BACKREFS,
0,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand Down
6 changes: 3 additions & 3 deletions crates/chia-consensus/src/gen/additions_and_removals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ where
mod test {
use super::*;
use crate::consensus_constants::TEST_CONSTANTS;
use crate::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE};
use crate::gen::flags::DONT_VALIDATE_SIGNATURE;
use crate::gen::run_block_generator::run_block_generator2;
use chia_bls::Signature;
use rstest::rstest;
Expand Down Expand Up @@ -180,7 +180,7 @@ mod test {
&generator,
&block_refs,
11_000_000_000,
ALLOW_BACKREFS | DONT_VALIDATE_SIGNATURE,
DONT_VALIDATE_SIGNATURE,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand Down Expand Up @@ -216,7 +216,7 @@ mod test {

// now run the function under test
let (additions, removals) =
additions_and_removals(&generator, &block_refs, ALLOW_BACKREFS, &TEST_CONSTANTS)
additions_and_removals(&generator, &block_refs, 0, &TEST_CONSTANTS)
.expect("additions_and_removals()");

assert_eq!(expect_additions.len(), additions.len());
Expand Down
4 changes: 0 additions & 4 deletions crates/chia-consensus/src/gen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ pub const NO_UNKNOWN_CONDS: u32 = 0x2_0000;
// currently supported for those conditions. This is meant for mempool-mode
pub const STRICT_ARGS_COUNT: u32 = 0x8_0000;

// when this flag is set, the block generator serialization is allowed to
// contain back-references
pub const ALLOW_BACKREFS: u32 = 0x0200_0000;

// By default, run_block_generator validates the signatures of any AGG_SIG
// condition. By passing in this flag, the signatures are not validated (saving
// time). This is useful when we've already validated a block but we need to
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/gen/get_puzzle_and_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn get_puzzle_and_solution_for_coin(
mod test {
use super::*;
use crate::consensus_constants::TEST_CONSTANTS;
use crate::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE, MEMPOOL_MODE};
use crate::gen::flags::{DONT_VALIDATE_SIGNATURE, MEMPOOL_MODE};
use crate::gen::make_aggsig_final_message::u64_to_bytes;
use crate::gen::run_block_generator::{run_block_generator2, setup_generator_args};
use chia_bls::Signature;
Expand Down Expand Up @@ -240,7 +240,7 @@ mod test {
&generator,
blocks,
MAX_COST,
ALLOW_BACKREFS | MEMPOOL_MODE | DONT_VALIDATE_SIGNATURE,
MEMPOOL_MODE | DONT_VALIDATE_SIGNATURE,
&Signature::default(),
None,
&TEST_CONSTANTS,
Expand Down
16 changes: 4 additions & 12 deletions crates/chia-consensus/src/gen/run_block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::gen::conditions::{
parse_spends, process_single_spend, validate_conditions, validate_signature, EmptyVisitor,
ParseState, SpendBundleConditions,
};
use crate::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE};
use crate::gen::flags::DONT_VALIDATE_SIGNATURE;
use crate::gen::validation_error::{first, ErrorCode, ValidationErr};
use crate::generator_rom::{CLVM_DESERIALIZER, GENERATOR_ROM};
use chia_bls::{BlsCache, Signature};
Expand All @@ -14,7 +14,7 @@ use clvmr::cost::Cost;
use clvmr::reduction::Reduction;
use clvmr::run_program::run_program;
use clvmr::serde::{node_from_bytes, node_from_bytes_backrefs, node_from_bytes_backrefs_record};
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;

pub fn subtract_cost(
a: &Allocator,
Expand Down Expand Up @@ -88,11 +88,7 @@ where
subtract_cost(a, &mut cost_left, byte_cost)?;

let generator_rom = node_from_bytes(a, &GENERATOR_ROM)?;
let program = if (flags & ALLOW_BACKREFS) != 0 {
node_from_bytes_backrefs(a, program)?
} else {
node_from_bytes(a, program)?
};
let program = node_from_bytes_backrefs(a, program)?;

// this is setting up the arguments to be passed to the generator ROM,
// not the actual generator (the ROM does that).
Expand Down Expand Up @@ -178,11 +174,7 @@ where
let mut cost_left = max_cost;
subtract_cost(a, &mut cost_left, byte_cost)?;

let (program, backrefs) = if (flags & ALLOW_BACKREFS) != 0 {
node_from_bytes_backrefs_record(a, program)?
} else {
(node_from_bytes(a, program)?, HashSet::<NodePtr>::new())
};
let (program, backrefs) = node_from_bytes_backrefs_record(a, program)?;

let args = setup_generator_args(a, block_refs)?;
let dialect = ChiaDialect::new(flags);
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/gen/test_generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::conditions::{NewCoin, SpendBundleConditions, SpendConditions};
use super::run_block_generator::{run_block_generator, run_block_generator2};
use crate::allocator::make_allocator;
use crate::consensus_constants::TEST_CONSTANTS;
use crate::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE, MEMPOOL_MODE};
use crate::gen::flags::{DONT_VALIDATE_SIGNATURE, MEMPOOL_MODE};
use chia_bls::Signature;
use chia_protocol::{Bytes, Bytes48};
use clvmr::allocator::NodePtr;
Expand Down Expand Up @@ -228,7 +228,7 @@ fn run_generator(#[case] name: &str) {
block_refs.push(hex::decode(env_hex).expect("hex decode env-file"));
}

const DEFAULT_FLAGS: u32 = ALLOW_BACKREFS;
const DEFAULT_FLAGS: u32 = 0;
for (flags, expected) in zip(&[DEFAULT_FLAGS, DEFAULT_FLAGS | MEMPOOL_MODE], expected) {
println!("flags: {flags:x}");
let mut a = make_allocator(*flags);
Expand Down
5 changes: 1 addition & 4 deletions crates/chia-consensus/src/spendbundle_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ mod tests {
}

#[cfg(not(debug_assertions))]
use crate::gen::flags::ALLOW_BACKREFS;

#[cfg(not(debug_assertions))]
const DEFAULT_FLAGS: u32 = ALLOW_BACKREFS | MEMPOOL_MODE;
const DEFAULT_FLAGS: u32 = MEMPOOL_MODE;

// given a block generator and block-refs, convert run the generator to
// produce the SpendBundle for the block without runningi, or validating,
Expand Down
36 changes: 16 additions & 20 deletions crates/chia-consensus/src/spendbundle_validation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::allocator::make_allocator;
use crate::consensus_constants::ConsensusConstants;
use crate::gen::flags::ALLOW_BACKREFS;
use crate::gen::owned_conditions::OwnedSpendBundleConditions;
use crate::gen::validation_error::ErrorCode;
use crate::spendbundle_conditions::run_spendbundle;
Expand Down Expand Up @@ -62,23 +61,20 @@ pub fn validate_clvm_and_signature(
pub fn get_flags_for_height_and_constants(height: u32, constants: &ConsensusConstants) -> u32 {
let mut flags: u32 = 0;

if height >= constants.hard_fork_height {
// the hard-fork initiated with 2.0. To activate June 2024
// * costs are ascribed to some unknown condition codes, to allow for
// soft-forking in new conditions with cost
// * a new condition, SOFTFORK, is added which takes a first parameter to
// specify its cost. This allows soft-forks similar to the softfork
// operator
// * BLS operators introduced in the soft-fork (behind the softfork
// guard) are made available outside of the guard.
// * division with negative numbers are allowed, and round toward
// negative infinity
// * AGG_SIG_* conditions are allowed to have unknown additional
// arguments
// * Allow the block generator to be serialized with the improved clvm
// serialization format (with back-references)
flags |= ALLOW_BACKREFS;
}
// the hard-fork initiated with 2.0. To activate June 2024
// * costs are ascribed to some unknown condition codes, to allow for
// soft-forking in new conditions with cost
// * a new condition, SOFTFORK, is added which takes a first parameter to
// specify its cost. This allows soft-forks similar to the softfork
// operator
// * BLS operators introduced in the soft-fork (behind the softfork
// guard) are made available outside of the guard.
// * division with negative numbers are allowed, and round toward
// negative infinity
// * AGG_SIG_* conditions are allowed to have unknown additional
// arguments
// * Allow the block generator to be serialized with the improved clvm
// serialization format (with back-references)

// The soft fork initiated with 2.5.0. The activation date is still TBD.
// Adds a new keccak256 operator under the softfork guard with extension 1.
Expand All @@ -105,8 +101,8 @@ mod tests {

#[rstest]
#[case(0, 0)]
#[case(TEST_CONSTANTS.hard_fork_height, ALLOW_BACKREFS)]
#[case(5_716_000, ALLOW_BACKREFS)]
#[case(TEST_CONSTANTS.hard_fork_height, 0)]
#[case(5_716_000, 0)]
fn test_get_flags(#[case] height: u32, #[case] expected_value: u32) {
assert_eq!(
get_flags_for_height_and_constants(height, &TEST_CONSTANTS),
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-tools/src/bin/analyze-chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::Write;
use std::time::SystemTime;

use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::flags::{ALLOW_BACKREFS, MEMPOOL_MODE};
use chia_consensus::gen::flags::MEMPOOL_MODE;
use chia_consensus::gen::run_block_generator::{run_block_generator, run_block_generator2};
use chia_tools::iterate_blocks;
use clvmr::Allocator;
Expand Down Expand Up @@ -33,7 +33,7 @@ struct Args {
fn main() {
let args = Args::parse();

let flags = if args.mempool_mode { MEMPOOL_MODE } else { 0 } | ALLOW_BACKREFS;
let flags = if args.mempool_mode { MEMPOOL_MODE } else { 0 };

let mut output =
std::fs::File::create("chain-resource-usage.log").expect("failed to open output file");
Expand Down
7 changes: 1 addition & 6 deletions crates/chia-tools/src/bin/test-block-generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use chia_bls::PublicKey;
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::conditions::{NewCoin, SpendBundleConditions, SpendConditions};
use chia_consensus::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE, MEMPOOL_MODE};
use chia_consensus::gen::flags::{DONT_VALIDATE_SIGNATURE, MEMPOOL_MODE};
use chia_consensus::gen::run_block_generator::{run_block_generator, run_block_generator2};
use chia_tools::iterate_blocks;
use clvmr::allocator::NodePtr;
Expand Down Expand Up @@ -190,11 +190,6 @@ fn main() {
run_block_generator
};
let flags = flags
| if height >= args.hard_fork_height {
ALLOW_BACKREFS
} else {
0
}
| if args.skip_signature_validation {
DONT_VALIDATE_SIGNATURE
} else {
Expand Down
5 changes: 2 additions & 3 deletions crates/chia-tools/src/bin/validate-blockchain-db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;

use chia_consensus::consensus_constants::ConsensusConstants;
use chia_consensus::consensus_constants::TEST_CONSTANTS;
use chia_consensus::gen::flags::{ALLOW_BACKREFS, DONT_VALIDATE_SIGNATURE};
use chia_consensus::gen::flags::DONT_VALIDATE_SIGNATURE;
use chia_consensus::gen::run_block_generator::{run_block_generator, run_block_generator2};
use chia_protocol::{Bytes32, Coin};
use chia_tools::iterate_blocks;
Expand Down Expand Up @@ -294,8 +294,7 @@ features that are validated:
} else {
run_block_generator
};
let flags = ALLOW_BACKREFS
| if args.skip_signature_validation {
let flags = if args.skip_signature_validation {
DONT_VALIDATE_SIGNATURE
} else {
0
Expand Down
9 changes: 4 additions & 5 deletions tests/test-generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from run_gen import run_gen, print_spend_bundle_conditions
from chia_rs import (
MEMPOOL_MODE,
ALLOW_BACKREFS,
SpendBundleConditions,
)
from dataclasses import dataclass
Expand Down Expand Up @@ -65,15 +64,15 @@ def validate_except_cost(output1: str, output2: str):
stdout.flush()
consensus = run_generator(
g,
ALLOW_BACKREFS,
0,
version=1,
)

stdout.write(f"{name} running generator2...\r")
stdout.flush()
consensus2 = run_generator(
g,
ALLOW_BACKREFS,
0,
version=2,
)
validate_except_cost(consensus.output, consensus2.output)
Expand All @@ -82,15 +81,15 @@ def validate_except_cost(output1: str, output2: str):
stdout.flush()
mempool = run_generator(
g,
ALLOW_BACKREFS | MEMPOOL_MODE,
MEMPOOL_MODE,
version=1,
)

stdout.write(f"{name} running generator2 (mempool mode)...\r")
stdout.flush()
mempool2 = run_generator(
g,
ALLOW_BACKREFS | MEMPOOL_MODE,
MEMPOOL_MODE,
version=2,
)
validate_except_cost(mempool.output, mempool2.output)
Expand Down
7 changes: 2 additions & 5 deletions tests/test_additions_and_removals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing import Optional
from chia_rs import (
additions_and_removals,
ALLOW_BACKREFS,
)
from chia_rs import additions_and_removals
from run_gen import DEFAULT_CONSTANTS
from pathlib import Path
import glob
Expand Down Expand Up @@ -31,7 +28,7 @@ def test_additions_and_removals() -> None:

try:
additions, removals = additions_and_removals(
generator, block_refs, ALLOW_BACKREFS, DEFAULT_CONSTANTS
generator, block_refs, 0, DEFAULT_CONSTANTS
)

# if this was an invalid block, it wasn't OK to pass it to
Expand Down
Loading

0 comments on commit 435fece

Please sign in to comment.