Skip to content

Commit

Permalink
introduce generic datums
Browse files Browse the repository at this point in the history
  • Loading branch information
jankun4 committed Oct 22, 2024
1 parent 4d650b8 commit 8862280
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 95 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Changelog](https://keepachangelog.com/en/1.1.0).
* Updated readme
* Updated cardano-transaction-lib to v9.3.1
* Updated the following smart contracts: `InitTokenPolicy`, `VersionOraclePolicy`, `CommitteePlainSchnorrSecp256k1ATMSPolicy`, `ReserveValidator`, `ReserveAuthPolicy`, `IlliquidCirculationSupplyValidator`

* Moved DParameter, Permissioned Candidates, CommitteeCandidateValidator, Reserve and IlliquidCirculationSupply to the Generic Validators implementation.
## Removed
* Removed `init` command
* the PoC test modules were removed
Expand Down
33 changes: 20 additions & 13 deletions offchain/src/TrustlessSidechain/CommitteeCandidateValidator.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module TrustlessSidechain.CommitteeCandidateValidator

import Contract.Prelude hiding (unit)

import Cardano.Types.BigInt as BigInt
import Cardano.Types.OutputDatum (outputDatumDatum)
import Cardano.Types.PlutusData (unit)
import Cardano.Types.PlutusScript as PlutusScript
Expand Down Expand Up @@ -53,6 +54,9 @@ import TrustlessSidechain.Utils.Address
, getOwnWalletAddress
, toAddress
)
import TrustlessSidechain.Utils.Data
( VersionedGenericDatum(VersionedGenericDatum)
)
import TrustlessSidechain.Utils.Scripts
( mkValidatorWithParams
)
Expand Down Expand Up @@ -121,7 +125,6 @@ newtype BlockProducerRegistration = BlockProducerRegistration
, sidechainPubKey :: ByteArray -- public key in the sidechain's desired format
, sidechainSignature :: ByteArray -- Signature of the sidechain candidate
, inputUtxo :: TransactionInput -- A UTxO that must be spent by the transaction
, ownPkh :: PaymentPubKeyHash -- Owner public key hash
, auraKey :: ByteArray -- sidechain authority discovery key
, grandpaKey :: ByteArray -- sidechain grandpa key
}
Expand All @@ -142,7 +145,6 @@ instance ToData BlockProducerRegistration where
, sidechainPubKey
, sidechainSignature
, inputUtxo
, ownPkh
, auraKey
, grandpaKey
}
Expand All @@ -152,31 +154,28 @@ instance ToData BlockProducerRegistration where
, toData sidechainPubKey
, toData sidechainSignature
, toData inputUtxo
, toData ownPkh
, toData auraKey
, toData grandpaKey
]

instance FromData BlockProducerRegistration where
fromData plutusData = case plutusData of
Constr n [ x1, x2, x3, x4, x5, x6, x7 ] -> do
Constr n [ x1, x2, x3, x4, x5, x6 ] -> do
guard (n == BigNum.fromInt 0)
x1' <- fromData x1
x2' <- fromData x2
x3' <- fromData x3
x4' <- fromData x4
x5' <- fromData x5
x6' <- fromData x6
x7' <- fromData x7
pure
( BlockProducerRegistration
{ stakeOwnership: x1'
, sidechainPubKey: x2'
, sidechainSignature: x3'
, inputUtxo: x4'
, ownPkh: x5'
, auraKey: x6'
, grandpaKey: x7'
, auraKey: x5'
, grandpaKey: x6'
}
)
_ -> Nothing
Expand Down Expand Up @@ -251,16 +250,21 @@ register
valUtxos

let
datum = BlockProducerRegistration
blockProducerDatum = BlockProducerRegistration
{ stakeOwnership
, sidechainPubKey
, sidechainSignature: sidechainSig
, inputUtxo: inputUtxo
, ownPkh
, auraKey
, grandpaKey
}

datum = toData $ VersionedGenericDatum
{ datum: ownPkh
, builtinData: toData blockProducerDatum
, version: BigInt.fromInt 0
}

matchingKeys (BlockProducerRegistration r1) (BlockProducerRegistration r2) =
r1.stakeOwnership == r2.stakeOwnership
&& r1.sidechainPubKey
Expand All @@ -270,7 +274,7 @@ register
&& r1.grandpaKey
== r2.grandpaKey

when (any (matchingKeys datum) ownRegistrationDatums) $
when (any (matchingKeys blockProducerDatum) ownRegistrationDatums) $
throw
( InvalidCLIParams
"BlockProducer with given set of keys is already registered"
Expand Down Expand Up @@ -349,10 +353,13 @@ findOwnRegistrations ownPkh spoPubKey validatorUtxos = do
\(input /\ TransactionOutput out) ->
pure do
d <- outputDatumDatum =<< out.datum
BlockProducerRegistration r <- fromData d
VersionedGenericDatum { builtinData, datum: pkh } ::
VersionedGenericDatum PaymentPubKeyHash <-
fromData d
BlockProducerRegistration r <- fromData builtinData
guard
( (getSPOPubKey r.stakeOwnership == spoPubKey) &&
(r.ownPkh == ownPkh)
(pkh == ownPkh)
)
pure (input /\ BlockProducerRegistration r)

Expand Down
36 changes: 25 additions & 11 deletions offchain/src/TrustlessSidechain/DParameter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Cardano.FromData (fromData)
import Cardano.ToData (toData)
import Cardano.Types.Asset (Asset(Asset))
import Cardano.Types.AssetName (AssetName)
import Cardano.Types.BigInt as BigInt
import Cardano.Types.Int as Int
import Cardano.Types.OutputDatum (OutputDatum(OutputDatum))
import Cardano.Types.PlutusData as PlutusData
Expand Down Expand Up @@ -43,6 +44,9 @@ import TrustlessSidechain.Error
import TrustlessSidechain.Governance.Admin as Governance
import TrustlessSidechain.SidechainParams (SidechainParams)
import TrustlessSidechain.Utils.Asset (emptyAssetName)
import TrustlessSidechain.Utils.Data
( VersionedGenericDatum(VersionedGenericDatum)
)
import Type.Row (type (+))

dParameterTokenName :: AssetName
Expand Down Expand Up @@ -88,6 +92,12 @@ mkInsertDParameterLookupsAndConstraints
dParameterDatum = toData $ DParameterValidatorDatum
{ permissionedCandidatesCount, registeredCandidatesCount }

datum = toData $ VersionedGenericDatum
{ datum: unit
, builtinData: dParameterDatum
, version: BigInt.fromInt 0
}

lookups :: ScriptLookups
lookups = Lookups.plutusMintingPolicy dParameterMintingPolicy
<> governanceLookups
Expand All @@ -99,7 +109,7 @@ mkInsertDParameterLookupsAndConstraints
(RedeemerDatum $ PlutusData.unit)
dParameterTokenName
(Int.fromInt 1)
<> Constraints.mustPayToScript dParameterValidatorHash dParameterDatum
<> Constraints.mustPayToScript dParameterValidatorHash datum
DatumInline
value
<> governanceConstraints
Expand Down Expand Up @@ -154,15 +164,13 @@ mkUpdateDParameterLookupsAndConstraints
-- if the old D Parameter is exactly the same as the new one, throw an error
case oldDParameterOutput of
TransactionOutput { datum: Just (OutputDatum d) }
-> case fromData d of
Just (DParameterValidatorDatum dParameter)
| dParameter.permissionedCandidatesCount == permissionedCandidatesCount
&& dParameter.registeredCandidatesCount
== registeredCandidatesCount -> throw
( InvalidCLIParams
"Provided values have already been set. Please check."
)
_ -> pure unit
| Just (VersionedGenericDatum { builtinData } :: VersionedGenericDatum Unit) <-
fromData d
, Just (DParameterValidatorDatum dParameter) <- fromData builtinData
, dParameter.permissionedCandidatesCount == permissionedCandidatesCount
, dParameter.registeredCandidatesCount == registeredCandidatesCount ->
throw $ InvalidCLIParams
"Provided values have already been set. Please check."
_ -> pure unit

when (dParameterTokenAmount <= BigNum.fromInt 0)
Expand All @@ -186,6 +194,12 @@ mkUpdateDParameterLookupsAndConstraints
dParameterDatum = toData $ DParameterValidatorDatum
{ permissionedCandidatesCount, registeredCandidatesCount }

datum = toData $ VersionedGenericDatum
{ datum: unit
, builtinData: dParameterDatum
, version: BigInt.fromInt 0
}

lookups :: ScriptLookups
lookups = Lookups.validator dParameterValidator
<> Lookups.unspentOutputs
Expand All @@ -199,7 +213,7 @@ mkUpdateDParameterLookupsAndConstraints

constraints :: TxConstraints
constraints =
Constraints.mustPayToScript dParameterValidatorHash dParameterDatum
Constraints.mustPayToScript dParameterValidatorHash datum
DatumInline
value
<> spendScriptOutputConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module TrustlessSidechain.NativeTokenManagement.IlliquidCirculationSupply
import Contract.Prelude

import Cardano.Types.AssetName (AssetName)
import Cardano.Types.BigInt as BigInt
import Cardano.Types.BigNum as BigNum
import Cardano.Types.Int as Int
import Cardano.Types.PlutusData (unit) as PlutusData
Expand Down Expand Up @@ -41,6 +42,9 @@ import TrustlessSidechain.NativeTokenManagement.Types
)
import TrustlessSidechain.SidechainParams (SidechainParams)
import TrustlessSidechain.Utils.Asset (emptyAssetName)
import TrustlessSidechain.Utils.Data
( VersionedGenericDatum(VersionedGenericDatum)
)
import TrustlessSidechain.Utils.Scripts
( mkValidatorWithParams
)
Expand Down Expand Up @@ -112,6 +116,12 @@ depositMoreToSupply sp depositedValue utxo = do
$ pure (value `Value.add` depositedValue)

let
datum = toData $ VersionedGenericDatum
{ datum: unit
, builtinData: toData PlutusData.unit
, version: BigInt.fromInt 0
}

lookups :: Lookups.ScriptLookups
lookups =
Lookups.unspentOutputs (uncurry Map.singleton utxo)
Expand All @@ -120,7 +130,7 @@ depositMoreToSupply sp depositedValue utxo = do
constraints =
TxConstraints.mustPayToScript
icsValidatorHash
PlutusData.unit
datum
DatumInline
newValue
<> TxConstraints.mustSpendScriptOutput (fst utxo)
Expand Down Expand Up @@ -169,6 +179,12 @@ withdrawFromSupply sp mintingPolicyHash withdrawnValue utxo = do
$ pure (value `Value.minus` withdrawnValue)

let
datum = toData $ VersionedGenericDatum
{ datum: unit
, builtinData: toData PlutusData.unit
, version: BigInt.fromInt 0
}

lookups :: Lookups.ScriptLookups
lookups =
Lookups.unspentOutputs (uncurry Map.singleton utxo)
Expand All @@ -178,7 +194,7 @@ withdrawFromSupply sp mintingPolicyHash withdrawnValue utxo = do
constraints =
TxConstraints.mustPayToScript
icsValidatorHash
PlutusData.unit
datum
DatumInline
newValue
<> TxConstraints.mustSpendScriptOutput (fst utxo)
Expand Down
Loading

0 comments on commit 8862280

Please sign in to comment.