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: Verified call decoding #134

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
21 changes: 15 additions & 6 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = 1
[[package]]
name = "alexandria_bytes"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=c1a604e#c1a604eaba40b026c2b477a9b293e40244432e0a"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=39ccd99#39ccd99b1c017ba1c6b53f6d74775f5ab0054735"
dependencies = [
"alexandria_data_structures",
"alexandria_math",
Expand All @@ -13,15 +13,15 @@ dependencies = [
[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=c1a604e#c1a604eaba40b026c2b477a9b293e40244432e0a"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=39ccd99#39ccd99b1c017ba1c6b53f6d74775f5ab0054735"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=c1a604e#c1a604eaba40b026c2b477a9b293e40244432e0a"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=39ccd99#39ccd99b1c017ba1c6b53f6d74775f5ab0054735"
dependencies = [
"alexandria_bytes",
"alexandria_math",
Expand All @@ -31,17 +31,26 @@ dependencies = [
[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=c1a604e#c1a604eaba40b026c2b477a9b293e40244432e0a"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=39ccd99#39ccd99b1c017ba1c6b53f6d74775f5ab0054735"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=c1a604e#c1a604eaba40b026c2b477a9b293e40244432e0a"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=39ccd99#39ccd99b1c017ba1c6b53f6d74775f5ab0054735"
dependencies = [
"alexandria_math",
"alexandria_searching",
]

[[package]]
name = "alexandria_searching"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=39ccd99#39ccd99b1c017ba1c6b53f6d74775f5ab0054735"
dependencies = [
"alexandria_data_structures",
]

[[package]]
Expand All @@ -68,7 +77,7 @@ source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.19.0#a3391dc
[[package]]
name = "succinct_sn"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/succinct-starknet.git?rev=7890c7b#7890c7b5d4ae1fc0ae1237bb85ade3ff131018b5"
source = "git+https://github.com/keep-starknet-strange/succinct-starknet.git?rev=c8f7941#c8f79418bb12487a1c21d1ea55a94835b7002c6a"
dependencies = [
"alexandria_bytes",
"openzeppelin",
Expand Down
6 changes: 3 additions & 3 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ test = "snforge test"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.19.0" }
starknet = ">=2.6.3"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", rev = "f22438c" }
alexandria_bytes = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "c1a604e" }
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "c1a604e" }
succinct_sn = { git = "https://github.com/keep-starknet-strange/succinct-starknet.git", rev = "7890c7b" }
alexandria_bytes = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "39ccd99" }
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "39ccd99" }
succinct_sn = { git = "https://github.com/keep-starknet-strange/succinct-starknet.git", rev = "c8f7941" }

[[target.starknet-contract]]
casm = true
Expand Down
14 changes: 11 additions & 3 deletions src/blobstreamx.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[starknet::contract]
mod blobstreamx {
use alexandria_bytes::{Bytes, BytesTrait};
use alexandria_encoding::sol_abi::{SolAbiEncodeTrait};
use alexandria_encoding::sol_abi::{SolAbiEncodeTrait, SolAbiDecodeTrait};
use blobstream_sn::interfaces::{
DataRoot, TendermintXErrors, IBlobstreamX, IDAOracle, ITendermintX
};
Expand Down Expand Up @@ -312,11 +312,15 @@ mod blobstreamx {
.encode_packed(_target_block);

// Get the output of the header_range proof from the gateway.
let (target_header, data_commitment) = ISuccinctGatewayDispatcher {
let verified_output = ISuccinctGatewayDispatcher {
contract_address: self.get_gateway()
}
.verified_call(self.get_header_range_id(), input);

let mut offset = 0;
let target_header = verified_output.decode(ref offset);
let data_commitment = verified_output.decode(ref offset);

let proof_nonce = self.get_state_proof_nonce();
self.block_height_to_header_hash.write(_target_block, target_header);
self.state_data_commitments.write(proof_nonce, data_commitment);
Expand Down Expand Up @@ -388,11 +392,15 @@ mod blobstreamx {
.encode_packed(trusted_header);

// Get the output of the next_header proof from the gateway.
let (next_header, data_commitment) = ISuccinctGatewayDispatcher {
let verified_output = ISuccinctGatewayDispatcher {
contract_address: self.gateway.read()
}
.verified_call(self.next_header_function_id.read(), input);

let mut offset = 0;
let next_header = verified_output.decode(ref offset);
let data_commitment = verified_output.decode(ref offset);

let proof_nonce = self.get_state_proof_nonce();
self.block_height_to_header_hash.write(next_block, next_header);
self.state_data_commitments.write(proof_nonce, data_commitment);
Expand Down
17 changes: 14 additions & 3 deletions src/tests/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use succinct_sn::function_registry::interfaces::{
IFunctionRegistryDispatcher, IFunctionRegistryDispatcherTrait
};
use succinct_sn::interfaces::{IFeeVaultDispatcher, IFeeVaultDispatcherTrait};
use succinct_sn::interfaces::{ISuccinctGatewayDispatcher, ISuccinctGatewayDispatcherTrait};

// https://sepolia.etherscan.io/tx/0xadced8dc7f4bb01d730ed78daecbf9640417c5bd60b0ada23c9045cc953481a5#eventlog
const TEST_START_BLOCK: u64 = 846054;
Expand All @@ -27,6 +28,10 @@ fn NEW_OWNER() -> ContractAddress {
contract_address_const::<'NEW_OWNER'>()
}

fn PROVER() -> ContractAddress {
contract_address_const::<'PROVER'>()
}

fn setup_base() -> ContractAddress {
// deploy the token associated with the fee vault
let mut calldata = array![];
Expand All @@ -49,7 +54,13 @@ fn setup_base() -> ContractAddress {
let gateway_addr = succinct_gateway_class
.deploy(@array![OWNER().into(), fee_vault_address.into()])
.unwrap();
let gateway = IFunctionRegistryDispatcher { contract_address: gateway_addr };
let gateway = ISuccinctGatewayDispatcher { contract_address: gateway_addr };
let function_registry = IFunctionRegistryDispatcher { contract_address: gateway_addr };

// Setup the gateway contract
snf::start_prank(CheatTarget::One(gateway.contract_address), OWNER());
gateway.set_prover(PROVER(), true);
snf::stop_prank(CheatTarget::One(gateway.contract_address));

// deploy the mock function verifier
let func_verifier_class = snf::declare("function_verifier_mock");
Expand All @@ -65,9 +76,9 @@ fn setup_base() -> ContractAddress {
let herodotus_facts_registry = herodotus_registry_class.deploy(@array![]).unwrap();

// register verifier functions w/ gateway
let header_range_func_id = gateway
let header_range_func_id = function_registry
.register_function(OWNER(), header_range_verifier, 'HEADER_RANGE');
let next_header_func_id = gateway
let next_header_func_id = function_registry
.register_function(OWNER(), next_header_verifier, 'NEXT_HEADER');

// deploy blobstreamx
Expand Down
10 changes: 7 additions & 3 deletions src/tests/test_blobstreamx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use blobstream_sn::mocks::evm_facts_registry::{
};
use blobstream_sn::tests::common::{
setup_base, setup_spied, setup_succinct_gateway, TEST_START_BLOCK, TEST_END_BLOCK, TEST_HEADER,
BLOBSTREAMX_L1_ADDRESS, OWNER
BLOBSTREAMX_L1_ADDRESS, OWNER, PROVER
};
use blobstream_sn::tree::binary::merkle_proof::BinaryMerkleProof;
use snforge_std as snf;
Expand Down Expand Up @@ -97,9 +97,10 @@ fn blobstreamx_fulfill_commit_header_range() {
.encode_packed(TEST_END_BLOCK);

let output = BytesTrait::new_empty()
.encode_packed(0x94a3afe8ce56375bedcb401c07a38a93a6b9d47461a01b6a410d5a958ca9bc7a_u256)
.encode_packed(0xAAA0E18EB3689B8D88BE03EA19589E3565DB343F6509C8601DB6AFA01255A488_u256);
.encode(0x94a3afe8ce56375bedcb401c07a38a93a6b9d47461a01b6a410d5a958ca9bc7a_u256)
.encode(0xAAA0E18EB3689B8D88BE03EA19589E3565DB343F6509C8601DB6AFA01255A488_u256);

snf::start_prank(CheatTarget::One(gateway.contract_address), PROVER());
gateway
.fulfill_call(
bsx.get_header_range_id(),
Expand All @@ -110,6 +111,7 @@ fn blobstreamx_fulfill_commit_header_range() {
selector!("commit_header_range"),
array![TEST_END_BLOCK.into()].span(),
);
snf::stop_prank(CheatTarget::One(gateway.contract_address));

assert!(
get_bsx_latest_block(bsx.contract_address) == TEST_END_BLOCK, "latest block does not match"
Expand Down Expand Up @@ -147,6 +149,7 @@ fn blobstreamx_commit_next_header() {
let latest_block = get_bsx_latest_block(bsx.contract_address);

// TODO: need test data for input, output, and proof as no txs on testnet
snf::start_prank(CheatTarget::One(gateway.contract_address), PROVER());
gateway
.fulfill_call(
bsx.get_next_header_id(),
Expand All @@ -157,6 +160,7 @@ fn blobstreamx_commit_next_header() {
selector!("commit_next_header"),
array![latest_block.into()].span(),
);
snf::stop_prank(CheatTarget::One(gateway.contract_address));

assert!(
get_bsx_latest_block(bsx.contract_address) == latest_block + 1,
Expand Down