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

Thursday night patch #4

Merged
merged 6 commits into from
Jul 21, 2023
Merged
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
50 changes: 25 additions & 25 deletions ink/spv_bridge/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod spv_bridge {
}

impl MerkleProof {
pub fn check_merkle_proof(_claim: Hash, proof: MerkleProof, _merkle_root: Hash) -> bool {
pub fn check_merkle_proof(_claim: HashValue, proof: MerkleProof, _merkle_root: u64) -> bool {
// This is where the actual merkle proof checking logic _would_ go
// if we weren't stubbing the proofs. Instead this stub is given.
proof.verifies
Expand All @@ -57,7 +57,7 @@ mod spv_bridge {
/// An instance of this struct would claim that a particular key holds a particular value.
///
/// For assignment purposes, the storage model doesn't matter so much because we stub the proofs.
/// Nonethless, we give a somewhat realistic model.
/// Nonetheless, we give a somewhat realistic model.
#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Decode, scale::Encode)]
#[cfg_attr(
feature = "std",
Expand Down Expand Up @@ -104,16 +104,20 @@ mod spv_bridge {
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
pub enum Error {
/// Insufficent relay fee
/// Insufficient fee provided when attempting to relay a block
InsufficientRelayFee,
/// Block is already known
/// Insufficient fee provided when attempting to verify a proof
InsufficientVerifyFee,
/// Header cannot be submitted because it was previously submitted
HeaderAlreadySubmitted,
/// Parent is not in the DB
UnknownParent,
/// Header height is invalid
IncorrectHeight,
/// PoW threshold has not been met
PoWThresholdNotMet
PoWThresholdNotMet,
/// Attempted reward payment to a relayer failed,
PaymentFailed,
}

/// Type alias for the contract's `Result` type.
Expand All @@ -125,7 +129,7 @@ mod spv_bridge {
block_hash: HashValue,
block_height: u64,
#[ink(topic)]
submitter: AccountId
submitter: AccountId,
}

/// An on-chain light client (or SPV client) for a foreign source chain.
Expand All @@ -140,7 +144,7 @@ mod spv_bridge {
/// history. We allow starting from a recent point in the source chain and verifying
/// thereafter.
///
/// This constructor allows the contract deployer to specifiy the recent block from which to start
/// This constructor allows the contract deployer to specify the recent block from which to start
#[ink(constructor)]
pub fn new(source_genesis_header: Header, difficulty: HashValue, init_relay_fee: Balance, init_verify_fee: Balance) -> Self {
let caller = Self::env().caller();
Expand Down Expand Up @@ -203,7 +207,7 @@ mod spv_bridge {
/// A min_depth of 1 means there is at least one block confirmation afterward.
/// 4. The merkle proof must be valid
#[ink(message, payable)]
pub fn verify_transaction(&mut self, tx_hash: HashValue, header_hash: HashValue, min_depth: u64, p: MerkleProof) -> bool {
pub fn verify_transaction(&mut self, tx_hash: HashValue, header_hash: HashValue, min_depth: u64, p: MerkleProof) -> Result<bool> {
todo!()
}

Expand All @@ -212,7 +216,7 @@ mod spv_bridge {
/// The checks performed are the same as when verifying a transaction.
/// However, in this chase, you pass the hash of the state claim
#[ink(message, payable)]
pub fn verify_state(&mut self, claim: StateClaim, block_hash: HashValue, min_depth: u64, p: MerkleProof) -> bool {
pub fn verify_state(&mut self, claim: StateClaim, block_hash: HashValue, min_depth: u64, p: MerkleProof) -> Result<bool> {
let mut claim_hash = <Sha2x256 as HashOutput>::Type::default();
ink::env::hash_encoded::<Sha2x256, _>(&claim, &mut claim_hash);

Expand All @@ -229,12 +233,12 @@ mod spv_bridge {
}

/// A helper function to detect whether a header exists in the storage
pub fn header_is_known(header_hash: HashValue) -> bool {
pub fn header_is_known(&self, header_hash: HashValue) -> bool {
todo!()
}

/// A helper unction to determine whether a header is in the canon chain
pub fn header_is_canon(header_hash: HashValue) -> bool {
/// A helper function to determine whether a header is in the canon chain
pub fn header_is_canon(&self, header_hash: HashValue) -> bool {
todo!()
}

Expand All @@ -247,7 +251,7 @@ mod spv_bridge {

/// This function is not graded. It is just for collecting feedback.
/// About how much time (in minutes) did you spend on the exercises in this section?
pub fn how_many_minutes_did_you_spend_on_this_section() -> f32 {
pub fn how_many_minutes_did_you_spend_on_this_section() -> u32 {
todo!()
}
}
Expand Down Expand Up @@ -307,8 +311,6 @@ mod spv_bridge {
};
let spv_bridge = SpvBridge::new(source_genesis_header, THRESHOLD, RELAY_FEE, VERIFY_FEE);

let hash_value = SpvBridge::hash_header(source_genesis_header);

(spv_bridge, source_genesis_header)
}

Expand Down Expand Up @@ -438,16 +440,16 @@ mod spv_bridge {
let (mut bridge, genesis_header) = deploy_bridge(default_accounts.alice);
let genesis_hash = SpvBridge::hash_header(genesis_header);
let a_header = make_child(genesis_header);
let a_hash = SpvBridge::hash_header(a_header);

let relay_response = ink::env::pay_with_call!(bridge.submit_new_header(a_header), RELAY_FEE);
assert_eq!(relay_response, Ok(()));

assert!(
debug_assert_eq!(
ink::env::pay_with_call!(
bridge.verify_transaction([0u8; 32],genesis_hash, 0, MerkleProof { verifies: true } ),
VERIFY_FEE
)
),
Ok(true)
);
}

Expand All @@ -461,7 +463,6 @@ mod spv_bridge {
let (mut bridge, genesis_header) = deploy_bridge(default_accounts.alice);
let genesis_hash = SpvBridge::hash_header(genesis_header);
let a_header = make_child(genesis_header);
let a_hash = SpvBridge::hash_header(a_header);

let relay_response = ink::env::pay_with_call!(bridge.submit_new_header(a_header), RELAY_FEE);
assert_eq!(relay_response, Ok(()));
Expand All @@ -471,7 +472,7 @@ mod spv_bridge {
bridge.verify_transaction([0u8; 32],genesis_hash, 0, MerkleProof { verifies: false } ),
VERIFY_FEE
),
false
Ok(false)
);
}

Expand All @@ -489,7 +490,6 @@ mod spv_bridge {
let (mut bridge, genesis_header) = deploy_bridge(default_accounts.alice);
let genesis_hash = SpvBridge::hash_header(genesis_header);
let a_header = make_child(genesis_header);
let a_hash = SpvBridge::hash_header(a_header);

let relay_response = ink::env::pay_with_call!(bridge.submit_new_header(a_header), RELAY_FEE);
assert_eq!(relay_response, Ok(()));
Expand All @@ -499,11 +499,12 @@ mod spv_bridge {
value: 0,
};

assert!(
assert_eq!(
ink::env::pay_with_call!(
bridge.verify_state(claim, genesis_hash, 0, MerkleProof { verifies: true } ),
VERIFY_FEE
)
),
Ok(true)
);
}

Expand All @@ -517,7 +518,6 @@ mod spv_bridge {
let (mut bridge, genesis_header) = deploy_bridge(default_accounts.alice);
let genesis_hash = SpvBridge::hash_header(genesis_header);
let a_header = make_child(genesis_header);
let a_hash = SpvBridge::hash_header(a_header);

let relay_response = ink::env::pay_with_call!(bridge.submit_new_header(a_header), RELAY_FEE);
assert_eq!(relay_response, Ok(()));
Expand All @@ -532,7 +532,7 @@ mod spv_bridge {
bridge.verify_state(claim, genesis_hash, 0, MerkleProof { verifies: false } ),
VERIFY_FEE
),
false
Ok(false)
);
}
}
Expand Down