-
Notifications
You must be signed in to change notification settings - Fork 170
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
Conversation
|
||
blockchainStateProvider.observeState() | ||
.onEach { | ||
it?.run { currentBlockHeight = bestChainHeight } | ||
} | ||
.launchIn(statusScope) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been resolved.
).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), | ||
"" | ||
) | ||
) |
There was a problem hiding this comment.
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.
).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()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
PerDay | ||
PerDay, | ||
PerBlock |
There was a problem hiding this comment.
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.
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) | ||
"" | ||
} |
There was a problem hiding this comment.
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
Executors.newSingleThreadExecutor().execute(() -> { | ||
blockchainStateDao.save(new BlockchainState(true)); | ||
}); | ||
blockchainStateDataProvider.resetBlockchainState(); |
There was a problem hiding this comment.
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.
@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? |
There was a problem hiding this comment.
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
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); |
There was a problem hiding this comment.
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...
fun updateImpediments(impediments: Set<Impediment>) { | ||
coroutineScope.launch { | ||
val blockchainState = blockchainStateDao.getState() | ||
if (blockchainState != null) { | ||
blockchainState.impediments.clear() | ||
blockchainState.impediments.addAll(impediments) | ||
blockchainStateDao.saveState(blockchainState) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great
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?
Checklist: