Browse Source

Boolinator (#288)

* Start anchor program

* pythnet folder

* Renames

* Implement processor

* Comments

* More comments

* Bump anchor

* Use new version of the wormhole package

* Get Solana chain id from wormhole core

* Pythnet bridge address

* Remove comment

* Fix borsh headers

* Format

* Fix precommit

* Fix precommit

* Precommit

* Add to CI

* Fix CI

* Use boolinator

* Enable boolinator

* Remove duplicate macro
guibescos 3 năm trước cách đây
mục cha
commit
48a5902cb0

+ 7 - 0
pythnet/remote-executor/Cargo.lock

@@ -275,6 +275,12 @@ dependencies = [
  "generic-array",
 ]
 
+[[package]]
+name = "boolinator"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9"
+
 [[package]]
 name = "borsh"
 version = "0.9.3"
@@ -1055,6 +1061,7 @@ name = "remote-executor"
 version = "0.1.0"
 dependencies = [
  "anchor-lang",
+ "boolinator",
  "wormhole-core",
  "wormhole-solana",
 ]

+ 1 - 0
pythnet/remote-executor/programs/remote-executor/Cargo.toml

@@ -22,3 +22,4 @@ overflow-checks = true
 anchor-lang = {version = "0.25.0", features = ["init-if-needed"]}
 wormhole-solana = { git = "https://github.com/guibescos/wormhole", branch = "gbescos/sdk-solana"}
 wormhole-core = { git = "https://github.com/guibescos/wormhole", branch = "gbescos/sdk-solana"}
+boolinator = "2.4.0"

+ 6 - 16
pythnet/remote-executor/programs/remote-executor/src/lib.rs

@@ -21,6 +21,7 @@ pub mod remote_executor {
         instruction::Instruction,
         program::invoke_signed,
     };
+    use boolinator::Boolinator;
     use wormhole::Chain::{
         self,
         Solana,
@@ -36,14 +37,11 @@ pub mod remote_executor {
     pub fn execute_posted_vaa(ctx: Context<ExecutePostedVaa>) -> Result<()> {
         let posted_vaa = &ctx.accounts.posted_vaa;
         let claim_record = &mut ctx.accounts.claim_record;
-        assert_or_err(
-            Chain::from(posted_vaa.emitter_chain) == Solana,
-            err!(ExecutorError::EmitterChainNotSolana),
-        )?;
-        assert_or_err(
-            posted_vaa.sequence > claim_record.sequence,
-            err!(ExecutorError::NonIncreasingSequence),
-        )?;
+
+        (Chain::from(posted_vaa.emitter_chain) == Solana)
+            .ok_or(error!(ExecutorError::EmitterChainNotSolana))?;
+        (posted_vaa.sequence > claim_record.sequence)
+            .ok_or(error!(ExecutorError::NonIncreasingSequence))?;
         claim_record.sequence = posted_vaa.sequence;
 
         let payload = ExecutorPayload::try_from_slice(&posted_vaa.payload)?;
@@ -80,11 +78,3 @@ pub struct ExecutePostedVaa<'info> {
     pub claim_record: Account<'info, ClaimRecord>,
     pub system_program: Program<'info, System>,
 }
-
-pub fn assert_or_err(condition: bool, error: Result<()>) -> Result<()> {
-    if !condition {
-        error
-    } else {
-        Result::Ok(())
-    }
-}

+ 10 - 20
pythnet/remote-executor/programs/remote-executor/src/state/governance_payload.rs

@@ -8,12 +8,10 @@ use anchor_lang::{
     prelude::*,
     solana_program::instruction::Instruction,
 };
+use boolinator::Boolinator;
 use wormhole::Chain;
 
-use crate::{
-    assert_or_err,
-    error::ExecutorError,
-};
+use crate::error::ExecutorError;
 
 pub const MAGIC_NUMBER: u32 = 0x4d475450; // Reverse order of the solidity contract because borsh uses little endian numbers (the solidity contract uses 0x5054474d)
 
@@ -126,21 +124,13 @@ impl ExecutorPayload {
     const ACTION: Action = Action::ExecutePostedVaa;
 
     pub fn check_header(&self) -> Result<()> {
-        assert_or_err(
-            self.header.magic_number == MAGIC_NUMBER,
-            err!(ExecutorError::GovernanceHeaderInvalidMagicNumber),
-        )?;
-        assert_or_err(
-            self.header.module == ExecutorPayload::MODULE,
-            err!(ExecutorError::GovernanceHeaderInvalidMagicNumber),
-        )?;
-        assert_or_err(
-            self.header.action == ExecutorPayload::ACTION,
-            err!(ExecutorError::GovernanceHeaderInvalidMagicNumber),
-        )?;
-        assert_or_err(
-            Chain::from(self.header.chain.value) == Chain::Pythnet,
-            err!(ExecutorError::GovernanceHeaderInvalidMagicNumber),
-        )
+        (self.header.magic_number == MAGIC_NUMBER)
+            .ok_or(error!(ExecutorError::GovernanceHeaderInvalidMagicNumber))?;
+        (self.header.module == ExecutorPayload::MODULE)
+            .ok_or(error!(ExecutorError::GovernanceHeaderInvalidMagicNumber))?;
+        (self.header.action == ExecutorPayload::ACTION)
+            .ok_or(error!(ExecutorError::GovernanceHeaderInvalidMagicNumber))?;
+        (Chain::from(self.header.chain.value) == Chain::Pythnet)
+            .ok_or(error!(ExecutorError::GovernanceHeaderInvalidMagicNumber))
     }
 }