Skip to content

Commit c424a80

Browse files
committed
handle wasm messages in a module
1 parent af79a1a commit c424a80

File tree

20 files changed

+2134
-1733
lines changed

20 files changed

+2134
-1733
lines changed

Cargo.lock

Lines changed: 1891 additions & 1591 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/nyxd-scraper-psql/src/storage/manager.rs

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::models::Coin;
54
use crate::storage::models::{CommitSignature, Validator};
65
use nyxd_scraper_shared::storage::helpers::log_db_operation_time;
76
use sqlx::types::time::PrimitiveDateTime;
@@ -403,10 +402,6 @@ pub(crate) async fn insert_message<'a, E>(
403402
value: JsonValue,
404403
involved_account_addresses: Vec<String>,
405404
height: i64,
406-
wasm_sender: Option<String>,
407-
wasm_contract_address: Option<String>,
408-
wasm_message_type: Option<String>,
409-
funds: Option<Vec<Coin>>,
410405
executor: E,
411406
) -> Result<(), sqlx::Error>
412407
where
@@ -415,55 +410,25 @@ where
415410
trace!("insert_message");
416411
let start = Instant::now();
417412

418-
// sqlx doesn't understand option types
419-
if let Some(coins) = funds {
420-
sqlx::query!(
421-
r#"
422-
INSERT INTO message(transaction_hash, index, type, value, involved_accounts_addresses, height, wasm_sender, wasm_contract_address, wasm_message_type, funds)
423-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
424-
ON CONFLICT (transaction_hash, index) DO UPDATE
425-
SET height = excluded.height,
426-
type = excluded.type,
427-
value = excluded.value,
428-
involved_accounts_addresses = excluded.involved_accounts_addresses
429-
"#,
430-
transaction_hash,
431-
index,
432-
typ,
433-
value,
434-
&involved_account_addresses,
435-
height,
436-
wasm_sender,
437-
wasm_contract_address,
438-
wasm_message_type,
439-
&coins as _,
440-
)
441-
.execute(executor)
442-
.await?;
443-
} else {
444-
sqlx::query!(
445-
r#"
446-
INSERT INTO message(transaction_hash, index, type, value, involved_accounts_addresses, height, wasm_sender, wasm_contract_address, wasm_message_type)
447-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
448-
ON CONFLICT (transaction_hash, index) DO UPDATE
449-
SET height = excluded.height,
450-
type = excluded.type,
451-
value = excluded.value,
452-
involved_accounts_addresses = excluded.involved_accounts_addresses
453-
"#,
454-
transaction_hash,
455-
index,
456-
typ,
457-
value,
458-
&involved_account_addresses,
459-
height,
460-
wasm_sender,
461-
wasm_contract_address,
462-
wasm_message_type,
463-
)
464-
.execute(executor)
465-
.await?;
466-
}
413+
sqlx::query!(
414+
r#"
415+
INSERT INTO message(transaction_hash, index, type, value, involved_accounts_addresses, height)
416+
VALUES ($1, $2, $3, $4, $5, $6)
417+
ON CONFLICT (transaction_hash, index) DO UPDATE
418+
SET height = excluded.height,
419+
type = excluded.type,
420+
value = excluded.value,
421+
involved_accounts_addresses = excluded.involved_accounts_addresses
422+
"#,
423+
transaction_hash,
424+
index,
425+
typ,
426+
value,
427+
&involved_account_addresses,
428+
height,
429+
)
430+
.execute(executor)
431+
.await?;
467432
log_db_operation_time("insert_message", start);
468433

469434
Ok(())
@@ -482,7 +447,7 @@ where
482447

483448
sqlx::query!(
484449
"UPDATE metadata SET last_processed_height = GREATEST(last_processed_height, $1)",
485-
height
450+
height as i32
486451
)
487452
.execute(executor)
488453
.await?;

common/nyxd-scraper-psql/src/storage/models.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ pub struct CommitSignature {
3232

3333
#[derive(Debug, Serialize, Deserialize, sqlx::Type)]
3434
#[sqlx(type_name = "coin")]
35-
pub struct Coin {
35+
pub struct DbCoin {
3636
pub amount: String,
3737
pub denom: String,
3838
}
39+
40+
impl From<cosmrs::proto::cosmos::base::v1beta1::Coin> for DbCoin {
41+
fn from(coin: cosmrs::proto::cosmos::base::v1beta1::Coin) -> Self {
42+
Self {
43+
amount: coin.amount,
44+
denom: coin.denom,
45+
}
46+
}
47+
}

common/nyxd-scraper-psql/src/storage/transaction.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::error::PostgresScraperError;
5-
use crate::models::Coin;
65
use crate::storage::helpers::parse_addresses_from_events;
76
use crate::storage::manager::{
87
insert_block, insert_message, insert_precommit, insert_transaction, insert_validator,
@@ -12,8 +11,6 @@ use async_trait::async_trait;
1211
use base64::engine::general_purpose;
1312
use base64::Engine as _;
1413
use cosmrs::proto;
15-
use cosmrs::proto::cosmwasm::wasm::v1::MsgExecuteContract;
16-
use cosmrs::proto::prost::Message;
1714
use nyxd_scraper_shared::helpers::{
1815
validator_consensus_address, validator_info, validator_pubkey_to_bech32,
1916
};
@@ -22,7 +19,7 @@ use nyxd_scraper_shared::storage::{
2219
validators, Block, Commit, CommitSig, NyxdScraperStorageError, NyxdScraperTransaction,
2320
};
2421
use nyxd_scraper_shared::ParsedTransactionResponse;
25-
use serde_json::{json, Value};
22+
use serde_json::json;
2623
use sqlx::types::time::{OffsetDateTime, PrimitiveDateTime};
2724
use sqlx::{Postgres, Transaction};
2825
use std::ops::{Deref, DerefMut};
@@ -201,45 +198,16 @@ impl PostgresStorageTransaction {
201198
for chain_tx in txs {
202199
let involved_addresses = parse_addresses_from_events(chain_tx);
203200
for (index, msg) in chain_tx.tx.body.messages.iter().enumerate() {
204-
let mut wasm_sender: Option<String> = None;
205-
let mut wasm_contract_address: Option<String> = None;
206-
let mut wasm_message_type: Option<String> = None;
207-
let mut funds: Option<Vec<Coin>> = None;
208-
209201
let parsed_message = chain_tx.parsed_messages.get(&index);
210202
let value = serde_json::to_value(parsed_message)?;
211203

212-
if msg.type_url == "/cosmwasm.wasm.v1.MsgExecuteContract" {
213-
if let Ok(wasm_execute) = MsgExecuteContract::decode(msg.value.as_ref()) {
214-
wasm_sender = Some(wasm_execute.sender);
215-
wasm_contract_address = Some(wasm_execute.contract);
216-
if let Some(raw_msg) = value.get("msg") {
217-
wasm_message_type = get_first_field_name(raw_msg);
218-
}
219-
funds = Some(
220-
wasm_execute
221-
.funds
222-
.iter()
223-
.map(|c| Coin {
224-
amount: c.amount.to_string(),
225-
denom: c.denom.clone(),
226-
})
227-
.collect(),
228-
);
229-
}
230-
}
231-
232204
insert_message(
233205
chain_tx.hash.to_string(),
234206
index as i64,
235207
msg.type_url.clone(),
236208
value,
237209
involved_addresses.clone(),
238210
chain_tx.height.into(),
239-
wasm_sender,
240-
wasm_contract_address,
241-
wasm_message_type,
242-
funds,
243211
self.inner.as_mut(),
244212
)
245213
.await?
@@ -256,14 +224,6 @@ impl PostgresStorageTransaction {
256224
}
257225
}
258226

259-
fn get_first_field_name(value: &Value) -> Option<String> {
260-
trace!("value:\n{value}");
261-
match value.as_object() {
262-
Some(map) => map.keys().next().cloned(),
263-
None => None,
264-
}
265-
}
266-
267227
#[async_trait]
268228
impl NyxdScraperTransaction for PostgresStorageTransaction {
269229
async fn commit(self) -> Result<(), NyxdScraperStorageError> {

common/nyxd-scraper-shared/src/block_processor/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct ParsedTransactionResponse {
2929
pub proof: Option<tx::Proof>,
3030

3131
pub parsed_messages: HashMap<usize, serde_json::Value>,
32+
33+
pub block: Block,
3234
}
3335

3436
#[derive(Debug)]

common/nyxd-scraper-shared/src/rpc_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl RpcClient {
9494
tx,
9595
proof: raw_tx.proof,
9696
parsed_messages,
97+
block: block.block.clone(),
9798
})
9899
}
99100

nym-data-observatory/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async-trait.workspace = true
1818
axum = { workspace = true, features = ["tokio"] }
1919
chrono = { workspace = true }
2020
clap = { workspace = true, features = ["cargo", "derive", "env"] }
21+
cosmrs = { workspace = true }
2122
nym-config = { path = "../common/config" }
2223
nym-bin-common = { path = "../common/bin-common", features = ["output_format"] }
2324
nym-network-defaults = { path = "../common/network-defaults" }
@@ -28,6 +29,7 @@ nyxd-scraper-shared = { path = "../common/nyxd-scraper-shared" }
2829
reqwest = { workspace = true, features = ["rustls-tls"] }
2930
schemars = { workspace = true }
3031
serde = { workspace = true, features = ["derive"] }
32+
serde_json = { workspace = true }
3133
sqlx = { workspace = true, features = ["runtime-tokio-rustls", "postgres", "time"] }
3234
thiserror = { workspace = true }
3335
time = { workspace = true }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CREATE TABLE wasm_execute_contract
2+
(
3+
sender TEXT NOT NULL,
4+
contract_address TEXT NOT NULL,
5+
message_type TEXT NULL,
6+
raw_contract_message JSONB NOT NULL DEFAULT '{}'::JSONB,
7+
funds COIN[] NOT NULL DEFAULT '{}',
8+
fee COIN[] NOT NULL DEFAULT '{}',
9+
executed_at TIMESTAMP NOT NULL,
10+
height BIGINT NOT NULL,
11+
hash TEXT NOT NULL,
12+
message_index BIGINT NOT NULL,
13+
memo TEXT NULL
14+
);
15+
CREATE INDEX execute_contract_height_index ON wasm_execute_contract (height);
16+
CREATE INDEX execute_contract_executed_at_index ON wasm_execute_contract (executed_at);
17+
CREATE INDEX execute_contract_message_type_index ON wasm_execute_contract (message_type);
18+
CREATE INDEX execute_contract_sender ON wasm_execute_contract (sender);

nym-data-observatory/migrations/0104_add_listener_failure_table.sql renamed to nym-data-observatory/migrations/0100_startup_info.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: GPL-3.0-only
44
*/
55

6-
CREATE TABLE watcher_execution
6+
CREATE TABLE startup_info
77
(
88
start_ts TIMESTAMPTZ NOT NULL,
99
end_ts TIMESTAMPTZ NOT NULL,

nym-data-observatory/migrations/0102_payment_transactions.sql

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)