Skip to content

Add ScriptContext test #6959

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

Merged
merged 31 commits into from
Mar 25, 2025
Merged

Add ScriptContext test #6959

merged 31 commits into from
Mar 25, 2025

Conversation

ana-pantilie
Copy link
Contributor

@ana-pantilie ana-pantilie commented Mar 17, 2025

Fixes https://github.com/IntersectMBO/plutus-private/issues/1479

The AsData version is actually slightly better (in script size) than the manually optimised version! I am not entirely sure why, since I tried multiple versions of the manually optimised one, and this was the best one.

@ana-pantilie ana-pantilie marked this pull request as ready for review March 24, 2025 13:56
@ana-pantilie ana-pantilie requested review from zliu41 and a team March 24, 2025 13:56
@@ -139,3 +192,34 @@ mkScriptContextEqualityOverheadCode sc =
in $$(PlutusTx.compile [|| scriptContextEqualityOverhead ||])
`PlutusTx.unsafeApplyCode` PlutusTx.liftCodeDef sc
`PlutusTx.unsafeApplyCode` PlutusTx.liftCodeDef d

forwardWithStakeTrick :: BuiltinData -> BuiltinData -> ()
Copy link
Member

Choose a reason for hiding this comment

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

Add some comments - it's hard to tell from the name - forwardWithStakeTrick - what it does.

error {Unit})
{all dead. dead})
{all dead. dead})
(Constr 0 [Constr 0 [B #736f6d6543726564656e7469616c]])
Copy link
Member

Choose a reason for hiding this comment

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

Don't include the script context in the golden files - it serves no purpose.

|| PlutusTx.equalsData obsScriptCred wdrlAtOne
then ()
else
if Map.member' obsScriptCred rest
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need member' here - you can use PlutusTx.Data.List.elem (or something very similar).

Even if you do need to expose member', it should be exposed from the List module (since it has nothing to do with Map), and it should have Haddock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The rest here is a map, not a list. Converting it to a list would be costly. I have, however, used member instead of member', and removed the member' and the insert' functions from the export list. I was not sure if the compiler could figure out that Map k a is representationally equivalent to BuiltinList (BuiltinPair ...), but it can so there is no overhead.

This reverts commit 3031e19.
@ana-pantilie ana-pantilie requested a review from zliu41 March 25, 2025 14:48
@ana-pantilie ana-pantilie added the No Changelog Required Add this to skip the Changelog Check label Mar 25, 2025
|| PlutusTx.equalsData obsScriptCred wdrlAtOne
then ()
else
if Map.member obsScriptCred (Map.unsafeFromBuiltinList rest)
Copy link
Member

Choose a reason for hiding this comment

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

This is still awkward. Do we need to convert Map into a BuiltinList? Can we convert it into a PlutusTx.Data.List, without losing efficiency? If BuiltinList is indeed more efficient, then it suggests that the Plinth library should have some utilities for working with BuiltinList - such as elem, find, lookup and so on.

Copy link
Contributor Author

@ana-pantilie ana-pantilie Mar 25, 2025

Choose a reason for hiding this comment

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

Can we convert it into a PlutusTx.Data.List, without losing efficiency?

No. Map is BuiltinList (BuiltinPair BuiltinData BuiltinData) and List is BuiltinList BuiltinData. We would need to traverse the BuiltinList and transform each BuiltinPair into a BuiltinData.

If BuiltinList is indeed more efficient, then it suggests that the Plinth library should have some utilities for working with BuiltinList - such as elem, find, lookup and so on.

Would we use BuiltinList in the ScriptContext directly?

@zliu41

Copy link
Member

Choose a reason for hiding this comment

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

No, but once you convert Map to BuiltinList, having to convert it back to a Map in order to call member or whatever, is awkward. Not to mention after some operations on the BuiltinList, it may no longer be a Map (and indeed, converting it back requires an unsafe operation).

It would be much nicer if there's a lookup operation and similar operations on BuiltinList, so once you have the list you just work with the list.

Copy link
Member

Choose a reason for hiding this comment

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

Could you open an issue for adding BuiltinList operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're right. Opened an issue: #6977

@ana-pantilie ana-pantilie requested a review from zliu41 March 25, 2025 18:40
@ana-pantilie ana-pantilie merged commit 5b436da into master Mar 25, 2025
8 of 9 checks passed
@ana-pantilie ana-pantilie deleted the ana/add-datasc-test branch March 25, 2025 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
No Changelog Required Add this to skip the Changelog Check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants