Skip to content

Commit

Permalink
trying to make groupIndipendentLets work, no luck
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Nov 22, 2024
1 parent 2399478 commit 31685b0
Show file tree
Hide file tree
Showing 17 changed files with 426 additions and 90 deletions.
4 changes: 2 additions & 2 deletions packages/offchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
"@harmoniclabs/bytestring": "^1.0.0",
"@harmoniclabs/cardano-costmodels-ts": "^1.2.0",
"@harmoniclabs/cardano-ledger-ts": "^0.2.6",
"@harmoniclabs/cbor": "^1.3.0",
"@harmoniclabs/cbor": "^1.3.1",
"@harmoniclabs/pair": "^1.0.0",
"@harmoniclabs/plutus-data": "^1.2.4",
"@harmoniclabs/plutus-machine": "^2.0.1",
"@harmoniclabs/uplc": "^1.2.3"
"@harmoniclabs/uplc": "^1.3.0"
},
"devDependencies": {
"@babel/preset-env": "^7.18.6",
Expand Down
52 changes: 34 additions & 18 deletions packages/offchain/src/TxBuilder/TxBuilderRunner/TxBuilderRunner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineReadOnlyProperty, isObject } from "@harmoniclabs/obj-utils";
import type { ITxRunnerProvider } from "../IProvider";
import type { TxBuilder } from "../TxBuilder";
import { ITxBuildArgs, ITxBuildOutput, NormalizedITxBuildArgs, cloneITxBuildArgs, normalizeITxBuildArgs } from "../../txBuild";
import { ITxBuildArgs, ITxBuildOutput, NormalizedITxBuildArgs, normalizeITxBuildArgs } from "../../txBuild";
import { Address, AddressStr, CertStakeDelegation, Certificate, Hash28, Hash32, ITxOut, ITxOutRef, IUTxO, IValuePolicyEntry, PlutusScriptType, PoolKeyHash, PoolParams, PubKeyHash, Script, ScriptType, StakeAddress, StakeAddressBech32, Credential, StakeValidatorHash, Tx, TxIn, TxMetadata, TxMetadatum, TxOutRefStr, UTxO, Value, forceTxOutRefStr, isITxOut, isIUTxO, CredentialType, CertStakeDeRegistration, CertStakeRegistration, CertPoolRegistration, IPoolParams, CanBeHash28, CertPoolRetirement, StakeCredentials, ITxWithdrawalsEntry, Vote, IAnchor, IVoter, ProtocolParameters, IProposalProcedure, ITxWithdrawals, TxWithdrawals, INewCommitteeEntry, IConstitution, VotingProcedure, VotingProcedures, forceTxOutRef, IVotingProceduresEntry, TxOutRef, VoterKind, GovActionType, isIVotingProceduresEntry, isIProposalProcedure, eqITxOutRef } from "@harmoniclabs/cardano-ledger-ts";
import { CanBeUInteger, canBeUInteger, forceBigUInt } from "../../utils/ints";
import { CanResolveToUTxO, cloneCanResolveToUTxO, shouldResolveToUTxO } from "../CanResolveToUTxO/CanResolveToUTxO";
Expand All @@ -19,17 +19,6 @@ import { IProtocolVerision } from "@harmoniclabs/cardano-ledger-ts/dist/ledger/p
import { Rational } from "../../utils/Rational";
import { eqIVoter } from "../../txBuild/ITxBuildVotingProcedure";

// /** sync */
// interface TxBuilderStep {
//
// }
//
// function cloneStep( step: TxBuilderStep ): TxBuilderStep
// {
// return {
//
// };
// }

type SimpleScriptInfos = {
inline: Script<PlutusScriptType>
Expand Down Expand Up @@ -110,6 +99,7 @@ const readonlyValueDescriptor = Object.freeze({

const _datumsCache: { [hash: string]: Data } = {};
const _datumsHashes: string[] = [];
const _MAX_DATUMS_CACHE_SIZE = 20;

function _saveResolvedDatum( datum: Data, hash?: string ): void
{
Expand All @@ -130,6 +120,11 @@ function _saveResolvedDatum( datum: Data, hash?: string ): void
_datumsCache[ hash ] = theData;
}
}
while( _datumsHashes.length > _MAX_DATUMS_CACHE_SIZE )
{
const h = _datumsHashes.shift()!;
delete _datumsCache[ h ];
}
}

function _getResolvedDatum( hash: string ): Data | undefined
Expand Down Expand Up @@ -1270,7 +1265,13 @@ export class TxBuilderRunner
datum?: CanBeData | undefined
): void
{
if( canBeData( datum ) ) _saveResolvedDatum( forceData( datum ) );
if( datum && canBeData( datum ) ) {
try
{
datum = forceData( datum );
_saveResolvedDatum( datum );
} catch {}
}

utxo = utxo instanceof UTxO ? utxo : new UTxO( utxo );
const paymentCreds = (utxo as UTxO).resolved.address.paymentCreds;
Expand All @@ -1296,7 +1297,13 @@ export class TxBuilderRunner
): TxBuilderRunner
{
// save datum before resolving utxo
if( canBeData( datum ) ) _saveResolvedDatum( forceData( datum ) );
if( datum && canBeData( datum ) ) {
try
{
datum = forceData( datum );
_saveResolvedDatum( datum );
} catch {}
}

for( const _utxo of utxos )
{
Expand All @@ -1321,7 +1328,13 @@ export class TxBuilderRunner
if( shouldResolveToUTxO( script_or_ref ) )
{
// save datum before resolving utxo
if( canBeData( datum ) ) _saveResolvedDatum( forceData( datum ) );
if( datum && canBeData( datum ) ) {
try
{
datum = forceData( datum );
_saveResolvedDatum( datum );
} catch {}
}

tasks.push({
kind: TxBuilderTaskKind.ResolveUTxO,
Expand Down Expand Up @@ -1544,7 +1557,7 @@ export class TxBuilderRunner
script_or_ref?: Script<PlutusScriptType> | CanResolveToUTxO
): TxBuilderRunner
{
redeemer = canBeData( redeemer ) ? forceData( redeemer ) : undefined;
redeemer = redeemer && canBeData( redeemer ) ? forceData( redeemer ) : undefined;
if( shouldResolveToUTxO( script_or_ref ) )
{
tasks.push({
Expand Down Expand Up @@ -1727,7 +1740,7 @@ export class TxBuilderRunner
script_or_ref?: Script<PlutusScriptType> | CanResolveToUTxO
): TxBuilderRunner
{
redeemer = canBeData( redeemer ) ? forceData( redeemer ) : undefined;
redeemer = redeemer && canBeData( redeemer ) ? forceData( redeemer ) : undefined;
if( shouldResolveToUTxO( script_or_ref ) )
{
tasks.push({
Expand Down Expand Up @@ -1968,7 +1981,10 @@ export class TxBuilderRunner
const resolvedDatums = await provider.resolveDatumHashes( datumHashesToResolve );
for( const { hash, datum } of resolvedDatums )
{
_saveResolvedDatum( forceData( datum ), hash.toString() );
try {
const forced = forceData( datum );
_saveResolvedDatum( forced, hash.toString() );
} catch {}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/onchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
"peerDependencies": {
"@harmoniclabs/pair": "^1.0.0",
"@harmoniclabs/bytestring": "^1.0.0",
"@harmoniclabs/cbor": "^1.3.0",
"@harmoniclabs/cbor": "^1.3.1",
"@harmoniclabs/plutus-data": "^1.2.4",
"@harmoniclabs/cardano-costmodels-ts": "^1.2.0",
"@harmoniclabs/plutus-machine": "^2.0.1",
"@harmoniclabs/uplc": "^1.2.4",
"@harmoniclabs/uplc": "^1.3.0",
"@harmoniclabs/cardano-ledger-ts": "^0.2.6"
},
"devDependencies": {
Expand Down
35 changes: 35 additions & 0 deletions packages/onchain/src/IR/IRNodes/utils/dependsByDbns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IRTerm } from "../../IRTerm";
import { iterTree } from "../../toUPLC/_internal/iterTree";
import { IRSelfCall } from "../IRSelfCall";
import { IRVar } from "../IRVar";

/**
* returns true if the term depends on *ANY* of the deBrujin indices specified
*/
export function dependsByDbns( term: IRTerm, depsDbns: readonly number[] ): boolean
{
if( depsDbns.length === 0 ) return false;
let doesDepend = false;
iterTree( term,
(node, diff) => {
if(
node instanceof IRVar ||
node instanceof IRSelfCall
)
{
doesDepend = doesDepend || depsDbns.some( dbn => {
const realDbn = dbn + diff;
// realDbn === 0 implies no variables are in scope
// so if node.dbn === 0 and realDbn === 1 then the
// variable is pointing to that definition
return node.dbn === realDbn;
});
}
},
// shouldSkipNode
undefined,
// shouldStop
() => doesDepend // exit early
);
return doesDepend;
}
30 changes: 30 additions & 0 deletions packages/onchain/src/IR/IRNodes/utils/isClosedAtDbn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IRTerm } from "../../IRTerm";
import { iterTree } from "../../toUPLC/_internal/iterTree";
import { IRSelfCall } from "../IRSelfCall";
import { IRVar } from "../IRVar";

export function isClosedAtDbn( term: IRTerm, dbn: number = 0 ): boolean
{
let isClosed = true;
iterTree( term,
(node, diff) => {
const realDbn = dbn + diff;
if(
node instanceof IRVar ||
node instanceof IRSelfCall
)
{
// realDbn === 0 implies no variables are in scope
// so if node.dbn === 0 and realDbn === 0 then the variable is open
// and is pointing outside of the scope
isClosed = isClosed || node.dbn < realDbn;
}

},
// shouldSkipNode
undefined,
// shouldStop
() => !isClosed // exit early
)
return isClosed;
}
12 changes: 8 additions & 4 deletions packages/onchain/src/IR/toUPLC/CompilerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isObject } from "@harmoniclabs/obj-utils";
import { defaultUplcVersion, UPLCVersion } from "@harmoniclabs/uplc";

export interface CompilerUplcOptimizations {
Expand Down Expand Up @@ -52,6 +53,7 @@ export function completeUplcOptimizations(
complete: CompilerUplcOptimizations = defaultUplcOptimizations
): CompilerUplcOptimizations
{
if( !isObject( options ) ) return { ...defaultUplcOptimizations };
return {
groupApplications: options.groupApplications ?? complete.groupApplications,
inlineSingleUse: options.inlineSingleUse ?? complete.inlineSingleUse,
Expand Down Expand Up @@ -137,24 +139,26 @@ export function completeCompilerOptions(
...defaultOptions,
...complete
};
let uplcOptimizations = complete.uplcOptimizations as CompilerUplcOptimizations;
let uplcOptimizations = options.uplcOptimizations as CompilerUplcOptimizations;
if( typeof options.uplcOptimizations === "boolean" )
{
if( options.uplcOptimizations )
{
uplcOptimizations = {
...uplcOptimizations,
...productionUplcOptimizations
...productionUplcOptimizations,
...uplcOptimizations
}
}
else
{
uplcOptimizations = {
...debugUplcOptimizations,
...uplcOptimizations,
...debugUplcOptimizations
}
}
}
// console.log( "uplcOptimizations", uplcOptimizations );
// console.log( "completeUplcOptimizations( uplcOptimizations )",completeUplcOptimizations( uplcOptimizations ))
return {
targetUplcVersion,
removeTraces: options.removeTraces ?? complete.removeTraces,
Expand Down
7 changes: 3 additions & 4 deletions packages/onchain/src/IR/toUPLC/compileIRToUPLC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ import { replaceNativesAndReturnRoot } from "./subRoutines/replaceNatives";
import { replaceClosedLettedWithHoisted } from "./subRoutines/replaceClosedLettedWithHoisted";
import { hoistForcedNatives } from "./subRoutines/hoistForcedNatives";
import { handleRecursiveTerms } from "./subRoutines/handleRecursiveTerms";
import { CompilerOptions, completeCompilerOptions } from "./CompilerOptions";
import { CompilerOptions, completeCompilerOptions, defaultOptions } from "./CompilerOptions";
import { replaceHoistedWithLetted } from "./subRoutines/replaceHoistedWithLetted";
import { IRApp, IRCase, IRConstr, IRNative, IRVar } from "../IRNodes";
import { replaceForcedNativesWithHoisted } from "./subRoutines/replaceForcedNativesWithHoisted";
import { performUplcOptimizationsAndReturnRoot } from "./subRoutines/performUplcOptimizationsAndReturnRoot";


export function compileIRToUPLC(
term: IRTerm,
paritalOptions: Partial<CompilerOptions> = {}
paritalOptions: Partial<CompilerOptions> = defaultOptions
): UPLCTerm
{
// most of the time we are just compiling small
// pre-execuded terms (hence constants)
if( term instanceof IRConst ) return _irToUplc( term ).term;

///////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------- //
// --------------------------------- init --------------------------------- //
Expand Down
Loading

0 comments on commit 31685b0

Please sign in to comment.