Skip to content

Commit 2e745ff

Browse files
authored
Merge pull request #215 from SethDusek/newtype_params
Add newtypes for BlockHeight/EpochLength/etc
2 parents 501250c + e080d7d commit 2e745ff

22 files changed

+334
-222
lines changed

core/src/box_kind/ballot_box.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
contracts::ballot::{
33
BallotContract, BallotContractError, BallotContractInputs, BallotContractParameters,
44
},
5+
oracle_types::BlockHeight,
56
pool_config::CastBallotBoxVoteParameters,
67
spec_token::{BallotTokenId, SpecToken, TokenIdKind, UpdateTokenId},
78
};
@@ -265,21 +266,21 @@ impl BallotBox for VoteBallotBoxWrapper {
265266
pub fn make_local_ballot_box_candidate(
266267
contract: &BallotContract,
267268
ballot_token_owner: ProveDlog,
268-
update_box_creation_height: u32,
269+
update_box_creation_height: BlockHeight,
269270
ballot_token: SpecToken<BallotTokenId>,
270271
pool_box_address_hash: Digest32,
271272
reward_tokens: Token,
272273
value: BoxValue,
273-
creation_height: u32,
274+
creation_height: BlockHeight,
274275
) -> Result<ErgoBoxCandidate, ErgoBoxCandidateBuilderError> {
275-
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height);
276+
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height.0);
276277
builder.set_register_value(
277278
NonMandatoryRegisterId::R4,
278279
(*ballot_token_owner.h).clone().into(),
279280
);
280281
builder.set_register_value(
281282
NonMandatoryRegisterId::R5,
282-
(update_box_creation_height as i32).into(),
283+
(update_box_creation_height.0 as i32).into(),
283284
);
284285
builder.set_register_value(NonMandatoryRegisterId::R6, pool_box_address_hash.into());
285286
builder.set_register_value(NonMandatoryRegisterId::R7, reward_tokens.token_id.into());

core/src/box_kind/oracle_box.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::contracts::oracle::OracleContract;
1414
use crate::contracts::oracle::OracleContractError;
1515
use crate::contracts::oracle::OracleContractInputs;
1616
use crate::contracts::oracle::OracleContractParameters;
17+
use crate::oracle_types::BlockHeight;
18+
use crate::oracle_types::EpochCounter;
1719
use crate::spec_token::OracleTokenId;
1820
use crate::spec_token::PoolTokenId;
1921
use crate::spec_token::RewardTokenId;
@@ -248,12 +250,14 @@ impl PostedOracleBox {
248250
&self.ergo_box
249251
}
250252

251-
pub fn epoch_counter(&self) -> u32 {
252-
self.ergo_box
253-
.get_register(NonMandatoryRegisterId::R5.into())
254-
.unwrap()
255-
.try_extract_into::<i32>()
256-
.unwrap() as u32
253+
pub fn epoch_counter(&self) -> EpochCounter {
254+
EpochCounter(
255+
self.ergo_box
256+
.get_register(NonMandatoryRegisterId::R5.into())
257+
.unwrap()
258+
.try_extract_into::<i32>()
259+
.unwrap() as u32,
260+
)
257261
}
258262

259263
pub fn rate(&self) -> u64 {
@@ -323,15 +327,15 @@ pub fn make_oracle_box_candidate(
323327
contract: &OracleContract,
324328
public_key: ProveDlog,
325329
datapoint: i64,
326-
epoch_counter: u32,
330+
epoch_counter: EpochCounter,
327331
oracle_token: SpecToken<OracleTokenId>,
328332
reward_token: SpecToken<RewardTokenId>,
329333
value: BoxValue,
330-
creation_height: u32,
334+
creation_height: BlockHeight,
331335
) -> Result<ErgoBoxCandidate, ErgoBoxCandidateBuilderError> {
332-
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height);
336+
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height.0);
333337
builder.set_register_value(NonMandatoryRegisterId::R4, (*public_key.h).clone().into());
334-
builder.set_register_value(NonMandatoryRegisterId::R5, (epoch_counter as i32).into());
338+
builder.set_register_value(NonMandatoryRegisterId::R5, (epoch_counter.0 as i32).into());
335339
builder.set_register_value(NonMandatoryRegisterId::R6, datapoint.into());
336340
builder.add_token(oracle_token.into());
337341
builder.add_token(reward_token.into());
@@ -346,9 +350,9 @@ pub fn make_collected_oracle_box_candidate(
346350
oracle_token: SpecToken<OracleTokenId>,
347351
reward_token: SpecToken<RewardTokenId>,
348352
value: BoxValue,
349-
creation_height: u32,
353+
creation_height: BlockHeight,
350354
) -> Result<ErgoBoxCandidate, ErgoBoxCandidateBuilderError> {
351-
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height);
355+
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height.0);
352356
builder.set_register_value(NonMandatoryRegisterId::R4, (*public_key.h).clone().into());
353357
builder.add_token(oracle_token.into());
354358
builder.add_token(reward_token.into());

core/src/box_kind/pool_box.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::contracts::pool::PoolContract;
1212
use crate::contracts::pool::PoolContractError;
1313
use crate::contracts::pool::PoolContractInputs;
1414
use crate::contracts::pool::PoolContractParameters;
15+
use crate::oracle_types::BlockHeight;
16+
use crate::oracle_types::EpochCounter;
1517
use crate::spec_token::PoolTokenId;
1618
use crate::spec_token::RefreshTokenId;
1719
use crate::spec_token::RewardTokenId;
@@ -23,7 +25,7 @@ pub trait PoolBox {
2325
fn contract(&self) -> &PoolContract;
2426
fn pool_nft_token(&self) -> SpecToken<PoolTokenId>;
2527
fn reward_token(&self) -> SpecToken<RewardTokenId>;
26-
fn epoch_counter(&self) -> u32;
28+
fn epoch_counter(&self) -> EpochCounter;
2729
fn rate(&self) -> i64;
2830
fn get_box(&self) -> &ErgoBox;
2931
}
@@ -113,12 +115,14 @@ impl PoolBox for PoolBoxWrapper {
113115
}
114116
}
115117

116-
fn epoch_counter(&self) -> u32 {
117-
self.ergo_box
118-
.get_register(NonMandatoryRegisterId::R5.into())
119-
.unwrap()
120-
.try_extract_into::<i32>()
121-
.unwrap() as u32
118+
fn epoch_counter(&self) -> EpochCounter {
119+
EpochCounter(
120+
self.ergo_box
121+
.get_register(NonMandatoryRegisterId::R5.into())
122+
.unwrap()
123+
.try_extract_into::<i32>()
124+
.unwrap() as u32,
125+
)
122126
}
123127

124128
fn rate(&self) -> i64 {
@@ -205,15 +209,15 @@ impl PoolBoxWrapperInputs {
205209
pub fn make_pool_box_candidate(
206210
contract: &PoolContract,
207211
datapoint: i64,
208-
epoch_counter: i32,
212+
epoch_counter: EpochCounter,
209213
pool_nft_token: SpecToken<PoolTokenId>,
210214
reward_token: SpecToken<RewardTokenId>,
211215
value: BoxValue,
212-
creation_height: u32,
216+
creation_height: BlockHeight,
213217
) -> Result<ErgoBoxCandidate, ErgoBoxCandidateBuilderError> {
214-
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height);
218+
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height.0);
215219
builder.set_register_value(NonMandatoryRegisterId::R4, datapoint.into());
216-
builder.set_register_value(NonMandatoryRegisterId::R5, epoch_counter.into());
220+
builder.set_register_value(NonMandatoryRegisterId::R5, (epoch_counter.0 as i32).into());
217221
builder.add_token(pool_nft_token.into());
218222
builder.add_token(reward_token.into());
219223
builder.build()
@@ -223,15 +227,15 @@ pub fn make_pool_box_candidate(
223227
pub fn make_pool_box_candidate_unchecked(
224228
contract: &PoolContract,
225229
datapoint: i64,
226-
epoch_counter: i32,
230+
epoch_counter: EpochCounter,
227231
pool_nft_token: SpecToken<PoolTokenId>,
228232
reward_token: Token,
229233
value: BoxValue,
230-
creation_height: u32,
234+
creation_height: BlockHeight,
231235
) -> Result<ErgoBoxCandidate, ErgoBoxCandidateBuilderError> {
232-
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height);
236+
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height.0);
233237
builder.set_register_value(NonMandatoryRegisterId::R4, datapoint.into());
234-
builder.set_register_value(NonMandatoryRegisterId::R5, epoch_counter.into());
238+
builder.set_register_value(NonMandatoryRegisterId::R5, (epoch_counter.0 as i32).into());
235239
builder.add_token(pool_nft_token.into());
236240
builder.add_token(reward_token);
237241
builder.build()

core/src/box_kind/refresh_box.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::contracts::refresh::RefreshContract;
1111
use crate::contracts::refresh::RefreshContractError;
1212
use crate::contracts::refresh::RefreshContractInputs;
1313
use crate::contracts::refresh::RefreshContractParameters;
14+
use crate::oracle_types::BlockHeight;
1415
use crate::spec_token::OracleTokenId;
1516
use crate::spec_token::PoolTokenId;
1617
use crate::spec_token::RefreshTokenId;
@@ -131,9 +132,9 @@ pub fn make_refresh_box_candidate(
131132
contract: &RefreshContract,
132133
refresh_nft: Token,
133134
value: BoxValue,
134-
creation_height: u32,
135+
creation_height: BlockHeight,
135136
) -> Result<ErgoBoxCandidate, ErgoBoxCandidateBuilderError> {
136-
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height);
137+
let mut builder = ErgoBoxCandidateBuilder::new(value, contract.ergo_tree(), creation_height.0);
137138
builder.add_token(refresh_nft.clone());
138139
builder.build()
139140
}

core/src/cli_commands/bootstrap.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use crate::{
4949
SignTransactionWithInputs, SubmitTransaction,
5050
},
5151
oracle_config::{BASE_FEE, ORACLE_CONFIG},
52+
oracle_types::{BlockHeight, EpochCounter},
5253
pool_config::{
5354
PoolConfig, PoolConfigError, PredefinedDataPointSource, TokenIds,
5455
DEFAULT_POOL_CONFIG_FILE_NAME,
@@ -83,7 +84,7 @@ pub fn bootstrap(config_file_name: String) -> Result<(), BootstrapError> {
8384
tx_fee: *BASE_FEE,
8485
erg_value_per_box,
8586
change_address: change_address.address(),
86-
height: node_api.node.current_block_height()? as u32,
87+
height: BlockHeight(node_api.node.current_block_height()? as u32),
8788
};
8889
let (oracle_config, submitted_tx_ids) = perform_bootstrap_chained_transaction(input)?;
8990
wait_for_txs_confirmation(submitted_tx_ids);
@@ -121,7 +122,7 @@ pub struct BootstrapInput<'a> {
121122
pub tx_fee: BoxValue,
122123
pub erg_value_per_box: BoxValue,
123124
pub change_address: Address,
124-
pub height: u32,
125+
pub height: BlockHeight,
125126
}
126127

127128
/// Perform and submit to the mempool the chained-transaction to boostrap the oracle pool. We first
@@ -212,14 +213,15 @@ pub(crate) fn perform_bootstrap_chained_transaction(
212213
};
213214
let token_box_guard =
214215
different_token_box_guard.unwrap_or_else(|| wallet_pk_ergo_tree.clone());
215-
let mut builder = ErgoBoxCandidateBuilder::new(erg_value_per_box, token_box_guard, height);
216+
let mut builder =
217+
ErgoBoxCandidateBuilder::new(erg_value_per_box, token_box_guard, height.0);
216218
builder.mint_token(token.clone(), token_name, token_desc, 0);
217219
let mut output_candidates = vec![builder.build()?];
218220

219221
let remaining_funds = ErgoBoxCandidateBuilder::new(
220222
calc_target_balance(*num_transactions_left - 1)?,
221223
wallet_pk_ergo_tree.clone(),
222-
height,
224+
height.0,
223225
)
224226
.build()?;
225227
output_candidates.push(remaining_funds.clone());
@@ -228,7 +230,7 @@ pub(crate) fn perform_bootstrap_chained_transaction(
228230
let tx_builder = TxBuilder::new(
229231
box_selection,
230232
output_candidates,
231-
height,
233+
height.0,
232234
tx_fee,
233235
change_address.clone(),
234236
);
@@ -392,7 +394,7 @@ pub(crate) fn perform_bootstrap_chained_transaction(
392394
&pool_contract,
393395
// We intentionally set the initial datapoint to be 0, as it's treated as 'undefined' during bootstrap.
394396
0,
395-
1,
397+
EpochCounter(1),
396398
SpecToken {
397399
token_id: token_ids.pool_nft_token_id.clone(),
398400
amount: pool_nft_token.amount,
@@ -410,7 +412,7 @@ pub(crate) fn perform_bootstrap_chained_transaction(
410412
let builder = ErgoBoxCandidateBuilder::new(
411413
calc_target_balance(num_transactions_left - 1)?,
412414
wallet_pk_ergo_tree.clone(),
413-
height,
415+
height.0,
414416
);
415417
output_candidates.push(builder.build()?);
416418

@@ -442,7 +444,7 @@ pub(crate) fn perform_bootstrap_chained_transaction(
442444
let tx_builder = TxBuilder::new(
443445
box_selection,
444446
output_candidates,
445-
height,
447+
height.0,
446448
tx_fee,
447449
change_address.clone(),
448450
);
@@ -498,7 +500,7 @@ pub(crate) fn perform_bootstrap_chained_transaction(
498500
let tx_builder = TxBuilder::new(
499501
box_selection,
500502
output_candidates,
501-
height,
503+
height.0,
502504
tx_fee,
503505
change_address.clone(),
504506
);
@@ -721,7 +723,7 @@ pub(crate) mod tests {
721723

722724
let bootstrap_config = BootstrapConfig::default();
723725

724-
let height = ctx.pre_header.height;
726+
let height = BlockHeight(ctx.pre_header.height);
725727
let submit_tx = SubmitTxMock::default();
726728
let oracle_config = perform_bootstrap_chained_transaction(BootstrapInput {
727729
oracle_address: address,
@@ -907,6 +909,6 @@ data_point_source_custom_script: ~
907909
oracle_address: 3Wy3BaCjGDWE3bjjZkNo3aWaMz3cYrePMFhchcKovY9uG9vhpAuW
908910
base_fee: 1100000
909911
").unwrap();
910-
assert_eq!(config.refresh_contract_parameters.min_data_points(), 2);
912+
assert_eq!(config.refresh_contract_parameters.min_data_points().0, 2);
911913
}
912914
}

core/src/cli_commands/extract_reward_tokens.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::{
3030
node_interface::{SignTransaction, SubmitTransaction},
3131
oracle_config::BASE_FEE,
3232
oracle_state::{LocalDatapointBoxSource, StageError},
33+
oracle_types::BlockHeight,
3334
spec_token::SpecToken,
3435
wallet::{WalletDataError, WalletDataSource},
3536
};
@@ -70,7 +71,7 @@ pub fn extract_reward_tokens(
7071
tx_submit: &dyn SubmitTransaction,
7172
local_datapoint_box_source: &dyn LocalDatapointBoxSource,
7273
rewards_destination_str: String,
73-
height: u32,
74+
height: BlockHeight,
7475
) -> Result<(), ExtractRewardTokensActionError> {
7576
let rewards_destination =
7677
AddressEncoder::unchecked_parse_network_address_from_str(&rewards_destination_str)?;
@@ -110,7 +111,7 @@ fn build_extract_reward_tokens_tx(
110111
local_datapoint_box_source: &dyn LocalDatapointBoxSource,
111112
wallet: &dyn WalletDataSource,
112113
rewards_destination: Address,
113-
height: u32,
114+
height: BlockHeight,
114115
change_address: Address,
115116
) -> Result<(UnsignedTransaction, u64), ExtractRewardTokensActionError> {
116117
let in_oracle_box = local_datapoint_box_source
@@ -154,7 +155,7 @@ fn build_extract_reward_tokens_tx(
154155

155156
// Build box to hold extracted tokens
156157
let mut builder =
157-
ErgoBoxCandidateBuilder::new(*BASE_FEE, rewards_destination.script()?, height);
158+
ErgoBoxCandidateBuilder::new(*BASE_FEE, rewards_destination.script()?, height.0);
158159

159160
let extracted_reward_tokens = Token {
160161
token_id: in_oracle_box.reward_token().token_id(),
@@ -180,7 +181,7 @@ fn build_extract_reward_tokens_tx(
180181
let mut tx_builder = TxBuilder::new(
181182
box_selection,
182183
vec![oracle_box_candidate, reward_box_candidate],
183-
height,
184+
height.0,
184185
*BASE_FEE,
185186
change_address,
186187
);
@@ -204,6 +205,7 @@ mod tests {
204205
use super::*;
205206
use crate::box_kind::{OracleBoxWrapper, OracleBoxWrapperInputs};
206207
use crate::contracts::oracle::OracleContractParameters;
208+
use crate::oracle_types::EpochCounter;
207209
use crate::pool_commands::test_utils::{
208210
find_input_boxes, generate_token_ids, make_datapoint_box, make_wallet_unspent_box,
209211
OracleBoxMock, WalletDataMock,
@@ -218,7 +220,7 @@ mod tests {
218220
#[test]
219221
fn test_extract_reward_tokens() {
220222
let ctx = force_any_val::<ErgoStateContext>();
221-
let height = ctx.pre_header.height;
223+
let height = BlockHeight(ctx.pre_header.height);
222224
let token_ids = generate_token_ids();
223225
let secret = force_any_val::<DlogProverInput>();
224226
let wallet = Wallet::from_secrets(vec![secret.clone().into()]);
@@ -233,10 +235,10 @@ mod tests {
233235
make_datapoint_box(
234236
*oracle_pub_key,
235237
200,
236-
1,
238+
EpochCounter(1),
237239
&token_ids,
238240
BASE_FEE.checked_mul_u32(100).unwrap(),
239-
height - 9,
241+
BlockHeight(height.0),
240242
),
241243
&oracle_box_wrapper_inputs,
242244
)

0 commit comments

Comments
 (0)