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

Prover: add verify_log_entry_in_bound api #919

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Changes from 2 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
121 changes: 95 additions & 26 deletions contracts/near/eth-prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,67 @@ impl EthProver {
#[serializer(borsh)] proof: Vec<Vec<u8>>,
#[serializer(borsh)] skip_bridge_call: bool,
) -> PromiseOrValue<bool> {
let min_header_height = None;
let max_header_height = None;
self.verify_log_entry_internal(
log_index,
log_entry_data,
receipt_index,
receipt_data,
header_data,
proof,
min_header_height,
max_header_height,
skip_bridge_call,
)
}

#[pause(except(roles(Role::UnrestrictedVerifyLogEntry, Role::DAO)))]
#[result_serializer(borsh)]
pub fn verify_log_entry_in_bound(
&self,
#[serializer(borsh)] log_index: u64,
#[serializer(borsh)] log_entry_data: Vec<u8>,
#[serializer(borsh)] receipt_index: u64,
#[serializer(borsh)] receipt_data: Vec<u8>,
#[serializer(borsh)] header_data: Vec<u8>,
#[serializer(borsh)] proof: Vec<Vec<u8>>,
#[serializer(borsh)] min_header_height: Option<u64>,
#[serializer(borsh)] max_header_height: Option<u64>,
#[serializer(borsh)] skip_bridge_call: bool,
) -> PromiseOrValue<bool> {
self.verify_log_entry_internal(
log_index,
log_entry_data,
receipt_index,
receipt_data,
header_data,
proof,
min_header_height,
max_header_height,
skip_bridge_call,
)
}

fn verify_log_entry_internal(
&self,
#[serializer(borsh)] log_index: u64,
#[serializer(borsh)] log_entry_data: Vec<u8>,
#[serializer(borsh)] receipt_index: u64,
#[serializer(borsh)] receipt_data: Vec<u8>,
#[serializer(borsh)] header_data: Vec<u8>,
#[serializer(borsh)] proof: Vec<Vec<u8>>,
#[serializer(borsh)] min_header_height: Option<u64>,
#[serializer(borsh)] max_header_height: Option<u64>,
#[serializer(borsh)] skip_bridge_call: bool,
) -> PromiseOrValue<bool> {
let header: BlockHeader = rlp::decode(header_data.as_slice()).unwrap();
if !Self::is_block_height_in_bound(header.number, min_header_height, max_header_height) {
return PromiseOrValue::Value(false);
}

let log_entry: LogEntry = rlp::decode(log_entry_data.as_slice()).unwrap();
let receipt: Receipt = rlp::decode(receipt_data.as_slice()).unwrap();
let header: BlockHeader = rlp::decode(header_data.as_slice()).unwrap();

// Verify log_entry included in receipt
let log_index_usize = usize::try_from(log_index).expect("Invalid log_index");
Expand Down Expand Up @@ -185,31 +243,8 @@ impl EthProver {
#[serializer(borsh)] skip_bridge_call: bool,
) -> PromiseOrValue<bool> {
let header: BlockHeader = rlp::decode(header_data.as_slice()).unwrap();

if let Some(min_header_height) = min_header_height {
if header.number < min_header_height {
env::log_str(
format!(
"Block height {} < Minimum header height {}",
header.number, min_header_height
)
.as_str(),
);
return PromiseOrValue::Value(false);
}
}

if let Some(max_header_height) = max_header_height {
if header.number > max_header_height {
env::log_str(
format!(
"Block height {} > Maximum header height {}",
header.number, max_header_height
)
.as_str(),
);
return PromiseOrValue::Value(false);
}
if !Self::is_block_height_in_bound(header.number, min_header_height, max_header_height) {
return PromiseOrValue::Value(false);
}

let account_key = near_keccak256(&contract_address).to_vec();
Expand Down Expand Up @@ -242,6 +277,40 @@ impl EthProver {
.into()
}

fn is_block_height_in_bound(
header_height: u64,
min_header_height: Option<u64>,
max_header_height: Option<u64>,
) -> bool {
if let Some(min_header_height) = min_header_height {
if header_height < min_header_height {
env::log_str(
format!(
"Block height {} < Minimum header height {}",
header_height, min_header_height
)
.as_str(),
);
return false;
}
}

if let Some(max_header_height) = max_header_height {
if header_height > max_header_height {
env::log_str(
format!(
"Block height {} > Maximum header height {}",
header_height, max_header_height
)
.as_str(),
);
return false;
}
}

true
}

/// Verify the proof recursively traversing through the key.
/// Return the value at the end of the key, in case the proof is valid.
///
Expand Down
Loading