Bitcoin Forum

Bitcoin => Project Development => Topic started by: rb1205 on March 26, 2014, 03:54:03 PM



Title: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 03:54:03 PM
This blockchain scripting contest is a way to raise awareness about the possibilities and powers of the scripting mechanism integrated in the Bitcoin protocol.

Every trial will be about a non-standard transaction output (scriptPubKey) broadcast by me and funded with a given amount. Objective of the trial is to find an appropriate script (scriptSig) that will succesfully resolve the stacked scripts, as requested by the bitcoin protocol (https://en.bitcoin.it/wiki/Transactions#Verification). The amount in the tx output is the award of the trial and can be claimed at will.

The difficulty of the trials will increase in each step.

Recommended Toolchain:
  • Raw transaction generator: http://brainwallet.org/#tx
  • Raw transaction decoder (for checking): https://blockchain.info/decode-tx
  • Verify that your transaction succesfully solves the puzzle: https://en.bitcoin.it/wiki/Raw_Transactions#Validate_a_transaction_without_broadcasting_it (I couldn't find an online tool for this, sorry)
  • Non-standard transaction pusher: http://eligius.st/~wizkid057/newstats/pushtxn.php

Documentation: Transactions (https://en.bitcoin.it/wiki/Transactions), Raw Transactions API (https://en.bitcoin.it/wiki/Raw_Transactions), Scripts and OPcodes reference (https://en.bitcoin.it/wiki/Script).

Have fun!

PS: Any amount sent to the address 1JHCn9wLLXHc4yfo968FrT259Um2hzeUpy will be used to fund the next trials.


First Stage: (Probably) claimed! https://bitcointalk.org/index.php?topic=534734.msg5913898#msg5913898
Second Stage: Open on new thread! https://bitcointalk.org/index.php?topic=538423.msg5931091#msg5931091


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 03:54:14 PM
reserved


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 03:54:43 PM
1st trial

Funding transaction/output: ab149362ea4e119d2bc5211b35083c23ec41842af6bbc2ff3c5f1e55941199cc (https://blockchain.info/tx/ab149362ea4e119d2bc5211b35083c23ec41842af6bbc2ff3c5f1e55941199cc), n=0

Claimable amount: 5 mBTC (remember to reserve at least 0.1mBTC for transaction fees)

scriptPubKey to solve:

Code:
OP_2DUP OP_ADD OP_8 OP_EQUALVERIFY OP_SUB OP_2 OP_EQUAL

Difficulty level: easy

State: Probably claimed, but unconfirmed.

EDIT: Seems like someone managed to send a valid transaction to eligius, which is now blocking anyone from sending a different transaction that uses the same output. You can check the validity of your transaction using the signrawtransaction RPC call in bitcoin, or by posting it here.

EDIT2: Detailed solution posted here (https://bitcointalk.org/index.php?topic=534734.msg5919405#msg5919405)


Title: Re: Blockchain scripting contest
Post by: 9inety7even on March 26, 2014, 05:15:07 PM
Are you sure that the scriptPubKey is correct? Doesn't OP_EQUALVERIFY need to be at the end?

Never mind, I realized my mistake and solved the puzzle. Do I have to have bitcoin-qt to submit the answer?


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 05:58:36 PM
As it's a non-standard transaction, you'll need to give personally the transaction to the pools that accept them, and eligius is the only one I know. They have a nice tool (http://eligius.st/~wizkid057/newstats/pushtxn.php) that lets you push a raw transaction directly via web.


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 06:06:01 PM
However, I'm getting reports that eligius is refusing correct solutions, so probably the output has already been claimed.


Title: Re: Blockchain scripting contest
Post by: 9inety7even on March 26, 2014, 07:08:53 PM
However, I'm getting reports that eligius is refusing correct solutions, so probably the output has already been claimed.

For what it's worth, blockchain.info shows the output as unclaimed still. I understand that I have to use the tool to push the transaction, but how do I construct the raw transaction now that I know the scriptSig.


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 07:29:34 PM
Again, from the toolchain links in the OP:

Quote
Raw transaction generator: http://brainwallet.org/#tx



Title: Re: Blockchain scripting contest
Post by: 9inety7even on March 26, 2014, 07:45:25 PM
I understand that I'm supposed to use that, but I don't know how. Can you explain how I can submit a custom scriptSig for the transaction you gave?


Title: Re: Blockchain scripting contest
Post by: RGBKey on March 26, 2014, 08:14:17 PM
I'm interested in this stuff, but even after half an hour of looking at wiki pages I barely understand anything more than I did when I started. If your goal is to teach people, could you explain the process after someone successfully claims the first one?


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 09:11:05 PM
Sure, that's my idea.

Actually, since someone probably already sent the winning transaction, i'll give out the solution now.

first, let's solve the first half of the problem. Here's the scriptPubKey object of this first trial:

  OP_2DUP OP_ADD OP_8 OP_EQUALVERIFY OP_SUB OP_2 OP_EQUAL

I'll explain it under the assumption that you roughly know what a stack-based calculator (https://en.wikipedia.org/wiki/Stack-oriented_programming_language) is. Also, please refer to the script wiki page (https://en.bitcoin.it/wiki/Script) as a reference for the commands used in the scripts.

As the bitcoin protocol requires, you have to find a scriptSig that, when concatenated with the above script and executed, is considered valid. That is, the first stack item at the end of the script execution is not 0 or null, no verification fails and no illegal or invalidating operation is performed. Let's see what the scriptPubKey do.

OP_2DUP duplicates the first 2 values in the stack as they are. This suggests that the scriptSig, which gets evaluated right before this instruction, must yeld at least two values, let's call them x and y.

OP_ADD takes the first 2 values in the stack, adds them and pushes the result in the stack

OP_8 pushes the number 8 in the stack

OP_EQUALVERIFY tests the first 2 values in the stacks to be equal, and invalidates the transaction if they aren't. Since the first value is 8 (we just pushed it in the last instruction) the one before that, which is the sum of the 2 variables "x" and "y" in the scriptSig, must be 8 as well. So, we can assert that x+y=8

OP_SUB, OP_2 and OP_EQUAL make a similar thing, calculating the difference between the other 2 numbers in the stack which, thanks to the OP_2DUP at the beginning of the script, are still the 2 variables from the scriptSig. this tell us that x-y=2

So, we have x+y=8 and x-y=2. Solving this simple math problem gives x=5 and y=3, and thus the scriptSig simply is "OP_5 OP_3".


Now, to create the custom transaction that spends that output. There are a number of different methods to achieve this, but my tool of choice is brainwallet.org tx builder (http://brainwallet.org/#tx). Since the input of this transaction won't be address-based, you can safely ignore the private key and source address field. Set the "Destination address" to one of your addresses to have a template going, then click on the "Json transaction" button on the top of the form. Now, you have to manually change the parameters of the "in" section to connect the output from the test transaction. In the bitcoin protocol, a txout is defined by the hash of the transaction that created it and the ID of the txout inside that transaction. As wrote above, the hash of the transaction is ab149362ea4e119d2bc5211b35083c23ec41842af6bbc2ff3c5f1e55941199cc and the ID of the relevant txout is 0 (that is, the first entry in the txout list). Enter those values in the "hash" and "n" fields in the "in" section, overwriting the default values. Now you have to enter the scriptSig you desumed above: scroll to the scriptSig field, delete the bogus values you'll find there and just write "OP_5 OP_3". Almost done, all left is the amount of btc: the tx builder fails to detect the amount of BTC in non-standard transactions, so you'll have to set it manually. The amount of BTC available are 5 mBTC, or 0.005 btc. However, don't forget miner fees! Set the amount to 0.0049, so that 0.1 mBTC goes to eligius for the trouble of including your transaction in the blockchain.

Done, you don't even have to re-sign the transaction, because no signature verification is done. The raw transaction is ready, you can check it to be correct using the "signrawtransaction" RPC code in bitcoin: it should return a "complete":true parameter that tells you that it's ready to be broadcast. However, since someone already did that, trying to push the transaction through the eligius service  (http://eligius.st/~wizkid057/newstats/pushtxn.php)will result in a failure because it will refuse to accept a double-spend transaction. Be quicker next time :D

Real life applications of this script:

Next to none. The only objective of this stage is to help understanding how the bitcoin scripting mechanism works and to familiarize with the tools needed to build a transaction with custom scripts.


Title: Re: Blockchain scripting contest
Post by: RGBKey on March 26, 2014, 09:29:07 PM
That's great but my brain is crying.

EDIT: Think I get it now.


Title: Re: Blockchain scripting contest
Post by: RGBKey on March 26, 2014, 09:41:02 PM
Ok I'm pretty sure I have a basic understanding of this. Throw your blockchain witchcraft at me! :P


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 09:51:54 PM
A more clear explanation of what's happening in the script.

StackOperationComment
x,y(scriptSig)scriptSig get executed, 2 numbers are pushed into the stack
x,y,x,yOP_2DUPThe first 2 values in the stack get duplicated
x,y,x+yOP_ADDThe first 2 values in the stack are added and the sum is pushed in the stack
x,y,x+y,8OP_8the number 8 is pushed in the stack
x,y OP_EQUALVERIFYThe first 2 values in the stack are tested, the script fails if they aren't equal (so, x+y=8)
x-yOP_SUBThe first 2 values in the stack are subtracted and the difference is pushed in the stack
x-y,2OP_2the number 2 is pushed in the stack
?OP_EQUALThe first 2 values in the stack are tested, if equal the number 1 gets pushed in the stack, 0 otherwise
?(final check)The first value in the stack must be not 0 or null. So, the previous test must be true, thus x-y=2



Title: Re: Blockchain scripting contest
Post by: rb1205 on March 26, 2014, 09:53:40 PM
Ok I'm pretty sure I have a basic understanding of this. Throw your blockchain witchcraft at me! :P

Great! Next stage is scheduled for tomorrow at around 14:00 GMT. Don't miss it!


Title: Re: Blockchain scripting contest
Post by: norbertVC on March 27, 2014, 09:24:23 AM
I guess I unterstood this now, waiting for the competition today :)


Title: Re: Blockchain scripting contest
Post by: rb1205 on March 27, 2014, 01:37:07 PM
Thread moved & new stage opened!

https://bitcointalk.org/index.php?topic=538423.msg5931067#msg5931067