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

feat: introduce starknet verify, verifier set #48

Merged
merged 73 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
cfc0a65
save what i have before go
puhtaytow Nov 25, 2024
408acc1
add missing fields
puhtaytow Nov 25, 2024
d496d96
forgot to add
puhtaytow Nov 25, 2024
acf89dd
save
puhtaytow Nov 25, 2024
619b606
looks like handle is all we need, i dont want to be wrong on this one
puhtaytow Nov 25, 2024
d2b1ea1
missing error
puhtaytow Nov 25, 2024
7cf2a69
remove obsolete parts
puhtaytow Nov 25, 2024
ae73371
further..
puhtaytow Nov 25, 2024
71afc6f
add TODO for verifier parts
puhtaytow Nov 25, 2024
7cc7927
looks like everything is here, but the starknet client needs to be ex…
puhtaytow Nov 25, 2024
b491a97
save
puhtaytow Nov 25, 2024
b902003
add module description
puhtaytow Nov 25, 2024
b0639f9
Add todo with check
puhtaytow Nov 25, 2024
c816ae4
for now
puhtaytow Nov 25, 2024
c2ab97b
adjust module description
puhtaytow Nov 25, 2024
2bcaeb2
align module name
puhtaytow Nov 25, 2024
f8e4dfe
add enum
puhtaytow Nov 25, 2024
1f7383c
save
puhtaytow Nov 25, 2024
f9a0493
Add missing event import from proto package
puhtaytow Nov 25, 2024
f4e80cd
save what i have before go
puhtaytow Nov 27, 2024
b5f9be2
its a bit further
puhtaytow Nov 27, 2024
ed3c401
now i have different lifetime error
puhtaytow Nov 27, 2024
7090852
one step further
puhtaytow Nov 27, 2024
25b4340
save after rebase
puhtaytow Nov 28, 2024
8dc26f3
add fixme
puhtaytow Nov 28, 2024
c9ce581
save
puhtaytow Nov 28, 2024
b49905a
save
puhtaytow Nov 29, 2024
b0d0f22
remove obsolete fixme and check
puhtaytow Nov 29, 2024
5baa99f
remove another fixme
puhtaytow Nov 29, 2024
9cde41d
extend on todo
puhtaytow Nov 29, 2024
73c01dc
port SignersRotated
puhtaytow Nov 29, 2024
7e708bd
change inner type for enum SignersRotated
puhtaytow Nov 29, 2024
a5aaa6d
add for now separate implementation
puhtaytow Nov 29, 2024
5294c4b
further
puhtaytow Nov 29, 2024
be2d203
save
puhtaytow Dec 1, 2024
a5f4dc8
remove tx hash as its not needed at this point
puhtaytow Dec 1, 2024
dd2cd90
cleanup
puhtaytow Dec 1, 2024
61387a7
add docs
puhtaytow Dec 1, 2024
39f5894
remove enum
puhtaytow Dec 1, 2024
7ad20f1
Add docs'
puhtaytow Dec 1, 2024
8dec630
remove TODO
puhtaytow Dec 1, 2024
cfbf48b
remove fixme
puhtaytow Dec 1, 2024
18a9adc
add init implementation / needs fix but not today
puhtaytow Dec 1, 2024
285acda
add from address
puhtaytow Dec 2, 2024
72f6759
fix PartEQ
puhtaytow Dec 2, 2024
afce53e
add meaningful comment about ..8 in nonce case
puhtaytow Dec 2, 2024
68b4796
reorganise / cleanup
puhtaytow Dec 2, 2024
3310478
save
puhtaytow Dec 2, 2024
01cf238
formatting
puhtaytow Dec 2, 2024
c747f2a
save
puhtaytow Dec 2, 2024
e07fe77
save
puhtaytow Dec 2, 2024
50ae57d
nonce should never be 0
puhtaytow Dec 2, 2024
ecc5573
add unhappy test
puhtaytow Dec 2, 2024
92f828d
remove random signers
puhtaytow Dec 2, 2024
f388c55
fix keys
puhtaytow Dec 2, 2024
b6be9fe
savE
puhtaytow Dec 2, 2024
c4ba4d6
align addresses
puhtaytow Dec 2, 2024
36baa82
use mock from multisig tests
puhtaytow Dec 2, 2024
df4e167
add binary tree
puhtaytow Dec 2, 2024
8243638
save
puhtaytow Dec 2, 2024
26ca09a
partial success
puhtaytow Dec 2, 2024
c1fef21
it works
puhtaytow Dec 2, 2024
6990b76
fix test
puhtaytow Dec 2, 2024
0ee6306
adjust name
puhtaytow Dec 2, 2024
86f54ec
save
puhtaytow Dec 2, 2024
3d99510
remove println from tests / left over after debugging
puhtaytow Dec 11, 2024
ad0c4ba
add tests
puhtaytow Dec 11, 2024
c0813a9
allow dead code
puhtaytow Dec 11, 2024
bcbd780
add mock for InvalidSignersRotatedEventMockTransport
puhtaytow Dec 11, 2024
c2917ef
WTF TEST?!??!?!?!?!?!?!?!
ctoyan Dec 11, 2024
7cdf55e
add test, fix bug
ctoyan Dec 12, 2024
c159159
test and clippy fixes
ctoyan Dec 12, 2024
1c5b95a
tests and fixes
ctoyan Dec 12, 2024
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
2,151 changes: 1,258 additions & 893 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ tokio-stream = "0.1.11"
tokio-util = "0.7.11"
voting-verifier = { version = "^1.1.0", path = "contracts/voting-verifier" }
axelar-core-std = { version = "^1.0.0", path = "packages/axelar-core-std" }
# Async
futures-concurrency = "7.4"
futures-util = "0.3"
futures = "0.3"
async-trait = "0"

[workspace.lints.clippy]
arithmetic_side_effects = "deny"
Expand Down
1 change: 1 addition & 0 deletions ampd/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod multisig;
pub mod mvx_verify_msg;
pub mod mvx_verify_verifier_set;
pub mod starknet_verify_msg;
pub mod starknet_verify_verifier_set;
pub(crate) mod stellar_verify_msg;
pub(crate) mod stellar_verify_verifier_set;
pub mod sui_verify_msg;
Expand Down
161 changes: 161 additions & 0 deletions ampd/src/handlers/starknet_verify_verifier_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//! Module responsible for handling verification of verifier set changes.
//! It processes events related to verifier set, verifies them against the Starknet chain,
//! and manages the voting process for confirming these changes.

use std::convert::TryInto;

use async_trait::async_trait;
use axelar_wasm_std::msg_id::HexTxHashAndEventIndex;
use axelar_wasm_std::voting::{PollId, Vote};
use cosmrs::cosmwasm::MsgExecuteContract;
use cosmrs::tx::Msg;
use cosmrs::Any;
use error_stack::ResultExt;
use events::Error::EventTypeMismatch;
use events::Event;
use events_derive::try_from;
use multisig::verifier_set::VerifierSet;
use serde::Deserialize;
use starknet_core::types::Felt;
use tokio::sync::watch::Receiver;
use tracing::{info, info_span};
use valuable::Valuable;
use voting_verifier::msg::ExecuteMsg;

use crate::event_processor::EventHandler;
use crate::handlers::errors::Error;
use crate::starknet::json_rpc::StarknetClient;
use crate::starknet::verifier::verify_verifier_set;
use crate::types::TMAddress;

#[derive(Deserialize, Debug)]
pub struct VerifierSetConfirmation {
pub message_id: HexTxHashAndEventIndex, // FIXME: in the future replace by FieldElementAndEventIndex
pub verifier_set: VerifierSet,
}

#[derive(Deserialize, Debug)]
#[try_from("wasm-verifier_set_poll_started")]
struct PollStartedEvent {
poll_id: PollId,
source_gateway_address: String,
verifier_set: VerifierSetConfirmation,
participants: Vec<TMAddress>,
expires_at: u64,
}

pub struct Handler<C>
where
C: StarknetClient + Send + Sync,
{
verifier: TMAddress,
voting_verifier_contract: TMAddress,
rpc_client: C,
latest_block_height: Receiver<u64>,
}

impl<C> Handler<C>
where
C: StarknetClient + Send + Sync,
{
/// Handler for verifying verifier set updates from Starknet
///
/// # Type Parameters
/// * `C` - A Starknet client type that implements the [`StarknetClient`] trait
#[allow(dead_code)]
pub fn new(
verifier: TMAddress,
voting_verifier_contract: TMAddress,
rpc_client: C,
latest_block_height: Receiver<u64>,
) -> Self {
Self {
verifier,
voting_verifier_contract,
rpc_client,
latest_block_height,
}
}

fn vote_msg(&self, poll_id: PollId, vote: Vote) -> MsgExecuteContract {
MsgExecuteContract {
sender: self.verifier.as_ref().clone(),
contract: self.voting_verifier_contract.as_ref().clone(),
msg: serde_json::to_vec(&ExecuteMsg::Vote {
poll_id,
votes: vec![vote],
})
.expect("vote msg should serialize"),
funds: vec![],
}
}
}

#[async_trait]
impl<C> EventHandler for Handler<C>
where
C: StarknetClient + Send + Sync + 'static,
{
type Err = Error;

async fn handle(&self, event: &Event) -> error_stack::Result<Vec<Any>, Self::Err> {
if !event.is_from_contract(self.voting_verifier_contract.as_ref()) {
return Ok(vec![]);
}

let PollStartedEvent {
poll_id,
source_gateway_address,
verifier_set,
expires_at,
participants,
} = match event.try_into() as error_stack::Result<_, _> {
Err(report) if matches!(report.current_context(), EventTypeMismatch(_)) => {
return Ok(vec![])
}
event => event.change_context(Error::DeserializeEvent)?,
};

if !participants.contains(&self.verifier) {
return Ok(vec![]);
}

if *self.latest_block_height.borrow() >= expires_at {
info!(poll_id = poll_id.to_string(), "skipping expired poll");
return Ok(vec![]);
}

let transaction_response = self
.rpc_client
.get_event_by_hash_signers_rotated(Felt::from_bytes_be(
&verifier_set.message_id.tx_hash,
))
.await
.unwrap();

let vote = info_span!(
"verify a new verifier set",
poll_id = poll_id.to_string(),
message_id = verifier_set.message_id.to_string(),
)
.in_scope(|| {
info!("ready to verify verifier set in poll",);

let vote = transaction_response.map_or(Vote::NotFound, |tx_receipt| {
verify_verifier_set(&tx_receipt.1, &verifier_set, &source_gateway_address)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is tx_receipt.1 always the gateway event itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption here is, that ampd receives to "verify" a correct id. If its not then verify_verifier_set would return Vote::NotFound, which is the expected behaviour.

});

info!(
vote = vote.as_value(),
"ready to vote for a new verifier set in poll"
);

vote
});

Ok(vec![self
.vote_msg(poll_id, vote)
.into_any()
.expect("vote msg should serialize")])
}
}
Loading
Loading