Skip to content

Commit

Permalink
Track key-0 separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed May 4, 2021
1 parent d273f94 commit 5a9701c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Expand Up @@ -77,6 +77,7 @@ data DelegationState k = DelegationState
rewardAccountKey :: k 'AccountK XPub
, nextKeyIx :: !(Index 'Soft 'AddressK)
, pointer :: !(Maybe PointerUTxO)
, isKey0Reg :: Bool
} deriving (Generic)

accountAtIx
Expand Down Expand Up @@ -149,12 +150,13 @@ data PointerUTxO = PointerUTxO TxIn Coin (Index 'Soft 'AddressK)
initialDelegationState
:: k 'AccountK XPub
-> DelegationState k
initialDelegationState accK = DelegationState accK minBound Nothing
initialDelegationState accK = DelegationState accK minBound Nothing False

unsafeDeserializeDelegationState
:: k 'AccountK XPub
-> Index 'Soft 'AddressK
-> Maybe PointerUTxO
-> Bool
-> DelegationState k
unsafeDeserializeDelegationState = DelegationState

Expand Down Expand Up @@ -261,15 +263,30 @@ applyTx (Tx cs _ins outs) s0 =
-> DelegationState k
modifyIx f s = s { nextKeyIx = f (nextKeyIx s) }

applyCert c s = flip modifyIx s $ case c of
applyCert c s = tweak $ flip modifyIx s $ case c of
RegisterKey _ -> id
Delegate k
| k == nextKey s -> succ
| otherwise -> id
DeRegisterKey k
| Just k == lastActiveKey s && hitGap k -> pred . pred -- i.e. to 0
| Just k == lastActiveKey s -> pred
-- TODO: Reason explicitly about the safety of this @pred@.
| otherwise -> id
where
tweak s' = case c of
RegisterKey k
| k == acct 0 -> s' { isKey0Reg = True }
| otherwise -> s'
DeRegisterKey k
| k == acct 0 -> s' { isKey0Reg = False }
| otherwise -> s'
_ -> s'

acct = toRewardAccount . keyAtIx s . toEnum

hitGap k = k == acct 1 && not (isKey0Reg s)


--------------------------------------------------------------------------------
-- Operations
Expand Down
Expand Up @@ -138,7 +138,11 @@ spec = do
$ property $ \cmds -> do
let ((s, _ledger), txs) = applyCmds (filter (not . isAdversarial) cmds)
let ((s', _ledger), _txs) = applyCmds $ cmdsFromChain txs
s' === s
-- Hack to allow isKey0Reg to differ. @cmdsFromChain@ won't be able
-- to generate @CmdOldWalletToggleFirstKey@.
-- This could be fixed. We can also question whether this test
-- actually is useful...?
s' === s { isKey0Reg = isKey0Reg s' }
it "(apply (cmds <> CmdSetPortfolioOf 0) s0) === s0"
$ property $ \cmds -> do
let ((s, _ledger), _chain) = applyCmds (cmds ++ [CmdSetPortfolioOf 0])
Expand Down

0 comments on commit 5a9701c

Please sign in to comment.