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

Fix onchain logic #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Riley-Kilgore
Copy link
Member

This is a direct PR from Brians fork to our own, I have yet to read through this completely and I will be leaving my own comments below. I have requested review from Brian, Siegfried, and Dz.

Thanks all.

Copy link
Member Author

@Riley-Kilgore Riley-Kilgore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that all of this looks good, I don't see an issue with any of these changes personally.

Ready for merge whenever someone wants to take that action. I'll wait for a second review before taking that action myself.

@bjing
Copy link

bjing commented Feb 19, 2022

Thanks Riley for creating this PR. I've been dealing with a really sick child and have not done anything since I chatted to you last time on Discord.

Here's a summary of the PR:

  • Fix logic where it bails (though traceError) due to invalid input.
    • Things got fixed are in the original function getOutput and getOutputPDatum where the functions didn't return the stated signatureBool and TxOut respectively,
    • If invalid input's received, the validator should always validate to False instead of throwing an error. Also trace calls aren't supposed to be used in production environment because they break referential transparency. They are only for testing purpose.
  • Remove unused variables
  • Fix shadowed variables. These are mainly caused by:
    • unprefixed record field names
    • library imports, e.g. before, after, outputs etc.
  • Remove unused imports in Bounty.hs.

getOutput txOuts asset =
case [o | o <- txOuts, containsClass o asset] of
[x] -> x
_ -> traceError "Fail here."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part where it doesn't return a TxOut, instead it calls traceError. If we don't always return a result, we should be returning a Maybe (or something similar) instead of bailing with traceError.

getOutputPDatum :: TxInfo -> [TxOut] -> TxOut
getOutputPDatum info txOuts = case [o | o <- txOuts, containsPot info o] of
[x] -> x
_ -> traceError "Fail here."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part where it doesn't return a TxOut, instead it calls traceError. If we don't always return a result, we need to return a maybe instead of bailing with traceError.

_ -> traceError "Fail here."
{-# INLINABLE findOutputForClass #-}
findOutputForClass :: AssetClass -> [TxOut] -> Maybe TxOut
findOutputForClass asset = find $ \o -> containsClass o asset
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming convention for getting a value (if possible) from a collection is find so I named it this way, it also corresponds with the find call in the implementation.

in case d of
Just PotDatum -> True
_ -> False
case findBountyDatum info o of
Copy link

@bjing bjing Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Haskell is expression based, you don't need to assign value to a variable and then pattern match on that variable, you can directly pattern match on the value/expression.


policy :: AssetClass -> Scripts.MintingPolicy
policy asset =
mkMintingPolicyScript $
$$(PlutusTx.compile [||Scripts.wrapMintingPolicy . mkPolicy||])
`PlutusTx.applyCode` PlutusTx.liftCode asset
`PlutusTx.applyCode`
PlutusTx.liftCode asset
Copy link

@bjing bjing Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this formatting 🤷‍♂️ . However, setting it like this will stop this formatting from getting into every PR as noise. I've asked about this particular issue on IOG's tech channel, this seems to be stylish-haskell's fault, well, I guess we need to live with it.

-- validateUseOfPot bounty mPotTxOut mpot mcollection = case mpot of
validateUseOfPot bounty (Just potTxOut) (Just _) (Just (CollectionDatum c)) =
solidCollection bounty c && correctCollection potTxOut c
validateUseOfPot _ _ _ _ = False
Copy link

@bjing bjing Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this part so that it's clearer in that line 157 lists the case for which we want to run the validator logic. For all other cases, we evaluate to False straight away.

When we write it in nested style like the removed code above, it's can be less obvious what cases (branches) have been covered and what have not.

bVoters :: ![PubKeyHash],
bRequiredVotes :: !Integer,
bCollectionMakerClass :: !AssetClass,
bCollectionToken :: !AssetClass
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's common practice to prefix record field names with data type initials. In this case one letter prefix is enough, however usually with several record data structures, two-letter prefixes are more common.

This has been one of the biggest issues with Record in Haskell. I think this particular variable scoping issue has been fixed in GHC 9.x, I've not tried it myself though so can't comment much on that.

@bjing
Copy link

bjing commented Feb 19, 2022

It doesn't make much sense for me to review my own code, but I've commented on a few changes hoping to make the intentions clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants