module EVM.StorageLayout where

-- Figures out the layout of storage slots for Solidity contracts.

import EVM.Dapp (DappInfo, dappAstSrcMap, dappAstIdMap)
import EVM.Solidity (SolcContract, creationSrcmap, SlotType(..))
import EVM.ABI (AbiType (..), parseTypeName)

import Data.Aeson (Value (..))
import Data.Aeson.Lens

import Control.Lens

import Data.Text (Text, unpack, pack, words)

import Data.Foldable (toList)
import Data.Maybe (fromMaybe, isJust)
import qualified Data.List.NonEmpty as NonEmpty

import qualified Data.Sequence as Seq

import Prelude hiding (words)

-- A contract has all the slots of its inherited contracts.
--
-- The slot order is determined by the inheritance linearization order,
-- so we first have to calculate that.
--
-- This information is available in the abstract syntax tree.

findContractDefinition :: DappInfo -> SolcContract -> Maybe Value
findContractDefinition :: DappInfo -> SolcContract -> Maybe Value
findContractDefinition DappInfo
dapp SolcContract
solc =
  -- The first source mapping in the contract's creation code
  -- corresponds to the source field of the contract definition.
  case forall a. Seq a -> ViewL a
Seq.viewl (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' SolcContract (Seq SrcMap)
creationSrcmap SolcContract
solc) of
    SrcMap
firstSrcMap Seq.:< Seq SrcMap
_ ->
      (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' DappInfo (SrcMap -> Maybe Value)
dappAstSrcMap DappInfo
dapp) SrcMap
firstSrcMap
    ViewL SrcMap
_ ->
      forall a. Maybe a
Nothing

storageLayout :: DappInfo -> SolcContract -> [Text]
storageLayout :: DappInfo -> SolcContract -> [Text]
storageLayout DappInfo
dapp SolcContract
solc =
  let
    root :: Value
    root :: Value
root =
      forall a. a -> Maybe a -> a
fromMaybe Value
Null
        (DappInfo -> SolcContract -> Maybe Value
findContractDefinition DappInfo
dapp SolcContract
solc)
  in
    case forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview ( forall t. AsValue t => Key -> Traversal' t Value
key Key
"attributes"
                 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Key -> Traversal' t Value
key Key
"linearizedBaseContracts"
                 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t (Vector Value)
_Array
                 ) Value
root of
      Maybe (Vector Value)
Nothing ->
        []
      Just ((forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList) -> [Value]
linearizedBaseContracts) ->
        forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [Value]
linearizedBaseContracts
          (\case
             Number Scientific
i -> forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"malformed AST JSON") forall a b. (a -> b) -> a -> b
$
               Value -> Maybe [Text]
storageVariablesForContract forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
                 forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (Lens' DappInfo (Map Int Value)
dappAstIdMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix (forall a b. (RealFrac a, Integral b) => a -> b
floor Scientific
i)) DappInfo
dapp
             Value
_ ->
               forall a. HasCallStack => [Char] -> a
error [Char]
"malformed AST JSON")

storageVariablesForContract :: Value -> Maybe [Text]
storageVariablesForContract :: Value -> Maybe [Text]
storageVariablesForContract Value
node = do
  Text
name <- forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index Value
"attributes" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Key -> Traversal' t Value
key Key
"name" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
node
  [Value]
vars <-
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
      (forall a. (a -> Bool) -> [a] -> [a]
filter Value -> Bool
isStorageVariableDeclaration forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList)
      (forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index Value
"children" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t (Vector Value)
_Array) Value
node)

  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> [a] -> [b]
map [Value]
vars forall a b. (a -> b) -> a -> b
$
    \Value
x ->
      case forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"attributes" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Key -> Traversal' t Value
key Key
"name" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
x of
        Just Text
variableName ->
          forall a. Monoid a => [a] -> a
mconcat
            [ Text
variableName
            , Text
" (", Text
name, Text
")"
            , Text
"\n", Text
"  Type: "
            , [Char] -> Text
pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show (Value -> SlotType
slotTypeForDeclaration Value
x)
            ]
        Maybe Text
Nothing ->
          forall a. HasCallStack => [Char] -> a
error [Char]
"malformed variable declaration"

nodeIs :: Text -> Value -> Bool
nodeIs :: Text -> Value -> Bool
nodeIs Text
t Value
x = Bool
isSourceNode Bool -> Bool -> Bool
&& Bool
hasRightName
  where
    isSourceNode :: Bool
isSourceNode =
      forall a. Maybe a -> Bool
isJust (forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"src") Value
x)
    hasRightName :: Bool
hasRightName =
      forall a. a -> Maybe a
Just Text
t forall a. Eq a => a -> a -> Bool
== forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"name" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
x

isStorageVariableDeclaration :: Value -> Bool
isStorageVariableDeclaration :: Value -> Bool
isStorageVariableDeclaration Value
x =
  Text -> Value -> Bool
nodeIs Text
"VariableDeclaration" Value
x
    Bool -> Bool -> Bool
&& forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"attributes" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Key -> Traversal' t Value
key Key
"constant" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Bool
_Bool) Value
x forall a. Eq a => a -> a -> Bool
/= forall a. a -> Maybe a
Just Bool
True

slotTypeForDeclaration :: Value -> SlotType
slotTypeForDeclaration :: Value -> SlotType
slotTypeForDeclaration Value
node =
  case forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"children" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t (Vector Value)
_Array) Value
node of
    Just (Value
x:[Value]
_) ->
      Value -> SlotType
grokDeclarationType Value
x
    Maybe [Value]
_ ->
      forall a. HasCallStack => [Char] -> a
error [Char]
"malformed AST"

grokDeclarationType :: Value -> SlotType
grokDeclarationType :: Value -> SlotType
grokDeclarationType Value
x =
  case forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"name" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
x of
    Just Text
"Mapping" ->
      case forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"children" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t (Vector Value)
_Array) Value
x of
        Just (forall (t :: * -> *) a. Foldable t => t a -> [a]
toList -> [Value]
xs) ->
          [Value] -> SlotType
grokMappingType [Value]
xs
        Maybe (Vector Value)
_ ->
          forall a. HasCallStack => [Char] -> a
error [Char]
"malformed AST"
    Just Text
_ ->
      AbiType -> SlotType
StorageValue (Value -> AbiType
grokValueType Value
x)
    Maybe Text
_ ->
      forall a. HasCallStack => [Char] -> a
error ([Char]
"malformed AST " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Value
x)

grokMappingType :: [Value] -> SlotType
grokMappingType :: [Value] -> SlotType
grokMappingType [Value
s, Value
t] =
  case (Value -> SlotType
grokDeclarationType Value
s, Value -> SlotType
grokDeclarationType Value
t) of
    (StorageValue AbiType
s', StorageMapping NonEmpty AbiType
t' AbiType
x) ->
      NonEmpty AbiType -> AbiType -> SlotType
StorageMapping (forall a. a -> NonEmpty a -> NonEmpty a
NonEmpty.cons AbiType
s' NonEmpty AbiType
t') AbiType
x
    (StorageValue AbiType
s', StorageValue AbiType
t') ->
      NonEmpty AbiType -> AbiType -> SlotType
StorageMapping (forall (f :: * -> *) a. Applicative f => a -> f a
pure AbiType
s') AbiType
t'
    (StorageMapping NonEmpty AbiType
_ AbiType
_, SlotType
_) ->
      forall a. HasCallStack => [Char] -> a
error [Char]
"unexpected mapping as mapping key"
grokMappingType [Value]
_ =
  forall a. HasCallStack => [Char] -> a
error [Char]
"unexpected AST child count for mapping"

grokValueType :: Value -> AbiType
grokValueType :: Value -> AbiType
grokValueType Value
x =
  case ( forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"name" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
x
       , forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"children" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t (Vector Value)
_Array) Value
x
       , forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"attributes" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Key -> Traversal' t Value
key Key
"type" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
x
       ) of
    (Just Text
"ElementaryTypeName", Maybe (Vector Value)
_, Just Text
typeName) ->
      forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error ([Char]
"ungrokked value type: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Text
typeName))
        (Vector AbiType -> Text -> Maybe AbiType
parseTypeName forall a. Monoid a => a
mempty (forall a. [a] -> a
head (Text -> [Text]
words Text
typeName)))
    (Just Text
"UserDefinedTypeName", Maybe (Vector Value)
_, Maybe Text
_) ->
      AbiType
AbiAddressType
    (Just Text
"ArrayTypeName", forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t a -> [a]
toList -> Just [Value
t], Maybe Text
_)->
      AbiType -> AbiType
AbiArrayDynamicType (Value -> AbiType
grokValueType Value
t)
    (Just Text
"ArrayTypeName", forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t a -> [a]
toList -> Just [Value
t, Value
n], Maybe Text
_)->
      case ( forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"name" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
n
           , forall s (m :: * -> *) a.
MonadReader s m =>
Getting (First a) s a -> m (Maybe a)
preview (forall t. AsValue t => Key -> Traversal' t Value
key Key
"attributes" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Key -> Traversal' t Value
key Key
"value" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. AsValue t => Prism' t Text
_String) Value
n
           ) of
        (Just Text
"Literal", Just ((forall a. Read a => [Char] -> a
read forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
unpack) -> Int
i)) ->
          Int -> AbiType -> AbiType
AbiArrayType Int
i (Value -> AbiType
grokValueType Value
t)
        (Maybe Text, Maybe Text)
_ ->
          forall a. HasCallStack => [Char] -> a
error [Char]
"malformed AST"
    (Maybe Text, Maybe (Vector Value), Maybe Text)
_ ->
      forall a. HasCallStack => [Char] -> a
error ([Char]
"unknown value type " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Value
x)