Skip to content

Commit

Permalink
[C++]: Add Blockchain entry to the Coin.cpp dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Nov 23, 2023
1 parent b2c0746 commit d523af4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
72 changes: 72 additions & 0 deletions codegen-v2/src/codegen/cpp/blockchain_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::codegen::cpp::cpp_source_directory;
use crate::registry::CoinItem;
use crate::utils::FileContent;
use crate::{Error, Result};
use std::path::PathBuf;

const COIN_INCLUDES_END: &str = "end_of_coin_includes_marker_do_not_modify";
const COIN_DISPATCHER_DECLARATIONS_END: &str =
"end_of_coin_dipatcher_declarations_marker_do_not_modify";
const COIN_DISPATCHER_SWITCH_END: &str = "end_of_coin_dipatcher_switch_marker_do_not_modify";

fn dispatcher_coin_cpp_path() -> PathBuf {
cpp_source_directory().join("Coin.cpp")
}

/// Represents `Coin.cpp`.
pub struct BlockchainDispatcher {
coin: CoinItem,
}

impl BlockchainDispatcher {
pub fn new(coin: CoinItem) -> BlockchainDispatcher {
BlockchainDispatcher { coin }
}

pub fn add(self) -> Result<()> {
let mut file_content = FileContent::read(dispatcher_coin_cpp_path())?;

self.add_include_of_blockchain_entry(&mut file_content)?;
self.add_blockchain_entry(&mut file_content)?;
self.add_blockchain_dispatcher(&mut file_content)?;

file_content.write()
}

fn add_include_of_blockchain_entry(&self, file_content: &mut FileContent) -> Result<()> {
let blockchain_type = self.coin.blockchain_type();

let mut line_marker = file_content.rfind_line(|line| line.contains(COIN_INCLUDES_END))?;
line_marker.push_line_before(format!(r#"#include "{blockchain_type}/Entry.h""#));

Ok(())
}

fn add_blockchain_entry(&self, file_content: &mut FileContent) -> Result<()> {
let blockchain_type = self.coin.blockchain_type();

let mut entries_region =
file_content.rfind_line(|line| line.contains(COIN_DISPATCHER_DECLARATIONS_END))?;
entries_region.push_line_before(format!("{blockchain_type}::Entry {blockchain_type}DP;"));

Ok(())
}

fn add_blockchain_dispatcher(&self, file_content: &mut FileContent) -> Result<()> {
let blockchain_type = self.coin.blockchain_type();

let mut entries_region =
file_content.rfind_line(|line| line.contains(COIN_DISPATCHER_SWITCH_END))?;
entries_region.push_line_before(format!(
" case TWBlockchain{blockchain_type}: entry = &{blockchain_type}DP; break;"
));

Ok(())
}
}
1 change: 1 addition & 0 deletions codegen-v2/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::env;
use std::path::PathBuf;

pub mod blockchain_registry;
pub mod coin_entry;
pub mod new_blockchain;
pub mod tw_blockchain;
Expand Down
3 changes: 3 additions & 0 deletions codegen-v2/src/codegen/cpp/new_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::codegen::cpp::blockchain_registry::BlockchainDispatcher;
use crate::codegen::cpp::coin_entry::BlockchainImpl;
use crate::codegen::cpp::tw_blockchain::TWBlockchain;
use crate::codegen::cpp::tw_coin_type::TWCoinType;
Expand All @@ -18,6 +19,8 @@ pub fn new_blockchain(coin: CoinItem) -> Result<()> {
TWCoinType::new(coin.clone()).add_coin_type_variant()?;
// Add the new blockchain type to the `TWBlockchain` enum.
TWBlockchain::new(coin.clone()).add_blockchain_type_variant()?;
// Add the blockchain entry to the dispatcher `Coin.cpp`.
BlockchainDispatcher::new(coin).add()?;

Ok(())
}
3 changes: 2 additions & 1 deletion codegen-v2/src/codegen/cpp/tw_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn tw_blockchain_path() -> PathBuf {
cpp_include_directory().join("TWBlockchain.h")
}

/// Represents `TWBlockchain.h`.
pub struct TWBlockchain {
coin: CoinItem,
}
Expand All @@ -27,7 +28,7 @@ impl TWBlockchain {
}

pub fn add_blockchain_type_variant(self) -> Result<()> {
let coin_type = self.coin.coin_type();
let coin_type = self.coin.blockchain_type();

let mut tw_blockchain_type_rs = FileContent::read(tw_blockchain_path())?;

Expand Down
3 changes: 2 additions & 1 deletion codegen-v2/src/codegen/cpp/tw_coin_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use crate::utils::FileContent;
use crate::Result;
use std::path::PathBuf;

const TW_COIN_TYPE_END: &str = "end_of_tw_coin_type";
const TW_COIN_TYPE_END: &str = "end_of_tw_coin_type_marker_do_not_modify";

pub fn tw_coin_type_path() -> PathBuf {
cpp_include_directory().join("TWCoinType.h")
}

/// Represents `TWCoinType.h`.
pub struct TWCoinType {
coin: CoinItem,
}
Expand Down
2 changes: 1 addition & 1 deletion include/TrustWalletCore/TWCoinType.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ enum TWCoinType {
TWCoinTypeZenEON = 7332,
TWCoinTypeInternetComputer = 223,
TWCoinTypeTia = 21000118,
// end_of_tw_coin_type
// end_of_tw_coin_type_marker_do_not_modify
};

/// Returns the blockchain for a coin type.
Expand Down

0 comments on commit d523af4

Please sign in to comment.