CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
115 stars 80 forks source link

Support for [CHIP-2021-02: Native Introspection Opcodes] #92

Closed monsterbitar closed 2 years ago

monsterbitar commented 3 years ago

Could we have a branch with support for the native introspection chip, to make it easier for people to test and evaluate when the testnet comes online?

List of opcodes: https://gitlab.com/GeneralProtocols/research/chips/-/blob/master/CHIP-2021-02-Add-Native-Introspection-Opcodes.md

rkalis commented 2 years ago

After going over the introspection CHIP, I came up with this tentative introspection syntax (along with the corresponding opcodes):

this.activeInputIndex -> OP_INPUTINDEX
this.activeBytecode -> OP_ACTIVEBYTECODE

tx.version -> OP_TXVERSION
tx.locktime -> OP_TXLOCKTIME

tx.inputs
tx.inputs.length -> OP_TXINPUTCOUNT
tx.inputs[i]
tx.inputs[i].value -> i OP_UTXOVALUE
tx.inputs[i].lockingBytecode -> i OP_UTXOBYTECODE
tx.inputs[i].outpointTransactionHash -> i OP_OUTPOINTTXHASH
tx.inputs[i].outpointIndex -> i OP_OUTPOINTINDEX
tx.inputs[i].unlockingBytecode -> i OP_INPUTBYTECODE
tx.inputs[i].sequenceNumber -> i OP_INPUTSEQUENCENUMBER

tx.outputs
tx.outputs.length -> OP_TXOUTPUTCOUNT
tx.outputs[i]
tx.outputs[i].value -> i OP_OUTPUTVALUE
tx.outputs[i].lockingBytecode -> i OP_OUTPUTBYTECODE

For example:

// Enforce that the contract sends the current UTXO's value (minus 1000 sats) back to the contract in output index 0
require(tx.outputs[0].value == tx.inputs[this.activeInputIndex].value - 1000);
require(tx.outputs[0].lockingBytecode == tx.inputs[this.activeInputIndex].lockingBytecode);

@nathanielCherian since you created the VS Code extension I'd like to run this past you with respect to code completion. Would it be easy/difficult to have code completion for that kind of syntax?

nathanielCherian commented 2 years ago

Shouldn't be a problem! There may be some caveats I don't realize until actually adding, but for the most part, it's pretty standard. Is there a date you're looking to release this by?

rkalis commented 2 years ago

Cool! The new BCH Script features will go live on May 15th 2022, so that's still more than half a year away.

That said there's a test network already live with these new features so I plan to release a cashscript@next version some time in november for people that already want to try it out on testnet.

I'm not sure what that means for the VS Code extension, since ideally we'd want it to show the old autocompletions if you're not using the "beta" version, but the new ones if you are using it.

If that is impossible or difficult it's also fine to make the changes to the extension when the changes go live in the main version of CashScript in May.

nathanielCherian commented 2 years ago

Awesome, can't wait to try it out! I can work on a separate branch and publish the .vsix package so those who want autocompletion with cashscript@next can manually install it.

rkalis commented 2 years ago

That makes sense, awesome!

rkalis commented 2 years ago

This has been implemented in v0.7.0-next.0. If some issues with it come up I'll have the time to fix that before May, if no issues come up, I will release v0.7.0 once the May 2022 upgrade hits mainnet.

rkalis commented 2 years ago

v0.7.0 has been released for mainnet support of native introspection