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

eleks/oracle: demonstrate how expensive || can be! #1

Open
wants to merge 1 commit into
base: eleks/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion eleks/oracle/src/Contracts/Oracle/OffChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ updateOracle oracle operatorPrivateKey params = do
, osmGameId = gameId
, osmGameStatus = gameStatus
}
let oracleData' = oracleData{ ovSignedMessage = Just $ signMessage oracleSignMessage operatorPrivateKey }
let oracleData' = oracleData{ ovSignedMessage = Nothing } -- This is not that hacky, the on-chain validation rule accepts it!
when (oracleData' /= oracleData) $ do
let requestTokenVal = assetClassValue (requestTokenClassFromOracle oracle) 1
collateralVal = Ada.toValue $ oCollateral oracle
Expand Down
13 changes: 10 additions & 3 deletions eleks/oracle/src/Contracts/Oracle/OnChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,18 @@ mkOracleValidator oracle oracleData r ctx =
>>= verifyOracleValueSigned (oOperatorKey oracle)
>>= (\(message, _) -> Just message)

isUpdateValid = (not isCurrentValueSigned) ||
(fromMaybe False $ validateGameStatusChanges <$>
(osmGameStatus <$> extractSigendMessage (ovSignedMessage oracleData)) <*>
-- if then else is way cheaper!
isUpdateValid = if not isCurrentValueSigned then True else
(fromMaybe False $ validateGameStatusChanges <$>
(osmGameStatus <$> extractSigendMessage (ovSignedMessage oracleData)) <*>
(osmGameStatus <$> extractSigendMessage outputSignedMessage))

-- || is surprisingly way more expensive!
-- isUpdateValid = (not isCurrentValueSigned) ||
-- (fromMaybe False $ validateGameStatusChanges <$>
-- (osmGameStatus <$> extractSigendMessage (ovSignedMessage oracleData)) <*>
-- (osmGameStatus <$> extractSigendMessage outputSignedMessage))

{-# INLINABLE verifyOracleValueSigned #-}
verifyOracleValueSigned :: PubKey -> SignedMessage OracleSignedMessage -> Maybe (OracleSignedMessage, TxConstraints Void Void)
verifyOracleValueSigned pubKey sm = case verifySignedMessageConstraints pubKey sm of
Expand Down
19 changes: 18 additions & 1 deletion eleks/oracle/src/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@ import qualified Spec.Oracle
import Test.Tasty
import Test.Tasty.Hedgehog (HedgehogTestLimit (..))

import Data.Default
import Ledger.Index
import Plutus.Trace

-- With `||`:
-- ( Sum {getSum = 9539}
-- , ExBudget {exBudgetCPU = ExCPU 1124168956, exBudgetMemory = ExMemory 3152700}
-- )

-- With `if then else`:
-- ( Sum {getSum = 3531}
-- , ExBudget {exBudgetCPU = ExCPU 698345767, exBudgetMemory = ExMemory 2069978}
-- )
main :: IO ()
main = defaultMain tests
main = print =<< writeScriptsTo
(ScriptsConfig "." (Scripts UnappliedValidators))
"updateOracleTrace"
Spec.Oracle.updateOracleTrace
def

-- | Number of successful tests for each hedgehog property.
-- The default is 100 but we use a smaller number here in order to speed up
Expand Down
1 change: 1 addition & 0 deletions eleks/oracle/src/test/Spec/Oracle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

module Spec.Oracle
( tests
, updateOracleTrace
) where

import Control.Lens
Expand Down
Binary file added eleks/oracle/updateOracleTrace-1-unapplied.flat
Binary file not shown.