-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
michele-nuzzi
committed
Apr 26, 2023
1 parent
156dd7f
commit d2b0b7a
Showing
7 changed files
with
313 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
240 changes: 240 additions & 0 deletions
240
src/offchain/tx/builder/__tests__/TxBuilder.build.time.test.ts
This file contains 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,240 @@ | ||
import { Address, Value, defaultProtocolParameters } from "../../../ledger" | ||
import { TxOut, TxOutRef, UTxO } from "../../body" | ||
import { TxBuilder } from "../TxBuilder" | ||
import { PScriptContext, compile, data, pfn, pmakeUnit, unit } from "../../../../onchain"; | ||
import { Script } from "../../../script/Script"; | ||
import { PaymentCredentials } from "../../../credentials"; | ||
import { DataI } from "../../../../types/Data"; | ||
|
||
|
||
describe("build time", () => { | ||
|
||
const txBuilder = new TxBuilder( | ||
"mainnet", | ||
defaultProtocolParameters | ||
); | ||
|
||
const txBuilderWithGenesis = new TxBuilder( | ||
"mainnet", | ||
defaultProtocolParameters, | ||
{ | ||
slotLengthInMilliseconds: 1000, | ||
systemStartPOSIX: (Math.round( Date.now() / 1e3 ) * 1e3) - 1e6 | ||
} | ||
); | ||
|
||
test("only invalid before results in validityStart", () => { | ||
|
||
const tx = txBuilder.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: Address.fake, | ||
value: Value.lovelaces(2_000_000) | ||
} | ||
}) | ||
} | ||
], | ||
invalidBefore: 42, | ||
changeAddress: Address.fake | ||
}); | ||
|
||
expect( tx.body.validityIntervalStart ) | ||
.not.toBe( undefined ); | ||
|
||
expect( tx.body.validityIntervalStart ) | ||
.toBe( 42n ); | ||
|
||
}); | ||
|
||
test("only invalid after results in validityStart=0 and ttl", () => { | ||
|
||
const tx = txBuilder.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: Address.fake, | ||
value: Value.lovelaces(2_000_000) | ||
} | ||
}) | ||
} | ||
], | ||
invalidAfter: 42, | ||
changeAddress: Address.fake | ||
}); | ||
|
||
expect( tx.body.validityIntervalStart ) | ||
.not.toBe( undefined ); | ||
|
||
expect( tx.body.ttl ) | ||
.not.toBe( undefined ); | ||
|
||
}); | ||
|
||
test("invalid before and invalid after", () => { | ||
|
||
const tx = txBuilder.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: Address.fake, | ||
value: Value.lovelaces(2_000_000) | ||
} | ||
}) | ||
} | ||
], | ||
invalidBefore: 42, | ||
invalidAfter: 69, | ||
changeAddress: Address.fake | ||
}); | ||
|
||
expect( tx.body.validityIntervalStart ) | ||
.not.toBe( undefined ); | ||
|
||
expect( tx.body.validityIntervalStart ) | ||
.toBe( 42n ); | ||
|
||
expect( tx.body.ttl ) | ||
.not.toBe( undefined ); | ||
|
||
expect( tx.body.ttl ) | ||
.toBe( 69n - 42n ); | ||
|
||
}); | ||
|
||
test("invalid before > invalid after; fails", () => { | ||
|
||
expect( () => txBuilder.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: Address.fake, | ||
value: Value.lovelaces(2_000_000) | ||
} | ||
}) | ||
} | ||
], | ||
invalidBefore: 69, | ||
invalidAfter: 42, | ||
changeAddress: Address.fake | ||
})).toThrow(); | ||
|
||
}); | ||
|
||
const simpleScript = new Script( | ||
"PlutusScriptV2", | ||
compile( | ||
pfn([ data, data, PScriptContext.type ], unit )( ( d, r, c ) => pmakeUnit() ) | ||
) | ||
); | ||
|
||
const simpleScriptAddress = Address.mainnet( | ||
PaymentCredentials.script( | ||
simpleScript.hash | ||
) | ||
); | ||
|
||
test("tx builder with time and script but no genesis fails", () => { | ||
|
||
expect( () => txBuilder.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: simpleScriptAddress, | ||
value: Value.lovelaces(2_000_000), | ||
datum: new DataI( 0 ) | ||
} | ||
}), | ||
inputScript: { | ||
script: simpleScript, | ||
datum: "inline", | ||
redeemer: new DataI( 0 ) | ||
} | ||
} | ||
], | ||
invalidBefore: 42, | ||
invalidAfter: 69, | ||
changeAddress: Address.fake | ||
})).toThrow(); | ||
|
||
}); | ||
|
||
test("tx builder with time and script and genesis is ok", () => { | ||
|
||
expect( () => txBuilderWithGenesis.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: simpleScriptAddress, | ||
value: Value.lovelaces(2_000_000), | ||
datum: new DataI( 0 ) | ||
} | ||
}), | ||
inputScript: { | ||
script: simpleScript, | ||
datum: "inline", | ||
redeemer: new DataI( 0 ) | ||
} | ||
} | ||
], | ||
invalidBefore: 42, | ||
invalidAfter: 69, | ||
changeAddress: Address.fake | ||
})).not.toThrow(); | ||
|
||
}); | ||
|
||
test("genesis can be setted", () => { | ||
|
||
const myTxBuilder = new TxBuilder( | ||
"mainnet", | ||
defaultProtocolParameters | ||
); | ||
|
||
const doStuff = () => myTxBuilder.buildSync({ | ||
inputs: [ | ||
{ | ||
utxo: new UTxO({ | ||
utxoRef: TxOutRef.fake, | ||
resolved: { | ||
address: simpleScriptAddress, | ||
value: Value.lovelaces(2_000_000), | ||
datum: new DataI( 0 ) | ||
} | ||
}), | ||
inputScript: { | ||
script: simpleScript, | ||
datum: "inline", | ||
redeemer: new DataI( 0 ) | ||
} | ||
} | ||
], | ||
invalidBefore: 42, | ||
invalidAfter: 69, | ||
changeAddress: Address.fake | ||
}) | ||
|
||
expect( doStuff ).toThrow(); | ||
|
||
myTxBuilder.setGenesisInfos({ | ||
slotLengthInMilliseconds: 1000, | ||
systemStartPOSIX: (Math.round( Date.now() / 1e3 ) * 1e3) - 1e6 | ||
}); | ||
|
||
expect( doStuff ).not.toThrow(); | ||
|
||
}); | ||
|
||
}) |
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./compile" | ||
export * from "./makeScript" | ||
export * from "./blueprint"; | ||
// export * from "./blueprint"; |
This file contains 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