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

[dev]: Script to generate a test data #27

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "raito"
version = "0.1.0"
edition = "2023_11"

[scripts]
gen_data_test= "chmod -x ./scripts/data/launch.sh && bash ./scripts/data/launch.sh"

[dependencies]

[dev-dependencies]
Expand Down
18 changes: 18 additions & 0 deletions scripts/data/block.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def block:
"
use raito::state::{Block, Header, Transaction, TxIn, TxOut};

pub fn test_data_block() -> Block {
Block {
header : Header {
version: \(.version),
prev_block_hash: 0x\(.previousblockhash),
merkle_root_hash: 0x\(.merkle_root),
time: \(.timestamp),
bits: \(.bits),
nonce: \(.nonce)
},"

;

block
67 changes: 67 additions & 0 deletions scripts/data/launch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#! /usr/bin/env bash

destPath="./src/"
tx_inJqPath="./scripts/data/tx_in.jq"
tx_outJqPath="./scripts/data/tx_out.jq"
blockJqPath="./scripts/data/block.jq"
blockHash=""
api="https://mempool.space/api/block/"

if [ ! -z "$1" ]; then
blockHash="$1"
else
read -p "Enter bitcoin block hash: " blockHash
fi

fileName="${destPath}block_$blockHash.cairo"


# Recive informations
btcBlock=$(curl -sSL "$api$blockHash")
btcBlockTxs=$(curl -sSL "$api$blockHash/txs")

#total transactions of the block
tx_count=$(echo $btcBlock | jq -r ".tx_count")
echo "Total of transactions: " $tx_count

#Put the header block in file
echo $btcBlock | jq -r -f $blockJqPath > $fileName
echo " txs: array![" >> $fileName

#declare at 1 for skipping the coinbase transaction
idx=1
total=1

#Put transactions in file
while (( $total < $tx_count )); do
if (( $idx % 25 == 0)) then
btcBlockTxs=$(curl -sSL "$api$blockHash/txs/$total")
idx=0
echo "$total / $tx_count transactions recived"
fi
tx=$(echo $btcBlockTxs | jq -r ".[$idx]")
echo " Transaction {" >> $fileName
echo " version: $(echo $tx | jq -r ".version")," >> $fileName
echo " lock_time: $(echo $tx | jq -r ".locktime")," >> $fileName
echo " inputs: array![" >> $fileName
echo $tx | jq -r ".vin[]" | jq -r -f $tx_inJqPath >> $fileName
echo " ].span()," >> $fileName
echo " outputs: array![" >> $fileName
echo $tx | jq -r ".vout[]" | jq -r -f $tx_outJqPath >> $fileName
echo " ].span()," >> $fileName
echo " }," >> $fileName
((idx++))
((total++))
done
if (( $total == $tx_count)) then
echo "$total / $tx_count transactions recived"
echo "Execution successful, file created in $fileName"
fi

#end file
echo " ].span()" >> $fileName
echo " }" >> $fileName
echo "}" >> $fileName
echo ""
echo -e "${green}add: \"pub mod block_$blockHash;\" in lib.cairo${reset}"
echo -e "${green}add: \"use raito::block_$blockHash::test_data_block;\" in your file${reset}"
9 changes: 9 additions & 0 deletions scripts/data/tx_in.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def tx_in:
" TxIn {
txid: 0x\(.txid),
index: \(.vout),
script: @\"\(.scriptsig)\",
sequence: \(.sequence),
},"
;
tx_in
7 changes: 7 additions & 0 deletions scripts/data/tx_out.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def tx_out:
" TxOut {
value : \(.value),
pk_script: @\"\(.scriptpubkey)\",
},"
;
tx_out
12 changes: 6 additions & 6 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ pub struct Transaction {
#[derive(Drop, Copy)]
pub struct TxOut {
/// The value of the output.
value: i64,
pub value: i64,
/// The public key script of the output.
pk_script: @ByteArray,
pub pk_script: @ByteArray,
}

/// Input of a transaction.
/// https://developer.bitcoin.org/reference/transactions.html#txin-a-transaction-input-non-coinbase
#[derive(Drop, Copy)]
pub struct TxIn {
/// The transaction ID of the input.
txid: u256,
pub txid: u256,
/// The index of the input.
index: u32,
pub index: u32,
/// The script of the input.
script: @ByteArray,
pub script: @ByteArray,
/// The sequence of the input.
sequence: u32,
pub sequence: u32,
}