Skip to content

[Bug] Incorrect events indexed from an anvil instance which are created via forge script  #1124

Closed as not planned

Description

@mesozoic-technology

Component

node-bindings, rpc, other

What version of Alloy are you on?

├── alloy v0.1.4 │ ├── alloy-core v0.7.7 │ │ ├── alloy-primitives v0.7.7 │ │ │ ├── alloy-rlp v0.3.7 │ │ │ │ ├── alloy-rlp-derive v0.3.7 (proc-macro) │ │ │ │ ├── alloy-rlp v0.3.7 () │ │ └── alloy-sol-types v0.7.7 │ │ ├── alloy-primitives v0.7.7 () │ │ ├── alloy-sol-macro v0.7.7 (proc-macro) │ │ │ ├── alloy-sol-macro-expander v0.7.7 │ │ │ │ ├── alloy-sol-macro-input v0.7.7 │ │ │ ├── alloy-sol-macro-input v0.7.7 () │ ├── alloy-eips v0.1.4 │ │ ├── alloy-primitives v0.7.7 () │ │ ├── alloy-rlp v0.3.7 () │ │ ├── alloy-serde v0.1.4 │ │ │ ├── alloy-primitives v0.7.7 () │ ├── alloy-node-bindings v0.1.4 │ │ ├── alloy-genesis v0.1.4 │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ ├── alloy-serde v0.1.4 () │ │ ├── alloy-primitives v0.7.7 () │ ├── alloy-provider v0.1.4 │ │ ├── alloy-chains v0.1.23 │ │ ├── alloy-consensus v0.1.4 │ │ │ ├── alloy-eips v0.1.4 () │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ ├── alloy-rlp v0.3.7 () │ │ │ ├── alloy-serde v0.1.4 () │ │ ├── alloy-eips v0.1.4 () │ │ ├── alloy-json-rpc v0.1.4 │ │ │ ├── alloy-primitives v0.7.7 () │ │ ├── alloy-network v0.1.4 │ │ │ ├── alloy-consensus v0.1.4 () │ │ │ ├── alloy-eips v0.1.4 () │ │ │ ├── alloy-json-rpc v0.1.4 () │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ ├── alloy-rpc-types-eth v0.1.4 │ │ │ │ ├── alloy-consensus v0.1.4 () │ │ │ │ ├── alloy-eips v0.1.4 () │ │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ │ ├── alloy-rlp v0.3.7 () │ │ │ │ ├── alloy-serde v0.1.4 () │ │ │ │ ├── alloy-sol-types v0.7.7 () │ │ │ ├── alloy-serde v0.1.4 () │ │ │ ├── alloy-signer v0.1.4 │ │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ ├── alloy-sol-types v0.7.7 () │ │ ├── alloy-node-bindings v0.1.4 () │ │ ├── alloy-primitives v0.7.7 () │ │ ├── alloy-rpc-client v0.1.4 │ │ │ ├── alloy-json-rpc v0.1.4 () │ │ │ ├── alloy-transport v0.1.4 │ │ │ │ ├── alloy-json-rpc v0.1.4 () │ │ │ ├── alloy-transport-http v0.1.4 │ │ │ │ ├── alloy-json-rpc v0.1.4 () │ │ │ │ ├── alloy-transport v0.1.4 () │ │ ├── alloy-rpc-types-anvil v0.1.4 │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ ├── alloy-serde v0.1.4 () │ │ ├── alloy-rpc-types-eth v0.1.4 () │ │ ├── alloy-signer-local v0.1.4 │ │ │ ├── alloy-consensus v0.1.4 () │ │ │ ├── alloy-network v0.1.4 () │ │ │ ├── alloy-primitives v0.7.7 () │ │ │ ├── alloy-signer v0.1.4 () │ │ ├── alloy-transport v0.1.4 () │ │ ├── alloy-transport-http v0.1.4 () │ ├── alloy-rpc-client v0.1.4 () │ └── alloy-rpc-types v0.1.4 │ ├── alloy-rpc-types-eth v0.1.4 () │ └── alloy-serde v0.1.4 () ├── alloy-sol-types v0.7.7 (*)

Operating System

macOS (Apple Silicon)

Describe the bug

I have a rust script that starts up an anvil instance.

I then use let mut output = std::process::Command::new("forge").... to run a forge script against this anvil instance, giving it the option -vvvv, which prints the script's call trace out.

Examining this calltrace, I see the events I expect to be emitted being emitted.

Then I use an alloy provider to index these events from the anvil instance.

However, the events I get are different. Notably in the timestamp that I put in the events.

It seems that the calltrace is reporting something different than is actually happening in the execution in the anvil node.

Here's a truncated version of my script. The forge script basically emits a number of KinoRegistered events.

use alloy::{
    hex,
    node_bindings::Anvil,
    primitives::{keccak256, Address, Log, B256, U256, Bytes},
    providers::{Provider, ProviderBuilder},
    rpc::types::eth::Filter,
    sol_types::{SolEvent, SolType, SolValue},
    sol
};
use regex::Regex;
use rust_decimal::prelude::*;
use rust_decimal::MathematicalOps;
use std::collections::HashMap;
use std::process::Command;
use std::thread;
use std::time::Duration;
use tokio::main;

sol!(
    event KinoRegistered(
        address indexed who, 
        bytes32 indexed node, 
        address indexed delegate, 
        uint256 amount,
        uint256 end,
        bytes32 root
    );
);

#[main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let mut anvil = Anvil::new()
        .block_time(4)
        .try_spawn()?;

    let Ok(mut provider) = ProviderBuilder::new()
        .on_builtin(&anvil.endpoint()).await else { todo!() };

    provider.raw_request(
        std::borrow::Cow::Borrowed("evm_setNextBlockTimestamp"), 
        [2037450657]
    ).await?;

    let blockno = provider.get_block_number().await?;
    let block = provider
        .get_block_by_number(blockno.into(), true)
        .await?;

    let mut output = Command::new("forge")
        .arg("script")
        .arg("script/KinoVotingRegistration.s.sol:KVRTestFixtures")
        .arg("--rpc-url")
        .arg(&anvil.endpoint())
        .arg("--broadcast")
        .arg("-vvvv")
        .current_dir("..")
        .output()
        .expect("failed");

    let mut KVR_ADDRESS = Address::default();

    if output.status.success() {

        let stdout = String::from_utf8(output.stdout.clone()).unwrap();

        println!("success {}", stdout);

        let re = Regex::new(r"kvr: address (0x[a-fA-F0-9]{40})").unwrap();
        
        if let Some(captures) = re.captures(&stdout) {
            KVR_ADDRESS = Address::from_str(&captures[1]).unwrap();
            println!("Extracted KVR_ADDRESS: {}", KVR_ADDRESS);
        } else {
            println!("Failed to extract KVR_ADDRESS");
        }

    } else {
        println!(
            "error {}",
            String::from_utf8(output.stderr).unwrap()
        );
    }

    let registration_filter = Filter::new()
        .address(KVR_ADDRESS)
        .events([KinoRegistered::SIGNATURE]);

    let Ok(registration_events) = provider
        .get_logs(&registration_filter)
        .await else { todo!() };

If you need an executable example, I can work on that when I have a moment.

Activity

  1. added
    bug
    on Aug 4, 2024
  2. mesozoic-technology commented on Aug 5, 2024

    @mesozoic-technology
    Author

    Part of the stack trace compared to part of the printout of the events in rust shows the discrepancy. These should be the same values:

      [173924] VotingRegistration::auctionProposal(0x6c6569626f7769747a, FreeAuction: [0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e])
        ├─ emit AuctionProposed(
        number: 1, 
        auction: FreeAuction: [0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e], 
        label: 0x94c6cc3402136f13f332d24eaf81163179211dec7a4de43b9eb0d6b6470d6be0, 
        tlz: 0x6c6569626f7769747a, 
        end: 2000086400 [2e9], 
        root: 0x419ad8eb8c04f4407ef94d693fb46e27d1292c8683722b196560a259501deaa1)
    
    extracting proposal: 
    1
    0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e
    0x94c6cc3402136f13f332d24eaf81163179211dec7a4de43b9eb0d6b6470d6be0
    0x6c6569626f7769747a
    2000086404
    0x75a0c091f86edf865c72addc4038fed54a3866bb342952a683110011ca567b61
    

    You can see that the "end" and "root" values are different. The "end" value is different by 4, which incidentally is the blocktime Anvil was set to.

  3. added
    needs triage
    and removed on Feb 20, 2025
  4. jenpaff commented on Feb 28, 2025

    @jenpaff
    Contributor

    weren't able to reproduce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions