@@ -19,6 +19,8 @@ module EVM.Fetch
1919 , FetchCache (.. )
2020 , addFetchCache
2121 , saveCache
22+ , RPCContract (.. )
23+ , makeContractFromRPC
2224 ) where
2325
2426import Prelude hiding (Foldable (.. ))
@@ -41,7 +43,6 @@ import Data.Aeson.Encode.Pretty (encodePretty)
4143import qualified Data.ByteString.Lazy as BSL
4244import Data.Bifunctor (first )
4345import Control.Exception (try , SomeException )
44- import Control.Monad (join )
4546
4647import Control.Monad.Trans.Maybe
4748import Control.Applicative (Alternative (.. ))
@@ -75,36 +76,11 @@ data Session = Session
7576 }
7677
7778data FetchCache = FetchCache
78- { contractCache :: Map. Map Addr Contract
79+ { contractCache :: Map. Map Addr RPCContract
7980 , slotCache :: Map. Map (Addr , W256 ) W256
8081 , blockCache :: Map. Map W256 Block
8182 } deriving (Generic )
8283
83- instance ToJSON Contract where
84- toJSON c = object
85- [ " code" .= case c. code of
86- RuntimeCode (ConcreteRuntimeCode bs) -> Just (ByteStringS bs)
87- _ -> Nothing
88- , " nonce" .= c. nonce
89- , " balance" .= case c. balance of
90- Lit w -> Just w
91- _ -> Nothing
92- , " external" .= c. external
93- ]
94-
95- instance FromJSON Contract where
96- parseJSON = withObject " Contract" $ \ v -> do
97- maybeCodeText <- v .:? " code"
98- let mcCode = hexText <$> join maybeCodeText
99- mcNonce <- v .: " nonce"
100- maybeBalance <- v .:? " balance"
101- let mcBalance = join maybeBalance
102- external <- v .: " external"
103- let code = maybe (RuntimeCode (ConcreteRuntimeCode " " )) (RuntimeCode . ConcreteRuntimeCode ) mcCode
104- pure $ initialContract code
105- & # nonce .~ mcNonce
106- & # balance .~ maybe (Lit 0 ) Lit mcBalance
107- & # external .~ external
10884
10985instance ToJSON FetchCache where
11086 toJSON (FetchCache cs ss bs) = object
@@ -150,11 +126,15 @@ data BlockNumber = Latest | BlockNumber W256
150126deriving instance Show (RpcQuery a )
151127
152128data RPCContract = RPCContract
153- { mcCode :: BS. ByteString
154- , mcNonce :: W64
155- , mcBalance :: W256
129+ { code :: ByteStringS
130+ , nonce :: W64
131+ , balance :: W256
156132 }
157- deriving (Eq , Show )
133+ deriving (Eq , Show , Generic )
134+
135+ instance ToJSON RPCContract
136+
137+ instance FromJSON RPCContract
158138
159139data RpcInfo = RpcInfo
160140 { blockNumURL :: Maybe (BlockNumber , Text ) -- ^ (block number, RPC url)
@@ -196,7 +176,7 @@ instance ToRPC BlockNumber where
196176readText :: Read a => Text -> a
197177readText = read . unpack
198178
199- addFetchCache :: Session -> Addr -> Contract -> IO ()
179+ addFetchCache :: Session -> Addr -> RPCContract -> IO ()
200180addFetchCache sess address ctrct = do
201181 cache <- readMVar sess. sharedCache
202182 liftIO $ modifyMVar_ sess. sharedCache $ \ c -> pure $ c { contractCache = (Map. insert address ctrct cache. contractCache) }
@@ -296,7 +276,7 @@ fetchWithSession url sess x = do
296276 r <- asValue =<< NetSession. post sess (unpack url) x
297277 pure (r ^? (lensVL responseBody) % key " result" )
298278
299- fetchContractWithSession :: Config -> Session -> BlockNumber -> Text -> Addr -> IO (Maybe Contract )
279+ fetchContractWithSession :: Config -> Session -> BlockNumber -> Text -> Addr -> IO (Maybe RPCContract )
300280fetchContractWithSession conf sess nPre url addr = do
301281 n <- getLatestBlockNum conf sess nPre url
302282 cache <- readMVar sess. sharedCache
@@ -312,7 +292,7 @@ fetchContractWithSession conf sess nPre url addr = do
312292 code <- MaybeT $ fetch (QueryCode addr)
313293 nonce <- MaybeT $ fetch (QueryNonce addr)
314294 balance <- MaybeT $ fetch (QueryBalance addr)
315- let contr = makeContractFromRPC ( RPCContract code nonce balance)
295+ let contr = RPCContract ( ByteStringS code) nonce balance
316296 liftIO $ modifyMVar_ sess. sharedCache $ \ c ->
317297 pure $ c { contractCache = Map. insert addr contr c. contractCache }
318298 pure contr
@@ -338,7 +318,7 @@ getLatestBlockNum conf sess n url =
338318 _ -> pure n
339319
340320makeContractFromRPC :: RPCContract -> Contract
341- makeContractFromRPC (RPCContract code nonce balance) =
321+ makeContractFromRPC (RPCContract ( ByteStringS code) nonce balance) =
342322 initialContract (RuntimeCode (ConcreteRuntimeCode code))
343323 & set # nonce (Just nonce)
344324 & set # balance (Lit balance)
@@ -490,13 +470,13 @@ oracle solvers preSess rpcInfo q = do
490470 case Map. lookup addr cache. contractCache of
491471 Just c -> do
492472 when (conf. debug) $ liftIO $ putStrLn $ " -> Using cached contract at " ++ show addr
493- pure $ continue c
473+ pure $ continue $ makeContractFromRPC c
494474 Nothing -> do
495475 when (conf. debug) $ liftIO $ putStrLn $ " Fetching contract at " ++ show addr
496476 when (addr == 0 && conf. verb > 0 ) $ liftIO $ putStrLn " Warning: fetching contract at address 0"
497477 contract <- case rpcInfo. blockNumURL of
498478 Nothing -> pure $ Just $ nothingContract base addr
499- Just (block, url) -> liftIO $ fetchContractWithSession conf sess block url addr
479+ Just (block, url) -> liftIO $ fmap ( fmap makeContractFromRPC) $ fetchContractWithSession conf sess block url addr
500480 case contract of
501481 Just x -> pure $ continue x
502482 Nothing -> internalError $ " oracle error: " ++ show q
0 commit comments