-
Notifications
You must be signed in to change notification settings - Fork 107
feat: implement system message to revalidate a user message with external proofs #670
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,8 @@ use crate::proto::message_data::Body; | |
| use crate::proto::BlockEvent; | ||
| use crate::proto::{ | ||
| self, hub_event, FarcasterNetwork, HubEvent, HubEventType, MessageType, OnChainEvent, | ||
| OnChainEventType, Protocol, ShardChunk, Transaction, UserDataType, UserNameProof, | ||
| OnChainEventType, Protocol, RevalidateMessage, ShardChunk, Transaction, UserDataType, | ||
| UserNameProof, | ||
| }; | ||
| use crate::storage::db::{PageOptions, RocksDB, RocksDbTransactionBatch}; | ||
| use crate::storage::store::account::{ | ||
|
|
@@ -606,6 +607,35 @@ impl ShardEngine { | |
| }) | ||
| } | ||
|
|
||
| fn revalidate_user_message( | ||
| &mut self, | ||
| revalidate_message: &RevalidateMessage, | ||
| timestamp: &FarcasterTime, | ||
| version: EngineVersion, | ||
| txn_batch: &RocksDbTransactionBatch, | ||
| ) -> bool { | ||
| let message = revalidate_message.message.as_ref().unwrap(); | ||
| match &message.data.as_ref().unwrap().body { | ||
| Some(Body::UsernameProofBody(username_proof)) => { | ||
| if let Some(external_data) = &revalidate_message.external_data { | ||
| if let Some(ens_resolved_address) = &external_data.ens_resolved_address { | ||
| if let Err(_) = self.stores.validate_ens_username_proof( | ||
| message.fid(), | ||
| &username_proof, | ||
| ens_resolved_address, | ||
| ) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| _ => {} | ||
| }; | ||
|
|
||
| self.validate_user_message(&message, timestamp, version, true, txn_batch) | ||
| .is_ok() | ||
| } | ||
|
|
||
| pub(crate) fn replay_snapchain_txn( | ||
| &mut self, | ||
| trie_ctx: &merkle_trie::Context, | ||
|
|
@@ -650,6 +680,29 @@ impl ShardEngine { | |
| } | ||
| }); | ||
| for msg in sorted_system_messages { | ||
| if version.is_enabled(ProtocolFeature::RevalidateMessage) { | ||
| if let Some(revalidate_message) = &msg.revalidate_message { | ||
| let is_valid = self.revalidate_user_message( | ||
| &revalidate_message, | ||
| timestamp, | ||
| version, | ||
| txn_batch, | ||
| ); | ||
|
|
||
| if !is_valid { | ||
| if let Ok(event) = self.stores.revoke_message( | ||
| &revalidate_message.message.as_ref().unwrap(), | ||
| txn_batch, | ||
| ) { | ||
| self.update_trie(trie_ctx, &event, txn_batch)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be worth logging here if we're revoking due to a revalidate. I don't think we have any other visibility into the fact that revalidates are happening? |
||
| events.push(event); | ||
| } | ||
| } | ||
|
|
||
| system_messages_count += 1; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should emit a metric for revalidate messages somewhere. Otherwise we'd have no visibility that these messages are being submitted and processed. |
||
| } | ||
| } | ||
|
|
||
| if let Some(onchain_event) = &msg.on_chain_event { | ||
| if onchain_event.r#type() == OnChainEventType::EventTypeTierPurchase | ||
| && !version.is_enabled(ProtocolFeature::FarcasterPro) | ||
|
|
@@ -854,7 +907,11 @@ impl ShardEngine { | |
|
|
||
| for msg in &sorted_user_messages { | ||
| // Errors are validated based on the shard root | ||
| match self.validate_user_message(msg, timestamp, version, txn_batch) { | ||
| let is_pro_user = self | ||
| .stores | ||
| .is_pro_user(msg.fid(), timestamp) | ||
| .map_err(|err| HubError::internal_db_error(&err.to_string()))?; | ||
| match self.validate_user_message(msg, timestamp, version, is_pro_user, txn_batch) { | ||
| Ok(()) => { | ||
| let result = self.merge_message(msg, txn_batch); | ||
| match result { | ||
|
|
@@ -1270,6 +1327,7 @@ impl ShardEngine { | |
| message: &proto::Message, | ||
| timestamp: &FarcasterTime, | ||
| version: EngineVersion, | ||
| is_pro_user: bool, | ||
| txn_batch: &RocksDbTransactionBatch, | ||
| ) -> Result<(), MessageValidationError> { | ||
| let now = std::time::Instant::now(); | ||
|
|
@@ -1286,10 +1344,6 @@ impl ShardEngine { | |
| .as_ref() | ||
| .ok_or(MessageValidationError::NoMessageData)?; | ||
|
|
||
| let is_pro_user = self | ||
| .stores | ||
| .is_pro_user(message.fid(), timestamp) | ||
| .map_err(|err| HubError::internal_db_error(&err.to_string()))?; | ||
| validations::message::validate_message( | ||
| message, | ||
| self.network, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why hardcode true for
is_pro_user?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to determine if the user was a pro user at the block timestamp where the message was originally processed in order to infer this state correctly. We don't have a good way to go from message -> block right now. Defaulting to true essentially always allows the pro-user specific features and bypasses this validation.