Skip to content

Commit ee41999

Browse files
more work
1 parent b0bf2f3 commit ee41999

File tree

11 files changed

+238
-62
lines changed

11 files changed

+238
-62
lines changed

packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/distribution/perpetual/block_based.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ mod perpetual_distribution_block {
397397

398398
let platform_state = platform.state.load();
399399

400-
let (identity, signer, key) =
401-
setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5));
400+
let (identity, _, _) = setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5));
402401

403402
let (identity_2, signer_2, key_2) =
404403
setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5));

packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/distribution/perpetual/time_based.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,4 +1256,126 @@ mod perpetual_distribution_time {
12561256
.expect("expected to fetch token balance");
12571257
assert_eq!(token_balance, Some(72057594046349056));
12581258
}
1259+
1260+
#[test]
1261+
fn test_token_perpetual_distribution_time_linear_high_values_old_contract_should_handle_overflow(
1262+
) {
1263+
let platform_version = PlatformVersion::latest();
1264+
let mut platform = TestPlatformBuilder::new()
1265+
.with_latest_protocol_version()
1266+
.build_with_mock_rpc()
1267+
.set_genesis_state();
1268+
1269+
let mut rng = StdRng::seed_from_u64(4981);
1270+
1271+
let platform_state = platform.state.load();
1272+
1273+
let (identity, _, _) = setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5));
1274+
1275+
let (identity_2, signer_2, key_2) =
1276+
setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5));
1277+
1278+
let (contract, token_id) = create_token_contract_with_owner_identity(
1279+
&mut platform,
1280+
identity.id(),
1281+
Some(|token_configuration: &mut TokenConfiguration| {
1282+
token_configuration
1283+
.distribution_rules_mut()
1284+
.set_perpetual_distribution(Some(TokenPerpetualDistribution::V0(
1285+
TokenPerpetualDistributionV0 {
1286+
distribution_type: RewardDistributionType::TimeBasedDistribution {
1287+
// every 1 millisecond
1288+
interval: 1,
1289+
function: DistributionFunction::Linear {
1290+
a: MAX_LINEAR_SLOPE_PARAM as i64, // Strongest slope
1291+
d: 1, // No division
1292+
start_step: None,
1293+
starting_amount: MAX_DISTRIBUTION_PARAM,
1294+
min_value: None,
1295+
max_value: None,
1296+
},
1297+
},
1298+
distribution_recipient: TokenDistributionRecipient::Identity(
1299+
identity_2.id(),
1300+
),
1301+
},
1302+
)));
1303+
}),
1304+
None,
1305+
None,
1306+
platform_version,
1307+
);
1308+
1309+
// 5 hours later
1310+
fast_forward_to_block(&platform, 18_000_000, 40, 42, 1, false);
1311+
1312+
for i in 0..256 {
1313+
// We are only claiming for 256 cycles
1314+
let claim_transition = BatchTransition::new_token_claim_transition(
1315+
token_id,
1316+
identity_2.id(),
1317+
contract.id(),
1318+
0,
1319+
TokenDistributionType::Perpetual,
1320+
None,
1321+
&key_2,
1322+
2 + i,
1323+
0,
1324+
&signer_2,
1325+
platform_version,
1326+
None,
1327+
None,
1328+
None,
1329+
)
1330+
.expect("expect to create documents batch transition");
1331+
1332+
let claim_serialized_transition = claim_transition
1333+
.serialize_to_bytes()
1334+
.expect("expected documents batch serialized state transition");
1335+
1336+
let transaction = platform.drive.grove.start_transaction();
1337+
1338+
let processing_result = platform
1339+
.platform
1340+
.process_raw_state_transitions(
1341+
&vec![claim_serialized_transition.clone()],
1342+
&platform_state,
1343+
&BlockInfo {
1344+
time_ms: 18_100_000,
1345+
height: 41,
1346+
core_height: 42,
1347+
epoch: Epoch::new(1).unwrap(),
1348+
},
1349+
&transaction,
1350+
platform_version,
1351+
false,
1352+
None,
1353+
)
1354+
.expect("expected to process state transition");
1355+
1356+
assert_matches!(
1357+
processing_result.execution_results().as_slice(),
1358+
[StateTransitionExecutionResult::SuccessfulExecution(_, _)]
1359+
);
1360+
1361+
platform
1362+
.drive
1363+
.grove
1364+
.commit_transaction(transaction)
1365+
.unwrap()
1366+
.expect("expected to commit transaction");
1367+
}
1368+
1369+
let token_balance = platform
1370+
.drive
1371+
.fetch_identity_token_balance(
1372+
token_id.to_buffer(),
1373+
identity_2.id().to_buffer(),
1374+
None,
1375+
platform_version,
1376+
)
1377+
.expect("expected to fetch token balance");
1378+
// This is i64::Max
1379+
assert_eq!(token_balance, Some(9223372036854675807));
1380+
}
12591381
}

packages/rs-drive/src/drive/tokens/mint/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl Drive {
1818
identity_id: [u8; 32],
1919
issuance_amount: u64,
2020
allow_first_mint: bool,
21+
allow_saturation: bool,
2122
block_info: &BlockInfo,
2223
apply: bool,
2324
transaction: TransactionArg,
@@ -29,6 +30,7 @@ impl Drive {
2930
identity_id,
3031
issuance_amount,
3132
allow_first_mint,
33+
allow_saturation,
3234
block_info,
3335
apply,
3436
transaction,
@@ -49,6 +51,7 @@ impl Drive {
4951
identity_id: [u8; 32],
5052
issuance_amount: u64,
5153
allow_first_mint: bool,
54+
allow_saturation: bool,
5255
apply: bool,
5356
transaction: TransactionArg,
5457
drive_operations: &mut Vec<LowLevelDriveOperation>,
@@ -60,6 +63,7 @@ impl Drive {
6063
identity_id,
6164
issuance_amount,
6265
allow_first_mint,
66+
allow_saturation,
6367
apply,
6468
transaction,
6569
drive_operations,
@@ -80,6 +84,7 @@ impl Drive {
8084
identity_id: [u8; 32],
8185
issuance_amount: u64,
8286
allow_first_mint: bool,
87+
allow_saturation: bool,
8388
estimated_costs_only_with_layer_info: &mut Option<
8489
HashMap<KeyInfoPath, EstimatedLayerInformation>,
8590
>,
@@ -92,6 +97,7 @@ impl Drive {
9297
identity_id,
9398
issuance_amount,
9499
allow_first_mint,
100+
allow_saturation,
95101
estimated_costs_only_with_layer_info,
96102
transaction,
97103
platform_version,

packages/rs-drive/src/drive/tokens/mint/v0/mod.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ impl Drive {
1414
identity_id: [u8; 32],
1515
issuance_amount: u64,
1616
allow_first_mint: bool,
17+
allow_saturation: bool,
1718
block_info: &BlockInfo,
1819
apply: bool,
1920
transaction: TransactionArg,
@@ -26,6 +27,7 @@ impl Drive {
2627
identity_id,
2728
issuance_amount,
2829
allow_first_mint,
30+
allow_saturation,
2931
apply,
3032
transaction,
3133
&mut drive_operations,
@@ -50,6 +52,7 @@ impl Drive {
5052
identity_id: [u8; 32],
5153
issuance_amount: u64,
5254
allow_first_mint: bool,
55+
allow_saturation: bool,
5356
apply: bool,
5457
transaction: TransactionArg,
5558
drive_operations: &mut Vec<LowLevelDriveOperation>,
@@ -63,6 +66,7 @@ impl Drive {
6366
identity_id,
6467
issuance_amount,
6568
allow_first_mint,
69+
allow_saturation,
6670
&mut estimated_costs_only_with_layer_info,
6771
transaction,
6872
platform_version,
@@ -83,6 +87,7 @@ impl Drive {
8387
identity_id: [u8; 32],
8488
issuance_amount: u64,
8589
allow_first_mint: bool,
90+
allow_saturation: bool,
8691
estimated_costs_only_with_layer_info: &mut Option<
8792
HashMap<KeyInfoPath, EstimatedLayerInformation>,
8893
>,
@@ -91,24 +96,30 @@ impl Drive {
9196
) -> Result<Vec<LowLevelDriveOperation>, Error> {
9297
let mut drive_operations = vec![];
9398

99+
let (add_to_supply_operations, actual_issuance_amount) = self
100+
.add_to_token_total_supply_operations(
101+
token_id,
102+
issuance_amount,
103+
allow_first_mint,
104+
allow_saturation,
105+
estimated_costs_only_with_layer_info,
106+
transaction,
107+
platform_version,
108+
)?;
109+
110+
// There is a chance that we can't add more to the supply because it would overflow, in that case we issue what can be issued if allow saturation is set to true
111+
94112
// Update identity balance
95113
drive_operations.extend(self.add_to_identity_token_balance_operations(
96114
token_id,
97115
identity_id,
98-
issuance_amount,
116+
actual_issuance_amount,
99117
estimated_costs_only_with_layer_info,
100118
transaction,
101119
platform_version,
102120
)?);
103121

104-
drive_operations.extend(self.add_to_token_total_supply_operations(
105-
token_id,
106-
issuance_amount,
107-
allow_first_mint,
108-
estimated_costs_only_with_layer_info,
109-
transaction,
110-
platform_version,
111-
)?);
122+
drive_operations.extend(add_to_supply_operations);
112123

113124
Ok(drive_operations)
114125
}

packages/rs-drive/src/drive/tokens/mint_many/v0/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ impl Drive {
129129

130130
// Update total supply
131131

132-
drive_operations.extend(self.add_to_token_total_supply_operations(
133-
token_id.to_buffer(),
134-
total_mint_amount,
135-
allow_first_mint,
136-
estimated_costs_only_with_layer_info,
137-
transaction,
138-
platform_version,
139-
)?);
132+
drive_operations.extend(
133+
self.add_to_token_total_supply_operations(
134+
token_id.to_buffer(),
135+
total_mint_amount,
136+
allow_first_mint,
137+
false,
138+
estimated_costs_only_with_layer_info,
139+
transaction,
140+
platform_version,
141+
)?
142+
.0,
143+
);
140144

141145
Ok(drive_operations)
142146
}

packages/rs-drive/src/drive/tokens/system/add_to_token_total_supply/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::drive::Drive;
44
use crate::error::drive::DriveError;
55
use crate::error::Error;
66
use crate::fees::op::LowLevelDriveOperation;
7+
use dpp::balances::credits::TokenAmount;
78
use dpp::block::block_info::BlockInfo;
89
use dpp::fee::fee_result::FeeResult;
910
use dpp::version::PlatformVersion;
@@ -16,13 +17,14 @@ impl Drive {
1617
pub fn add_to_token_total_supply(
1718
&self,
1819
token_id: [u8; 32],
19-
amount: u64,
20+
amount: TokenAmount,
2021
allow_first_mint: bool,
22+
allow_saturation: bool,
2123
block_info: &BlockInfo,
2224
apply: bool,
2325
transaction: TransactionArg,
2426
platform_version: &PlatformVersion,
25-
) -> Result<FeeResult, Error> {
27+
) -> Result<(FeeResult, TokenAmount), Error> {
2628
match platform_version
2729
.drive
2830
.methods
@@ -34,6 +36,7 @@ impl Drive {
3436
token_id,
3537
amount,
3638
allow_first_mint,
39+
allow_saturation,
3740
block_info,
3841
apply,
3942
transaction,
@@ -51,13 +54,14 @@ impl Drive {
5154
pub fn add_to_token_total_supply_add_to_operations(
5255
&self,
5356
token_id: [u8; 32],
54-
amount: u64,
57+
amount: TokenAmount,
5558
allow_first_mint: bool,
59+
allow_saturation: bool,
5660
apply: bool,
5761
transaction: TransactionArg,
5862
drive_operations: &mut Vec<LowLevelDriveOperation>,
5963
platform_version: &PlatformVersion,
60-
) -> Result<(), Error> {
64+
) -> Result<TokenAmount, Error> {
6165
match platform_version
6266
.drive
6367
.methods
@@ -69,6 +73,7 @@ impl Drive {
6973
token_id,
7074
amount,
7175
allow_first_mint,
76+
allow_saturation,
7277
apply,
7378
transaction,
7479
drive_operations,
@@ -86,14 +91,15 @@ impl Drive {
8691
pub fn add_to_token_total_supply_operations(
8792
&self,
8893
token_id: [u8; 32],
89-
amount: u64,
94+
amount: TokenAmount,
9095
allow_first_mint: bool,
96+
allow_saturation: bool,
9197
estimated_costs_only_with_layer_info: &mut Option<
9298
HashMap<KeyInfoPath, EstimatedLayerInformation>,
9399
>,
94100
transaction: TransactionArg,
95101
platform_version: &PlatformVersion,
96-
) -> Result<Vec<LowLevelDriveOperation>, Error> {
102+
) -> Result<(Vec<LowLevelDriveOperation>, TokenAmount), Error> {
97103
match platform_version
98104
.drive
99105
.methods
@@ -105,6 +111,7 @@ impl Drive {
105111
token_id,
106112
amount,
107113
allow_first_mint,
114+
allow_saturation,
108115
estimated_costs_only_with_layer_info,
109116
transaction,
110117
platform_version,

0 commit comments

Comments
 (0)