Skip to content

Commit 522f22b

Browse files
authored
adds testnet support for genesis block (oreoslabs#22)
1 parent befc07b commit 522f22b

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

.DS_Store

6 KB
Binary file not shown.

crates/networking/src/rpc_abi.rs

Lines changed: 2 additions & 10 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

@@ -28,15 +28,6 @@ pub struct BlockInfo {
2828
pub sequence: u64,
2929
}
3030

31-
impl Default for BlockInfo {
32-
fn default() -> Self {
33-
Self {
34-
hash: MAINNET_GENESIS_HASH.to_string(),
35-
sequence: MAINNET_GENESIS_SEQUENCE as u64,
36-
}
37-
}
38-
}
39-
4031
#[derive(Debug, Deserialize, Serialize)]
4132
#[serde(rename_all = "camelCase")]
4233
pub struct RpcImportAccountRequest {
@@ -333,6 +324,7 @@ pub struct BlockIdentifier {
333324
#[serde(rename_all = "camelCase")]
334325
pub struct RpcGetLatestBlockResponse {
335326
pub current_block_identifier: BlockIdentifier,
327+
pub genesis_block_identifier: BlockIdentifier,
336328
}
337329

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

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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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::{
1111
decryption_message::{DecryptionMessage, ScanRequest, ScanResponse, SuccessResponse},
@@ -30,7 +30,7 @@ pub async fn import_account_handler<T: DBHandler>(
3030
) -> impl IntoResponse {
3131
let account_name = shared
3232
.db_handler
33-
.save_account(import.clone().to_account(), 0)
33+
.save_account(import.clone().to_account(shared.genesis_hash.clone()), 0)
3434
.await;
3535
if let Err(e) = account_name {
3636
return e.into_response();
@@ -68,7 +68,10 @@ pub async fn import_account_handler<T: DBHandler>(
6868
account: account_name.clone(),
6969
})
7070
.map(|x| {
71-
let head = x.data.account.head.unwrap_or(BlockInfo::default());
71+
let head = x.data.account.head.unwrap_or(BlockInfo {
72+
hash: shared.genesis_hash.clone(),
73+
sequence: MAINNET_GENESIS_SEQUENCE as u64,
74+
});
7275
if latest_height - head.sequence > 1000 {
7376
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
7477
account: account_name.clone(),
@@ -157,7 +160,7 @@ pub async fn account_status_handler<T: DBHandler>(
157160
Some(_) => {}
158161
None => {
159162
result.data.account.head = Some(BlockInfo {
160-
hash: MAINNET_GENESIS_HASH.to_string(),
163+
hash: shared.genesis_hash.clone(),
161164
sequence: MAINNET_GENESIS_SEQUENCE as u64,
162165
})
163166
}
@@ -197,7 +200,10 @@ pub async fn rescan_account_handler<T: DBHandler>(
197200
account: account.name.clone(),
198201
})
199202
{
200-
let head = status.data.account.head.unwrap_or(BlockInfo::default());
203+
let head = BlockInfo {
204+
hash: account.create_hash.unwrap_or(shared.genesis_hash.clone()),
205+
sequence: account.create_head.unwrap_or(MAINNET_GENESIS_SEQUENCE) as u64,
206+
};
201207
let scan_request = ScanRequest {
202208
address: account.address.clone(),
203209
in_vk: account.in_vk.clone(),

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
}
@@ -86,6 +88,13 @@ pub async fn run_server(
8688
sk_u8: [u8; 32],
8789
pk_u8: [u8; 33],
8890
) -> Result<()> {
91+
let genesis_hash;
92+
{
93+
let temp_handler: RpcHandler = RpcHandler::new(rpc_server.clone().into());
94+
let latest_block_response = temp_handler.get_latest_block()?.data;
95+
genesis_hash = latest_block_response.genesis_block_identifier.hash;
96+
}
97+
info!("Genesis hash: {}", genesis_hash);
8998
let shared_resource = Arc::new(SharedState::new(
9099
db_handler,
91100
&rpc_server,
@@ -94,6 +103,7 @@ pub async fn run_server(
94103
sk: sk_u8,
95104
pk: pk_u8,
96105
},
106+
genesis_hash,
97107
));
98108
let auth_middleware = from_fn_with_state(shared_resource.clone(), auth);
99109

0 commit comments

Comments
 (0)