Copyright | © 2021 IOHK |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
An address pool caches a collection of addresses. The purpose of this data structure is to aid in BIP-44 style address discovery with an address gap.
Synopsis
- data Pool addr ix
- addressFromIx :: Pool addr ix -> ix -> addr
- addresses :: Pool addr ix -> Map addr (ix, AddressState )
- usedAddresses :: Pool addr ix -> [addr]
- gap :: Pool addr ix -> Int
- lookup :: Ord addr => addr -> Pool addr ix -> Maybe ix
- size :: Pool addr ix -> Int
- successor :: Enum ix => Pool addr ix -> ix -> Maybe ix
- new :: ( Ord addr, Enum ix) => (ix -> addr) -> Int -> Pool addr ix
- load :: ( Ord addr, Ord ix, Enum ix) => Pool addr ix -> Map addr (ix, AddressState ) -> Maybe ( Pool addr ix)
- update :: ( Ord addr, Enum ix) => addr -> Pool addr ix -> Pool addr ix
- clear :: ( Ord addr, Enum ix) => Pool addr ix -> Pool addr ix
- discover :: ( Enum ix, Ord addr, Monad m, Monoid txs, Eq txs) => (addr -> m txs) -> Pool addr ix -> m (txs, Pool addr ix)
- loadUnsafe :: Pool addr ix -> Map addr (ix, AddressState ) -> Pool addr ix
- prop_sequence :: ( Ord ix, Enum ix) => Pool addr ix -> Bool
- prop_gap :: Ord ix => Pool addr ix -> Bool
- prop_fresh :: Ord ix => Pool addr ix -> Bool
- prop_fromIx :: Eq addr => Pool addr ix -> Bool
- prop_consistent :: ( Ord ix, Enum ix, Eq addr) => Pool addr ix -> Bool
Documentation
An address pool caches a collection of addresses (type
addr
)
which are derived from a numeric index (type
ix
).
Instances
( Show addr, Show ix) => Show ( Pool addr ix) Source # | |
Generic ( Pool addr ix) Source # | |
( NFData addr, NFData ix) => NFData ( Pool addr ix) Source # | |
Defined in Cardano.Wallet.Address.Pool |
|
Buildable ( Pool addr ix) Source # | |
type Rep ( Pool addr ix) Source # | |
Defined in Cardano.Wallet.Address.Pool
type
Rep
(
Pool
addr ix) =
D1
('
MetaData
"Pool" "Cardano.Wallet.Address.Pool" "cardano-wallet-core-2022.7.1-AGKhlyz9liLKN3QqZD1gj" '
False
) (
C1
('
MetaCons
"Pool" '
PrefixI
'
True
) (
S1
('
MetaSel
('
Just
"addressFromIx") '
NoSourceUnpackedness
'
NoSourceStrictness
'
DecidedLazy
) (
Rec0
(ix -> addr))
:*:
(
S1
('
MetaSel
('
Just
"gap") '
NoSourceUnpackedness
'
NoSourceStrictness
'
DecidedLazy
) (
Rec0
Int
)
:*:
S1
('
MetaSel
('
Just
"addresses") '
NoSourceUnpackedness
'
NoSourceStrictness
'
DecidedLazy
) (
Rec0
(
Map
addr (ix,
AddressState
))))))
|
addressFromIx :: Pool addr ix -> ix -> addr Source #
Mapping from a numeric index to its corresponding address.
This mapping is supposed to be (practically) a one-way function:
Given an
addr
, it is impossible to compute the preimage
ix
in practice.
The purpose of the
Pool
data structure is to help inverting
this function regardless. The idea is that addresses
with small indices
0,1,…
are
Used
before addresses with larger
indices; specifically, only less than
gap
many addresses in sequence
may be
Unused
before the next
Used
address.
This usage scheme restricts the search space considerably
and allows us to practically invert the
addressFromIx
function.
addresses :: Pool addr ix -> Map addr (ix, AddressState ) Source #
Partial, cached inverse of the
addressFromIx
.
This map contains all cached addresses
addr
,
their corresponding indices
ix
,
and whether they are
Used
or
Unused
.
See
prop_sequence
.
usedAddresses :: Pool addr ix -> [addr] Source #
Sorted list of all addresses that are marked
Used
in the pool.
gap :: Pool addr ix -> Int Source #
The pool gap determines how
Used
and
Unused
have to be distributed.
See
prop_gap
and
prop_fresh
.
successor :: Enum ix => Pool addr ix -> ix -> Maybe ix Source #
Given an index
ix
, return the enumerated successor
Just (succ ix)
as long as the address corresponding to this successor is still
in the pool.
This function is useful for address discovery in a light client setting,
where the discovery procedure is:
Start with index
ix = 0
, query the corresponding address in an explorer,
update
address pool and repeat with
successor ix
until the latter
returns
Nothing
. According to the BIP-44 standard,
the account may not contain any other addresses than the ones discovered.
This function is not useful for generating change addresses,
as it does not take
Used
or
Unused
status into account.
new :: ( Ord addr, Enum ix) => (ix -> addr) -> Int -> Pool addr ix Source #
Create a new address pool.
load :: ( Ord addr, Ord ix, Enum ix) => Pool addr ix -> Map addr (ix, AddressState ) -> Maybe ( Pool addr ix) Source #
Replace the collection of addresses in a pool,
but only if this collection satisfies the necessary invariants
such as
prop_sequence
etc.
update :: ( Ord addr, Enum ix) => addr -> Pool addr ix -> Pool addr ix Source #
Update an address to the
Used
status
and create new
Unused
addresses in order to satisfy
prop_fresh
.
Does nothing if the address was not in the pool.
clear :: ( Ord addr, Enum ix) => Pool addr ix -> Pool addr ix Source #
Remove all previously discovered addresses,
i.e. create a new pool with the same
addressFromIx
and
gap
as the old pool.
Address Discovery
discover :: ( Enum ix, Ord addr, Monad m, Monoid txs, Eq txs) => (addr -> m txs) -> Pool addr ix -> m (txs, Pool addr ix) Source #
Discover transactions and addresses by
using an efficient query
addr -> m txs
and an address pool.
Internal
loadUnsafe :: Pool addr ix -> Map addr (ix, AddressState ) -> Pool addr ix Source #
Replace the collection of addresses in a pool, but skips checking the invariants.
prop_sequence :: ( Ord ix, Enum ix) => Pool addr ix -> Bool Source #
Internal invariant:
The indices of the addresses in a pool form a finite
sequence beginning with
fromEnum
0
.
prop_fromIx :: Eq addr => Pool addr ix -> Bool Source #
Internal invariant:
All
addresses
in the pool have been generated from their index
via the pool
addressFromIx
.