Skip to content

Commit 58dc34b

Browse files
committed
adds genesis hash flagging so that oreowallet will support testnet
1 parent 055e1e7 commit 58dc34b

File tree

8 files changed

+62
-29
lines changed

8 files changed

+62
-29
lines changed

crates/constants/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub const OREOS_VALUE: &str = "1";
77
pub const OREOSRIPTIONS_ENDPOINT: &str = "http://localhost:20001/api";
88
pub const MAINNET_GENESIS_HASH: &str =
99
"eac623b099b8081d2bde92d43a4a7795385c94e2c0ae4097ef488972e83ff2b3";
10+
pub const TESTNET_GENESIS_HASH: &str =
11+
"7999c680bbd15d9adb7392e0c27a7caac7e596de5560c18e96365d0fd68140e3";
1012
pub const MAINNET_GENESIS_SEQUENCE: i64 = 1;
1113
pub const REORG_DEPTH: i64 = 100;
1214
pub const SECONDARY_BATCH: i64 = 10000;

crates/dservice/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl Manager {
244244
if let Some(task_info) = self.task_mapping.read().await.get(&task_id) {
245245
let block_hash = task_info.hash.to_string();
246246
if !response.data.is_empty() {
247-
info!("account info: {:?}", account);
247+
debug!("account info: {:?}", account);
248248
info!("new available block {} for account {}", block_hash, address);
249249
account.blocks.insert(
250250
block_hash,

crates/dworker/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub async fn handle_connection(
131131
while let Some(Ok(message)) = socket_r_handler.next().await {
132132
match message {
133133
DMessage::DRequest(request) => {
134-
info!("new task from scheduler: {}", request.id.clone());
134+
debug!("new task from scheduler: {}", request.id.clone());
135135
let response = decrypt(worker_pool.clone(), request).await;
136136
if let Err(e) = task_tx.send(DMessage::DResponse(response)).await {
137137
error!("failed to send response to write channel, {}", e);

crates/networking/src/rpc_abi.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use axum::{response::IntoResponse, Json};
2-
use constants::{IRON_NATIVE_ASSET, MAINNET_GENESIS_HASH, MAINNET_GENESIS_SEQUENCE};
2+
use constants::IRON_NATIVE_ASSET;
33
use serde::{Deserialize, Serialize};
44
use ureq::json;
55

@@ -23,15 +23,6 @@ pub struct BlockInfo {
2323
pub sequence: u64,
2424
}
2525

26-
impl Default for BlockInfo {
27-
fn default() -> Self {
28-
Self {
29-
hash: MAINNET_GENESIS_HASH.to_string(),
30-
sequence: MAINNET_GENESIS_SEQUENCE as u64,
31-
}
32-
}
33-
}
34-
3526
#[derive(Debug, Deserialize, Serialize)]
3627
#[serde(rename_all = "camelCase")]
3728
pub struct RpcImportAccountRequest {
@@ -100,7 +91,7 @@ pub struct BlockWithHash {
10091
pub transactions: Vec<TransactionWithHash>,
10192
}
10293

103-
#[derive(Debug, Deserialize, Serialize)]
94+
#[derive(Debug, Deserialize, Serialize, Clone)]
10495
pub struct RpcSetAccountHeadRequest {
10596
pub account: String,
10697
pub start: String,
@@ -328,6 +319,7 @@ pub struct BlockIdentifier {
328319
#[serde(rename_all = "camelCase")]
329320
pub struct RpcGetLatestBlockResponse {
330321
pub current_block_identifier: BlockIdentifier,
322+
pub genesis_block_identifier: BlockIdentifier,
331323
}
332324

333325
#[derive(Debug, Deserialize, Serialize)]

crates/networking/src/rpc_handler/handler.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fmt::Debug, time::Duration};
33
use oreo_errors::OreoError;
44
use serde::Deserialize;
55
use serde_json::json;
6-
use tracing::debug;
6+
use tracing::{debug, info};
77
use ureq::{Agent, AgentBuilder, Error, Response};
88

99
use crate::{
@@ -144,7 +144,26 @@ impl RpcHandler {
144144
) -> Result<RpcResponse<RpcGetTransactionsResponse>, OreoError> {
145145
let path = format!("http://{}/wallet/getAccountTransactions", self.endpoint);
146146
let resp = self.agent.clone().post(&path).send_json(&request);
147-
handle_response(resp)
147+
148+
match resp {
149+
Ok(response) => {
150+
let mut buffer = Vec::new();
151+
response.into_reader().read_to_end(&mut buffer).map_err(|e| OreoError::InternalRpcError(e.to_string()))?;
152+
let transactions_response: RpcResponse<RpcGetTransactionsResponse> = if buffer.is_empty() {
153+
RpcResponse {
154+
data: RpcGetTransactionsResponse {
155+
transactions: Vec::new(),
156+
},
157+
status: 200,
158+
}
159+
} else {
160+
serde_json::from_slice(&buffer)
161+
.map_err(|e| OreoError::InternalRpcError(e.to_string()))?
162+
};
163+
Ok(transactions_response)
164+
}
165+
Err(e) => handle_response(Err(e)),
166+
}
148167
}
149168

150169
pub fn create_transaction(
@@ -211,6 +230,7 @@ impl RpcHandler {
211230
pub fn handle_response<S: Debug + for<'a> Deserialize<'a>>(
212231
resp: Result<Response, Error>,
213232
) -> Result<RpcResponse<S>, OreoError> {
233+
info!("Handle response: {:?}", resp);
214234
let res = match resp {
215235
Ok(response) => match response.into_json::<RpcResponse<S>>() {
216236
Ok(data) => Ok(data),

crates/networking/src/web_abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use constants::{MAINNET_GENESIS_HASH, MAINNET_GENESIS_SEQUENCE};
1+
use constants::MAINNET_GENESIS_SEQUENCE;
22
use db_handler::{address_to_name, Account};
33
use oreo_errors::OreoError;
44
use serde::{Deserialize, Serialize};
@@ -23,7 +23,7 @@ pub struct ImportAccountResponse {
2323
}
2424

2525
impl ImportAccountRequest {
26-
pub fn to_account(&self) -> Account {
26+
pub fn to_account(&self, genesis_hash: String) -> Account {
2727
let (create_head, create_hash) = match &self.created_at {
2828
Some(creat) => (Some(creat.sequence as i64), Some(creat.hash.clone())),
2929
None => (None, None),
@@ -34,7 +34,7 @@ impl ImportAccountRequest {
3434
create_head,
3535
create_hash: create_hash.clone(),
3636
head: create_head.unwrap_or(MAINNET_GENESIS_SEQUENCE),
37-
hash: create_hash.unwrap_or(MAINNET_GENESIS_HASH.to_string()),
37+
hash: create_hash.unwrap_or(genesis_hash),
3838
in_vk: self.incoming_view_key.clone(),
3939
out_vk: self.outgoing_view_key.clone(),
4040
vk: self.view_key.clone(),

crates/server/src/handlers.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ use axum::{
55
response::IntoResponse,
66
Json,
77
};
8-
use constants::{ACCOUNT_VERSION, MAINNET_GENESIS_HASH, MAINNET_GENESIS_SEQUENCE};
8+
use constants::{ACCOUNT_VERSION, MAINNET_GENESIS_SEQUENCE};
99
use db_handler::DBHandler;
1010
use networking::{
11-
decryption_message::{DecryptionMessage, ScanRequest, ScanResponse, SuccessResponse},
12-
rpc_abi::{
11+
decryption_message::{DecryptionMessage, ScanRequest, ScanResponse, SuccessResponse}, rpc_abi::{
1312
BlockInfo, OutPut, RpcBroadcastTxRequest, RpcCreateTxRequest, RpcGetAccountStatusRequest,
1413
RpcGetAccountTransactionRequest, RpcGetBalancesRequest, RpcGetBalancesResponse,
1514
RpcGetTransactionsRequest, RpcImportAccountRequest, RpcImportAccountResponse,
1615
RpcRemoveAccountRequest, RpcResetAccountRequest, RpcResponse, RpcSetScanningRequest,
17-
},
18-
web_abi::{GetTransactionDetailResponse, ImportAccountRequest, RescanAccountResponse},
16+
}, web_abi::{GetTransactionDetailResponse, ImportAccountRequest, RescanAccountResponse}
1917
};
2018
use oreo_errors::OreoError;
2119
use serde_json::json;
20+
use tracing::info;
2221
use utils::{default_secp, sign, verify, Signature};
2322

2423
use crate::SharedState;
@@ -29,7 +28,7 @@ pub async fn import_account_handler<T: DBHandler>(
2928
) -> impl IntoResponse {
3029
let account_name = shared
3130
.db_handler
32-
.save_account(import.clone().to_account(), 0)
31+
.save_account(import.clone().to_account(shared.genesis_hash.clone()), 0)
3332
.await;
3433
if let Err(e) = account_name {
3534
return e.into_response();
@@ -67,7 +66,10 @@ pub async fn import_account_handler<T: DBHandler>(
6766
account: account_name.clone(),
6867
})
6968
.map(|x| {
70-
let head = x.data.account.head.unwrap_or(BlockInfo::default());
69+
let head = x.data.account.head.unwrap_or(BlockInfo {
70+
hash: shared.genesis_hash.clone(),
71+
sequence: MAINNET_GENESIS_SEQUENCE as u64,
72+
});
7173
if latest_height - head.sequence > 1000 {
7274
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
7375
account: account_name.clone(),
@@ -156,7 +158,7 @@ pub async fn account_status_handler<T: DBHandler>(
156158
Some(_) => {}
157159
None => {
158160
result.data.account.head = Some(BlockInfo {
159-
hash: MAINNET_GENESIS_HASH.to_string(),
161+
hash: shared.genesis_hash.clone(),
160162
sequence: MAINNET_GENESIS_SEQUENCE as u64,
161163
})
162164
}
@@ -196,7 +198,10 @@ pub async fn rescan_account_handler<T: DBHandler>(
196198
account: account.name.clone(),
197199
})
198200
{
199-
let head = status.data.account.head.unwrap_or(BlockInfo::default());
201+
let head = status.data.account.head.unwrap_or(BlockInfo {
202+
hash: shared.genesis_hash.clone(),
203+
sequence: MAINNET_GENESIS_SEQUENCE as u64,
204+
});
200205
let scan_request = ScanRequest {
201206
address: account.address.clone(),
202207
in_vk: account.in_vk.clone(),
@@ -243,7 +248,11 @@ pub async fn update_scan_status_handler<T: DBHandler>(
243248
}
244249
let account = db_account.unwrap();
245250
message.account = account.name.clone();
246-
let _ = shared.rpc_handler.set_account_head(message);
251+
info!("set account message: {:?}", message.clone());
252+
let resp = shared.rpc_handler.set_account_head(message);
253+
if resp.is_err() {
254+
info!("Failed to update account head: {:?}", resp.unwrap_err());
255+
}
247256
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
248257
account: account.name.clone(),
249258
enabled: true,

crates/server/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ pub struct SharedState<T: DBHandler> {
3535
pub rpc_handler: RpcHandler,
3636
pub scan_handler: ServerHandler,
3737
pub secp: SecpKey,
38+
pub genesis_hash: String,
3839
}
3940

4041
impl<T> SharedState<T>
4142
where
4243
T: DBHandler,
4344
{
44-
pub fn new(db_handler: T, endpoint: &str, scan: &str, secp: SecpKey) -> Self {
45+
pub fn new(db_handler: T, endpoint: &str, scan: &str, secp: SecpKey, genesis_hash: String) -> Self {
4546
Self {
4647
db_handler: db_handler,
4748
rpc_handler: RpcHandler::new(endpoint.into()),
4849
scan_handler: ServerHandler::new(scan.into()),
4950
secp,
51+
genesis_hash,
5052
}
5153
}
5254
}
@@ -59,6 +61,13 @@ pub async fn run_server(
5961
sk_u8: [u8; 32],
6062
pk_u8: [u8; 33],
6163
) -> Result<()> {
64+
let genesis_hash;
65+
{
66+
let temp_handler: RpcHandler = RpcHandler::new(rpc_server.clone().into());
67+
let latest_block_response = temp_handler.get_latest_block()?.data;
68+
genesis_hash = latest_block_response.genesis_block_identifier.hash;
69+
}
70+
info!("Genesis hash: {}", genesis_hash);
6271
let shared_resource = Arc::new(SharedState::new(
6372
db_handler,
6473
&rpc_server,
@@ -67,6 +76,7 @@ pub async fn run_server(
6776
sk: sk_u8,
6877
pk: pk_u8,
6978
},
79+
genesis_hash,
7080
));
7181

7282
let router = Router::new()

0 commit comments

Comments
 (0)