FuelLabs/fuel-core · error · anyhow::Error

Contract code should not exist

Error message

Contract code should not exist

What it means

Duplicate guard for ContractsRawCode during on-chain genesis import: replace(&contract_id, contract) returned Some, meaning bytecode for this contract id was already inserted in the same genesis batch or already exists in the database.

Source

Thrown at crates/fuel-core/src/service/genesis/importer/on_chain.rs:305

    }

    Ok(())
}

fn init_contract_raw_code(
    transaction: &mut StorageTransaction<&mut GenesisDatabase>,
    entry: &TableEntry<ContractsRawCode>,
) -> anyhow::Result<()> {
    let contract = entry.value.as_ref();
    let contract_id = entry.key;

    // insert contract code
    if transaction
        .storage::<ContractsRawCode>()
        .replace(&contract_id, contract)?
        .is_some()
    {
        return Err(anyhow!("Contract code should not exist"));
    }

    Ok(())
}

fn init_da_message(
    transaction: &mut StorageTransaction<&mut GenesisDatabase>,
    msg: TableEntry<Messages>,
    da_height: DaBlockHeight,
) -> anyhow::Result<()> {
    let message: Message = msg.value;

    if message.da_height() > da_height {
        return Err(anyhow!(
            "message da_height cannot be greater than genesis da block height"
        ));
    }

View on GitHub (pinned to b9d4d170da)

Solutions

  1. Dedupe contracts_raw_code entries by contract id in the genesis state and re-import.
  2. Wipe the on-chain db directory and retry.
  3. Regenerate the snapshot with tooling that emits each contract exactly once.

Example fix

// before: duplicate contract entries
"contracts": [
  { "key": "0xcafe...", "value": "0x60016000" },
  { "key": "0xcafe...", "value": "0x60016000" }
]

// after: one entry per contract id
"contracts": [ { "key": "0xcafe...", "value": "0x60016000" } ]
Defensive patterns

Strategy: validation

Validate before calling

fn validate_contract_code_uniqueness(
    entries: &[TableEntry<ContractsRawCode>],
) -> Result<(), String> {
    let mut seen = std::collections::HashSet::new();
    for e in entries {
        if !seen.insert(e.key) {
            return Err(format!("duplicate ContractsRawCode entry for contract {}", e.key));
        }
    }
    Ok(())
}

Try / catch

match importer.run().await {
    Err(e) if e.to_string().contains("Contract code should not exist") => {
        // dedupe contracts_raw_code by contract id and re-import
    }
    rest => rest,
}

Prevention

When it happens

Trigger: The genesis state lists the same contract id twice under contracts_raw_code, or the importer targets storage that already holds that contract code.

Common situations: Snapshot merging or generation bugs producing repeated contract entries; dirty db from a previous import run; test fixtures that add the same contract twice.

Related errors


AI-assisted analysis of FuelLabs/fuel-core@b9d4d170da (2026-08-16). Data as JSON: /api/errors/0be608faf9fbb6bf. Report an issue: GitHub.