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

Random input selection impl #140

Merged
merged 16 commits into from
Apr 4, 2019

Conversation

paweljakubas
Copy link
Contributor

@paweljakubas paweljakubas commented Apr 2, 2019

Issue Number

#92

Overview

  • I have implemented random input selection
  • I have added respective unit and property tests

Comments

pure res


-- Selecting coins to cover at least the specified value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how I understand how this algorithm operates

withIx m' ix = Just (Map.elemAt ix m', Map.deleteAt ix m')


instance MonadRandom m => MonadRandom (ExceptT e m) where
Copy link
Contributor Author

Choose a reason for hiding this comment

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

need this unfortunately

@paweljakubas paweljakubas force-pushed the paweljakubas/92/adding-random-input-selection branch from 720a1dc to b5de826 Compare April 2, 2019 10:42
@paweljakubas paweljakubas self-assigned this Apr 2, 2019
@paweljakubas paweljakubas force-pushed the paweljakubas/92/adding-random-input-selection branch from b5de826 to c672899 Compare April 3, 2019 18:54
})

describe "Coin selection properties : Random algorithm" $ do
xit "forall (UTxO, NonEmpty TxOut), \
Copy link
Contributor Author

Choose a reason for hiding this comment

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

it stucks when random is called, probably because of wrong impl of MonadRandom instance of Identity at the end of this file

<*> vectorOf n arbitrary
return $ UTxO $ Map.fromList utxo

instance MonadRandom Identity where
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is probably wrong

Copy link
Contributor

@akegalj akegalj Apr 4, 2019

Choose a reason for hiding this comment

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

it makes sense to me that this will loop forever. On the right side you are calling the same getRandomBytes defined in the instance MonadRandom Identity , with the same params.

Looking at https://www.stackage.org/haddock/lts-13.15/cryptonite-0.25/Crypto-Random-Types.html what I believe you should have aimed for is to use one of two implementations of class DRG (from the same module) for implementation of MonadRandom Identity as you would like to use some deterministic pseudo random generator in this pure code.

I can have a closer look later and experiment if this doesn't give any clue (hope its the right one)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @akegalj for the comment. Indeed, random monad within random algo needs more effects power than the monad within largestFirst. there is a need of more effects to enable randomness - be in IO or have initial seed/generator.

$ NE.sortBy (flip $ comparing coin) txOutputs
let n = maximumNumberOfInputs opt

let moneyRequested = sum $ (getCoin . coin) <$> txOutputsSorted
Copy link
Contributor

Choose a reason for hiding this comment

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

I think parens can be dropped here

sum $ getCoin . coin <$> txOutputsSorted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

<*> vectorOf n arbitrary
return $ UTxO $ Map.fromList utxo

instance MonadRandom Identity where
Copy link
Contributor

@akegalj akegalj Apr 4, 2019

Choose a reason for hiding this comment

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

it makes sense to me that this will loop forever. On the right side you are calling the same getRandomBytes defined in the instance MonadRandom Identity , with the same params.

Looking at https://www.stackage.org/haddock/lts-13.15/cryptonite-0.25/Crypto-Random-Types.html what I believe you should have aimed for is to use one of two implementations of class DRG (from the same module) for implementation of MonadRandom Identity as you would like to use some deterministic pseudo random generator in this pure code.

I can have a closer look later and experiment if this doesn't give any clue (hope its the right one)

@paweljakubas paweljakubas force-pushed the paweljakubas/92/adding-random-input-selection branch from cd287bd to 939f3f6 Compare April 4, 2019 08:39
@paweljakubas
Copy link
Contributor Author

paweljakubas commented Apr 4, 2019

resigned from using runIdentity with random and used newDRG and withDRG instead

@paweljakubas paweljakubas force-pushed the paweljakubas/92/adding-random-input-selection branch 2 times, most recently from fc03426 to 328976e Compare April 4, 2019 09:08
-- step.)
--
-- 2. Randomly select outputs from the UTxO, considering for each output if that
-- output is animprovement. If it is, add it to the transaction, and keep
Copy link
Contributor Author

Choose a reason for hiding this comment

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

animprovement -> an improvement

targetAim <-
if isValidCoin (Coin $ 2*v) then Just (Coin $ 2*v) else Nothing
targetMax <-
if isValidCoin (Coin $ 3*v) then Just (Coin $ 3*v) else Nothing
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we are not afraid of situation that for instance targetMax will exceed upperBound of what Coin can have???

paweljakubas and others added 13 commits April 4, 2019 17:35
Add NotEnoughMoney and UtxoNotEnoughFragmented errors
Don't use Identity with MonadRandom

Fix maxNumberOfInput in propertyFragmentation

Hush hlint
We don't need these various targets to be 'Coin', some may actually not
be; in the end, we will select several coins, so it's not impossible
that the sum of their value will overflow the max coin value. In the
rare case this would happen, we can simply fallback to splitting the
output into two change coins.
@KtorZ KtorZ force-pushed the paweljakubas/92/adding-random-input-selection branch from fd04299 to a5bddd3 Compare April 4, 2019 15:35
@KtorZ KtorZ merged commit 548a039 into master Apr 4, 2019
@KtorZ KtorZ deleted the paweljakubas/92/adding-random-input-selection branch April 4, 2019 15:55
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.

None yet

3 participants