Skip to content

Commit

Permalink
AccountId mod and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Dec 2, 2024
1 parent 6d131e4 commit 3afafaa
Showing 9 changed files with 102 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions node-gui/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -36,3 +36,9 @@ serde_with.workspace = true
thiserror.workspace = true
tokio.workspace = true
variant_count.workspace = true

[dev-dependencies]
test-utils = { path = "../../test-utils" }

rstest.workspace = true
serde_json.workspace = true
80 changes: 80 additions & 0 deletions node-gui/backend/src/account_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2021-2024 RBB S.r.l
// [email protected]
// 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 crypto::key::hdkd::u31::U31;
use serde::{de::Error, Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct AccountId(U31);

impl AccountId {
pub fn new(index: U31) -> Self {
Self(index)
}

pub fn account_index(&self) -> U31 {
self.0
}
}

impl Serialize for AccountId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.into_u32().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for AccountId {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let i = u32::deserialize(deserializer)?;
let i = U31::from_u32(i).ok_or_else(|| {
D::Error::custom(format!("Integer has invalid value for AccountId ({i})"))
})?;
Ok(Self::new(i))
}
}

#[cfg(test)]
mod tests {
use super::*;

use rstest::rstest;
use test_utils::random::{Rng, Seed};

#[rstest]
#[trace]
#[case(Seed::from_entropy())]
fn test_json_valid(#[case] seed: Seed) {
let mut rng = test_utils::random::make_seedable_rng(seed);

let id = AccountId::new(U31::from_u32_with_msb(rng.gen::<u32>()).0);

let id_json = serde_json::to_string(&id).unwrap();
let id_decoded = serde_json::from_str::<AccountId>(&id_json).unwrap();
assert_eq!(id_decoded, id);
}

#[rstest]
#[trace]
#[case(Seed::from_entropy())]
fn test_json_invalid(#[case] seed: Seed) {
use crypto::key::hdkd::u31::MSB_BIT;

let mut rng = test_utils::random::make_seedable_rng(seed);

let str = rng.gen_range(MSB_BIT..u32::MAX).to_string();

serde_json::from_str::<AccountId>(&str).unwrap_err();
}
}
3 changes: 2 additions & 1 deletion node-gui/backend/src/backend_impl.rs
Original file line number Diff line number Diff line change
@@ -46,10 +46,11 @@ use wallet_types::{
};

use super::{
account_id::AccountId,
chainstate_event_handler::ChainstateEventHandler,
error::BackendError,
messages::{
AccountId, AccountInfo, AddressInfo, BackendEvent, BackendRequest, CreateDelegationRequest,
AccountInfo, AddressInfo, BackendEvent, BackendRequest, CreateDelegationRequest,
DecommissionPoolRequest, DelegateStakingRequest, EncryptionAction, EncryptionState,
SendDelegateToAddressRequest, SendRequest, StakeRequest, TransactionInfo, WalletId,
WalletInfo,
2 changes: 1 addition & 1 deletion node-gui/backend/src/error.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::messages::{AccountId, WalletId};
use super::{account_id::AccountId, messages::WalletId};

#[derive(thiserror::Error, Debug, Clone)]
pub enum BackendError {
3 changes: 3 additions & 0 deletions node-gui/backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ mod error;
mod p2p_event_handler;
mod wallet_events;

mod account_id;
pub use account_id::AccountId;

use chainstate::ChainInfo;
use common::address::{Address, AddressError};
use common::chain::{ChainConfig, Destination};
35 changes: 3 additions & 32 deletions node-gui/backend/src/messages.rs
Original file line number Diff line number Diff line change
@@ -20,14 +20,14 @@ use std::{
sync::atomic::{AtomicU64, Ordering},
};

use serde::{de::Error, Deserialize, Serialize};
use serde::{Deserialize, Serialize};

use chainstate::ChainInfo;
use common::{
chain::{DelegationId, GenBlock, PoolId, SignedTransaction},
primitives::{Amount, BlockHeight, Id},
};
use crypto::key::hdkd::{child_number::ChildNumber, u31::U31};
use crypto::key::hdkd::child_number::ChildNumber;
use p2p::P2pEvent;
use serialization::hex_encoded::hex_encoded_serialization;
use wallet::account::transaction_list::TransactionList;
@@ -36,7 +36,7 @@ use wallet_controller::types::Balances;
use wallet_rpc_lib::types::PoolInfo;
use wallet_types::wallet_type::WalletType;

use super::{BackendError, ImportOrCreate};
use super::{AccountId, BackendError, ImportOrCreate};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct WalletId(u64);
@@ -49,35 +49,6 @@ impl WalletId {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct AccountId(U31);

impl AccountId {
pub fn new(index: U31) -> Self {
Self(index)
}

pub fn account_index(&self) -> U31 {
self.0
}
}

impl Serialize for AccountId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.into_u32().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for AccountId {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let i = u32::deserialize(deserializer)?;
let i = U31::from_u32(i).ok_or(D::Error::custom(format!(
"Integer has invalid value for AccountId ({i})"
)))?;
Ok(Self::new(i))
}
}

#[derive(Debug, Clone, Serialize)]
pub struct WalletInfo {
pub wallet_id: WalletId,
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ use iced::{
Alignment, Element, Length,
};

use node_gui_backend::messages::{AccountId, WalletInfo};
use node_gui_backend::{messages::WalletInfo, AccountId};
use wallet_types::wallet_type::WalletType;

use crate::main_window::NodeState;
6 changes: 3 additions & 3 deletions node-gui/src/main_window/main_widget/tabs/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -39,10 +39,10 @@ use iced_aw::tab_bar::TabLabel;
use common::chain::DelegationId;
use node_gui_backend::{
messages::{
AccountId, BackendRequest, CreateDelegationRequest, DecommissionPoolRequest,
DelegateStakingRequest, SendDelegateToAddressRequest, SendRequest, StakeRequest, WalletId,
BackendRequest, CreateDelegationRequest, DecommissionPoolRequest, DelegateStakingRequest,
SendDelegateToAddressRequest, SendRequest, StakeRequest, WalletId,
},
BackendSender,
AccountId, BackendSender,
};
use wallet_controller::DEFAULT_ACCOUNT_INDEX;
use wallet_types::wallet_type::WalletType;

0 comments on commit 3afafaa

Please sign in to comment.