Skip to content

Commit

Permalink
wip: some plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed May 4, 2021
1 parent 75b7088 commit 683186d
Showing 1 changed file with 21 additions and 15 deletions.
Expand Up @@ -22,6 +22,10 @@ import Cardano.Wallet.Primitive.AddressDerivation
, ToRewardAccount (..)
)
import Cardano.Wallet.Primitive.AddressDiscovery.Delegation
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Tx
( TxIn, TxOut )
import Control.Monad.Trans.Class
( lift )
import Control.Monad.Trans.State.Strict
Expand Down Expand Up @@ -57,11 +61,10 @@ import qualified Data.Set as Set
spec :: Spec
spec = do
let acc = toRewardAccount @StakeKey' . toEnum
let applyTx' = applyTx undefined [] []
let regAndDeleg i = applyTx'
let regAndDeleg i = applyTx' $ Tx
[ RegisterKey $ acc i
, Delegate $ acc i
]
] [] []

let s0 = initialDelegationState accK
describe "initialDelegationState" $ do
Expand Down Expand Up @@ -182,11 +185,12 @@ accK :: StakeKey' 'AccountK XPub
accK = StakeKey' 0

apply :: [Tx] -> DelegationState StakeKey' -> DelegationState StakeKey'
apply txs s = foldl (flip (applyTx txid ins outs)) s (map unTx txs)
apply txs s = foldl (flip applyTx') s txs

applyTx' :: Tx -> DelegationState StakeKey' -> DelegationState StakeKey'
applyTx' (Tx cs is os) = applyTx txid is os cs
where
txid = error "todo: txid"
ins = [] -- TODO
outs = [] -- TODO

applyLedger
:: [Tx]
Expand All @@ -197,7 +201,8 @@ applyLedger txs = execStateT (mapM_ applyTxLedger txs)
applyTxLedger
:: Tx
-> StateT Ledger (Either String) ()
applyTxLedger (Tx certs) = mapM_ applyCert' certs
applyTxLedger tx = mapM_ applyCert' (certs tx)
-- TODO: mock ledger also needs to check UTxO rules

applyCert' c = do
l <- get
Expand Down Expand Up @@ -240,7 +245,11 @@ instance SoftDerivation StakeKey' where
-- Mock chain of delegation certificates
--

newtype Tx = Tx { unTx :: [Cert] }
data Tx = Tx
{ certs :: [Cert]
, ins :: [(TxIn, Coin)]
, outs :: [TxOut]
}
deriving (Eq, Generic)
deriving Show via (Quiet Tx)

Expand All @@ -251,10 +260,6 @@ instance Arbitrary Cert where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary Tx where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary (StakeKey' depth key) where
arbitrary = StakeKey' <$> arbitrary

Expand Down Expand Up @@ -291,12 +296,13 @@ cmdsFromChain :: [Tx] -> [Cmd]
cmdsFromChain =
map (CmdSetPortfolioOf . length . activeKeys)
. scanl (flip applyTx') (initialDelegationState accK)
where
applyTx' = applyTx undefined [] [] . unTx

chainFromCmds :: [Cmd] -> [Tx]
chainFromCmds goals = map Tx . filter (not . null) $ reverse $ go 0 [] [] goals
chainFromCmds goals = map mkTx . filter (not . null) $ reverse $ go 0 [] [] goals
where
-- TODO: We need to add inputs and outputs here.
-- We need to use the @DelegationState@ itself to do so.
mkTx cs = Tx cs [] []
-- NOTE: For convenience we treat ix=-1 as no keys, as ix=0 means the first
-- key (at index 0) is registered and delegating.
go i chain tx (cmd:rest) = case cmd of
Expand Down

0 comments on commit 683186d

Please sign in to comment.