Skip to content

Commit

Permalink
merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Jan 27, 2025
2 parents 5290ba3 + 7e8c05d commit 30dc455
Show file tree
Hide file tree
Showing 48 changed files with 2,165 additions and 731 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.8.2
scarb 2.9.2
90 changes: 52 additions & 38 deletions packages/cmds/src/main.cairo
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
use shinigami_compiler::compiler::CompilerImpl;
use shinigami_engine::engine::{EngineImpl, EngineInternalImpl};
use shinigami_engine::transaction::{EngineInternalTransactionImpl, EngineInternalTransactionTrait};
use shinigami_engine::transaction::{
EngineInternalTransactionImpl, EngineInternalTransactionTrait, UTXO,
};
use shinigami_engine::flags;
use shinigami_engine::witness;
use shinigami_engine::hash_cache::HashCacheImpl;
use shinigami_utils::byte_array::felt252_to_byte_array;
use shinigami_utils::bytecode::hex_to_bytecode;
use shinigami_tests::utxo::UTXO;
use shinigami_tests::validate;

#[derive(Clone, Drop)]
struct InputData {
ScriptSig: ByteArray,
ScriptPubKey: ByteArray
ScriptPubKey: ByteArray,
txid: u256,
}

#[derive(Clone, Drop)]
struct InputDataWithFlags {
ScriptSig: ByteArray,
ScriptPubKey: ByteArray,
Flags: ByteArray
Flags: ByteArray,
txid: u256,
}

#[derive(Clone, Drop)]
struct InputDataWithWitness {
ScriptSig: ByteArray,
ScriptPubKey: ByteArray,
Flags: ByteArray,
Witness: ByteArray
Witness: ByteArray,
txid: u256,
}

fn run_with_flags(input: InputDataWithFlags) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}', ScriptPubKey: '{}' and Flags: '{}'",
input.ScriptSig,
input.ScriptPubKey,
input.Flags
input.Flags,
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let tx = EngineInternalTransactionImpl::new_signed(
script_sig, script_pubkey.clone(), input.txid, array![],
);
let flags = flags::parse_flags(input.Flags);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, flags, 0, @hash_cache)?;
Expand All @@ -55,7 +61,7 @@ fn run_with_witness(input: InputDataWithWitness) -> Result<(), felt252> {
input.ScriptSig,
input.ScriptPubKey,
input.Flags,
input.Witness
input.Witness,
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
Expand All @@ -64,7 +70,7 @@ fn run_with_witness(input: InputDataWithWitness) -> Result<(), felt252> {
let witness = witness::parse_witness_input(input.Witness);
let value = 1; // TODO
let tx = EngineInternalTransactionImpl::new_signed_witness(
script_sig, script_pubkey.clone(), witness, value
script_sig, script_pubkey.clone(), witness, value, input.txid, array![],
);
let flags = flags::parse_flags(input.Flags);
let hash_cache = HashCacheImpl::new(@tx);
Expand All @@ -77,13 +83,15 @@ fn run(input: InputData) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}' and ScriptPubKey: '{}'",
input.ScriptSig,
input.ScriptPubKey
input.ScriptPubKey,
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let tx = EngineInternalTransactionImpl::new_signed(
script_sig, script_pubkey.clone(), input.txid, array![],
);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let res = engine.execute();
Expand All @@ -95,21 +103,23 @@ fn run(input: InputData) -> Result<(), felt252> {
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
Result::Err(e)
}
},
}
}

fn run_with_json(input: InputData) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}' and ScriptPubKey: '{}'",
input.ScriptSig,
input.ScriptPubKey
input.ScriptPubKey,
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let tx = EngineInternalTransactionImpl::new_signed(
script_sig, script_pubkey.clone(), input.txid, array![],
);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let _ = engine.execute()?;
Expand All @@ -121,13 +131,15 @@ fn debug(input: InputData) -> Result<bool, felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}' and ScriptPubKey: '{}'",
input.ScriptSig,
input.ScriptPubKey
input.ScriptPubKey,
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let tx = EngineInternalTransactionImpl::new_signed(
script_sig, script_pubkey.clone(), input.txid, array![],
);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let mut res = Result::Ok(true);
Expand All @@ -154,7 +166,7 @@ fn main(input: InputDataWithFlags) -> u8 {
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
},
}
}

Expand All @@ -168,7 +180,7 @@ fn main_with_witness(input: InputDataWithWitness) -> u8 {
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
},
}
}

Expand All @@ -182,7 +194,7 @@ fn backend_run(input: InputData) -> u8 {
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
},
}
}

Expand All @@ -196,7 +208,7 @@ fn backend_debug(input: InputData) -> u8 {
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
},
}
}

Expand All @@ -205,12 +217,12 @@ struct ValidateRawInput {
raw_transaction: ByteArray,
utxo_hints: Array<UTXO>,
flags: ByteArray,
txid: u256 // from raito or calculate from raw_transaction in decode ?
}

fn run_raw_transaction(mut input: ValidateRawInput) -> u8 {
println!("Running Bitcoin Script with raw transaction: '{}'", input.raw_transaction);
let raw_transaction = hex_to_bytecode(@input.raw_transaction);
let transaction = EngineInternalTransactionTrait::deserialize(raw_transaction);

// Parse the flags
let script_flags = flags::parse_flags(input.flags);
Expand All @@ -223,23 +235,25 @@ fn run_raw_transaction(mut input: ValidateRawInput) -> u8 {
}

let mut utxo_hints = array![];
for hint in input.utxo_hints.span() {
println!("UTXO hint: 'amount: {}, script_pubkey: {}'", hint.amount, hint.pubkey_script);
let pubkey_script = hex_to_bytecode(hint.pubkey_script);
utxo_hints
.append(
UTXO {
amount: *hint.amount,
pubkey_script: pubkey_script,
block_height: *hint.block_height,
},
);
};

let transaction = EngineInternalTransactionTrait::deserialize(
raw_transaction, input.txid, utxo_hints,
);
// transaction.set_utxos(utxo_hints);

for hint in input
.utxo_hints
.span() {
println!("UTXO hint: 'amount: {}, script_pubkey: {}'", hint.amount, hint.pubkey_script);
let pubkey_script = hex_to_bytecode(hint.pubkey_script);
utxo_hints
.append(
UTXO {
amount: *hint.amount,
pubkey_script: pubkey_script,
block_height: *hint.block_height,
}
);
};

let res = validate::validate_transaction(@transaction, script_flags, utxo_hints);
let res = validate::validate_transaction(@transaction, script_flags);
match res {
Result::Ok(_) => {
println!("Execution successful");
Expand All @@ -248,6 +262,6 @@ fn run_raw_transaction(mut input: ValidateRawInput) -> u8 {
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
},
}
}
3 changes: 2 additions & 1 deletion packages/compiler/src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::utils::{is_hex, is_number, is_string};
#[derive(Destruct)]
pub struct Compiler {
// Dict containing opcode names to their bytecode representation
opcodes: Felt252Dict<Nullable<u8>>
opcodes: Felt252Dict<Nullable<u8>>,
}

pub trait CompilerTrait {
Expand Down Expand Up @@ -310,6 +310,7 @@ pub impl CompilerImpl of CompilerTrait {
compiler.add_opcode('OP_CLTV', Opcode::OP_CHECKLOCKTIMEVERIFY);
compiler.add_opcode('OP_CHECKSEQUENCEVERIFY', Opcode::OP_CHECKSEQUENCEVERIFY);
compiler.add_opcode('OP_CSV', Opcode::OP_CHECKSEQUENCEVERIFY);
compiler.add_opcode('OP_CHECKSIGADD', Opcode::OP_CHECKSIGADD);

compiler
}
Expand Down
4 changes: 2 additions & 2 deletions packages/engine/src/cond_stack.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct ConditionalStack {
#[generate_trait()]
pub impl ConditionalStackImpl of ConditionalStackTrait {
fn new() -> ConditionalStack {
ConditionalStack { stack: Default::default(), len: 0, }
ConditionalStack { stack: Default::default(), len: 0 }
}

fn push(ref self: ConditionalStack, value: u8) {
Expand Down Expand Up @@ -43,7 +43,7 @@ pub impl ConditionalStackImpl of ConditionalStackTrait {
0 => self.stack.insert(cond_idx.into(), 1),
1 => self.stack.insert(cond_idx.into(), 0),
2 => self.stack.insert(cond_idx.into(), 2),
_ => panic!("Invalid condition")
_ => panic!("Invalid condition"),
}
}
}
Loading

0 comments on commit 30dc455

Please sign in to comment.