1#!/usr/bin/env python3 2# Copyright (c) 2015-2016 The Bitcoin Core developers 3# Distributed under the MIT software license, see the accompanying 4# file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 6from test_framework.test_framework import BitcoinTestFramework 7from test_framework.util import * 8from test_framework.script import * 9from test_framework.mininode import * 10from test_framework.qtum import * 11from test_framework.qtumconfig import * 12import sys 13 14class OpCreateTest(BitcoinTestFramework): 15 def set_test_params(self): 16 self.setup_clean_chain = True 17 self.num_nodes = 2 18 self.extra_args = [['-txindex=1']]*2 19 20 def skip_test_if_missing_module(self): 21 self.skip_if_no_wallet() 22 23 # Creates a simple contract via a raw tx 24 def basic_contract_is_created_raw_tx_test(self): 25 for i in range(2): 26 assert(len(self.nodes[i].listcontracts()) == 0+NUM_DEFAULT_DGP_CONTRACTS) 27 node = self.nodes[0] 28 amount = 10*COIN 29 30 """ 31 pragma solidity ^0.4.11; 32 contract Example { 33 function () payable {} 34 } 35 """ 36 tx = make_transaction(node, 37 [make_vin(node, 500000*QTUM_MIN_GAS_PRICE)], 38 [make_op_create_output(node, 0, 4, CScriptNum(500000), CScriptNum(QTUM_MIN_GAS_PRICE), bytes.fromhex("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a72305820e3bed070fd3a81dd00e02efd22d18a3b47b70860155d6063e47e1e2674fc5acb0029"))] 39 ) 40 #node.createcontract("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a72305820e3bed070fd3a81dd00e02efd22d18a3b47b70860155d6063e47e1e2674fc5acb0029"); 41 node.sendrawtransaction(tx) 42 node.generate(1) 43 sync_blocks(self.nodes) 44 # for i in range(2): 45 # assert(len(self.nodes[i].listcontracts()) == 1) 46 47 # Verifies that large contracts can be deployed 48 def large_contract_creation_test(self): 49 node = self.nodes[0] 50 """ 51 contract Factory { 52 bytes32[] Names; 53 address[] newContracts; 54 function createContract (bytes32 name) { 55 address newContract = new Contract(name); 56 newContracts.push(newContract); 57 } 58 function getName (uint i) { 59 Contract con = Contract(newContracts[i]); 60 Names[i] = con.Name(); 61 } 62 } 63 contract Contract { 64 bytes32 public Name; 65 function Contract (bytes32 name) { 66 Name = name; 67 } 68 function () payable {} 69 } 70 """ 71 node.createcontract("606060405234610000575b61034a806100196000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633f811b80146100495780636b8ff5741461006a575b610000565b3461000057610068600480803560001916906020019091905050610087565b005b3461000057610085600480803590602001909190505061015b565b005b60008160405160e18061023e833901808260001916600019168152602001915050604051809103906000f08015610000579050600180548060010182818154818355818115116101035781836000526020600020918201910161010291905b808211156100fe5760008160009055506001016100e6565b5090565b5b505050916000526020600020900160005b83909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550505b5050565b6000600182815481101561000057906000526020600020900160005b9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690508073ffffffffffffffffffffffffffffffffffffffff16638052474d6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050602060405180830381600087803b156100005760325a03f1156100005750505060405180519050600083815481101561000057906000526020600020900160005b5081600019169055505b50505600606060405234610000576040516020806100e1833981016040528080519060200190919050505b80600081600019169055505b505b609f806100426000396000f30060606040523615603d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638052474d146045575b60435b5b565b005b34600057604f606d565b60405180826000191660001916815260200191505060405180910390f35b600054815600a165627a7a723058209bd85f2ac8e941766991a59f8a0183902c8168f671bfdb430ad9eab85fd697b70029a165627a7a72305820ed128e4929006fce038bc859dd8837890e7ee8d296cd3ed30b66603a8423397e0029", 1000000) 72 block_height = node.getblockcount() 73 node.generate(1) 74 sync_blocks(self.nodes) 75 for i in range(2): 76 assert(self.nodes[i].getblockcount() == block_height+1) 77 assert(len(self.nodes[i].listcontracts()) == 2+NUM_DEFAULT_DGP_CONTRACTS) 78 79 80 # Tests mining many contracts in one block 81 def many_contracts_in_one_block_test(self): 82 node = self.nodes[0] 83 num_new_contracts = 25 84 for _ in range(num_new_contracts): 85 """ 86 pragma solidity ^0.4.10; 87 contract Example { 88 function () payable {} 89 } 90 """ 91 node.createcontract("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a7230582092926a9814888ff08700cbd86cf4ff8c50052f5fd894e794570d9551733591d60029") 92 93 block_height = node.getblockcount() 94 node.generate(1) 95 sync_blocks(self.nodes) 96 for i in range(2): 97 assert(self.nodes[i].getblockcount() == block_height+1) 98 assert(len(self.nodes[i].listcontracts(1, 10000)) == 2+num_new_contracts+NUM_DEFAULT_DGP_CONTRACTS) 99 100 # Checks that contracts are removed if the block it was mined in was invalidated 101 def contract_reorg_test(self): 102 node = self.nodes[0] 103 num_old_contracts = len(node.listcontracts(1, 1000)) 104 old_block_height = node.getblockcount() 105 """ 106 pragma solidity ^0.4.10; 107 contract Example { 108 function () payable {} 109 } 110 """ 111 node.createcontract("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a7230582092926a9814888ff08700cbd86cf4ff8c50052f5fd894e794570d9551733591d60029") 112 new_block_hash = node.generate(1)[0] 113 assert_equal(node.getblockcount(), old_block_height+1) 114 assert_equal(len(node.listcontracts(1, 1000)), num_old_contracts+1) 115 node.invalidateblock(new_block_hash) 116 assert_equal(node.getblockcount(), old_block_height) 117 assert_equal(len(node.listcontracts(1, 1000)), num_old_contracts) 118 119 def gas_limit_signedness_test(self): 120 node = self.nodes[0] 121 num_old_contracts = len(node.listcontracts(1, 1000)) 122 123 """ 124 pragma solidity ^0.4.10; 125 contract Example { 126 function () payable {} 127 } 128 """ 129 tx = make_transaction(node, 130 [make_vin(node, 0xffff*1000)], 131 # changing the gas limit \xff\xff -> \xff\xff\x00 results in success. 132 [make_op_create_output(node, 0, b"\x04", b"\xff\xff", 1000, bytes.fromhex("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a7230582092926a9814888ff08700cbd86cf4ff8c50052f5fd894e794570d9551733591d60029"))] 133 ) 134 try: 135 node.sendrawtransaction(tx) 136 assert(False) 137 except: 138 pass 139 140 141 def gas_limit_signedness_2_test(self): 142 node = self.nodes[0] 143 num_old_contracts = len(node.listcontracts(1, 1000)) 144 145 """ 146 pragma solidity ^0.4.10; 147 contract Example { 148 function () payable {} 149 } 150 """ 151 tx = make_transaction(node, 152 [make_vin(node, 2*0xffff*1000)], 153 # changing the gas limit \xff\xff -> \xff\xff\x00 results in success. 154 [make_op_create_output(node, 0, b"\x04", b"\xff\x4f", 1000, bytes.fromhex("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a7230582092926a9814888ff08700cbd86cf4ff8c50052f5fd894e794570d9551733591d60029")), 155 make_op_create_output(node, 0, b"\x04", b"\xff\xff", 1000, bytes.fromhex("60606040523415600b57fe5b5b60398060196000396000f30060606040525b600b5b5b565b0000a165627a7a7230582092926a9814888ff08700cbd86cf4ff8c50052f5fd894e794570d9551733591d60029"))] 156 ) 157 try: 158 node.sendrawtransaction(tx) 159 assert(False) 160 except: 161 pass 162 163 164 def run_test(self): 165 connect_nodes(self.nodes[0], 1) 166 generatesynchronized(self.nodes[0], COINBASE_MATURITY+40, None, self.nodes) 167 self.vins = [make_vin(self.nodes[0], 10*COIN) for _ in range(10)] 168 self.basic_contract_is_created_raw_tx_test() 169 self.large_contract_creation_test() 170 self.many_contracts_in_one_block_test() 171 self.contract_reorg_test() 172 self.gas_limit_signedness_test() 173 self.gas_limit_signedness_2_test() 174 175if __name__ == '__main__': 176 OpCreateTest().main() 177