keep-starknet-strange / raito

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

[feat] Check coinbase maturity #108

Closed m-kus closed 1 month ago

m-kus commented 2 months ago

Extend transaction validation helper with a check that if there are inputs spending previous coinbase ouputs, those outputs are 100 or more blocks old.

You'd also need to add is_coinbase flag to the OutPoint and modify the scripts accordingly.

More info: https://learnmeabitcoin.com/technical/mining/coinbase-transaction/

mubarak23 commented 2 months ago

@m-kus can i work on this, am available pick this right away

onlydustapp[bot] commented 2 months ago

Hey @mubarak23! Thanks for showing interest. We've created an application for you to contribute to Raito - Bitcoin ZK Client. Go check it out on OnlyDust!

mubarak23 commented 2 months ago

@m-kus if is_coinbase flag is added to OutPoint struct, can you point me to the part of the scripts i need to modify

m-kus commented 2 months ago

You need to modify this function https://github.com/keep-starknet-strange/raito/blob/0918b074a2b3b461b03d326b2db1f65a24f31017/scripts/data/get_block.py#L57C5-L57C13

The way you can determine if it's a coinbase transaction is checking for the specific input in the rawtransaction RPC response:

{
  "result": {
    "txid": "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082",
    "hash": "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082",
    "version": 1,
    "size": 134,
    "vsize": 134,
    "weight": 536,
    "locktime": 0,
    "vin": [
      {
        "coinbase": "04ffff001d0102",
        "sequence": 4294967295
      }
    ],
    "vout": [
      {
        "value": 50,
        "n": 0,
        "scriptPubKey": {
          "asm": "04d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725b OP_CHECKSIG",
          "desc": "pk(04d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725b)#pezndevg",
          "hex": "4104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac",
          "type": "pubkey"
        }
      }
    ],
    "hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0102ffffffff0100f2052a01000000434104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac00000000",
    "blockhash": "00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee",
    "confirmations": 859230,
    "time": 1231731025,
    "blocktime": 1231731025
  },
  "error": null,
  "id": 0
}
mubarak23 commented 2 months ago

{ "result": { "txid": "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082", "hash": "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082", "version": 1, "size": 134, "vsize": 134, "weight": 536, "locktime": 0, "vin": [ { "coinbase": "04ffff001d0102", "sequence": 4294967295 } ], "vout": [ { "value": 50, "n": 0, "scriptPubKey": { "asm": "04d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725b OP_CHECKSIG", "desc": "pk(04d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725b)#pezndevg", "hex": "4104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac", "type": "pubkey" } } ], "hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0102ffffffff0100f2052a01000000434104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac00000000", "blockhash": "00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee", "confirmations": 859230, "time": 1231731025, "blocktime": 1231731025 }, "error": null, "id": 0 }

Input (VIN) with the presence of "coinbase" in the array object signifies that the transaction has a coinbase input. I need to validate that the "coinbase" input has 100 block confirmations before it can be spent.

m-kus commented 2 months ago

In the outpoint code you query previous transaction and if you see that it has a single conibase specific input then you know that its outputs have to be marked as is_coinbase=true

maciejka commented 2 months ago

@mubarak23 what is the status of the issue? We would like to complete all open Milestone 2 issues this week. Do need need any help?

mubarak23 commented 2 months ago

@mubarak23 what is the status of the issue? We would like to complete all open Milestone 2 issues this week. Do need need any help?

am having issue with querying previous transaction.

how do i query previous transaction

m-kus commented 2 months ago

@mubarak23 what is the status of the issue? We would like to complete all open Milestone 2 issues this week. Do need need any help?

am having issue with querying previous transaction.

how do i query previous transaction

It's already queried here https://github.com/keep-starknet-strange/raito/blob/0918b074a2b3b461b03d326b2db1f65a24f31017/scripts/data/get_block.py#L59

mubarak23 commented 2 months ago

@mubarak23 what is the status of the issue? We would like to complete all open Milestone 2 issues this week. Do need need any help?

am having issue with querying previous transaction. how do i query previous transaction

It's already queried here

https://github.com/keep-starknet-strange/raito/blob/0918b074a2b3b461b03d326b2db1f65a24f31017/scripts/data/get_block.py#L59

got this part.

do need to return the is_coinbase flag inside validate_coinbase function @m-kus

m-kus commented 2 months ago

You need to put is_coinbase flag inside OutPoint (add a new field) so that later in validate_transaction you can check that this particular output has block_height <= current block_height - 100

mubarak23 commented 2 months ago

You need to put is_coinbase flag inside OutPoint (add a new field) so that later in validate_transaction you can check that this particular output has block_height <= current block_height - 100

this check will be done if is_coinbase flag is true ,

mubarak23 commented 2 months ago

adding is_coinbase break alot of the test,

i will send a PR

mubarak23 commented 2 months ago

@maciejka @m-kus adding is_coinbase break all the test inside tests/blocks , to fix this, i need to add is_coinbase to over 1426 transaction,

what is the best way here