Skip to content

Commit

Permalink
Merge pull request #45 from keep-starknet-strange/mk/test_data
Browse files Browse the repository at this point in the history
[feat] Script to generate test data, with a bit refactoring
  • Loading branch information
maciejka authored Aug 9, 2024
2 parents c07ed7d + 02d0124 commit 522a324
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 194 deletions.
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2023_11"

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

[dependencies]

Expand Down
18 changes: 0 additions & 18 deletions scripts/data/block.jq

This file was deleted.

72 changes: 72 additions & 0 deletions scripts/data/block_filter.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
def txin_coinbase:
"TxIn {
script: from_base16(\"\(.coinbase)\"),
sequence: \(.sequence),
previous_output: OutPoint {
txid: 0_u256,
vout: 0xffffffff_u32,
txo_index: 0, // TODO: implement
},
}"
;

def txin_regular:
"TxIn {
script: from_base16(\"\(.scriptSig.hex)\"),
sequence: \(.sequence),
previous_output: OutPoint {
txid: 0x\(.txid),
vout: \(.vout),
txo_index: 0, // TODO: implement
},
}"
;

def txin:
if .coinbase then
txin_coinbase
else
txin_regular
end
;

def txout:
"TxOut {
value: \(.value*100000000)_u64,
pk_script: from_base16(\"\(.scriptPubKey.hex)\"),
}"
;

def tx:
"Transaction {
version: \(.version),
is_segwit: false,
inputs: array![\(.vin | map(txin) | join(",\n"))].span(),
outputs: array![\(.vout | map(txout) | join(",\n"))].span(),
lock_time: \(.locktime)
}"
;


def block:
"Block {
header : Header {
version: \(.version)_u32,
time: \(.time)_u32,
nonce: \(.nonce)_u32
},
txs: array![\(.tx | map(tx) | join(",\n"))].span()
};"
;

def fixture:
"use super::state::{Block, Header, Transaction, OutPoint, TxIn, TxOut};
pub fn block_\(.height)() -> Block {
// block hash: \(.hash)
\( . | block )
}"
;

.result | fixture

20 changes: 20 additions & 0 deletions scripts/data/get_block.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /usr/bin/env bash
set -e;
set -o pipefail;

HEIGHT=$(curl -s --user $USERPWD -s -d '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockheader", "params": ["'${1}'"] }' -H 'content-type: text/plain;' $BITCOIN_RPC | jq -r '.result.height')

curl \
-s \
--user $USERPWD \
-d '{
"jsonrpc": "1.0",
"id": "curltest",
"method": "getblock",
"params": ["'${1}'", 2]
}' \
-H 'content-type: text/plain;' $BITCOIN_RPC \
| jq -r -f scripts/data/block_filter.jq > tests/blocks/block_${HEIGHT}.cairo

validate_target, validate_timestamp, validate_proof_of_work, compute_block_reward,
compute_total_work,
67 changes: 0 additions & 67 deletions scripts/data/launch.sh

This file was deleted.

9 changes: 0 additions & 9 deletions scripts/data/tx_in.jq

This file was deleted.

7 changes: 0 additions & 7 deletions scripts/data/tx_out.jq

This file was deleted.

24 changes: 17 additions & 7 deletions src/state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct ChainState {
/// Best block.
pub best_block_hash: u256,
/// Current block.
pub current_target: u32,
pub current_target: u256,
/// Start of the current epoch.
pub epoch_start_time: u32,
/// Previous timestamps.
Expand Down Expand Up @@ -73,11 +73,11 @@ pub struct Block {
pub struct Header {
/// The version of the block.
pub version: u32,
/// The hash of the previous block in the blockchain.
pub prev_block_hash: u256,
/// The timestamp of the block.
pub time: u32,
/// The difficulty target for mining the block.
/// Not strictly necessary since it can be computed from target
/// But it is cheaper to validate than compute
pub bits: u32,
/// The nonce used in mining the block.
pub nonce: u32,
Expand All @@ -99,10 +99,6 @@ pub struct Transaction {
pub inputs: Span<TxIn>,
/// The outputs of the transaction.
pub outputs: Span<TxOut>,
/// The list of witnesses, one for each input.
/// Each witness is a list of elements that are to be pushed onto stack.
/// Witnesses do not contribute to TXID but do contribute to wTXID.
pub witnesses: Span<Span<ByteArray>>,
/// The lock time of the transaction.
pub lock_time: u32,
}
Expand All @@ -128,6 +124,20 @@ pub struct TxIn {
pub script: @ByteArray,
/// The sequence number of the input.
pub sequence: u32,
/// The reference to the previous output that is being used as an input.
pub previous_output: OutPoint,
/// The witness data for transactions.
pub witness: Span<ByteArray>,
}


/// A reference to a transaction output.
#[derive(Drop, Copy)]
pub struct OutPoint {
/// The hash of the referenced transaction.
pub txid: u256,
/// The index of the specific output in the transaction.
pub vout: u32,
/// The index of output in the utreexo set (meta field).
pub txo_index: u64,
}
Loading

0 comments on commit 522a324

Please sign in to comment.