keep-starknet-strange / raito

Bitcoin ZK client written in Cairo.
https://raito.wtf
MIT License
40 stars 34 forks source link

[feat] implement block_hash function #70

Closed Gerson2102 closed 2 months ago

Gerson2102 commented 2 months ago

Draft PR with the initialization of the block_hash function.

vercel[bot] commented 2 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
raito ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 27, 2024 4:23pm
TAdev0 commented 2 months ago

hi @Gerson2102 , whats up with your PR? I can see tests are failing because of imports issues and unused variables, can you solve this?

Gerson2102 commented 2 months ago

hi @Gerson2102 , whats up with your PR? I can see tests are failing because of imports issues and unused variables, can you solve this?

I added the chain state in the block_filter.jq script. With that I think I can get the block_hash_expected to test the code that I wrote.

Gerson2102 commented 2 months ago

This is my test:

#[test]
    fn test_block_hash() {
        let mut chain_state = ChainState {
            block_height: Option::Some(1),
            total_work: 1,
            best_block_hash: 1_u256.into(),
            current_target: 1,
            epoch_start_time: 1,
            prev_timestamps: array![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].span(),
            utreexo_state: UtreexoState { roots: array![].span() },
        };
        let mut block = Block {
            header: Header { version: 1, time: 12, nonce: 1, bits: 1 },
            txs: ArrayTrait::new().span(),
        };

        let mut txids: Array<Hash> = array![
            0xacd9825be8bece7782ec746a80b52f44d6a8af41c63dbab59b03e29558469682_u256.into(),
        ];
        let merkle_root = merkle_root(ref txids);

        let block_hash_expected = 0;

        let block_hash_result = block_hash(@chain_state, @block, merkle_root);

        assert_eq!(block_hash_result, block_hash_expected);
    }

I dont know how to get the expected block hash. I know this resource: https://learnmeabitcoin.com/technical/block/hash/ But not helpful, cause i need the hash of a header to get the block hash, and i dont know how to hash the header.

Gerson2102 commented 2 months ago

I was trying to print just the hash from the header, this is what I get: HASH RESULT TESTS: Result::Ok(Hash { value: [1843744656, 1543879891, 2943381699, 2643075783, 4148049649, 2232427024, 260566661, 435529794] }) I thought that i can get something like this: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

And I'm getting that value by just doing this:

fn block_hash(self: @ChainState, block: @Block, merkle_root: Hash) -> Result<Hash, ByteArray> {
    let header = block.header;
    let mut header_data = ArrayTrait::<u32>::new();
    header_data.append(*header.version);
    header_data.append(*header.time);
    header_data.append(*header.bits);
    header_data.append(*header.nonce);
    // header_data.append_span(merkle_root.value.span());
    // header_data.append_span(self.best_block_hash.value.span());

    let mut hashed_header_data = compute_sha256_u32_array(
        compute_sha256_u32_array(header_data, 0, 0).span().into(), 0, 0
    );

    Result::Ok(HashTrait::to_hash(hashed_header_data))
}
Gerson2102 commented 2 months ago

This is the error:

running 1 test
MERKLE: Hash { value: [2899935835, 3904818807, 2196534378, 2159357764, 3601379137, 3325934261, 2600723093, 1481021058] }
test raito::validation::tests::test_block_hash ... fail (gas usage est.: 7533860)
failures:
   raito::validation::tests::test_block_hash - Panicked with "assertion `block_hash_result == block_hash_expected` failed.
block_hash_result: Hash { value: [3286434916, 1508717724, 893657502, 2701536361, 1780252504, 1390839551, 402763117, 2437899237] }
block_hash_expected: Hash { value: [3305235487, 2575145300, 2800175491, 877328424, 2467168098, 1544803334, 1584306281, 243591140] }".

I dont know if I did something wrong while constructing the header and then the block in this site: https://learnmeabitcoin.com/technical/block/hash/

Jeanmichel7 commented 2 months ago

Waiting #90 to pass tests on blocks 0 and 170, currently bad transaction txid

m-kus commented 2 months ago

Let's minimise the scope here to complete the task:

  1. Please revert your latest commit and then rebase your branch on top of main: there were some errors during the merge, current diff does not make much sense
  2. Remove all the chain state related stuff in the jq filter
  3. Assume that you already have computed merkle root and write a unit test based on some real block data

Please note that merkle roots and block hashes obtained through RPC have to be reversed before you put them into from_hex in your tests.

Gerson2102 commented 2 months ago

Let's minimise the scope here to complete the task:

  1. Please revert your latest commit and then rebase your branch on top of main: there were some errors during the merge, current diff does not make much sense
  2. Remove all the chain state related stuff in the jq filter
  3. Assume that you already have computed merkle root and write a unit test based on some real block data

Please note that merkle roots and block hashes obtained through RPC have to be reversed before you put them into from_hex in your tests.

So the current test does not work for the point 3?

Jeanmichel7 commented 2 months ago

The current test simulates block 170, perhaps you should add tests on other blocks. Right now, the test probably won't work, as I need to finish my PR #103 first.

I would have liked to see your PR finalized to adjust my PR