Skip to content

Commit

Permalink
adds genesis hash flagging so that oreowallet will support testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks committed Oct 10, 2024
1 parent 055e1e7 commit 58dc34b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
2 changes: 2 additions & 0 deletions crates/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub const OREOS_VALUE: &str = "1";
pub const OREOSRIPTIONS_ENDPOINT: &str = "http://localhost:20001/api";
pub const MAINNET_GENESIS_HASH: &str =
"eac623b099b8081d2bde92d43a4a7795385c94e2c0ae4097ef488972e83ff2b3";
pub const TESTNET_GENESIS_HASH: &str =
"7999c680bbd15d9adb7392e0c27a7caac7e596de5560c18e96365d0fd68140e3";
pub const MAINNET_GENESIS_SEQUENCE: i64 = 1;
pub const REORG_DEPTH: i64 = 100;
pub const SECONDARY_BATCH: i64 = 10000;
Expand Down
2 changes: 1 addition & 1 deletion crates/dservice/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Manager {
if let Some(task_info) = self.task_mapping.read().await.get(&task_id) {
let block_hash = task_info.hash.to_string();
if !response.data.is_empty() {
info!("account info: {:?}", account);
debug!("account info: {:?}", account);
info!("new available block {} for account {}", block_hash, address);
account.blocks.insert(
block_hash,
Expand Down
2 changes: 1 addition & 1 deletion crates/dworker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub async fn handle_connection(
while let Some(Ok(message)) = socket_r_handler.next().await {
match message {
DMessage::DRequest(request) => {
info!("new task from scheduler: {}", request.id.clone());
debug!("new task from scheduler: {}", request.id.clone());
let response = decrypt(worker_pool.clone(), request).await;
if let Err(e) = task_tx.send(DMessage::DResponse(response)).await {
error!("failed to send response to write channel, {}", e);
Expand Down
14 changes: 3 additions & 11 deletions crates/networking/src/rpc_abi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::{response::IntoResponse, Json};
use constants::{IRON_NATIVE_ASSET, MAINNET_GENESIS_HASH, MAINNET_GENESIS_SEQUENCE};
use constants::IRON_NATIVE_ASSET;
use serde::{Deserialize, Serialize};
use ureq::json;

Expand All @@ -23,15 +23,6 @@ pub struct BlockInfo {
pub sequence: u64,
}

impl Default for BlockInfo {
fn default() -> Self {
Self {
hash: MAINNET_GENESIS_HASH.to_string(),
sequence: MAINNET_GENESIS_SEQUENCE as u64,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcImportAccountRequest {
Expand Down Expand Up @@ -100,7 +91,7 @@ pub struct BlockWithHash {
pub transactions: Vec<TransactionWithHash>,
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct RpcSetAccountHeadRequest {
pub account: String,
pub start: String,
Expand Down Expand Up @@ -328,6 +319,7 @@ pub struct BlockIdentifier {
#[serde(rename_all = "camelCase")]
pub struct RpcGetLatestBlockResponse {
pub current_block_identifier: BlockIdentifier,
pub genesis_block_identifier: BlockIdentifier,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
24 changes: 22 additions & 2 deletions crates/networking/src/rpc_handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt::Debug, time::Duration};
use oreo_errors::OreoError;
use serde::Deserialize;
use serde_json::json;
use tracing::debug;
use tracing::{debug, info};
use ureq::{Agent, AgentBuilder, Error, Response};

use crate::{
Expand Down Expand Up @@ -144,7 +144,26 @@ impl RpcHandler {
) -> Result<RpcResponse<RpcGetTransactionsResponse>, OreoError> {
let path = format!("http://{}/wallet/getAccountTransactions", self.endpoint);
let resp = self.agent.clone().post(&path).send_json(&request);
handle_response(resp)

match resp {
Ok(response) => {
let mut buffer = Vec::new();
response.into_reader().read_to_end(&mut buffer).map_err(|e| OreoError::InternalRpcError(e.to_string()))?;
let transactions_response: RpcResponse<RpcGetTransactionsResponse> = if buffer.is_empty() {
RpcResponse {
data: RpcGetTransactionsResponse {
transactions: Vec::new(),
},
status: 200,
}
} else {
serde_json::from_slice(&buffer)
.map_err(|e| OreoError::InternalRpcError(e.to_string()))?
};
Ok(transactions_response)
}
Err(e) => handle_response(Err(e)),
}
}

pub fn create_transaction(
Expand Down Expand Up @@ -211,6 +230,7 @@ impl RpcHandler {
pub fn handle_response<S: Debug + for<'a> Deserialize<'a>>(
resp: Result<Response, Error>,
) -> Result<RpcResponse<S>, OreoError> {
info!("Handle response: {:?}", resp);
let res = match resp {
Ok(response) => match response.into_json::<RpcResponse<S>>() {
Ok(data) => Ok(data),
Expand Down
6 changes: 3 additions & 3 deletions crates/networking/src/web_abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use constants::{MAINNET_GENESIS_HASH, MAINNET_GENESIS_SEQUENCE};
use constants::MAINNET_GENESIS_SEQUENCE;
use db_handler::{address_to_name, Account};
use oreo_errors::OreoError;
use serde::{Deserialize, Serialize};
Expand All @@ -23,7 +23,7 @@ pub struct ImportAccountResponse {
}

impl ImportAccountRequest {
pub fn to_account(&self) -> Account {
pub fn to_account(&self, genesis_hash: String) -> Account {
let (create_head, create_hash) = match &self.created_at {
Some(creat) => (Some(creat.sequence as i64), Some(creat.hash.clone())),
None => (None, None),
Expand All @@ -34,7 +34,7 @@ impl ImportAccountRequest {
create_head,
create_hash: create_hash.clone(),
head: create_head.unwrap_or(MAINNET_GENESIS_SEQUENCE),
hash: create_hash.unwrap_or(MAINNET_GENESIS_HASH.to_string()),
hash: create_hash.unwrap_or(genesis_hash),
in_vk: self.incoming_view_key.clone(),
out_vk: self.outgoing_view_key.clone(),
vk: self.view_key.clone(),
Expand Down
29 changes: 19 additions & 10 deletions crates/server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ use axum::{
response::IntoResponse,
Json,
};
use constants::{ACCOUNT_VERSION, MAINNET_GENESIS_HASH, MAINNET_GENESIS_SEQUENCE};
use constants::{ACCOUNT_VERSION, MAINNET_GENESIS_SEQUENCE};
use db_handler::DBHandler;
use networking::{
decryption_message::{DecryptionMessage, ScanRequest, ScanResponse, SuccessResponse},
rpc_abi::{
decryption_message::{DecryptionMessage, ScanRequest, ScanResponse, SuccessResponse}, rpc_abi::{
BlockInfo, OutPut, RpcBroadcastTxRequest, RpcCreateTxRequest, RpcGetAccountStatusRequest,
RpcGetAccountTransactionRequest, RpcGetBalancesRequest, RpcGetBalancesResponse,
RpcGetTransactionsRequest, RpcImportAccountRequest, RpcImportAccountResponse,
RpcRemoveAccountRequest, RpcResetAccountRequest, RpcResponse, RpcSetScanningRequest,
},
web_abi::{GetTransactionDetailResponse, ImportAccountRequest, RescanAccountResponse},
}, web_abi::{GetTransactionDetailResponse, ImportAccountRequest, RescanAccountResponse}
};
use oreo_errors::OreoError;
use serde_json::json;
use tracing::info;
use utils::{default_secp, sign, verify, Signature};

use crate::SharedState;
Expand All @@ -29,7 +28,7 @@ pub async fn import_account_handler<T: DBHandler>(
) -> impl IntoResponse {
let account_name = shared
.db_handler
.save_account(import.clone().to_account(), 0)
.save_account(import.clone().to_account(shared.genesis_hash.clone()), 0)
.await;
if let Err(e) = account_name {
return e.into_response();
Expand Down Expand Up @@ -67,7 +66,10 @@ pub async fn import_account_handler<T: DBHandler>(
account: account_name.clone(),
})
.map(|x| {
let head = x.data.account.head.unwrap_or(BlockInfo::default());
let head = x.data.account.head.unwrap_or(BlockInfo {
hash: shared.genesis_hash.clone(),
sequence: MAINNET_GENESIS_SEQUENCE as u64,
});
if latest_height - head.sequence > 1000 {
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
account: account_name.clone(),
Expand Down Expand Up @@ -156,7 +158,7 @@ pub async fn account_status_handler<T: DBHandler>(
Some(_) => {}
None => {
result.data.account.head = Some(BlockInfo {
hash: MAINNET_GENESIS_HASH.to_string(),
hash: shared.genesis_hash.clone(),
sequence: MAINNET_GENESIS_SEQUENCE as u64,
})
}
Expand Down Expand Up @@ -196,7 +198,10 @@ pub async fn rescan_account_handler<T: DBHandler>(
account: account.name.clone(),
})
{
let head = status.data.account.head.unwrap_or(BlockInfo::default());
let head = status.data.account.head.unwrap_or(BlockInfo {
hash: shared.genesis_hash.clone(),
sequence: MAINNET_GENESIS_SEQUENCE as u64,
});
let scan_request = ScanRequest {
address: account.address.clone(),
in_vk: account.in_vk.clone(),
Expand Down Expand Up @@ -243,7 +248,11 @@ pub async fn update_scan_status_handler<T: DBHandler>(
}
let account = db_account.unwrap();
message.account = account.name.clone();
let _ = shared.rpc_handler.set_account_head(message);
info!("set account message: {:?}", message.clone());
let resp = shared.rpc_handler.set_account_head(message);
if resp.is_err() {
info!("Failed to update account head: {:?}", resp.unwrap_err());
}
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
account: account.name.clone(),
enabled: true,
Expand Down
12 changes: 11 additions & 1 deletion crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ pub struct SharedState<T: DBHandler> {
pub rpc_handler: RpcHandler,
pub scan_handler: ServerHandler,
pub secp: SecpKey,
pub genesis_hash: String,
}

impl<T> SharedState<T>
where
T: DBHandler,
{
pub fn new(db_handler: T, endpoint: &str, scan: &str, secp: SecpKey) -> Self {
pub fn new(db_handler: T, endpoint: &str, scan: &str, secp: SecpKey, genesis_hash: String) -> Self {
Self {
db_handler: db_handler,
rpc_handler: RpcHandler::new(endpoint.into()),
scan_handler: ServerHandler::new(scan.into()),
secp,
genesis_hash,
}
}
}
Expand All @@ -59,6 +61,13 @@ pub async fn run_server(
sk_u8: [u8; 32],
pk_u8: [u8; 33],
) -> Result<()> {
let genesis_hash;
{
let temp_handler: RpcHandler = RpcHandler::new(rpc_server.clone().into());
let latest_block_response = temp_handler.get_latest_block()?.data;
genesis_hash = latest_block_response.genesis_block_identifier.hash;
}
info!("Genesis hash: {}", genesis_hash);
let shared_resource = Arc::new(SharedState::new(
db_handler,
&rpc_server,
Expand All @@ -67,6 +76,7 @@ pub async fn run_server(
sk: sk_u8,
pk: pk_u8,
},
genesis_hash,
));

let router = Router::new()
Expand Down

0 comments on commit 58dc34b

Please sign in to comment.