Skip to content

Commit 6b99f27

Browse files
committed
Orders V1 support in wasm bindings; some cleanup
1 parent 8b58c2c commit 6b99f27

5 files changed

Lines changed: 248 additions & 75 deletions

File tree

common/src/chain/config/builder.rs

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,51 @@ use super::{
4949
MagicBytes,
5050
};
5151

52-
// The fork at which we upgrade consensus to dis-incentivize large pools + enable tokens v1
53-
const TESTNET_FORK_HEIGHT_1_TOKENS_V1_AND_CONSENSUS_UPGRADE: BlockHeight = BlockHeight::new(78_440);
54-
// The fork at which we upgrade chainstate to distribute reward to staker proportionally to their balance
55-
// and change various tokens fees
56-
const TESTNET_FORK_HEIGHT_2_STAKER_REWARD_AND_TOKENS_FEE: BlockHeight = BlockHeight::new(138_244);
57-
// The fork at which txs with htlc outputs become valid, data deposit fee and size, max future block time offset changed
58-
const TESTNET_FORK_HEIGHT_3_HTLC_AND_DATA_DEPOSIT_FEE: BlockHeight = BlockHeight::new(297_550);
59-
// The fork at which order outputs become valid
52+
// Note: the names of the "FORK_HEIGHT" constants below only contain the short description
53+
// of the most prominent change that happens at that height, which serves as the "proper name"
54+
// for the fork.
55+
56+
// The fork at which we:
57+
// * enable tokens v1;
58+
// * upgrade consensus to PoSConsensusVersion::V1 to dis-incentivize large pools.
59+
const TESTNET_FORK_HEIGHT_1_TOKENS_V1: BlockHeight = BlockHeight::new(78_440);
60+
61+
// The fork at which we:
62+
// * upgrade chainstate to RewardDistributionVersion::V1 to distribute reward to the staker
63+
// proportionally to their balance;
64+
// * change various tokens fees.
65+
const TESTNET_FORK_HEIGHT_2_STAKER_REWARD_DISTRIBUTION: BlockHeight = BlockHeight::new(138_244);
66+
67+
// The fork at which:
68+
// * txs with htlc outputs become valid;
69+
// * data deposit fee and size are changed;
70+
// * token metadata uri change is allowed;
71+
// * max future block time offset is changed.
72+
const TESTNET_FORK_HEIGHT_3_HTLC: BlockHeight = BlockHeight::new(297_550);
73+
74+
// The fork at which:
75+
// * order outputs become valid;
76+
// * frozen token validation logic is changed to FrozenTokensValidationVersion::V1.
6077
const TESTNET_FORK_HEIGHT_4_ORDERS: BlockHeight = BlockHeight::new(325_180);
61-
// The fork at which we enable orders v1 and prohibit updating the staker destination in ProduceBlockFromStake.
62-
const TESTNET_FORK_HEIGHT_5_ORDERS_V1_AND_STAKER_DESTINATION_UPDATE_PROHIBITION: BlockHeight =
63-
BlockHeight::new(999_999_999);
6478

65-
// The fork at which txs with htlc and orders outputs become valid
79+
// The fork at which we:
80+
// * enable orders v1;
81+
// * enable token id generation v1;
82+
// * prohibit updating the staker destination in ProduceBlockFromStake.
83+
const TESTNET_FORK_HEIGHT_5_ORDERS_V1: BlockHeight = BlockHeight::new(999_999_999);
84+
85+
// The fork at which:
86+
// * txs with htlc and order outputs become valid;
87+
// * data deposit fee and size are changed;
88+
// * token metadata uri change is allowed;
89+
// * frozen token validation logic is changed to FrozenTokensValidationVersion::V1.
6690
const MAINNET_FORK_HEIGHT_1_HTLC_AND_ORDERS: BlockHeight = BlockHeight::new(254_740);
67-
// The fork at which we enable orders v1 and prohibit updating the staker destination in ProduceBlockFromStake.
68-
const MAINNET_FORK_HEIGHT_2_ORDERS_V1_AND_STAKER_DESTINATION_UPDATE_PROHIBITION: BlockHeight =
69-
BlockHeight::new(999_999_999);
91+
92+
// The fork at which we:
93+
// * enable orders v1;
94+
// * enable token id generation v1;
95+
// * prohibit updating the staker destination in ProduceBlockFromStake.
96+
const MAINNET_FORK_HEIGHT_2_ORDERS_V1: BlockHeight = BlockHeight::new(999_999_999);
7097

7198
impl ChainType {
7299
fn default_genesis_init(&self) -> GenesisBlockInit {
@@ -151,7 +178,7 @@ impl ChainType {
151178
},
152179
),
153180
(
154-
TESTNET_FORK_HEIGHT_1_TOKENS_V1_AND_CONSENSUS_UPGRADE,
181+
TESTNET_FORK_HEIGHT_1_TOKENS_V1,
155182
ConsensusUpgrade::PoS {
156183
initial_difficulty: None,
157184
config: PoSChainConfig::new(
@@ -193,15 +220,12 @@ impl ChainType {
193220
.htlc_activated(HtlcActivated::Yes)
194221
.orders_activated(OrdersActivated::Yes)
195222
})
196-
.then(
197-
MAINNET_FORK_HEIGHT_2_ORDERS_V1_AND_STAKER_DESTINATION_UPDATE_PROHIBITION,
198-
|builder| {
199-
builder
200-
.orders_version(OrdersVersion::V1)
201-
.staker_destination_update_forbidden(StakerDestinationUpdateForbidden::Yes)
202-
.token_id_generation_version(TokenIdGenerationVersion::V1)
203-
},
204-
)
223+
.then(MAINNET_FORK_HEIGHT_2_ORDERS_V1, |builder| {
224+
builder
225+
.orders_version(OrdersVersion::V1)
226+
.staker_destination_update_forbidden(StakerDestinationUpdateForbidden::Yes)
227+
.token_id_generation_version(TokenIdGenerationVersion::V1)
228+
})
205229
.build(),
206230
ChainType::Regtest | ChainType::Signet => {
207231
let upgrades = vec![(
@@ -235,19 +259,18 @@ impl ChainType {
235259
StakerDestinationUpdateForbidden::No,
236260
TokenIdGenerationVersion::V0,
237261
))
262+
.then(TESTNET_FORK_HEIGHT_1_TOKENS_V1, |builder| {
263+
builder.token_issuance_version(TokenIssuanceVersion::V1)
264+
})
238265
.then(
239-
TESTNET_FORK_HEIGHT_1_TOKENS_V1_AND_CONSENSUS_UPGRADE,
240-
|builder| builder.token_issuance_version(TokenIssuanceVersion::V1),
241-
)
242-
.then(
243-
TESTNET_FORK_HEIGHT_2_STAKER_REWARD_AND_TOKENS_FEE,
266+
TESTNET_FORK_HEIGHT_2_STAKER_REWARD_DISTRIBUTION,
244267
|builder| {
245268
builder
246269
.reward_distribution_version(RewardDistributionVersion::V1)
247270
.tokens_fee_version(TokensFeeVersion::V1)
248271
},
249272
)
250-
.then(TESTNET_FORK_HEIGHT_3_HTLC_AND_DATA_DEPOSIT_FEE, |builder| {
273+
.then(TESTNET_FORK_HEIGHT_3_HTLC, |builder| {
251274
builder
252275
.data_deposit_fee_version(DataDepositFeeVersion::V1)
253276
.change_token_metadata_uri_activated(ChangeTokenMetadataUriActivated::Yes)
@@ -258,15 +281,12 @@ impl ChainType {
258281
.frozen_tokens_validation_version(FrozenTokensValidationVersion::V1)
259282
.orders_activated(OrdersActivated::Yes)
260283
})
261-
.then(
262-
TESTNET_FORK_HEIGHT_5_ORDERS_V1_AND_STAKER_DESTINATION_UPDATE_PROHIBITION,
263-
|builder| {
264-
builder
265-
.orders_version(OrdersVersion::V1)
266-
.staker_destination_update_forbidden(StakerDestinationUpdateForbidden::Yes)
267-
.token_id_generation_version(TokenIdGenerationVersion::V1)
268-
},
269-
)
284+
.then(TESTNET_FORK_HEIGHT_5_ORDERS_V1, |builder| {
285+
builder
286+
.orders_version(OrdersVersion::V1)
287+
.staker_destination_update_forbidden(StakerDestinationUpdateForbidden::Yes)
288+
.token_id_generation_version(TokenIdGenerationVersion::V1)
289+
})
270290
.build(),
271291
}
272292
}
@@ -661,23 +681,20 @@ mod tests {
661681
{
662682
let config = Builder::new(ChainType::Testnet).build();
663683

664-
let before_the_fork = BlockHeight::new(
665-
rng.gen_range(0..TESTNET_FORK_HEIGHT_3_HTLC_AND_DATA_DEPOSIT_FEE.into_int()),
666-
);
684+
let before_the_fork =
685+
BlockHeight::new(rng.gen_range(0..TESTNET_FORK_HEIGHT_3_HTLC.into_int()));
667686
assert_eq!(
668687
DEFAULT_MAX_FUTURE_BLOCK_TIME_OFFSET_V1,
669688
config.max_future_block_time_offset(before_the_fork)
670689
);
671690

672691
assert_eq!(
673692
DEFAULT_MAX_FUTURE_BLOCK_TIME_OFFSET_V2,
674-
config
675-
.max_future_block_time_offset(TESTNET_FORK_HEIGHT_3_HTLC_AND_DATA_DEPOSIT_FEE)
693+
config.max_future_block_time_offset(TESTNET_FORK_HEIGHT_3_HTLC)
676694
);
677695

678-
let after_the_fork = BlockHeight::new(
679-
rng.gen_range(TESTNET_FORK_HEIGHT_3_HTLC_AND_DATA_DEPOSIT_FEE.into_int()..u64::MAX),
680-
);
696+
let after_the_fork =
697+
BlockHeight::new(rng.gen_range(TESTNET_FORK_HEIGHT_3_HTLC.into_int()..u64::MAX));
681698
assert_eq!(
682699
DEFAULT_MAX_FUTURE_BLOCK_TIME_OFFSET_V2,
683700
config.max_future_block_time_offset(after_the_fork)
@@ -749,7 +766,7 @@ mod tests {
749766
),
750767
),
751768
(
752-
MAINNET_FORK_HEIGHT_2_ORDERS_V1_AND_STAKER_DESTINATION_UPDATE_PROHIBITION,
769+
MAINNET_FORK_HEIGHT_2_ORDERS_V1,
753770
ChainstateUpgrade::new(
754771
TokenIssuanceVersion::V1,
755772
RewardDistributionVersion::V1,
@@ -793,7 +810,7 @@ mod tests {
793810
),
794811
),
795812
(
796-
TESTNET_FORK_HEIGHT_1_TOKENS_V1_AND_CONSENSUS_UPGRADE,
813+
TESTNET_FORK_HEIGHT_1_TOKENS_V1,
797814
ChainstateUpgrade::new(
798815
TokenIssuanceVersion::V1,
799816
RewardDistributionVersion::V0,
@@ -809,7 +826,7 @@ mod tests {
809826
),
810827
),
811828
(
812-
TESTNET_FORK_HEIGHT_2_STAKER_REWARD_AND_TOKENS_FEE,
829+
TESTNET_FORK_HEIGHT_2_STAKER_REWARD_DISTRIBUTION,
813830
ChainstateUpgrade::new(
814831
TokenIssuanceVersion::V1,
815832
RewardDistributionVersion::V1,
@@ -825,7 +842,7 @@ mod tests {
825842
),
826843
),
827844
(
828-
TESTNET_FORK_HEIGHT_3_HTLC_AND_DATA_DEPOSIT_FEE,
845+
TESTNET_FORK_HEIGHT_3_HTLC,
829846
ChainstateUpgrade::new(
830847
TokenIssuanceVersion::V1,
831848
RewardDistributionVersion::V1,
@@ -857,7 +874,7 @@ mod tests {
857874
),
858875
),
859876
(
860-
TESTNET_FORK_HEIGHT_5_ORDERS_V1_AND_STAKER_DESTINATION_UPDATE_PROHIBITION,
877+
TESTNET_FORK_HEIGHT_5_ORDERS_V1,
861878
ChainstateUpgrade::new(
862879
TokenIssuanceVersion::V1,
863880
RewardDistributionVersion::V1,

wasm-wrappers/WASM-API.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,22 @@ Given ask and give amounts and a conclude key create output that creates an orde
366366
Given an amount to fill an order (which is described in terms of ask currency) and a destination
367367
for result outputs create an input that fills the order.
368368

369+
Note: the nonce is only needed before the orders V1 fork activation. After the fork the nonce is
370+
ignored and any value can be passed for the parameter.
371+
372+
### Function: `encode_input_for_freeze_order`
373+
374+
Given an order id create an input that freezes the order.
375+
376+
Note: order freezing is available only after the orders V1 fork activation.
377+
369378
### Function: `encode_input_for_conclude_order`
370379

371380
Given an order id create an input that concludes the order.
372381

382+
Note: the nonce is only needed before the orders V1 fork activation. After the fork the nonce is
383+
ignored and any value can be passed for the parameter.
384+
373385
### Enum: `Network`
374386

375387
The network, for which an operation to be done. Mainnet, testnet, etc.

wasm-wrappers/js-bindings/wasm_test.js

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
extract_htlc_secret,
3939
encode_create_order_output,
4040
encode_input_for_fill_order,
41+
encode_input_for_freeze_order,
4142
encode_input_for_conclude_order,
4243
SignatureHashType,
4344
encode_input_for_withdraw_from_delegation,
@@ -66,12 +67,14 @@ import {
6667
encode_input_for_change_token_metadata_uri,
6768
} from "../pkg/wasm_wrappers.js";
6869

70+
// Taken from TESTNET_FORK_HEIGHT_5_ORDERS_V1 in common/src/chain/config/builder.rs.
71+
// This will be updated to the actual height after we choose one.
72+
const ORDERS_V1_TESTNET_FORK_HEIGHT = 999999999;
73+
6974
function assert_eq_arrays(arr1, arr2) {
70-
assert(arr1.length == arr2.length, "array lengths are different");
75+
const equal = arr1.length == arr2.length && arr1.every((value, index) => value == arr2[index]);
7176

72-
arr1.forEach((elem, idx) => {
73-
assert(elem == arr2[idx], `element at index ${idx} is different`);
74-
});
77+
assert(equal, `array1 [${arr1}] differs from array2 [${arr2}]`);
7578
}
7679

7780
function assert(condition, message) {
@@ -962,14 +965,20 @@ export async function run_test() {
962965
console.log("create order tokens for coins encoding ok");
963966

964967
const order_id = "tordr1xxt0avjtt4flkq0tnlyphmdm4aaj9vmkx5r2m4g863nw3lgf7nzs7mlkqc";
965-
const fill_order_input = encode_input_for_fill_order(
968+
// Note: the exact heights don't matter as long as they are at the "correct side" of the fork.
969+
const order_v0_height = Math.floor(Math.random() * ORDERS_V1_TESTNET_FORK_HEIGHT);
970+
const order_v1_height = order_v0_height + ORDERS_V1_TESTNET_FORK_HEIGHT;
971+
// Note: the nonce is ignored since orders v1.
972+
const order_v1_nonce = Math.floor(Math.random() * 1000000);
973+
const fill_order_v0_input = encode_input_for_fill_order(
966974
order_id,
967975
Amount.from_atoms("40000"),
968976
address,
969977
BigInt(1),
978+
BigInt(order_v0_height),
970979
Network.Testnet
971980
);
972-
const expected_fill_order_input = [
981+
const expected_fill_order_v0_input = [
973982
2, 4, 7, 49, 150, 254, 178, 75, 93, 83, 251,
974983
1, 235, 159, 200, 27, 237, 187, 175, 123, 34, 179,
975984
118, 53, 6, 173, 213, 7, 212, 102, 232, 253, 9,
@@ -978,24 +987,94 @@ export async function run_test() {
978987
103, 207, 80, 217, 178
979988
];
980989

981-
assert_eq_arrays(fill_order_input, expected_fill_order_input);
982-
console.log("fill order encoding ok");
990+
assert_eq_arrays(fill_order_v0_input, expected_fill_order_v0_input);
991+
console.log("fill order v0 encoding ok");
983992

984-
const conclude_order_input = encode_input_for_conclude_order(
993+
const fill_order_v1_input = encode_input_for_fill_order(
994+
order_id,
995+
Amount.from_atoms("40000"),
996+
address,
997+
BigInt(order_v1_nonce),
998+
BigInt(order_v1_height),
999+
Network.Testnet
1000+
);
1001+
const expected_fill_order_v1_input = [
1002+
3, 0, 49, 150, 254, 178, 75, 93,
1003+
83, 251, 1, 235, 159, 200, 27, 237,
1004+
187, 175, 123, 34, 179, 118, 53, 6,
1005+
173, 213, 7, 212, 102, 232, 253, 9,
1006+
244, 197, 2, 113, 2, 0, 1, 91,
1007+
58, 110, 176, 100, 207, 6, 194, 41,
1008+
193, 30, 91, 4, 195, 202, 103, 207,
1009+
80, 217, 178
1010+
];
1011+
1012+
assert_eq_arrays(fill_order_v1_input, expected_fill_order_v1_input);
1013+
console.log("fill order v1 encoding ok");
1014+
1015+
const conclude_order_v0_input = encode_input_for_conclude_order(
9851016
order_id,
9861017
BigInt(1),
1018+
BigInt(order_v0_height),
9871019
Network.Testnet
9881020
);
989-
const expected_conclude_order_input = [
1021+
const expected_conclude_order_v0_input = [
9901022
2, 4, 6, 49, 150, 254, 178, 75,
9911023
93, 83, 251, 1, 235, 159, 200, 27,
9921024
237, 187, 175, 123, 34, 179, 118, 53,
9931025
6, 173, 213, 7, 212, 102, 232, 253,
9941026
9, 244, 197
9951027
];
9961028

997-
assert_eq_arrays(conclude_order_input, expected_conclude_order_input);
998-
console.log("conclude order encoding ok");
1029+
assert_eq_arrays(conclude_order_v0_input, expected_conclude_order_v0_input);
1030+
console.log("conclude order v0 encoding ok");
1031+
1032+
const conclude_order_v1_input = encode_input_for_conclude_order(
1033+
order_id,
1034+
BigInt(order_v1_nonce),
1035+
BigInt(order_v1_height),
1036+
Network.Testnet
1037+
);
1038+
const expected_conclude_order_v1_input = [
1039+
3, 2, 49, 150, 254, 178, 75, 93,
1040+
83, 251, 1, 235, 159, 200, 27, 237,
1041+
187, 175, 123, 34, 179, 118, 53, 6,
1042+
173, 213, 7, 212, 102, 232, 253, 9,
1043+
244, 197
1044+
];
1045+
1046+
assert_eq_arrays(conclude_order_v1_input, expected_conclude_order_v1_input);
1047+
console.log("conclude order v1 encoding ok");
1048+
1049+
const freeze_order_input = encode_input_for_freeze_order(
1050+
order_id,
1051+
BigInt(order_v1_height),
1052+
Network.Testnet
1053+
);
1054+
const expected_freeze_order_input = [
1055+
3, 1, 49, 150, 254, 178, 75, 93,
1056+
83, 251, 1, 235, 159, 200, 27, 237,
1057+
187, 175, 123, 34, 179, 118, 53, 6,
1058+
173, 213, 7, 212, 102, 232, 253, 9,
1059+
244, 197
1060+
];
1061+
1062+
assert_eq_arrays(freeze_order_input, expected_freeze_order_input);
1063+
console.log("freeze order encoding ok");
1064+
1065+
try {
1066+
encode_input_for_freeze_order(
1067+
order_id,
1068+
BigInt(order_v0_height),
1069+
Network.Testnet
1070+
);
1071+
throw new Error("Freezing an order before v1 worked somehow!");
1072+
} catch (e) {
1073+
if (!e.includes("Orders V1 not activated")) {
1074+
throw e;
1075+
}
1076+
console.log("Tested order freezing before v1 successfully");
1077+
}
9991078

10001079
try {
10011080
const invalid_inputs = "invalid inputs";

0 commit comments

Comments
 (0)