-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new address discovery schemes for testing and benchmarking
- Loading branch information
Showing
10 changed files
with
234 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
test/bench/Cardano/Wallet/Primitive/AddressDiscovery/Fixed.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
-- License: MIT | ||
-- | ||
-- Custom address discovery schemes used for testing and benchmarking. | ||
-- | ||
|
||
module Cardano.Wallet.Primitive.AddressDiscovery.Fixed | ||
( -- * Fixed address list | ||
FixedAddressState (..) | ||
|
||
-- ** State | ||
, AnyAddressState (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet | ||
( InitState (..), NewWallet (..) ) | ||
import Cardano.Wallet.Primitive.Types | ||
( Address (..), IsOurs (..), WalletId (..) ) | ||
import Control.DeepSeq | ||
( NFData ) | ||
import Crypto.Hash | ||
( hash ) | ||
import Data.Digest.CRC32 | ||
( crc32 ) | ||
import Data.Set | ||
( Set ) | ||
import Data.Word | ||
( Word32 ) | ||
import GHC.Generics | ||
( Generic ) | ||
|
||
import qualified Data.ByteString.Char8 as B8 | ||
import qualified Data.Set as Set | ||
|
||
---------------------------------------------------------------------------- | ||
|
||
-- | Fixed Address Derivation | ||
-- | ||
-- The given set of addresses are recognized as "ours". | ||
data FixedAddressState = FixedAddressState | ||
{ knownAddresses :: !(Set Address) | ||
} | ||
deriving stock (Generic, Show) | ||
|
||
instance NFData FixedAddressState | ||
|
||
instance IsOurs FixedAddressState where | ||
type AddressDiscoveryConfig FixedAddressState = FixedAddressState | ||
isOurs addr s = (Set.member addr (knownAddresses s), s) | ||
|
||
instance InitState FixedAddressState where | ||
initState w = (walletId cfg, cfg) | ||
where cfg = addressDiscoveryConfig w | ||
|
||
---------------------------------------------------------------------------- | ||
|
||
-- | Any Address Derivation | ||
-- | ||
-- An arbitrary fraction of addreses are recognized as "ours". This is done by | ||
-- looking at a checksum of the address. | ||
data AnyAddressState = AnyAddressState | ||
{ oursProportion :: !Double | ||
} | ||
deriving stock (Generic, Show) | ||
|
||
instance NFData AnyAddressState | ||
|
||
instance IsOurs AnyAddressState where | ||
type AddressDiscoveryConfig AnyAddressState = AnyAddressState | ||
|
||
isOurs (Address addr) s@(AnyAddressState p) = (crc32 addr < p', s) | ||
where | ||
p' = floor (fromIntegral (maxBound :: Word32) * p) | ||
|
||
instance InitState AnyAddressState where | ||
initState w = (walletId cfg, cfg) | ||
where cfg = addressDiscoveryConfig w | ||
|
||
walletId :: Show a => a -> WalletId | ||
walletId = WalletId . hash . B8.pack . show |
Oops, something went wrong.