-
Notifications
You must be signed in to change notification settings - Fork 484
Benchmark: Linear Vesting #7166
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
Open
Unisay
wants to merge
3
commits into
master
Choose a base branch
from
yura/benchmark-linear-vesting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Main (main) where | ||
|
||
import Data.Text qualified as Text | ||
import LinearVesting (validatorCodeFullyApplied) | ||
import PlutusTx.Test (displayEvalResult, evaluateCompiledCode) | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "" | ||
putStrLn $ | ||
Text.unpack $ | ||
displayEvalResult $ | ||
evaluateCompiledCode validatorCodeFullyApplied |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE MultiWayIf #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
{-# LANGUAGE Strict #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} | ||
{-# OPTIONS_GHC -fno-full-laziness #-} | ||
{-# OPTIONS_GHC -fno-ignore-interface-pragmas #-} | ||
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-} | ||
{-# OPTIONS_GHC -fno-spec-constr #-} | ||
{-# OPTIONS_GHC -fno-specialise #-} | ||
{-# OPTIONS_GHC -fno-strictness #-} | ||
{-# OPTIONS_GHC -fno-unbox-small-strict-fields #-} | ||
{-# OPTIONS_GHC -fno-unbox-strict-fields #-} | ||
{-# OPTIONS_GHC -fplugin PlutusTx.Plugin #-} | ||
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:conservative-optimisation #-} | ||
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:no-remove-trace #-} | ||
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:preserve-logging #-} | ||
|
||
module LinearVesting where | ||
|
||
import PlutusTx | ||
import PlutusTx.Prelude | ||
import Prelude qualified as Haskell | ||
|
||
import PlutusLedgerApi.Data.V3 | ||
import PlutusLedgerApi.V1.Data.Value (AssetClass, assetClass, assetClassValueOf) | ||
import PlutusLedgerApi.V3.Data.Contexts (txSignedBy) | ||
import PlutusTx.Data.AssocMap qualified as Map | ||
import PlutusTx.Data.List (List) | ||
import PlutusTx.Data.List qualified as List | ||
|
||
data VestingDatum = VestingDatum | ||
{ beneficiary :: Address | ||
, vestingAsset :: AssetClass | ||
, totalVestingQty :: Integer | ||
, vestingPeriodStart :: Integer | ||
, vestingPeriodEnd :: Integer | ||
, firstUnlockPossibleAfter :: Integer | ||
, totalInstallments :: Integer | ||
} | ||
deriving stock (Haskell.Show) | ||
|
||
$(makeLift ''VestingDatum) | ||
$(makeIsDataIndexed ''VestingDatum [('VestingDatum, 0)]) | ||
|
||
data VestingRedeemer = PartialUnlock | FullUnlock | ||
|
||
$(PlutusTx.makeLift ''VestingRedeemer) | ||
$( PlutusTx.makeIsDataIndexed | ||
''VestingRedeemer | ||
[('PartialUnlock, 0), ('FullUnlock, 1)] | ||
) | ||
|
||
countInputsAtScript :: ScriptHash -> List TxInInfo -> Integer | ||
countInputsAtScript scriptHash = go 0 | ||
where | ||
go :: Integer -> List TxInInfo -> Integer | ||
go n = List.caseList' n \txIn txIns -> | ||
case addressCredential (txOutAddress (txInInfoResolved txIn)) of | ||
ScriptCredential vh | vh == scriptHash -> go (n + 1) txIns | ||
_ -> go n txIns | ||
|
||
validateVestingPartialUnlock :: ScriptContext -> Bool | ||
validateVestingPartialUnlock ctx = | ||
let | ||
txInfo :: TxInfo = scriptContextTxInfo ctx | ||
SpendingScript ownRef (Just (Datum datum)) = scriptContextScriptInfo ctx | ||
vestingDatum :: VestingDatum = unsafeFromBuiltinData datum | ||
inputs = txInfoInputs txInfo | ||
|
||
Just ownVestingInput = List.find ((== ownRef) . txInInfoOutRef) inputs | ||
resolvedOut = txInInfoResolved ownVestingInput | ||
inputAddress = txOutAddress resolvedOut | ||
|
||
ScriptCredential scriptHash = addressCredential inputAddress | ||
Just ownVestingOutput = | ||
List.find ((== inputAddress) . txOutAddress) (txInfoOutputs txInfo) | ||
outputDatum = txOutDatum ownVestingOutput | ||
|
||
divCeil :: Integer -> Integer -> Integer | ||
divCeil x y = 1 + (x - 1) `divide` y | ||
|
||
asset :: AssetClass = | ||
vestingAsset vestingDatum | ||
oldRemainingQty :: Integer = | ||
assetClassValueOf (txOutValue resolvedOut) asset | ||
newRemainingQty :: Integer = | ||
assetClassValueOf (txOutValue ownVestingOutput) asset | ||
vestingPeriodLength :: Integer = | ||
vestingPeriodEnd vestingDatum - vestingPeriodStart vestingDatum | ||
currentTimeApproximation :: Integer = | ||
getPOSIXTime (getLowerInclusiveTimeRange (txInfoValidRange txInfo)) | ||
vestingTimeRemaining :: Integer = | ||
vestingPeriodEnd vestingDatum - currentTimeApproximation | ||
timeBetweenTwoInstallments :: Integer = | ||
vestingPeriodLength `divCeil` totalInstallments vestingDatum | ||
futureInstallments :: Integer = | ||
vestingTimeRemaining `divCeil` timeBetweenTwoInstallments | ||
expectedRemainingQty :: Integer = | ||
(futureInstallments * totalVestingQty vestingDatum) | ||
`divCeil` totalInstallments vestingDatum | ||
PubKeyCredential beneficiaryHash = | ||
addressCredential (beneficiary vestingDatum) | ||
in | ||
if | ||
| not (txSignedBy txInfo beneficiaryHash) -> | ||
traceError "Missing beneficiary signature" | ||
| firstUnlockPossibleAfter vestingDatum >= currentTimeApproximation -> | ||
traceError "Unlock not permitted until firstUnlockPossibleAfter time" | ||
| newRemainingQty <= 0 -> | ||
traceError "Zero remaining assets not allowed" | ||
| newRemainingQty > oldRemainingQty -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It doesn't really matter though |
||
traceError "Remaining asset exceed old asset" | ||
| expectedRemainingQty /= newRemainingQty -> | ||
traceError "Mismatched remaining asset" | ||
| txOutDatum resolvedOut /= outputDatum -> | ||
traceError "Datum Modification Prohibited" | ||
| countInputsAtScript scriptHash inputs /= 1 -> | ||
traceError "Double satisfaction" | ||
| otherwise -> | ||
True | ||
|
||
validateVestingFullUnlock :: ScriptContext -> Bool | ||
validateVestingFullUnlock ctx = | ||
let | ||
txInfo :: TxInfo = scriptContextTxInfo ctx | ||
currentTimeApproximation :: Integer = | ||
getPOSIXTime (getLowerInclusiveTimeRange (txInfoValidRange txInfo)) | ||
SpendingScript _ownRef (Just (Datum datum)) = scriptContextScriptInfo ctx | ||
vestingDatum :: VestingDatum = unsafeFromBuiltinData datum | ||
PubKeyCredential beneficiaryKey = addressCredential (beneficiary vestingDatum) | ||
in | ||
if | ||
| not (txSignedBy txInfo beneficiaryKey) -> | ||
traceError "Missing beneficiary signature" | ||
| vestingPeriodEnd vestingDatum >= currentTimeApproximation -> | ||
traceError "Unlock not permitted until vestingPeriodEnd time" | ||
| otherwise -> | ||
True | ||
|
||
getLowerInclusiveTimeRange :: POSIXTimeRange -> POSIXTime | ||
getLowerInclusiveTimeRange = \case | ||
Interval (LowerBound (Finite posixTime) inclusive) _upperBound -> | ||
if inclusive then posixTime else posixTime + 1 | ||
_ -> traceError "Time range not Finite" | ||
|
||
{-# INLINEABLE typedValidator #-} | ||
typedValidator :: ScriptContext -> Bool | ||
typedValidator context = | ||
trace "Validation completed" | ||
$ case redeemer of | ||
FullUnlock -> | ||
validateVestingFullUnlock $ trace "Full unlock requested" context | ||
PartialUnlock -> | ||
validateVestingPartialUnlock $ trace "Partial unlock requested" context | ||
where | ||
{-# INLINEABLE redeemer #-} | ||
redeemer :: VestingRedeemer | ||
redeemer = | ||
case fromBuiltinData (getRedeemer (scriptContextRedeemer context)) of | ||
Nothing -> traceError "Failed to parse Redeemer" | ||
Just r -> trace "Parsed Redeemer" r | ||
|
||
{-# INLINEABLE untypedValidator #-} | ||
untypedValidator :: BuiltinData -> BuiltinUnit | ||
untypedValidator scriptContextData = | ||
case trace "Parsing ScriptContext..." (fromBuiltinData scriptContextData) of | ||
Nothing -> traceError "Failed to parse ScriptContext" | ||
Just ctx -> check $ typedValidator $ trace "Parsed ScriptContext" ctx | ||
|
||
validatorCode :: CompiledCode (BuiltinData -> BuiltinUnit) | ||
validatorCode = $$(compile [||untypedValidator||]) | ||
|
||
validatorCodeFullyApplied :: CompiledCode BuiltinUnit | ||
validatorCodeFullyApplied = | ||
validatorCode `unsafeApplyCode` liftCodeDef (toBuiltinData testScriptContext) | ||
|
||
testScriptContext :: ScriptContext | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have this as an another file. |
||
testScriptContext = | ||
ScriptContext | ||
{ scriptContextTxInfo = txInfo | ||
, scriptContextRedeemer | ||
, scriptContextScriptInfo | ||
} | ||
where | ||
txInfo = | ||
TxInfo | ||
{ txInfoInputs = mempty | ||
, txInfoReferenceInputs = mempty | ||
, txInfoOutputs = mempty | ||
, txInfoTxCerts = mempty | ||
, txInfoRedeemers = Map.empty | ||
, txInfoVotes = Map.empty | ||
, txInfoProposalProcedures = mempty | ||
, txInfoCurrentTreasuryAmount = Nothing | ||
, txInfoTreasuryDonation = Nothing | ||
, txInfoFee = 0 | ||
, txInfoMint = emptyMintValue | ||
, txInfoWdrl = Map.empty | ||
, txInfoValidRange = | ||
Interval | ||
(LowerBound (Finite 110) True) | ||
(UpperBound (Finite 1100) True) | ||
, txInfoSignatories = List.singleton testBeneficiaryPKH | ||
, txInfoData = Map.empty | ||
, txInfoId = "058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145" | ||
} | ||
|
||
scriptContextRedeemer :: Redeemer | ||
scriptContextRedeemer = Redeemer (toBuiltinData FullUnlock) | ||
|
||
scriptContextScriptInfo :: ScriptInfo | ||
scriptContextScriptInfo = | ||
SpendingScript (TxOutRef txOutRefId txOutRefIdx) (Just datum) | ||
where | ||
txOutRefId = "058fdca70be67c74151cea3846be7f73342d92c0090b62c1052e6790ad83f145" | ||
txOutRefIdx = 0 | ||
datum :: Datum | ||
datum = Datum (toBuiltinData testVestingDatum) | ||
|
||
testVestingDatum :: VestingDatum | ||
testVestingDatum = | ||
VestingDatum | ||
{ beneficiary = Address (PubKeyCredential testBeneficiaryPKH) Nothing | ||
, vestingAsset = assetClass (CurrencySymbol "$") (TokenName "test-asset") | ||
, totalVestingQty = 1000 | ||
, vestingPeriodStart = 0 | ||
, vestingPeriodEnd = 100 | ||
, firstUnlockPossibleAfter = 10 | ||
, totalInstallments = 10 | ||
} | ||
|
||
testBeneficiaryPKH :: PubKeyHash | ||
testBeneficiaryPKH = PubKeyHash "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, using
not
is not preferable. I'm not sure our optimizer or GHC deal with this liketo