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

feat(crowdnode): limit withdrawals to once per block #1229

Merged
merged 5 commits into from
Nov 28, 2023

Conversation

HashEngineering
Copy link
Collaborator

Issue being fixed or feature implemented

Limit WD to once per block. Still WiP

Related PR's and Dependencies

Screenshots / Videos

How Has This Been Tested?

  • QA (Mobile Team)

Checklist:

  • I have performed a self-review of my own code and added comments where necessary
  • I have added or updated relevant unit/integration/functional/e2e tests

@HashEngineering HashEngineering self-assigned this Nov 23, 2023
Comment on lines 161 to 166

blockchainStateProvider.observeState()
.onEach {
it?.run { currentBlockHeight = bestChainHeight }
}
.launchIn(statusScope)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to expose currentBlockHeight as a property (if possible) or suspend function to avoid dancing with observables. Here and in many other places, we might only need to get it once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a such a suspend function already.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been resolved.

Comment on lines 348 to 359
).show(requireActivity()) {
// TODO: what do we do here?
// close this page?
// findNavController().popBackStack()
// show an error?
safeNavigate(
TransferFragmentDirections.transferToResult(
true,
getString(R.string.crowdnode_withdraw_error),
""
)
)
Copy link
Member

@Syn-McJ Syn-McJ Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To properly handle the progress dialog, we need to invoke the suspend method for showing the dialog which is called showAsync (not sure if it's a good name). The return value can be discarded.

Suggested change
).show(requireActivity()) {
// TODO: what do we do here?
// close this page?
// findNavController().popBackStack()
// show an error?
safeNavigate(
TransferFragmentDirections.transferToResult(
true,
getString(R.string.crowdnode_withdraw_error),
""
)
)
).showAsync(requireActivity())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines -33 to +34
PerDay
PerDay,
PerBlock
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add PerBlock limit period.

Comment on lines 342 to 359
val limits = viewModel.getWithdrawalLimits()
val okButtonText = if (period == WithdrawalLimitPeriod.PerTransaction) {
if (viewModel.onlineAccountStatus == OnlineAccountStatus.Done) {
getString(R.string.read_withdrawal_policy)
if (period == WithdrawalLimitPeriod.PerBlock) {
AdaptiveDialog.create(
R.drawable.ic_warning,
"Please wait before initiating the next withdrawal",
"Please wait 5 minutes before initiating another withdrawal",
getString(R.string.button_okay)
).showAsync(requireActivity())
} else {
val limits = viewModel.getWithdrawalLimits()
val okButtonText = if (period == WithdrawalLimitPeriod.PerTransaction) {
if (viewModel.onlineAccountStatus == OnlineAccountStatus.Done) {
getString(R.string.read_withdrawal_policy)
} else {
getString(R.string.online_account_create)
}
} else {
getString(R.string.online_account_create)
""
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the period is PerBlock, then show one dialog, otherwise show the regular limit dialog

Comment on lines -752 to +755
Executors.newSingleThreadExecutor().execute(() -> {
blockchainStateDao.save(new BlockchainState(true));
});
blockchainStateDataProvider.resetBlockchainState();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this function to BlockchainStateDataProvider to run in a coroutine -- same for the next change.

Comment on lines 33 to +44
@Insert(onConflict = OnConflictStrategy.REPLACE)
protected abstract fun insert(blockchainState: BlockchainState)
protected abstract suspend fun insert(blockchainState: BlockchainState)

fun save(blockchainState: BlockchainState) {
suspend fun saveState(blockchainState: BlockchainState) {
if (blockchainState.replaying && blockchainState.percentageSync == 100) {
blockchainState.replaying = false
}
insert(blockchainState)
}

@Query("SELECT * FROM blockchain_state LIMIT 1")
abstract fun load(): LiveData<BlockchainState?>

@Query("SELECT * FROM blockchain_state LIMIT 1")
abstract fun loadSync(): BlockchainState?

@Query("SELECT * FROM blockchain_state LIMIT 1")
abstract suspend fun get(): BlockchainState?
abstract suspend fun getState(): BlockchainState?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All load and save functions will be suspend

Comment on lines 1164 to +1165
private void updateBlockchainStateImpediments() {
executor.execute(() -> {
BlockchainState blockchainState = blockchainStateDao.loadSync();
if (blockchainState != null) {
blockchainState.getImpediments().clear();
blockchainState.getImpediments().addAll(impediments);
blockchainStateDao.save(blockchainState);
}
});
blockchainStateDataProvider.updateImpediments(impediments);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to BlockchainStateDataProvider to run in a coroutine...

Comment on lines +87 to +97
fun updateImpediments(impediments: Set<Impediment>) {
coroutineScope.launch {
val blockchainState = blockchainStateDao.getState()
if (blockchainState != null) {
blockchainState.impediments.clear()
blockchainState.impediments.addAll(impediments)
blockchainStateDao.saveState(blockchainState)
}
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all run and forget methods which update the database table row, but they were also run and forget using Executors previously.

@HashEngineering HashEngineering marked this pull request as ready for review November 27, 2023 16:37
Copy link
Member

@Syn-McJ Syn-McJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

@HashEngineering HashEngineering merged commit d9ba8e7 into master Nov 28, 2023
2 checks passed
@HashEngineering HashEngineering deleted the feature-crowdnode-wd-limit branch December 15, 2023 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants