Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mine_event difficulty is off #9

Closed
plebhash opened this issue Sep 18, 2024 · 1 comment
Closed

mine_event difficulty is off #9

plebhash opened this issue Sep 18, 2024 · 1 comment

Comments

@plebhash
Copy link

plebhash commented Sep 18, 2024

I'm doing some experiments with the code. In my local version, fn mine_event() returns a MinedResult.

Here's a broken test that makes me wonder if there's something wrong with mine_event function:

#[test]
    fn test_mine_event() {
        let event = NostrEvent {
            pubkey: "e771af0b05c8e95fcdf6feb3500544d2fb1ccd384788e9f490bb3ee28e8ed66f".to_string(),
            kind: 1,
            content: "hello worlda".to_string(),
            tags: vec![],
            id: None,
            created_at: Some(1668680774),
        };

        let event_json = to_string(&event).unwrap();

        let difficulty = 10;
        let mined_result = mine_event(&event_json, difficulty, 0, 1); // I modified the return type to MinedResult

        assert_eq!(mined_result.event.pubkey, event.pubkey);
        assert_eq!(mined_result.event.kind, event.kind);
        assert_eq!(mined_result.event.content, event.content);
        assert_eq!(mined_result.event.created_at, event.created_at);

        assert_eq!(mined_result.event.tags[0][0], "nonce");
        assert_eq!(mined_result.event.tags[0][2], difficulty.to_string());

        let id = mined_result.event.id.expect("expect mined_result.event.id is Some");
        let id_bytes = Vec::from_hex(id).expect("expect valid sha256 as hex");

        assert_eq!(get_pow(&id_bytes), difficulty); // panic here
    }

and the test logs:

test tests::test_mine_event ... FAILED

failures:

---- tests::test_mine_event stdout ----
thread 'tests::test_mine_event' panicked at src/lib.rs:215:9:
assertion `left == right` failed
  left: 11
 right: 10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@plebhash plebhash changed the title mine_event difficulty is off-by-one mine_event difficulty is off Sep 18, 2024
@plebhash
Copy link
Author

plebhash commented Sep 18, 2024

with different difficulties:

---- tests::test_mine_event stdout ----
thread 'tests::test_mine_event' panicked at src/lib.rs:215:9:
assertion `left == right` failed
  left: 23
 right: 21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

So I guess there's no real issue here. This is just a consequence of the statistical nature of PoW.

I just need to change my assertion to:

- assert_eq!(get_pow(&id_bytes), difficulty); // panic here
+ assert!(get_pow(&id_bytes) >= difficulty); // always correct

dskvr added a commit that referenced this issue Oct 28, 2024
# This is the 1st commit message:

monorepo

# The commit message #2 will be skipped:

# rearrange and break everything

# The commit message #3 will be skipped:

# stuff

# The commit message #4 will be skipped:

# add svelte package/demo

# The commit message #5 will be skipped:

# add svelte package/demo

# The commit message #6 will be skipped:

# remove yarn lock

# The commit message #7 will be skipped:

# update README add reactjs package

# The commit message #8 will be skipped:

# update wrapper

# The commit message #9 will be skipped:

# update svelte demo

# The commit message #10 will be skipped:

# remove accidental node_modules and add build scripts

# The commit message #11 will be skipped:

# move core to rust and update scripts so whenever rust is built is copies to packages/core

# The commit message #12 will be skipped:

# remove demo from @notemine/wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant