Skip to content

Commit

Permalink
Remove needless use of lenses.
Browse files Browse the repository at this point in the history
  • Loading branch information
abizjak committed Dec 24, 2021
1 parent 6e76821 commit bb7d9f9
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 30 deletions.
30 changes: 13 additions & 17 deletions concordium-consensus/src/Concordium/GlobalState/Instance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import Data.Aeson
import Data.Serialize
import qualified Data.Set as Set
import qualified Concordium.Crypto.SHA256 as H
import Lens.Micro.Platform (makeLenses, (^.))
import Concordium.Types
import Concordium.Types.HashableTo
import qualified Concordium.Wasm as Wasm
import qualified Concordium.GlobalState.Wasm as GSWasm
import Data.Word
import Lens.Micro (to, Lens')

-- |The fixed parameters associated with a smart contract instance
data InstanceParameters v = InstanceParameters {
Expand Down Expand Up @@ -57,32 +55,30 @@ data InstanceV v = InstanceV {
_instanceVHash :: H.Hash
}

makeLenses 'InstanceV

class HasInstanceFields a where
instanceAmount :: Lens' a Amount
instanceModel :: Lens' a Wasm.ContractState
instanceHash :: Lens' a H.Hash
instanceAmount :: a -> Amount
instanceModel :: a -> Wasm.ContractState
instanceHash :: a -> H.Hash

instance HasInstanceFields (InstanceV v) where
{-# INLINE instanceAmount #-}
instanceAmount = instanceVAmount
instanceAmount = _instanceVAmount
{-# INLINE instanceModel #-}
instanceModel = instanceVModel
instanceModel = _instanceVModel
{-# INLINE instanceHash #-}
instanceHash = instanceVHash
instanceHash = _instanceVHash

instance HasInstanceFields Instance where
instanceAmount f (InstanceV0 i) = InstanceV0 <$> instanceAmount f i
instanceAmount f (InstanceV1 i) = InstanceV1 <$> instanceAmount f i
instanceModel f (InstanceV0 i) = InstanceV0 <$> instanceModel f i
instanceModel f (InstanceV1 i) = InstanceV1 <$> instanceModel f i
instanceHash f (InstanceV0 i) = InstanceV0 <$> instanceHash f i
instanceHash f (InstanceV1 i) = InstanceV1 <$> instanceHash f i
instanceAmount (InstanceV0 i) = instanceAmount i
instanceAmount (InstanceV1 i) = instanceAmount i
instanceModel (InstanceV0 i) = instanceModel i
instanceModel (InstanceV1 i) = instanceModel i
instanceHash (InstanceV0 i) = instanceHash i
instanceHash (InstanceV1 i) = instanceHash i


instance HasInstanceParameters (InstanceV v) where
instanceAddress = (^. instanceVParameters . to instanceAddress)
instanceAddress = instanceAddress . _instanceVParameters

instance HasInstanceParameters Instance where
instanceAddress (InstanceV0 i) = instanceAddress i
Expand Down
6 changes: 3 additions & 3 deletions concordium-consensus/src/Concordium/GlobalState/Paired.hs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ instance (Monad m, C.HasGlobalStateContext (PairGSContext lc rc) r, BlockStateQu
getContractInstance (ls, rs) caddr = do
c1 <- coerceBSML (getContractInstance ls caddr)
c2 <- coerceBSMR (getContractInstance rs caddr)
assert (((==) `on` fmap (^. instanceHash)) c1 c2) $ return c1
assert (((==) `on` fmap instanceHash) c1 c2) $ return c1
getModuleList (ls, rs) = do
m1 <- coerceBSML (getModuleList ls)
m2 <- coerceBSMR (getModuleList rs)
Expand All @@ -226,7 +226,7 @@ instance (Monad m, C.HasGlobalStateContext (PairGSContext lc rc) r, BlockStateQu
getContractInstanceList (ls, rs) = do
a1 <- coerceBSML (getContractInstanceList ls)
a2 <- coerceBSMR (getContractInstanceList rs)
assert (((==) `on` fmap (^. instanceHash)) a1 a2) $ return a1
assert (((==) `on` fmap instanceHash) a1 a2) $ return a1
getSeedState (ls, rs) = do
ss1 <- coerceBSML (getSeedState ls)
ss2 <- coerceBSMR (getSeedState rs)
Expand Down Expand Up @@ -378,7 +378,7 @@ instance (MonadLogger m, C.HasGlobalStateContext (PairGSContext lc rc) r, BlockS
bsoGetInstance (bs1, bs2) iref = do
r1 <- coerceBSML $ bsoGetInstance bs1 iref
r2 <- coerceBSMR $ bsoGetInstance bs2 iref
assert (((==) `on` fmap (^. instanceHash)) r1 r2) $ return r1
assert (((==) `on` fmap instanceHash) r1 r2) $ return r1
bsoAddressWouldClash (bs1, bs2) addr = do
r1 <- coerceBSML $ bsoAddressWouldClash bs1 addr
r2 <- coerceBSMR $ bsoAddressWouldClash bs2 addr
Expand Down
6 changes: 3 additions & 3 deletions concordium-consensus/src/Concordium/Scheduler/Environment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ addContractAmountToCS istance amnt cs =
cs & instanceUpdates . at addr %~ \case Just (d, v) -> Just (d + amnt, v)
Nothing -> Just (amnt, model)
where addr = instanceAddress istance
model = istance ^. instanceModel
model = instanceModel istance

-- |Add the given contract address to the set of initialized contract instances.
-- As the changes on the blockstate are already performed in the handler for this operation,
Expand Down Expand Up @@ -813,7 +813,7 @@ instance SchedulerMonad pv m => TransactionMonad pv (LocalT r m) where
getCurrentContractInstanceState istance = do
newStates <- use (changeSet . instanceUpdates)
case newStates ^. at (instanceAddress istance) of
Nothing -> return (istance ^. instanceModel)
Nothing -> return (instanceModel istance)
Just (_, s) -> return s

{-# INLINE getStateAccount #-}
Expand Down Expand Up @@ -859,7 +859,7 @@ instance SchedulerMonad pv m => TransactionMonad pv (LocalT r m) where

{-# INLINE getCurrentContractAmount #-}
getCurrentContractAmount inst = do
let amnt = inst ^. instanceAmount
let amnt = instanceAmount inst
let addr = instanceAddress inst
use (changeSet . instanceUpdates . at addr) >>= \case
Just (delta, _) -> return $! applyAmountDelta delta amnt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ instance Arbitrary InstanceData where
return $ InstanceData model amount

instanceData :: Instance -> InstanceData
instanceData inst = InstanceData (inst ^. instanceModel) (inst ^. instanceAmount)
instanceData inst = InstanceData (instanceModel inst) (instanceAmount inst)

data Model = Model {
-- Data of instances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ testCases =
fibSpec n bs = specify "Contract state" $
case getInstance (Types.ContractAddress 0 0) (bs ^. blockInstances) of
Nothing -> assertFailure "Instnace at <0,0> does not exist."
Just istance -> assertEqual "State contains the n-th Fibonacci number." (fibNBytes n) (istance ^. instanceModel)
Just istance -> assertEqual "State contains the n-th Fibonacci number." (fibNBytes n) (instanceModel istance)

fib n = let go = 1:1:zipWith (+) go (tail go)
in go !! n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ checkInitResult proxy (suc, fails, instances) = do
assertEqual "There should be no failed transactions." [] fails
assertEqual "There should be no rejected transactions." [] reject
assertEqual "There should be 1 instance." 1 (length instances)
let model = contractState . (^. instanceModel) . snd . head $ instances
let model = contractState . instanceModel . snd . head $ instances
assertEqual "Instance model is the sender address of the account which inialized it." model (encode (senderAccount proxy))
where
reject = filter (\case (_, Types.TxSuccess{}) -> False
Expand All @@ -99,3 +99,5 @@ tests =
testInit (Proxy @'P2) >>= checkInitResult (Proxy @'P2)
specify "Passing init context to contract P3" $
testInit (Proxy @'P3) >>= checkInitResult (Proxy @'P3)
specify "Passing init context to contract P4" $
testInit (Proxy @'P4) >>= checkInitResult (Proxy @'P4)
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ testCases =
counterSpec n bs = specify "Contract state" $
case getInstance (Types.ContractAddress 0 0) (bs ^. blockInstances) of
Nothing -> assertFailure "Instance at <0,0> does not exist."
Just istance -> assertEqual ("State contains " ++ show n ++ ".") (ContractState (runPut (putWord64le n))) (istance ^. instanceModel)
Just istance -> assertEqual ("State contains " ++ show n ++ ".") (ContractState (runPut (putWord64le n))) (instanceModel istance)

tests :: Spec
tests = describe "V1: Counter counts." $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ testCases =
counterSpec n bs = specify "Contract state" $
case getInstance (Types.ContractAddress 0 0) (bs ^. blockInstances) of
Nothing -> assertFailure "Instance at <0,0> does not exist."
Just istance -> assertEqual ("State contains " ++ show n ++ ".") (ContractState (runPut (putWord64le n))) (istance ^. instanceModel)
Just istance -> assertEqual ("State contains " ++ show n ++ ".") (ContractState (runPut (putWord64le n))) (instanceModel istance)

tests :: Spec
tests = describe "V1: Counter with cross-messaging." $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ testCases =
case getInstance (Types.ContractAddress 0 0) (bs ^. blockInstances) of
Nothing -> assertFailure "Instance at <0,0> does not exist."
Just istance -> do
assertEqual ("State contains.") (ContractState "") (istance ^. instanceModel)
assertEqual ("Contract has 0 CCD.") (Types.Amount 0) (istance ^. instanceAmount)
assertEqual ("State contains.") (ContractState "") (instanceModel istance)
assertEqual ("Contract has 0 CCD.") (Types.Amount 0) (instanceAmount istance)

tests :: Spec
tests = describe "V1: Transfer from contract to account." $
Expand Down

0 comments on commit bb7d9f9

Please sign in to comment.