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

RPC json elements now can return proper addresses instead of hex values #1238

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 34 additions & 4 deletions chainstate/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

mod types;

use std::io::{Read, Write};
use std::{
convert::Infallible,
io::{Read, Write},
sync::Arc,
};

use crate::{Block, BlockSource, ChainInfo, GenBlock};
use chainstate_types::BlockIndex;
use common::{
address::dehexify::dehexify_all_addresses,
chain::{
tokens::{RPCTokenInfo, TokenId},
DelegationId, PoolId, SignedTransaction, Transaction,
ChainConfig, DelegationId, PoolId, SignedTransaction, Transaction,
},
primitives::{Amount, BlockHeight, Id},
};
Expand Down Expand Up @@ -168,7 +173,19 @@ impl ChainstateRpcServer for super::ChainstateHandle {
.await,
)?;
let rpc_blk = both.map(|(block, block_index)| RpcBlock::new(block, block_index));
Ok(rpc_blk.map(JsonEncoded::new).map(|blk| blk.to_string()))
let result = rpc_blk.map(JsonEncoded::new).map(|blk| blk.to_string());

let chain_config: Arc<ChainConfig> = rpc::handle_result(
self.call(move |this| {
let chain_config = Arc::clone(this.get_chain_config());
Ok::<_, Infallible>(chain_config)
})
.await,
)?;

let result: Option<String> = result.map(|res| dehexify_all_addresses(&chain_config, &res));

Ok(result)
}

async fn get_transaction(
Expand All @@ -184,7 +201,20 @@ impl ChainstateRpcServer for super::ChainstateHandle {
let tx: Option<SignedTransaction> =
rpc::handle_result(self.call(move |this| this.get_transaction(&id)).await)?;
let rpc_tx = tx.map(RpcSignedTransaction::new);
Ok(rpc_tx.map(JsonEncoded::new).map(|tx| tx.to_string()))

let chain_config: Arc<ChainConfig> = rpc::handle_result(
self.call(move |this| {
let chain_config = Arc::clone(this.get_chain_config());
Ok::<_, Infallible>(chain_config)
})
.await,
)?;

let result = rpc_tx.map(JsonEncoded::new).map(|tx| tx.to_string());

let result: Option<String> = result.map(|res| dehexify_all_addresses(&chain_config, &res));

Ok(result)
}

async fn get_mainchain_blocks(
Expand Down
3 changes: 3 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ ref-cast.workspace = true
serde = { workspace = true, features = ["derive"] }
static_assertions.workspace = true
thiserror.workspace = true
variant_count.workspace = true

regex = "1.9"

[dev-dependencies]
test-utils = {path = '../test-utils'}
Expand Down
30 changes: 30 additions & 0 deletions common/src/address/dehexify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023 RBB S.r.l
// opensource@mintlayer.org
// SPDX-License-Identifier: MIT
// Licensed under the MIT License;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::chain::{tokens::TokenId, ChainConfig, DelegationId, Destination, PoolId};

use super::hexified::HexifiedAddress;

#[allow(clippy::let_and_return)]
pub fn dehexify_all_addresses(conf: &ChainConfig, input: &str) -> String {
let result = HexifiedAddress::<Destination>::replace_with_address(conf, input).to_string();
let result = HexifiedAddress::<PoolId>::replace_with_address(conf, &result).to_string();
let result = HexifiedAddress::<DelegationId>::replace_with_address(conf, &result).to_string();
let result = HexifiedAddress::<TokenId>::replace_with_address(conf, &result).to_string();

result
}

// TODO: add tests that create blocks, and ensure the replacement in json works properly.
Loading
Loading