Skip to content

Commit

Permalink
starknet: make more methods take mut self (#316)
Browse files Browse the repository at this point in the history
* free ci space to avoid errors

* dont free on macos

* needs sudo

* needs sudo

* make more methods take mut self

* replace class too

* clippy
  • Loading branch information
edg-l authored Oct 17, 2023
1 parent 0841034 commit 273ce25
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 76 deletions.
42 changes: 23 additions & 19 deletions examples/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct SyscallHandler;

impl StarkNetSyscallHandler for SyscallHandler {
fn get_block_hash(
&self,
&mut self,
block_number: u64,
_gas: &mut u64,
) -> SyscallResult<cairo_felt::Felt252> {
Expand Down Expand Up @@ -53,7 +53,7 @@ impl StarkNetSyscallHandler for SyscallHandler {
}

fn deploy(
&self,
&mut self,
class_hash: cairo_felt::Felt252,
contract_address_salt: cairo_felt::Felt252,
calldata: &[cairo_felt::Felt252],
Expand All @@ -67,13 +67,17 @@ impl StarkNetSyscallHandler for SyscallHandler {
))
}

fn replace_class(&self, class_hash: cairo_felt::Felt252, _gas: &mut u64) -> SyscallResult<()> {
fn replace_class(
&mut self,
class_hash: cairo_felt::Felt252,
_gas: &mut u64,
) -> SyscallResult<()> {
println!("Called `replace_class({class_hash})` from MLIR.");
Ok(())
}

fn library_call(
&self,
&mut self,
class_hash: cairo_felt::Felt252,
function_selector: cairo_felt::Felt252,
calldata: &[cairo_felt::Felt252],
Expand Down Expand Up @@ -145,7 +149,7 @@ impl StarkNetSyscallHandler for SyscallHandler {
}

fn secp256k1_add(
&self,
&mut self,
_p0: cairo_native::starknet::Secp256k1Point,
_p1: cairo_native::starknet::Secp256k1Point,
_gas: &mut u64,
Expand Down Expand Up @@ -224,63 +228,63 @@ impl StarkNetSyscallHandler for SyscallHandler {
}

fn secp256r1_new(
&self,
&mut self,
_x: cairo_native::starknet::U256,
_y: cairo_native::starknet::U256,
_gas: &mut u64,
) -> SyscallResult<Option<cairo_native::starknet::Secp256k1Point>> {
todo!()
}

fn pop_log(&self) {
fn pop_log(&mut self) {
todo!()
}

fn set_account_contract_address(&self, _contract_address: cairo_felt::Felt252) {
fn set_account_contract_address(&mut self, _contract_address: cairo_felt::Felt252) {
todo!()
}

fn set_block_number(&self, _block_number: u64) {
fn set_block_number(&mut self, _block_number: u64) {
todo!()
}

fn set_block_timestamp(&self, _block_timestamp: u64) {
fn set_block_timestamp(&mut self, _block_timestamp: u64) {
todo!()
}

fn set_caller_address(&self, _address: cairo_felt::Felt252) {
fn set_caller_address(&mut self, _address: cairo_felt::Felt252) {
todo!()
}

fn set_chain_id(&self, _chain_id: cairo_felt::Felt252) {
fn set_chain_id(&mut self, _chain_id: cairo_felt::Felt252) {
todo!()
}

fn set_contract_address(&self, _address: cairo_felt::Felt252) {
fn set_contract_address(&mut self, _address: cairo_felt::Felt252) {
todo!()
}

fn set_max_fee(&self, _max_fee: u128) {
fn set_max_fee(&mut self, _max_fee: u128) {
todo!()
}

fn set_nonce(&self, _nonce: cairo_felt::Felt252) {
fn set_nonce(&mut self, _nonce: cairo_felt::Felt252) {
todo!()
}

fn set_sequencer_address(&self, _address: cairo_felt::Felt252) {
fn set_sequencer_address(&mut self, _address: cairo_felt::Felt252) {
todo!()
}

fn set_signature(&self, _signature: &[cairo_felt::Felt252]) {
fn set_signature(&mut self, _signature: &[cairo_felt::Felt252]) {
todo!()
}

fn set_transaction_hash(&self, _transaction_hash: cairo_felt::Felt252) {
fn set_transaction_hash(&mut self, _transaction_hash: cairo_felt::Felt252) {
todo!()
}

fn set_version(&self, _version: cairo_felt::Felt252) {
fn set_version(&mut self, _version: cairo_felt::Felt252) {
todo!()
}
}
Expand Down
42 changes: 23 additions & 19 deletions examples/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct SyscallHandler;

impl StarkNetSyscallHandler for SyscallHandler {
fn get_block_hash(
&self,
&mut self,
block_number: u64,
_gas: &mut u64,
) -> SyscallResult<cairo_felt::Felt252> {
Expand Down Expand Up @@ -53,7 +53,7 @@ impl StarkNetSyscallHandler for SyscallHandler {
}

fn deploy(
&self,
&mut self,
class_hash: cairo_felt::Felt252,
contract_address_salt: cairo_felt::Felt252,
calldata: &[cairo_felt::Felt252],
Expand All @@ -67,13 +67,17 @@ impl StarkNetSyscallHandler for SyscallHandler {
))
}

fn replace_class(&self, class_hash: cairo_felt::Felt252, _gas: &mut u64) -> SyscallResult<()> {
fn replace_class(
&mut self,
class_hash: cairo_felt::Felt252,
_gas: &mut u64,
) -> SyscallResult<()> {
println!("Called `replace_class({class_hash})` from MLIR.");
Ok(())
}

fn library_call(
&self,
&mut self,
class_hash: cairo_felt::Felt252,
function_selector: cairo_felt::Felt252,
calldata: &[cairo_felt::Felt252],
Expand Down Expand Up @@ -145,7 +149,7 @@ impl StarkNetSyscallHandler for SyscallHandler {
}

fn secp256k1_add(
&self,
&mut self,
_p0: cairo_native::starknet::Secp256k1Point,
_p1: cairo_native::starknet::Secp256k1Point,
_gas: &mut u64,
Expand Down Expand Up @@ -224,63 +228,63 @@ impl StarkNetSyscallHandler for SyscallHandler {
}

fn secp256r1_new(
&self,
&mut self,
_x: cairo_native::starknet::U256,
_y: cairo_native::starknet::U256,
_gas: &mut u64,
) -> SyscallResult<Option<cairo_native::starknet::Secp256k1Point>> {
todo!()
}

fn pop_log(&self) {
fn pop_log(&mut self) {
todo!()
}

fn set_account_contract_address(&self, _contract_address: cairo_felt::Felt252) {
fn set_account_contract_address(&mut self, _contract_address: cairo_felt::Felt252) {
todo!()
}

fn set_block_number(&self, _block_number: u64) {
fn set_block_number(&mut self, _block_number: u64) {
todo!()
}

fn set_block_timestamp(&self, _block_timestamp: u64) {
fn set_block_timestamp(&mut self, _block_timestamp: u64) {
todo!()
}

fn set_caller_address(&self, _address: cairo_felt::Felt252) {
fn set_caller_address(&mut self, _address: cairo_felt::Felt252) {
todo!()
}

fn set_chain_id(&self, _chain_id: cairo_felt::Felt252) {
fn set_chain_id(&mut self, _chain_id: cairo_felt::Felt252) {
todo!()
}

fn set_contract_address(&self, _address: cairo_felt::Felt252) {
fn set_contract_address(&mut self, _address: cairo_felt::Felt252) {
todo!()
}

fn set_max_fee(&self, _max_fee: u128) {
fn set_max_fee(&mut self, _max_fee: u128) {
todo!()
}

fn set_nonce(&self, _nonce: cairo_felt::Felt252) {
fn set_nonce(&mut self, _nonce: cairo_felt::Felt252) {
todo!()
}

fn set_sequencer_address(&self, _address: cairo_felt::Felt252) {
fn set_sequencer_address(&mut self, _address: cairo_felt::Felt252) {
todo!()
}

fn set_signature(&self, _signature: &[cairo_felt::Felt252]) {
fn set_signature(&mut self, _signature: &[cairo_felt::Felt252]) {
todo!()
}

fn set_transaction_hash(&self, _transaction_hash: cairo_felt::Felt252) {
fn set_transaction_hash(&mut self, _transaction_hash: cairo_felt::Felt252) {
todo!()
}

fn set_version(&self, _version: cairo_felt::Felt252) {
fn set_version(&mut self, _version: cairo_felt::Felt252) {
todo!()
}
}
Expand Down
42 changes: 23 additions & 19 deletions src/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,25 @@ pub struct Secp256r1Point {
}

pub trait StarkNetSyscallHandler {
fn get_block_hash(&self, block_number: u64, remaining_gas: &mut u64) -> SyscallResult<Felt252>;
fn get_block_hash(
&mut self,
block_number: u64,
remaining_gas: &mut u64,
) -> SyscallResult<Felt252>;
fn get_execution_info(&self, remaining_gas: &mut u64) -> SyscallResult<ExecutionInfo>;

fn deploy(
&self,
&mut self,
class_hash: Felt252,
contract_address_salt: Felt252,
calldata: &[Felt252],
deploy_from_zero: bool,
remaining_gas: &mut u64,
) -> SyscallResult<(Felt252, Vec<Felt252>)>;
fn replace_class(&self, class_hash: Felt252, remaining_gas: &mut u64) -> SyscallResult<()>;
fn replace_class(&mut self, class_hash: Felt252, remaining_gas: &mut u64) -> SyscallResult<()>;

fn library_call(
&self,
&mut self,
class_hash: Felt252,
function_selector: Felt252,
calldata: &[Felt252],
Expand Down Expand Up @@ -114,7 +118,7 @@ pub trait StarkNetSyscallHandler {

// TODO: secp256k1 syscalls
fn secp256k1_add(
&self,
&mut self,
p0: Secp256k1Point,
p1: Secp256k1Point,
remaining_gas: &mut u64,
Expand Down Expand Up @@ -176,39 +180,39 @@ pub trait StarkNetSyscallHandler {
) -> SyscallResult<Option<Secp256k1Point>>;

fn secp256r1_new(
&self,
&mut self,
x: U256,
y: U256,
remaining_gas: &mut u64,
) -> SyscallResult<Option<Secp256k1Point>>;

// Testing syscalls.
// TODO: Make them optional. Crash if called but not implemented.
fn pop_log(&self);
fn pop_log(&mut self);

fn set_account_contract_address(&self, contract_address: Felt252);
fn set_account_contract_address(&mut self, contract_address: Felt252);

fn set_block_number(&self, block_number: u64);
fn set_block_number(&mut self, block_number: u64);

fn set_block_timestamp(&self, block_timestamp: u64);
fn set_block_timestamp(&mut self, block_timestamp: u64);

fn set_caller_address(&self, address: Felt252);
fn set_caller_address(&mut self, address: Felt252);

fn set_chain_id(&self, chain_id: Felt252);
fn set_chain_id(&mut self, chain_id: Felt252);

fn set_contract_address(&self, address: Felt252);
fn set_contract_address(&mut self, address: Felt252);

fn set_max_fee(&self, max_fee: u128);
fn set_max_fee(&mut self, max_fee: u128);

fn set_nonce(&self, nonce: Felt252);
fn set_nonce(&mut self, nonce: Felt252);

fn set_sequencer_address(&self, address: Felt252);
fn set_sequencer_address(&mut self, address: Felt252);

fn set_signature(&self, signature: &[Felt252]);
fn set_signature(&mut self, signature: &[Felt252]);

fn set_transaction_hash(&self, transaction_hash: Felt252);
fn set_transaction_hash(&mut self, transaction_hash: Felt252);

fn set_version(&self, version: Felt252);
fn set_version(&mut self, version: Felt252);
}

// TODO: Move to the correct place or remove if unused.
Expand Down
Loading

0 comments on commit 273ce25

Please sign in to comment.