Skip to content

Commit

Permalink
Merge ElementsProject#704: RPC: Fix tweakfedpegscript for dynafed, ad…
Browse files Browse the repository at this point in the history
…d basic test

f3ce121 RPC: Fix tweakfedpegscript for dynafed, add basic test (Gregory Sanders)

Pull request description:

Tree-SHA512: eae8f7133ae4eda8f57a3a00ba66f03e34103855ae08443dc8ab5894c5bd974f5684e26dcccdee0bb07d72baaced9b646cba778fa82118d863c29a48b84cfdfc
  • Loading branch information
stevenroose committed Sep 10, 2019
2 parents 5b2fe55 + f3ce121 commit d75d1f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ UniValue tweakfedpegscript(const JSONRPCRequest& request)

std::vector<unsigned char> scriptData = ParseHex(request.params[0].get_str());
CScript claim_script = CScript(scriptData.begin(), scriptData.end());
CScript tweaked_script = calculate_contract(Params().GetConsensus().fedpegScript, claim_script);

const auto& fedpegscripts = GetValidFedpegScripts(chainActive.Tip(), Params().GetConsensus(), true /* nextblock_validation */);
CScript tweaked_script = calculate_contract(fedpegscripts.front().second, claim_script);
CTxDestination parent_addr(ScriptHash(GetScriptForWitness(tweaked_script)));

UniValue ret(UniValue::VOBJ);
Expand Down
31 changes: 28 additions & 3 deletions test/functional/rpc_tweakfedpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
OP_TRUE_SCRIPT="51"
OP_CMS_SCRIPT="52210307fd375ed7cced0f50723e3e1a97bbe7ccff7318c815df4e99a59bc94dbcd819210367c4f666f18279009c941e57fab3e42653c6553e5ca092c104d1db279e328a2852ae"
LIQUID_SCRIPT="745c87635b21020e0338c96a8870479f2396c373cc7696ba124e8635d41b0ea581112b678172612102675333a4e4b8fb51d9d4e22fa5a8eaced3fdac8a8cbf9be8c030f75712e6af992102896807d54bc55c24981f24a453c60ad3e8993d693732288068a23df3d9f50d4821029e51a5ef5db3137051de8323b001749932f2ff0d34c82e96a2c2461de96ae56c2102a4e1a9638d46923272c266631d94d36bdb03a64ee0e14c7518e49d2f29bc40102102f8a00b269f8c5e59c67d36db3cdc11b11b21f64b4bffb2815e9100d9aa8daf072103079e252e85abffd3c401a69b087e590a9b86f33f574f08129ccbd3521ecf516b2103111cf405b627e22135b3b3733a4a34aa5723fb0f58379a16d32861bf576b0ec2210318f331b3e5d38156da6633b31929c5b220349859cc9ca3d33fb4e68aa08401742103230dae6b4ac93480aeab26d000841298e3b8f6157028e47b0897c1e025165de121035abff4281ff00660f99ab27bb53e6b33689c2cd8dcd364bc3c90ca5aea0d71a62103bd45cddfacf2083b14310ae4a84e25de61e451637346325222747b157446614c2103cc297026b06c71cbfa52089149157b5ff23de027ac5ab781800a578192d175462103d3bde5d63bdb3a6379b461be64dad45eabff42f758543a9645afd42f6d4248282103ed1e8d5109c9ed66f7941bc53cc71137baa76d50d274bda8d5e8ffbd6e61fe9a5f6702c00fb275522103aab896d53a8e7d6433137bbba940f9c521e085dd07e60994579b64a6d992cf79210291b7d0b1b692f8f524516ed950872e5da10fb1b808b5a526dedc6fed1cf29807210386aa9372fbab374593466bc5451dc59954e90787f08060964d95c87ef34ca5bb5368ae"
DYNAFED_SCRIPT = "75"+LIQUID_SCRIPT # Add non-OP_DEPTH opcode to make parseable non-liquid script


class TweakFedpegTest(BitcoinTestFramework):

Expand All @@ -22,7 +24,8 @@ def set_test_params(self):
"-fedpegscript="+OP_CMS_SCRIPT
],
[
"-fedpegscript="+LIQUID_SCRIPT
"-fedpegscript="+LIQUID_SCRIPT,
"-con_dyna_deploy_start=0" # test dynafed derivation
]]

def setup_network(self):
Expand Down Expand Up @@ -75,9 +78,31 @@ def run_test(self):


# Liquid Daemon example:
assert_equal(self.nodes[2].tweakfedpegscript("0014008b1fdbaec5a82a9322af1640e871727245bce1")["script"],
"745c87635b2103258857ed88024e5296436f851337451d3d68522fbb21dc5a047b965cd2f790932103ab43d77e8706a799cee3c14678574bccaca98c101217369cee987073cd2546492102ee5c8dfdf742bcc2b08386a6a3ec4b6087d39a6fd7e6f0fdeb82ecc30b11d0342102b104b6a469d8b3ccf0a671198e5f7d7010dbf2eb5587733b168f6e0d3be75760210207fc37d4877529fb0d63b1de352689627400a43f18717fa182fe0eea283bdd7921025d8724d82c61708458bc1a08ec9a7fcaf0ab6747ccf0a7d378faea07a261ab3e2102af6859d48d0a4518a4811f12adb974bbd051931e3f96f0b6ae16142c43ae6fd12102458215967f7977effc21b964bd92d870ae7eca98f343801031639150139e63ed2102cdef66bf4b5d26d0be22cabd5761970cfa84be905dc0137ad08ab634f104cb9e210297fb764f808c126f46ce0444124213bdc6a18f2904e75c9144f53bed4c19376e2103c631e77d14a5eb2fc61496ee3e9ea19d1e56e7e65a07aaa786f25ffc2921efde210270addb9011a4b41987b0848a8dc52e5442964fe5188c6a4c9320fbb9016390772103bf9aa75444d0013c46dcf10d70831b75b46a25d901fb7c6360beb2f6cac9c503210278f303dbaad1410a26d99e4dca8925fae4c90532a156c29e8ab1abf5ccaa663d210394cc0983add2dc60aa988c9faedebdc5463106184d72dd430ef24d215daf8b935f6702c00fb275522103aab896d53a8e7d6433137bbba940f9c521e085dd07e60994579b64a6d992cf79210291b7d0b1b692f8f524516ed950872e5da10fb1b808b5a526dedc6fed1cf29807210386aa9372fbab374593466bc5451dc59954e90787f08060964d95c87ef34ca5bb5368ae")
claim_script = "0014008b1fdbaec5a82a9322af1640e871727245bce1"
liquid_tweaked = "745c87635b2103258857ed88024e5296436f851337451d3d68522fbb21dc5a047b965cd2f790932103ab43d77e8706a799cee3c14678574bccaca98c101217369cee987073cd2546492102ee5c8dfdf742bcc2b08386a6a3ec4b6087d39a6fd7e6f0fdeb82ecc30b11d0342102b104b6a469d8b3ccf0a671198e5f7d7010dbf2eb5587733b168f6e0d3be75760210207fc37d4877529fb0d63b1de352689627400a43f18717fa182fe0eea283bdd7921025d8724d82c61708458bc1a08ec9a7fcaf0ab6747ccf0a7d378faea07a261ab3e2102af6859d48d0a4518a4811f12adb974bbd051931e3f96f0b6ae16142c43ae6fd12102458215967f7977effc21b964bd92d870ae7eca98f343801031639150139e63ed2102cdef66bf4b5d26d0be22cabd5761970cfa84be905dc0137ad08ab634f104cb9e210297fb764f808c126f46ce0444124213bdc6a18f2904e75c9144f53bed4c19376e2103c631e77d14a5eb2fc61496ee3e9ea19d1e56e7e65a07aaa786f25ffc2921efde210270addb9011a4b41987b0848a8dc52e5442964fe5188c6a4c9320fbb9016390772103bf9aa75444d0013c46dcf10d70831b75b46a25d901fb7c6360beb2f6cac9c503210278f303dbaad1410a26d99e4dca8925fae4c90532a156c29e8ab1abf5ccaa663d210394cc0983add2dc60aa988c9faedebdc5463106184d72dd430ef24d215daf8b935f6702c00fb275522103aab896d53a8e7d6433137bbba940f9c521e085dd07e60994579b64a6d992cf79210291b7d0b1b692f8f524516ed950872e5da10fb1b808b5a526dedc6fed1cf29807210386aa9372fbab374593466bc5451dc59954e90787f08060964d95c87ef34ca5bb5368ae"
assert_equal(self.nodes[2].tweakfedpegscript(claim_script)["script"], liquid_tweaked)

# Advance to dynamic federations activation, which has pubkeys
# after OP_ELSE get tweaked except the exact liquidv1 template to
# maintain compatibility
self.nodes[2].generate(433)
assert_equal(self.nodes[2].getblockchaininfo()['bip9_softforks']['dynafed']['status'], 'active')
assert_equal(self.nodes[2].tweakfedpegscript(claim_script)["script"], liquid_tweaked)

WSH_OP_TRUE = self.nodes[2].decodescript("51")["segwit"]["hex"]
for i in range(20):
block = self.nodes[2].getnewblockhex(0, {"signblockscript":WSH_OP_TRUE, "max_block_witness":3, "fedpegscript":DYNAFED_SCRIPT, "extension_space":[]})
self.nodes[2].submitblock(block)

assert_equal(self.nodes[2].getblockcount(), 433+20)
# Now it will be different because emergency keys are tweaked as well
# A bunch is the same before the OP_ELSE however
assert_equal(self.nodes[2].tweakfedpegscript(claim_script)["script"][:1050], ("75"+liquid_tweaked)[:1050])
assert self.nodes[2].tweakfedpegscript(claim_script)["script"] != "75"+liquid_tweaked

# Example computed to protect against regressions
dyna_fed_tweaked = "75745c87635b2103258857ed88024e5296436f851337451d3d68522fbb21dc5a047b965cd2f790932103ab43d77e8706a799cee3c14678574bccaca98c101217369cee987073cd2546492102ee5c8dfdf742bcc2b08386a6a3ec4b6087d39a6fd7e6f0fdeb82ecc30b11d0342102b104b6a469d8b3ccf0a671198e5f7d7010dbf2eb5587733b168f6e0d3be75760210207fc37d4877529fb0d63b1de352689627400a43f18717fa182fe0eea283bdd7921025d8724d82c61708458bc1a08ec9a7fcaf0ab6747ccf0a7d378faea07a261ab3e2102af6859d48d0a4518a4811f12adb974bbd051931e3f96f0b6ae16142c43ae6fd12102458215967f7977effc21b964bd92d870ae7eca98f343801031639150139e63ed2102cdef66bf4b5d26d0be22cabd5761970cfa84be905dc0137ad08ab634f104cb9e210297fb764f808c126f46ce0444124213bdc6a18f2904e75c9144f53bed4c19376e2103c631e77d14a5eb2fc61496ee3e9ea19d1e56e7e65a07aaa786f25ffc2921efde210270addb9011a4b41987b0848a8dc52e5442964fe5188c6a4c9320fbb9016390772103bf9aa75444d0013c46dcf10d70831b75b46a25d901fb7c6360beb2f6cac9c503210278f303dbaad1410a26d99e4dca8925fae4c90532a156c29e8ab1abf5ccaa663d210394cc0983add2dc60aa988c9faedebdc5463106184d72dd430ef24d215daf8b935f6702c00fb275522102892d66841804e5ad236f9e012bd8f1082857c9355f83aa4cd64dbe19e487d50821032d090e7b1d5047a3ac626de4b2a18e210501734895c5b7717412dac0234dd97b21026a693f0278011a09d56c04cf6b62ce8253c92a5f03a5010e6633b497ce3989085368ae"
assert_equal(self.nodes[2].tweakfedpegscript(claim_script)["script"], dyna_fed_tweaked)

if __name__ == '__main__':
TweakFedpegTest().main()

0 comments on commit d75d1f3

Please sign in to comment.