EP
Ergo Platform•4y ago
bk

ContextExtension

I am playing around with org.ergoplatform.playground code but am struggling to use ContextExtension to provide a custom value. Can someone point me to an example?
8 Replies
greenhat
greenhat•4y ago
I don't think ContextExtension is exposed in the playground API. I tried to make as simple as possible.
bk
bkOP•4y ago
I tried working around the playground API limitations by creating a tx through the API and then remaking it with my custom bindings like:
val initTx = Transaction(
// tx details...
)

val initTxWithExt = new UnsignedErgoLikeTransaction(
initTx.inputs.map(i => new UnsignedInput(i.boxId, i.extension.copy(bindings.toMap))),
initTx.dataInputs,
initTx.outputCandidates
)
val initTx = Transaction(
// tx details...
)

val initTxWithExt = new UnsignedErgoLikeTransaction(
initTx.inputs.map(i => new UnsignedInput(i.boxId, i.extension.copy(bindings.toMap))),
initTx.dataInputs,
initTx.outputCandidates
)
but then when I send the tx to the mock blockchain I get this:
[error] java.lang.IllegalArgumentException: requirement failed: box (index 0) verification failed (cost 10151)
[error] at scala.Predef$.require(Predef.scala:281)
[error] at org.ergoplatform.playgroundenv.utils.TransactionVerifier$.$anonfun$verify$1(TransactionVerifier.scala:53)
[error] at org.ergoplatform.playgroundenv.utils.TransactionVerifier$.$anonfun$verify$1$adapted(TransactionVerifier.scala:33)
[error] java.lang.IllegalArgumentException: requirement failed: box (index 0) verification failed (cost 10151)
[error] at scala.Predef$.require(Predef.scala:281)
[error] at org.ergoplatform.playgroundenv.utils.TransactionVerifier$.$anonfun$verify$1(TransactionVerifier.scala:53)
[error] at org.ergoplatform.playgroundenv.utils.TransactionVerifier$.$anonfun$verify$1$adapted(TransactionVerifier.scala:33)
in the verifier. Is that because of playground limitations as well?
greenhat
greenhat•4y ago
Tx verifier honor context extension - https://github.com/ergoplatform/ergo-playgrounds/blob/03a6e776be652cd5cd68a32c47236c6fe63b0487/playground-env/src/main/scala/org/ergoplatform/playgroundenv/utils/TransactionVerifier.scala#L46 What's the script in that input box? Maybe it fails for another reason (not context extension).
GitHub
ergo-playgrounds/TransactionVerifier.scala at 03a6e776be652cd5cd68a...
Run contracts + off-chain code in the browser. Contribute to ergoplatform/ergo-playgrounds development by creating an account on GitHub.
bk
bkOP•4y ago
At this point I am just setting "true" as the contract. I hacked it down just to the essentials:
val blockchainSim = newBlockChainSimulationScenario("mockchain")

val party = blockchainSim.newParty("party")
party.generateUnspentBoxes(toSpend = 800000000)

val contract = "true"

val bindings = Map(
1.toByte -> Values.IntConstant(10),
)

val initTx_ = Transaction(
inputs = party.selectUnspentBoxes(toSpend = 100000000),
outputs = List(Box(value = 100000000, script = ErgoScriptCompiler.compile(scriptEnv, contract))),
fee = MinTxFee,
sendChangeTo = party.wallet.getAddress
)

val initTx = new UnsignedErgoLikeTransaction(
initTx_.inputs.map(i => new UnsignedInput(i.boxId, i.extension.add(bindings.toMap.toArray:_*))),
initTx_.dataInputs,
initTx_.outputCandidates
)

blockchainSim.send(party.wallet.sign(initTx))
val blockchainSim = newBlockChainSimulationScenario("mockchain")

val party = blockchainSim.newParty("party")
party.generateUnspentBoxes(toSpend = 800000000)

val contract = "true"

val bindings = Map(
1.toByte -> Values.IntConstant(10),
)

val initTx_ = Transaction(
inputs = party.selectUnspentBoxes(toSpend = 100000000),
outputs = List(Box(value = 100000000, script = ErgoScriptCompiler.compile(scriptEnv, contract))),
fee = MinTxFee,
sendChangeTo = party.wallet.getAddress
)

val initTx = new UnsignedErgoLikeTransaction(
initTx_.inputs.map(i => new UnsignedInput(i.boxId, i.extension.add(bindings.toMap.toArray:_*))),
initTx_.dataInputs,
initTx_.outputCandidates
)

blockchainSim.send(party.wallet.sign(initTx))
It works if I comment out 1.toByte -> Values.IntConstant(10), but when I leave it in I get the requirement failed: box (index 0) verification failed (cost 10151) error.
greenhat
greenhat•4y ago
I think the cause is the prover in wallet.sign ignores and removes the context extensions, but the signed message is a serialized tx with context extensions. The ergo-playgrounds uses an old appkit version where context extensions are ignored in the prover. Thus, producing invalid proofs, which the verifier fails to verify. I tried to update the appkit version, but it breaks the build, probably due to way too different sigmastate-interpreter versions used in ergo-script-compiler and appkit.
greenhat
greenhat•4y ago
I made https://github.com/ergoplatform/ergo-playgrounds/issues/27 but I'm knee-deep in oracle v2 and sigma-rust now so I'm not sure when I'll be able to take care of it. Feel free to take a look at it if you're interested. I'll be happy to help.
GitHub
Update appkit to support context extension in tx signing · Issue #2...
See https://discord.com/channels/668903786361651200/1042839384698925106/1042839384698925106 Updating appkit breaks the build, probably because of too many differences in the sigmastate version in a...
bk
bkOP•4y ago
I tried to upgrade to a few different version combinations of app-kit / ergo-scala-compiler / sigmastate but they all produced crazy lowlevel runtime exceptions. 🥹
greenhat
greenhat•4y ago
I feel you! I suspect ergo-scala-compiler since it does some crazy stuff with macros.

Did you find this page helpful?