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

registry contract <-> peer #16

Merged
merged 7 commits into from
May 6, 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
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb nightly-2024-03-16
scarb 2.6.3
starknet-foundry 0.20.1
24 changes: 22 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
[env]
CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true

[tasks.format]
[tasks.rust-format]
workspace = false
install_crate = "rustfmt"
command = "cargo"
args = ["fmt", "--", "--emit=files"]
args = ["fmt"]

[tasks.scarb-format]
workspace = false
command = "scarb"
args = ["fmt"]

[tasks.cairo-format]
workspace = false
script = [
"find cairo -name '*.cairo' ! -path 'cairo/build/*' -exec sh -c 'cairo-format -i \"$1\"; echo \"Formatted: $1\"' _ {} \\;"
]

[tasks.full-format]
workspace = false
dependencies = [
"rust-format",
"scarb-format",
"cairo-format",
]

[tasks.clean]
command = "cargo"
Expand Down
10 changes: 6 additions & 4 deletions cairo/bootloader/starknet/execute_task.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func execute_task{builtin_ptrs: BuiltinData*, self_range_check_ptr}(

let ec_op_ptr = cast(input_builtin_ptrs.ec_op, EcOpBuiltin*);
with ec_op_ptr {
let (res) = check_ecdsa_signature(message=hash, public_key=public_key, signature_r=signature_r, signature_s=signature_s);
let (res) = check_ecdsa_signature(
message=hash, public_key=public_key, signature_r=signature_r, signature_s=signature_s
);
assert res = TRUE;
}

Expand Down Expand Up @@ -174,7 +176,7 @@ func execute_task{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
assert isinstance(task, Task)
n_builtins = len(task.get_program().builtins)
new_task_locals = {}

if isinstance(task, CairoPieTask):
ret_pc = ids.ret_pc_label.instruction_offset_ - ids.call_task.instruction_offset_ + pc
load_cairo_pie(
Expand Down Expand Up @@ -207,7 +209,7 @@ func execute_task{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
// Allocate a struct containing all builtin pointers just after the program returns.
local return_builtin_ptrs: BuiltinData;
%{
from bootloader.recursive_with_poseidon.builtins import ALL_BUILTINS
from bootloader.starknet.builtins import ALL_BUILTINS
from bootloader.utils import write_return_builtins

# Fill the values of all builtin pointers after executing the task.
Expand Down Expand Up @@ -259,4 +261,4 @@ func execute_task{builtin_ptrs: BuiltinData*, self_range_check_ptr}(

let builtin_ptrs = &return_builtin_ptrs;
return ();
}
}
72 changes: 42 additions & 30 deletions cairo/bootloader/starknet/run_simple_bootloader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@ func run_simple_bootloader{
local task_range_check_ptr;

%{
n_tasks = 1
memory[ids.output_ptr] = n_tasks

# Task range checks are located right after simple bootloader validation range checks, and
# this is validated later in this function.
ids.task_range_check_ptr = ids.range_check_ptr + ids.BuiltinData.SIZE * n_tasks

ids.task_range_check_ptr = ids.range_check_ptr + ids.BuiltinData.SIZE
# A list of fact_toplogies that instruct how to generate the fact from the program output
# for each task.
fact_topologies = []
%}

let n_tasks = [output_ptr];
let output_ptr = output_ptr + 1;

// A struct containing the pointer to each builtin.
local builtin_ptrs_before: BuiltinData = BuiltinData(
output=cast(output_ptr, felt),
Expand All @@ -61,13 +54,7 @@ func run_simple_bootloader{
);

local builtin_instance_sizes: BuiltinData = BuiltinData(
output=1,
pedersen=3,
range_check=1,
ecdsa=2,
bitwise=5,
ec_op=7,
poseidon=6,
output=1, pedersen=3, range_check=1, ecdsa=2, bitwise=5, ec_op=7, poseidon=6
);

// Call execute_tasks.
Expand All @@ -76,10 +63,8 @@ func run_simple_bootloader{
let builtin_ptrs = &builtin_ptrs_before;
let self_range_check_ptr = range_check_ptr;
with builtin_ptrs, self_range_check_ptr {
execute_tasks(
builtin_encodings=&builtin_encodings,
builtin_instance_sizes=&builtin_instance_sizes,
n_tasks=n_tasks,
execute(
builtin_encodings=&builtin_encodings, builtin_instance_sizes=&builtin_instance_sizes
);
}

Expand Down Expand Up @@ -138,13 +123,15 @@ func verify_non_negative(num: felt, n_bits: felt) {
// self_range_check_ptr - range_check pointer (used for validating the builtins).
//
// Hint arguments:
// tasks - A list of tasks to execute.
func execute_tasks{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
builtin_encodings: BuiltinData*, builtin_instance_sizes: BuiltinData*, n_tasks: felt
// job - A job to execute.
func execute{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
builtin_encodings: BuiltinData*, builtin_instance_sizes: BuiltinData*
) {
if (n_tasks == 0) {
return ();
}
// Allocate memory for local variables.
alloc_locals;

// Get the value of fp.
let (local __fp__, _) = get_fp_and_pc();

%{
from bootloader.objects import Task
Expand All @@ -160,9 +147,34 @@ func execute_tasks{builtin_ptrs: BuiltinData*, self_range_check_ptr}(
use_poseidon=use_poseidon,
);

return execute_tasks(
builtin_encodings=builtin_encodings,
builtin_instance_sizes=builtin_instance_sizes,
n_tasks=n_tasks - 1,
local reward: felt;
local num_of_steps: felt;
local executor: felt;
local delegator: felt;

%{
ids.reward = simple_bootloader_input.job.job_data.reward
ids.num_of_steps = simple_bootloader_input.job.job_data.num_of_steps
ids.executor = simple_bootloader_input.public_key
ids.delegator = simple_bootloader_input.job.public_key
%}

assert [builtin_ptrs.output + 0] = reward;
assert [builtin_ptrs.output + 1] = num_of_steps;
assert [builtin_ptrs.output + 2] = executor;
assert [builtin_ptrs.output + 3] = delegator;

local return_builtin_ptrs: BuiltinData = BuiltinData(
output=builtin_ptrs.output + 4,
pedersen=builtin_ptrs.pedersen,
range_check=builtin_ptrs.range_check,
ecdsa=builtin_ptrs.ecdsa,
bitwise=builtin_ptrs.bitwise,
ec_op=builtin_ptrs.ec_op,
poseidon=builtin_ptrs.poseidon,
);
}

let builtin_ptrs = &return_builtin_ptrs;

return ();
}
6 changes: 2 additions & 4 deletions cairo/bootloader/starknet/simple_bootloader.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
%builtins output pedersen range_check ecdsa bitwise ec_op poseidon

from bootloader.starknet.run_simple_bootloader import (
run_simple_bootloader,
)
from bootloader.starknet.run_simple_bootloader import run_simple_bootloader
from common.cairo_builtins import HashBuiltin, PoseidonBuiltin
from common.registers import get_fp_and_pc

Expand Down Expand Up @@ -47,4 +45,4 @@ func main{
)
%}
return ();
}
}
2 changes: 1 addition & 1 deletion cairo/common/builtin_poseidon/poseidon.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ func poseidon_hash_many{poseidon_ptr: PoseidonBuiltin*}(n: felt, elements: felt*
let res = poseidon_ptr.output.s0;
let poseidon_ptr = poseidon_ptr + PoseidonBuiltin.SIZE;
return (res=res);
}
}
2 changes: 1 addition & 1 deletion cairo/common/cairo_builtins.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ struct KeccakBuiltin {
struct PoseidonBuiltin {
input: PoseidonBuiltinState,
output: PoseidonBuiltinState,
}
}
2 changes: 1 addition & 1 deletion cairo/common/ec_point.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
struct EcPoint {
x: felt,
y: felt,
}
}
2 changes: 1 addition & 1 deletion cairo/common/hash_chain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ func hash_chain{hash_ptr: HashBuiltin*}(data_ptr: felt*) -> (hash: felt) {
// Set the hash_ptr implicit argument and return the result.
let hash_ptr = next_frame.hash_ptr;
return (hash=next_frame.cur_hash);
}
}
2 changes: 1 addition & 1 deletion cairo/common/keccak_state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ struct KeccakBuiltinState {
s5: felt,
s6: felt,
s7: felt,
}
}
2 changes: 1 addition & 1 deletion cairo/common/poseidon_state.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ struct PoseidonBuiltinState {
s0: felt,
s1: felt,
s2: felt,
}
}
2 changes: 1 addition & 1 deletion cairo/common/registers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ func get_label_location(label_value: codeoffset) -> (res: felt*) {

ret_pc_label:
return (res=pc_val + (label_value - ret_pc_label));
}
}
2 changes: 1 addition & 1 deletion cairo/lang/compiler/lib/registers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ func get_ap() -> (ap_val: felt*) {
// Hence, the desired ap value is fp - 2.
let (fp_val, pc_val) = get_fp_and_pc();
return (ap_val=fp_val - 2);
}
}
28 changes: 25 additions & 3 deletions crates/peer/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ where
Box::pin(stream)
}

pub async fn stake<S>(
pub async fn deposit<S>(
&self,
amount: FieldElement,
account: SingleOwnerAccount<P, S>,
Expand All @@ -90,14 +90,36 @@ where
let result = account
.execute(vec![Call {
to: self.registry_address,
selector: get_selector_from_name("stake").unwrap(),
selector: get_selector_from_name("deposit").unwrap(),
calldata: vec![amount],
}])
.send()
.await
.unwrap();

trace!("Stake result: {:?}", result);
trace!("Deposit result: {:?}", result);
Ok(())
}

pub async fn balance<S>(
&self,
target: FieldElement,
account: SingleOwnerAccount<P, S>,
) -> Result<(), Box<dyn Error>>
where
S: Signer + Sync + Send + 'static,
{
let result = account
.execute(vec![Call {
to: self.registry_address,
selector: get_selector_from_name("balance").unwrap(),
calldata: vec![target],
}])
.send()
.await
.unwrap();

trace!("Balance result: {:?}", result);
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/runner/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn main() {
.arg("--output")
.arg(&out_dir.join(bootloader_out_name))
.arg("--proof_mode")
.arg("--no_debug_info")
.output()
.expect("bootloader compile failed");
}
Expand Down
Loading
Loading