Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amount ser/deser #4699

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions massa-api-exports/src/ledger.rs

This file was deleted.

2 changes: 0 additions & 2 deletions massa-api-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub mod endorsement;
pub mod error;
/// execution
pub mod execution;
/// ledger structures
pub mod ledger;
/// node related structure
pub mod node;
/// operations
Expand Down
3 changes: 1 addition & 2 deletions massa-api-exports/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ mod tests {
use massa_models::{amount::Amount, operation::OperationType};
use serial_test::serial;
use std::collections::BTreeMap;
use std::str::FromStr;

#[test]
#[serial]
fn test_execute_sc_with_datastore() {
let expected_op = OperationType::ExecuteSC {
max_gas: 123,
max_coins: Amount::from_str("5000000").unwrap(),
max_coins: Amount::from_raw(5000000),
data: vec![23u8, 123u8, 44u8],
datastore: BTreeMap::from([
(vec![1, 2, 3], vec![4, 5, 6, 7, 8, 9]),
Expand Down
44 changes: 39 additions & 5 deletions massa-api/src/tests/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ async fn send_operations_low_fee() {
let addr: SocketAddr = "[::]:5049".parse().unwrap();
let (mut api_public, mut config) = start_public_api(addr);

config.minimal_fees = Amount::from_str("0.01").unwrap();
api_public.0.api_settings.minimal_fees = Amount::from_str("0.01").unwrap();
config.minimal_fees = Amount::from_raw(10000000);
api_public.0.api_settings.minimal_fees = Amount::from_raw(10000000);

let mut pool_ctrl = MockPoolController::new();
pool_ctrl.expect_clone_box().returning(|| {
Expand Down Expand Up @@ -559,6 +559,7 @@ async fn send_operations() {
serialized_content: operation.serialized_data,
};

dbg!(serde_json::to_string(&input).unwrap());
let response: Vec<OperationId> = client
.request("send_operations", rpc_params![vec![input]])
.await
Expand Down Expand Up @@ -762,6 +763,39 @@ async fn execute_read_only_bytecode() {
assert!(response.is_err());
api_public_handle.stop().await;
}
#[test]
fn test_amount() {
// decimal now return error
assert!(Amount::from_str("15463.123").is_err());
assert!(Amount::from_str("100.0").is_err());

// from str now uses nanoMassa
let amount = Amount::from_str("100").unwrap(); // nanoMassa

// print no longer display massa unit as decimal
// print display nanoMassa
// println!("{}", amount); // Should print 100 (nanoMassa)

// to_raw return nanoMassa as u64 like before
assert_eq!(amount.to_raw(), 100);
// to_string return nanoMassa as string
assert_eq!(amount.to_string(), "100".to_string());

let serialized_serde = serde_json::to_string(&amount).unwrap();
assert_eq!(serialized_serde, "100".to_string());

// serde from_str
let deserialized_serde: Amount = serde_json::from_str(&serialized_serde).unwrap();
// amount from_str
let deserialized_amount: Amount = Amount::from_str(&serialized_serde).unwrap();

assert_eq!(deserialized_serde.to_string(), "100");
assert_eq!(deserialized_serde.to_raw(), 100);

assert_eq!(deserialized_amount.to_string(), "100");
assert_eq!(deserialized_amount.to_raw(), 100);
assert_eq!(deserialized_serde, deserialized_amount);
}

#[tokio::test]
async fn execute_read_only_call() {
Expand Down Expand Up @@ -821,7 +855,7 @@ async fn execute_read_only_call() {
target_function: "hello".to_string(),
parameter: vec![],
caller_address: None,
fee: None,
fee: Some(Amount::from_raw(10000000)),
coins: None,
}]];
let response: Vec<ExecuteReadOnlyResponse> = client
Expand All @@ -842,8 +876,8 @@ async fn get_addresses() {
exec_ctrl.expect_get_addresses_infos().returning(|a, _s| {
a.iter()
.map(|_addr| ExecutionAddressInfo {
candidate_balance: Amount::from_str("100000").unwrap(),
final_balance: Amount::from_str("80000").unwrap(),
candidate_balance: Amount::from_raw(10000000),
final_balance: Amount::from_raw(80000),
final_roll_count: 55,
final_datastore_keys: std::collections::BTreeSet::new(),
candidate_roll_count: 12,
Expand Down
6 changes: 3 additions & 3 deletions massa-async-pool/src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ mod tests {
);

let mut message2 = message.clone();
message2.fee = Amount::from_str("2").unwrap();
message2.fee = Amount::from_raw(2000000000);
assert_ne!(message.compute_id(), message2.compute_id());

let mut message3 = message.clone();
message3.fee = Amount::from_str("3").unwrap();
message3.fee = Amount::from_raw(3000000000);
assert_ne!(message.compute_id(), message3.compute_id());

changes
.0
.insert(message2.compute_id(), SetUpdateOrDelete::Delete);

let update3 = AsyncMessageUpdate {
coins: SetOrKeep::Set(Amount::from_str("3").unwrap()),
coins: SetOrKeep::Set(Amount::from_raw(3000000000)),
..Default::default()
};

Expand Down
36 changes: 18 additions & 18 deletions massa-async-pool/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Serializer<AsyncMessageId> for AsyncMessageIdSerializer {
/// Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
/// String::from("test"),
/// 10000000,
/// Amount::from_str("1").unwrap(),
/// Amount::from_str("1").unwrap(),
/// Amount::from_raw(1000000000),
/// Amount::from_raw(1000000000),
/// Slot::new(2, 0),
/// Slot::new(3, 0),
/// vec![1, 2, 3, 4],
Expand Down Expand Up @@ -138,8 +138,8 @@ impl Deserializer<AsyncMessageId> for AsyncMessageIdDeserializer {
/// Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
/// String::from("test"),
/// 10000000,
/// Amount::from_str("1").unwrap(),
/// Amount::from_str("1").unwrap(),
/// Amount::from_raw(1000000000),
/// Amount::from_raw(1000000000),
/// Slot::new(2, 0),
/// Slot::new(3, 0),
/// vec![1, 2, 3, 4],
Expand Down Expand Up @@ -429,8 +429,8 @@ impl Serializer<AsyncMessage> for AsyncMessageSerializer {
/// Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
/// String::from("test"),
/// 10000000,
/// Amount::from_str("1").unwrap(),
/// Amount::from_str("1").unwrap(),
/// Amount::from_raw(1000000000),
/// Amount::from_raw(1000000000),
/// Slot::new(2, 0),
/// Slot::new(3, 0),
/// vec![1, 2, 3, 4],
Expand Down Expand Up @@ -546,8 +546,8 @@ impl Deserializer<AsyncMessage> for AsyncMessageDeserializer {
/// Address::from_str("AS12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
/// String::from("test"),
/// 10000000,
/// Amount::from_str("1").unwrap(),
/// Amount::from_str("1").unwrap(),
/// Amount::from_raw(1000000000),
/// Amount::from_raw(1000000000),
/// Slot::new(2, 0),
/// Slot::new(3, 0),
/// vec![1, 2, 3, 4],
Expand Down Expand Up @@ -1039,8 +1039,8 @@ mod tests {
.unwrap(),
function: String::from(""),
max_gas: 0,
fee: Amount::from_str("0").unwrap(),
coins: Amount::from_str("0").unwrap(),
fee: Amount::zero(),
coins: Amount::zero(),
validity_start: Slot::new(0, 0),
validity_end: Slot::new(0, 0),
function_params: vec![],
Expand Down Expand Up @@ -1082,8 +1082,8 @@ mod tests {
.unwrap(),
function: String::from(""),
max_gas: 0,
fee: Amount::from_str("0").unwrap(),
coins: Amount::from_str("0").unwrap(),
fee: Amount::zero(),
coins: Amount::zero(),
validity_start: Slot::new(0, 0),
validity_end: Slot::new(0, 0),
function_params: vec![],
Expand Down Expand Up @@ -1162,8 +1162,8 @@ mod tests {
.unwrap(),
function: String::from(""),
max_gas: 0,
fee: Amount::from_str("0").unwrap(),
coins: Amount::from_str("0").unwrap(),
fee: Amount::zero(),
coins: Amount::zero(),
validity_start: Slot::new(0, 0),
validity_end: Slot::new(0, 0),
function_params: vec![],
Expand Down Expand Up @@ -1315,8 +1315,8 @@ mod tests {
.unwrap(),
function: String::from(""),
max_gas: 0,
fee: Amount::from_str("0").unwrap(),
coins: Amount::from_str("0").unwrap(),
fee: Amount::zero(),
coins: Amount::zero(),
validity_start: Slot::new(0, 0),
validity_end: Slot::new(0, 0),
function_params: vec![],
Expand Down Expand Up @@ -1479,8 +1479,8 @@ mod tests {
Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
String::from("test"),
10000000,
Amount::from_str("1").unwrap(),
Amount::from_str("1").unwrap(),
Amount::from_raw(1000000000),
Amount::from_raw(1000000000),
Slot::new(2, 0),
Slot::new(3, 0),
vec![1, 2, 3, 4],
Expand Down
4 changes: 2 additions & 2 deletions massa-async-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,8 +1075,8 @@ mod tests {
Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
String::from("test"),
10000000,
Amount::from_str("1").unwrap(),
Amount::from_str("1").unwrap(),
Amount::from_raw(1000000000),
Amount::from_raw(1000000000),
Slot::new(2, 0),
Slot::new(3, 0),
vec![1, 2, 3, 4],
Expand Down
3 changes: 1 addition & 2 deletions massa-async-pool/src/test_exports/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::AsyncMessage;
use massa_models::{address::Address, amount::Amount, slot::Slot};
use massa_signature::KeyPair;
use rand::Rng;
use std::str::FromStr;

/// This file defines tools to test the asynchronous pool bootstrap

Expand All @@ -23,7 +22,7 @@ pub fn get_random_message(fee: Option<Amount>, thread_count: u8) -> AsyncMessage
String::from("test"),
10_000,
fee.unwrap_or_default(),
Amount::from_str("100").unwrap(),
Amount::from_raw(100000000000),
Slot::new(2, 0),
Slot::new(4, 0),
vec![1, 2, 3],
Expand Down
20 changes: 9 additions & 11 deletions massa-execution-worker/src/tests/scenarios_mandatories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn final_state_boilerplate(
ledger_controller.set_expectations(|ledger_controller| {
ledger_controller
.expect_get_balance()
.returning(move |_| Some(Amount::from_str("100").unwrap()));
.returning(move |_| Some(Amount::from_raw(100000000000)));
if let Some(saved_bytecode) = saved_bytecode {
ledger_controller
.expect_get_bytecode()
Expand Down Expand Up @@ -254,7 +254,7 @@ fn test_readonly_execution() {
.set_expectations(|ledger_controller| {
ledger_controller
.expect_get_balance()
.returning(move |_| Some(Amount::from_str("100").unwrap()));
.returning(move |_| Some(Amount::from_raw(100000000000)));
ledger_controller
.expect_entry_exists()
.times(1)
Expand Down Expand Up @@ -413,8 +413,8 @@ fn test_nested_call_gas_usage() {
let operation = ExecutionTestUniverse::create_call_sc_operation(
&KeyPair::from_str(TEST_SK_2).unwrap(),
10000000,
Amount::from_str("0").unwrap(),
Amount::from_str("0").unwrap(),
Amount::zero(),
Amount::zero(),
Address::from_str(&address).unwrap(),
String::from("test"),
address.as_bytes().to_vec(),
Expand Down Expand Up @@ -516,7 +516,7 @@ fn test_get_call_coins() {
let operation = ExecutionTestUniverse::create_call_sc_operation(
&KeyPair::from_str(TEST_SK_2).unwrap(),
10000000,
Amount::from_str("0").unwrap(),
Amount::zero(),
coins_sent,
Address::from_str(&address).unwrap(),
String::from("test"),
Expand Down Expand Up @@ -681,7 +681,7 @@ fn send_and_receive_async_message() {
SetUpdateOrDelete::Update(change_sc_update) => {
assert_eq!(
change_sc_update.balance,
SetOrKeep::Set(Amount::from_str("100.0000001").unwrap())
SetOrKeep::Set(Amount::from_raw(100000000100))
);
}
_ => panic!("wrong change type"),
Expand Down Expand Up @@ -816,9 +816,7 @@ fn cancel_async_message() {
assert_eq!(
changes.ledger_changes.0.get(&sender_addr).unwrap(),
&SetUpdateOrDelete::Update(LedgerEntryUpdate {
balance: massa_ledger_exports::SetOrKeep::Set(
Amount::from_str("100.670399899").unwrap()
),
balance: massa_ledger_exports::SetOrKeep::Set(Amount::from_raw(100670399899)),
bytecode: massa_ledger_exports::SetOrKeep::Keep,
datastore: BTreeMap::new()
})
Expand All @@ -841,7 +839,7 @@ fn cancel_async_message() {
SetUpdateOrDelete::Update(change_sender_update) => {
assert_eq!(
change_sender_update.balance,
SetOrKeep::Set(Amount::from_str("100.0000001").unwrap())
SetOrKeep::Set(Amount::from_raw(100000000100))
);
}
_ => panic!("wrong change type"),
Expand Down Expand Up @@ -993,7 +991,7 @@ fn local_execution() {
);
assert_eq!(events[2].data, "one local execution completed");
let amount = Amount::from_raw(events[5].data.parse().unwrap());
assert_eq!(Amount::from_str("89.6713").unwrap(), amount);
assert_eq!(Amount::from_raw(89671300000), amount);
assert_eq!(events[5].context.call_stack.len(), 1);
assert_eq!(
events[1].context.call_stack.back().unwrap(),
Expand Down
3 changes: 1 addition & 2 deletions massa-execution-worker/src/tests/universe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
collections::{BTreeMap, HashMap},
str::FromStr,
sync::Arc,
};

Expand Down Expand Up @@ -165,7 +164,7 @@ impl ExecutionTestUniverse {
// here we use 1.5B as most of the tests perform a SC creation:
// 314_000_000 (SP COMPIL) + 745_000_000 (CL COMPIL) + margin
max_gas: 1_500_000_000,
max_coins: Amount::from_str("5000000").unwrap(),
max_coins: Amount::from_raw(5000000000000),
datastore,
};
let op = Operation::new_verifiable(
Expand Down
4 changes: 2 additions & 2 deletions massa-factory-worker/src/tests/block_scenarios.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{str::FromStr, sync::Arc};
use std::sync::Arc;

use super::BlockTestFactory;
use massa_consensus_exports::MockConsensusController;
Expand Down Expand Up @@ -131,7 +131,7 @@ fn basic_creation_with_operation() {
.returning(move |slot| {
assert_eq!(*slot, Slot::new(1, 0));
let content = Operation {
fee: Amount::from_str("0.01").unwrap(),
fee: Amount::from_raw(10000000000000000),
expire_period: 2,
op: OperationType::RollBuy { roll_count: 1 },
};
Expand Down
Loading
Loading