-
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
4 changed files
with
140 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
-- License: MIT | ||
-- | ||
-- fixme: blurb | ||
|
||
module Cardano.Wallet.Primitive.AddressDiscovery.Fixed | ||
( -- * Fixed address list | ||
FixedAddressState (..) | ||
|
||
-- ** State | ||
, AnyAddressState (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Primitive.Types | ||
( Address(..), IsOurs (..), invariant, WalletId(..) ) | ||
import Cardano.Wallet ( InitState(..)) | ||
import Data.Set | ||
( Set ) | ||
import Data.Map.Strict | ||
( Map ) | ||
import Data.Maybe | ||
( isJust ) | ||
import Data.Text.Class | ||
( FromText (..), TextDecodingError (..), ToText (..) ) | ||
import GHC.Generics | ||
( Generic ) | ||
import Control.DeepSeq | ||
( NFData ) | ||
import Text.Read | ||
( readMaybe ) | ||
import Data.Word (Word32) | ||
import Data.Digest.CRC32 (crc32) | ||
import Crypto.Hash | ||
( Digest, HashAlgorithm, hash ) | ||
|
||
import qualified Data.ByteString.Char8 as B8 | ||
import qualified Data.Set as Set | ||
import qualified Data.Text as T | ||
|
||
{------------------------------------------------------------------------------- | ||
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 | ||
isOurs addr s = (Set.member addr (knownAddresses s), s) | ||
|
||
|
||
{------------------------------------------------------------------------------- | ||
Any Address Derivation | ||
An arbitrary proportion 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 | ||
isOurs (Address addr) s@(AnyAddressState p) = (crc32 addr < p', s) | ||
where | ||
p' = floor (fromIntegral (maxBound :: Word32) * p) | ||
|
||
instance InitState AnyAddressState where | ||
initState s = (walletId s, AnyAddressState 0.5) | ||
|
||
walletId :: Show a => a -> WalletId | ||
walletId = WalletId . hash . B8.pack . show |
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