Skip to content

Commit

Permalink
Problem: gRPC endpoints not supporting ibc-go 2.0.0 (#79)
Browse files Browse the repository at this point in the history
Solution: Changed amounts in solo machine to `U256`
  • Loading branch information
devashishdxt authored Nov 18, 2021
1 parent 76cb11d commit 2fda0da
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 70 deletions.
243 changes: 203 additions & 40 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion event-hooks/stdout-logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ edition = "2021"
crate-type = ["dylib"]

[dependencies]
anyhow = "1.0.44"
anyhow = "1.0.45"
async-trait = "0.1.51"
solo-machine-core = { path = "../../solo-machine-core" }
2 changes: 1 addition & 1 deletion signers/mnemonic-signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
crate-type = ["dylib"]

[dependencies]
anyhow = "1.0.44"
anyhow = "1.0.45"
async-trait = "0.1.51"
bip32 = { version = "0.2.2", features = ["bip39"] }
k256 = { version = "0.9.6", features = ["ecdsa"] }
Expand Down
7 changes: 4 additions & 3 deletions solo-machine-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.44"
anyhow = "1.0.45"
async-trait = "0.1.51"
bech32 = "0.8.1"
chrono = { version = "0.4.19", default-features = false }
Expand All @@ -16,14 +16,15 @@ ed25519-dalek = { version = "1.0.1", features = ["serde"] }
hex = { version = "0.4.3", features = ["serde"] }
k256 = { version = "0.9.6", features = ["ecdsa"] }
num-rational = { version = "0.4.0", features = ["serde"] }
primitive-types = { version = "0.10.1", features = ["serde"] }
prost = "0.9.0"
prost-types = "0.9.0"
rand = "0.8.4"
regex = "1.5.4"
ripemd160 = "0.9.1"
rust_decimal = "1.17.0"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.68"
serde_json = "1.0.69"
sha2 = "0.9.8"
sha3 = { version = "0.9.1", optional = true }
sqlx = { version = "0.5.9", features = [
Expand All @@ -36,7 +37,7 @@ sqlx = { version = "0.5.9", features = [
tendermint = "0.23.0"
tendermint-light-client = "0.23.0"
tendermint-rpc = { version = "0.23.0", features = ["http-client"] }
tokio = { version = "1.12.0", features = ["sync"] }
tokio = { version = "1.13.0", features = ["sync"] }
tonic = { version = "0.6.1", features = ["tls", "tls-roots"] }
urlencoding = "2.1.0"

Expand Down
5 changes: 3 additions & 2 deletions solo-machine-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod event_handler;

use anyhow::{anyhow, Result};
use primitive_types::U256;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::UnboundedSender;

Expand All @@ -28,7 +29,7 @@ pub enum Event {
/// Address of account on IBC enabled chain
to_address: String,
/// Amount of tokens minted
amount: u64,
amount: U256,
/// Denom of tokens minted
denom: Identifier,
/// Hash of transaction on IBC enabled chain (in hex)
Expand All @@ -43,7 +44,7 @@ pub enum Event {
/// Address of account on IBC enabled chain
from_address: String,
/// Amount of tokens minted
amount: u64,
amount: U256,
/// Denom of tokens minted
denom: Identifier,
/// Hash of transaction on IBC enabled chain (in hex)
Expand Down
21 changes: 13 additions & 8 deletions solo-machine-core/src/model/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use anyhow::{ensure, Context, Error, Result};
use chrono::{DateTime, Utc};
use primitive_types::U256;
use serde::{Deserialize, Serialize};
use sqlx::{types::Json, Executor, FromRow};

Expand All @@ -25,7 +26,7 @@ pub struct Operation {
/// Denom of tokens
pub denom: Identifier,
/// Amount of tokens
pub amount: u64,
pub amount: U256,
/// Type of operation
pub operation_type: OperationType,
/// On-chain transaction hash (in hex)
Expand Down Expand Up @@ -57,12 +58,14 @@ pub struct RawOperation {

impl From<Operation> for RawOperation {
fn from(op: Operation) -> Self {
let amount_bytes: [u8; 32] = op.amount.into();

Self {
id: op.id,
request_id: op.request_id,
address: op.address,
denom: op.denom.to_string(),
amount: op.amount.to_le_bytes().to_vec(),
amount: amount_bytes.to_vec(),
operation_type: Json(op.operation_type),
transaction_hash: op.transaction_hash,
created_at: op.created_at,
Expand All @@ -74,11 +77,11 @@ impl TryFrom<RawOperation> for Operation {
type Error = Error;

fn try_from(op: RawOperation) -> Result<Self, Self::Error> {
let mut amount_bytes = [0; 8];
let mut amount_bytes = [0; 32];

ensure!(
op.amount.len() == 8,
"expected amount in u64 little endian bytes {}",
op.amount.len() == 32,
"expected amount in u256 little endian bytes {}",
op.amount.len()
);

Expand All @@ -89,7 +92,7 @@ impl TryFrom<RawOperation> for Operation {
request_id: op.request_id,
address: op.address,
denom: op.denom.parse()?,
amount: u64::from_le_bytes(amount_bytes),
amount: amount_bytes.into(),
operation_type: op.operation_type.0,
transaction_hash: op.transaction_hash,
created_at: op.created_at,
Expand Down Expand Up @@ -127,19 +130,21 @@ pub async fn add_operation<'e>(
request_id: Option<&str>,
address: &str,
denom: &Identifier,
amount: u64,
amount: U256,
operation_type: &OperationType,
transaction_hash: &str,
) -> Result<()> {
let operation_type = Json(operation_type);

let amount_bytes: [u8; 32] = amount.into();

let rows_affected = sqlx::query(
"INSERT INTO operations (request_id, address, denom, amount, operation_type, transaction_hash) VALUES ($1, $2, $3, $4, $5, $6)",
)
.bind(request_id)
.bind(address)
.bind(denom.to_string())
.bind(amount.to_le_bytes().to_vec())
.bind(amount_bytes.to_vec())
.bind(operation_type)
.bind(transaction_hash)
.execute(executor)
Expand Down
5 changes: 3 additions & 2 deletions solo-machine-core/src/service/ibc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cosmos_sdk_proto::ibc::core::{
Version as ConnectionVersion,
},
};
use primitive_types::U256;
use sqlx::{Executor, Transaction};
use tendermint::{
abci::{
Expand Down Expand Up @@ -456,7 +457,7 @@ impl IbcService {
signer: impl Signer,
chain_id: ChainId,
request_id: Option<String>,
amount: u64,
amount: U256,
denom: Identifier,
receiver: Option<String>,
memo: String,
Expand Down Expand Up @@ -556,7 +557,7 @@ impl IbcService {
signer: impl Signer,
chain_id: ChainId,
request_id: Option<String>,
amount: u64,
amount: U256,
denom: Identifier,
memo: String,
) -> Result<String> {
Expand Down
5 changes: 3 additions & 2 deletions solo-machine-core/src/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use cosmos_sdk_proto::{
},
},
};
use primitive_types::U256;
use prost_types::{Any, Duration};
use serde::Serialize;
use serde_json::json;
Expand Down Expand Up @@ -403,7 +404,7 @@ pub async fn msg_token_send<C>(
signer: impl Signer,
rpc_client: &C,
chain: &mut Chain,
amount: u64,
amount: U256,
denom: &Identifier,
receiver: String,
memo: String,
Expand Down Expand Up @@ -480,7 +481,7 @@ where
pub async fn msg_token_receive(
signer: impl Signer,
chain: &Chain,
amount: u64,
amount: U256,
denom: &Identifier,
receiver: String,
memo: String,
Expand Down
7 changes: 4 additions & 3 deletions solo-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.44"
anyhow = "1.0.45"
async-trait = "0.1.51"
bip32 = { version = "0.2.2", features = ["bip39"] }
cli-table = { version = "0.4.6", default-features = false, features = [
Expand All @@ -21,17 +21,18 @@ k256 = { version = "0.9.6", features = ["ecdsa"] }
libloading = "0.7.1"
log = "0.4.14"
num-rational = "0.4.0"
primitive-types = "0.10.1"
prost = "0.9.0"
prost-types = "0.9.0"
rust_decimal = "1.17.0"
serde_json = "1.0.68"
serde_json = "1.0.69"
solo-machine-core = { path = "../solo-machine-core", features = [
"solomachine-v2",
] }
structopt = "0.3.25"
tendermint = "0.23.0"
termcolor = "1.1.2"
tokio = { version = "1.12.0", features = ["fs", "macros", "rt-multi-thread"] }
tokio = { version = "1.13.0", features = ["fs", "macros", "rt-multi-thread"] }
tonic = { version = "0.6.1", features = ["tls", "tls-roots"] }

[features]
Expand Down
6 changes: 3 additions & 3 deletions solo-machine/proto/ibc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ message MintRequest {
// Memo value to be used in cosmos sdk transaction
optional string memo = 3;
// Amount of tokens to be sent
uint64 amount = 4;
string amount = 4;
// Denom of tokens to be sent
string denom = 5;
// Receiver address on IBC enabled chain (if this is not provided, tokens will be sent to signer's address)
Expand All @@ -62,7 +62,7 @@ message BurnRequest {
// Memo value to be used in cosmos sdk transaction
optional string memo = 3;
// Amount of tokens to be sent
uint64 amount = 4;
string amount = 4;
// Denom of tokens to be sent
string denom = 5;
}
Expand Down Expand Up @@ -109,7 +109,7 @@ message Operation {
// Denom of account
string denom = 4;
// Amount associated with operation
uint64 amount = 5;
string amount = 5;
// Type of operation (e.g., mint, burn, send, receive)
string operation_type = 6;
// On-chain transaction hash (in hex)
Expand Down
5 changes: 3 additions & 2 deletions solo-machine/src/command/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cli_table::{
format::Justify, print_stdout, Cell, Color, ColorChoice, Row, RowStruct, Style, Table,
};
use k256::ecdsa::VerifyingKey;
use primitive_types::U256;
use serde_json::json;
use solo_machine_core::{
cosmos::crypto::{PublicKey, PublicKeyAlgo},
Expand Down Expand Up @@ -62,7 +63,7 @@ pub enum IbcCommand {
/// Chain ID of IBC enabled chain
chain_id: ChainId,
/// Amount to send to IBC enabled chain
amount: u64,
amount: U256,
/// Denom of tokens to send to IBC enabled chain
denom: Identifier,
/// Optional receiver address (if this is not provided, tokens will be sent to signer's address)
Expand All @@ -84,7 +85,7 @@ pub enum IbcCommand {
/// Chain ID of IBC enabled chain
chain_id: ChainId,
/// Amount to receive from IBC enabled chain
amount: u64,
amount: U256,
/// Denom of tokens to receive from IBC enabled chain
denom: Identifier,
/// Optional memo to include in transactions
Expand Down
9 changes: 6 additions & 3 deletions solo-machine/src/server/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ tonic::include_proto!("ibc");
use std::time::SystemTime;

use k256::ecdsa::VerifyingKey;
use primitive_types::U256;
use solo_machine_core::{
cosmos::crypto::{PublicKey, PublicKeyAlgo},
ibc::core::ics24_host::identifier::ChainId,
Expand Down Expand Up @@ -72,7 +73,8 @@ where
.map_err(|err: anyhow::Error| Status::invalid_argument(err.to_string()))?;
let request_id = request.request_id;
let memo = request.memo.unwrap_or_else(|| DEFAULT_MEMO.to_owned());
let amount = request.amount;
let amount = U256::from_dec_str(&request.amount)
.map_err(|err| Status::invalid_argument(err.to_string()))?;
let denom = request
.denom
.parse()
Expand Down Expand Up @@ -108,7 +110,8 @@ where
.map_err(|err: anyhow::Error| Status::invalid_argument(err.to_string()))?;
let request_id = request.request_id;
let memo = request.memo.unwrap_or_else(|| DEFAULT_MEMO.to_owned());
let amount = request.amount;
let amount = U256::from_dec_str(&request.amount)
.map_err(|err| Status::invalid_argument(err.to_string()))?;
let denom = request
.denom
.parse()
Expand Down Expand Up @@ -197,7 +200,7 @@ where
request_id: op.request_id,
address: op.address,
denom: op.denom.to_string(),
amount: op.amount,
amount: op.amount.to_string(),
operation_type: op.operation_type.to_string(),
transaction_hash: op.transaction_hash,
created_at: Some(SystemTime::from(op.created_at).into()),
Expand Down

0 comments on commit 2fda0da

Please sign in to comment.