Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New JSON instances for Tx with legacy parsing #895

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/Echidna/Types/Tx.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RecordWildCards #-}

module Echidna.Types.Tx where

import Prelude hiding (Word)

import Control.Applicative ((<|>))
import Control.Lens.TH (makePrisms, makeLenses)
import Data.Aeson (FromJSON, ToJSON, parseJSON, toJSON, object, withObject, (.=), (.:))
import Data.Aeson.TH (deriveJSON, defaultOptions)
import Data.Aeson.Types (Parser)
import Data.ByteString (ByteString)
import Data.Text (Text)
import Data.Word (Word64)
Expand Down Expand Up @@ -58,7 +62,41 @@ data Tx = Tx { _call :: TxCall -- | Call
, _delay :: (W256, W256) -- | (Time, # of blocks since last call)
} deriving (Eq, Ord, Show)
makeLenses ''Tx
$(deriveJSON defaultOptions ''Tx)

instance ToJSON Tx where
toJSON Tx{..} = object
[ "call" .= _call
, "src" .= _src
, "dst" .= _dst
, "gas" .= _gas'
, "gasprice" .= _gasprice'
, "value" .= _value
, "delay" .= _delay
]

instance FromJSON Tx where
-- For compatibility we try to parse the old corpus format. Will be removed
-- in the next major release.
parseJSON = withObject "Tx" $ \o -> legacyParseTx o <|> parseTx o
where
parseTx o =
Tx <$> o .: "call"
<*> o .: "src"
<*> o .: "dst"
<*> o .: "gas"
<*> o .: "gasprice"
<*> o .: "value"
<*> o .: "delay"
legacyParseTx o =
Tx <$> o .: "_call"
<*> o .: "_src"
<*> o .: "_dst"
-- We changed gas to Word64, keep compatible with already existing
-- corpuses that still store gas as a hex string
<*> (fromIntegral <$> (o .: "_gas'" :: Parser W256))
<*> o .: "_gasprice'"
<*> o .: "_value"
<*> o .: "_delay"

basicTx :: Text -- | Function name
-> [AbiValue] -- | Function args
Expand Down