diff --git a/ink/spv_bridge/lib.rs b/ink/spv_bridge/lib.rs index aa20635..74c0140 100755 --- a/ink/spv_bridge/lib.rs +++ b/ink/spv_bridge/lib.rs @@ -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 @@ -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", @@ -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. @@ -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. @@ -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(); @@ -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 { todo!() } @@ -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 { let mut claim_hash = ::Type::default(); ink::env::hash_encoded::(&claim, &mut claim_hash); @@ -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!() } @@ -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!() } } @@ -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) } @@ -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) ); } @@ -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(())); @@ -471,7 +472,7 @@ mod spv_bridge { bridge.verify_transaction([0u8; 32],genesis_hash, 0, MerkleProof { verifies: false } ), VERIFY_FEE ), - false + Ok(false) ); } @@ -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(())); @@ -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) ); } @@ -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(())); @@ -532,7 +532,7 @@ mod spv_bridge { bridge.verify_state(claim, genesis_hash, 0, MerkleProof { verifies: false } ), VERIFY_FEE ), - false + Ok(false) ); } }