The content of the invention
In view of this, the present invention provides a kind of intelligent contract virtual machine realization side for solving or partly solving the above problemsMethod.
To reach the effect of above-mentioned technical proposal, the technical scheme is that:A kind of intelligent contract virtual machine realization sideMethod, is comprised the steps of:
Intelligent contract virtual machine is an ether mill virtual machine, and ether mill virtual machine is a machine for carrying 256 bytesThe virtual machine based on stack of word;Intelligent contract is based on quantum catenary system, can run ether mill, and perform intelligent contractVirtual machine;
Comprising three kinds of command codes related with intelligence contract virtual machine in quantum catenary system, respectively OP_EXEC command codes,OP_EXEC_ASSIGN command codes, OP_TXHASH command codes;
OP_EXEC command codes, for triggering a special trading processing, and it is empty for the ether mill of transmission by performingThe bytecode of plan machine;
OP_EXEC_ASSIGN command codes, are similar to the processing that OP_EXEC command codes are performed, and transmit conjunction for triggeringIntelligent contract is given with address and data, the bytecode of intelligent contract, OP_EXEC_ASSIGN command code energy are performed during transmission dataIt is enough to give intelligent contract by given transfer of funds;Data are expressed as CALLERDATA in intelligent contract virtual machine;
OP_TXHASH command codes, the remainder for coordinating account book level of abstraction, and currently perform the transaction id of transactionCryptographic Hash is also forced into stack;
The specially treated of output script comprising OP_EXEC command codes and OP_EXEC_ASSIGN command codes, so that intelligenceBeing immediately performed for contract can be dissolved into block chain;The process that specially treated is included is:When the script in transaction detects OP_EXEC command codes or OP_EXEC_ASSIGN command codes, by the institute of block chain network after transaction is placed in block chain networkThere is node to be performed;Under the background of specially treated, the script of bit coin transfers data to intelligent conjunction as one kindA kind of about mode of virtual machine;When intelligent contract virtual machine performs OP_EXEC command codes or OP_EXEC_ASSIGN command codes, intelligenceEnergy contract virtual machine can change state in the slip condition database of oneself, i.e., a similar contract is performed on ether mill;
The transaction output of OP_EXEC command codes and OP_EXEC_ASSIGN command codes can be consumed, when transaction outputCode send fund to another contract or another public key cryptographic Hash address, the output of OP_EXEC_ASSIGN command codesConsumed by contract;The output of OP_EXEC command codes is consumed, when whenever intelligent contract is operated oneself using committing suisideRemoved from block chain;
Gas model is built, the value of the execution for weighing ether mill virtual machine operations code, each transaction will spend oneGas in quantitative gas model;After completion of transactions, remaining amount of gas can be returned to the people of transmission, when in contractThe quantity of the gas of execution exceedes the quantity that contract needs, then the state of transaction and action can be resumed;State is resumed,The gas of transaction has also been consumed;The detailed entry of gas is divided into GasLimit and GasPrice;GasLimit is used to recordThe amount of gas that contract is consumed in performing;GasPrice is used for the accurate price for recording each gas cell;In contract execution mostBig quantum is spent to be multiplied equal to GasLimit with GasPrice.
The present invention useful achievement be:The intelligent contract virtual machine realization method of the present invention is ether mill virtual machine (EVM),It can write out more powerful program on ether mill, be responsible for the execution of intelligent contract too in mill block chain, and have benefited fromContract in the too contract performing environment of the network in mill, ether mill is write and performed also therefore becomes very simple.
Embodiment 1:Intelligent contract can run ether mill, and perform intelligent contract virtual machine;Ether mill virtual machine quiltDesigned for the block chain in ether mill, it is therefore assumed that valuable transmission be all based on method based on account.
In Quantum Chain, script could only be performed when an output needs to be spent, and be illustrated, for standardPublic key cryptographic Hash is merchandised, although script is on block chain, and it will not be verified or perform in any way.Perform and verify notCan occur, until a transaction input is with reference to output.From this point of view, if input script can not provide an effective dataTo output script, it is caused to return to 1, transaction will be invalid.
But Quantum Chain, it is necessary to intelligent contract is adapted to, being immediately performed for intelligent contract has been dissolved into block chain, accomplishedThis process must pass through the specially treated to the output script comprising OP_EXEC or OP_EXEC_ASSIGN transaction.When in pinOne of these command codes are detected in this, are performed after transaction is placed in block by all nodes of network.ThisUnder pattern, actual bit coin script, less as script, is to transfer data to ether mill void more strictly speakingA kind of mode of plan machine.When ether mill virtual machine is performed by any one in these command codes, ether mill virtual machine can be withChange state is in the slip condition database of their own, actually just as a similar contract is executed on ether mill.In order toMake that the intelligent contract of Quantum Chain is as easy to use as possible, we must send authentication data as specific public key cryptographic Hash addressTo intelligent contract and its founder.
Relative to UTXO models, ether mill is a system based on accounts;Accounts model is related to elaborateIt is as follows:
In ether mill, state is made up of the object for being referred to as " accounts ", and each accounts have the address of 20 bytes, stateConversion transmission value and information directly between accounts;Ether mill accounts include four parts:
Nonce random numbers, are to ensure that every transaction can only handle counter once;
Current ether mill remaining sum in accounts;
The contract code of accounts;
The storage (being defaulted as sky) of accounts;
" ether " is the main inside encryption fuel in ether mill, is generally used for payment transaction expense;In general, have two hereThe account of type:The accounts and contract accounts of the outside owner, the accounts of the outside owner are by private key control, contract accountsControlled by their contract code;
The accounts that outside possesses do not have code, and can be disappeared by being sent in setting up and signing the accounts that transaction possesses from outsideBreath.In contract accounts, whenever contract accounts receive a code activation information, it is allowed to which its reading and writing internal is stored and sentOther message create contract successively.
In the system in ether mill, the balance of account is managed in accounting system, and any increasing of wherein account balance adds deductLess by more like the bank account in real world, and each, the generation of new block is possible to the purpose global state generation that can check accountInfluence.
Each accounts have the remaining sum of oneself, and storage and space encoder based on contract can be referred to as accounts or address, pass throughMemory storage implementing result.In current ether mill accounting system, user can be assisted by client/remote procedure callView carries out one-to-one transaction, it means that each transaction can only be generated from an accounts into another accounts every time.To the greatest extentPipe is sent to more accounts possibly through intelligent contract, and these insider transactions can only be shown in the remaining sum of each accounts,It is difficult to follow the trail of them on the open account book in ether mill.
Embodiment 2:Gas model
The subject matter faced in Quantum Chain for the clever completeness of figure can be increased into the block chain of bit coin, be specially fromThe size of transaction can be not solely depended in theory to determine the suitable expense of miner's cost.The reason for causing above-mentioned generation is oneIndividual simple, small transaction when miner is acted on block chain in can be absorbed in Infinite Cyclic, and cause whole block chainShut down.What gas model created to solve the above problems.
There is a value in the execution that the concept of gas model is summarized as each ether mill virtual machine operations symbol, andEach transaction is required for spending a certain amount of gas.After completion of transactions, remaining amount of gas can be returned to the people of transmission.When the quantity of the gas performed in contract exceedes the quantity that contract needs, then the state of transaction and action can be resumed.That mean it is any required changed permanently store and can be restored to initial state, and the contract expense of any cost also canIt is resumed so that they will not be spent.
It is reduced although institute is stateful, all gas of transaction are consumed and give miner to transactional operation.Reason is that computing resource has been spent during miner operates, so while causing state transformation not to be in block chainVery safe, the process of operation has been spent, and is spent by miner.
Although Quantum Chain uses the gas model in ether mill, it is contemplated that gas plan (each ether mill virtual machineThe gas price of operation) made a big difference with ether mill.Reason is the certain operations in Quantum Chain than the operation in ether millExpensive, some are cheap.Accurate price is determined, and is the price present in ether mill and is compared command code in Quantum ChainThe quantity of the block chain resource needed is determined.
When needing to create a contract fund or deployment transaction, user needs to formulate the detailed entry of gas:
First detailed entry is GasLimit, for recording the amount of gas consumed in contract execution.
Second detailed entry is GasPrice, the accurate price for recording each gas cell;
Maximum quantum cost is multiplied by GasLimit to GasPrice during contract is performed.If maximum takes overTranaction costs, then transaction is identified incorrect and will not be by digging ore deposit and operation.The calculation of transaction size expenseSubtract each other for remaining tranaction costs with maximum transaction, this calculating process is similar to standard bits coin cost model.
For the proper priority determined deal, miner has to decide on two variables.
First, transaction size expense is the scale of All Activity, and this calculates minimum coin number by each bit common recognitionAmount, and use in this course per kilobytes computing formula.
Second variable is that gas price GasPrice contract is performed.
Share proves there is very big in most important, the best transaction that (POS) miner includes and handled within a blockSelection autonomy.
This allows cost model to be worked as a free market, can optimize in miner among these and userBest expense, when this best expense is paid for, can adapt to the speed and price in transaction.
(a) reimbursement
Using UTXO models, it is not negotiable that fund is sent to miner as tranaction costs.If transaction is for ore depositWork is easier processing than anticipation, then miner can not be allowed to return expense.However, because gas model is useful, necessarilyThere is method can return sender by expense.
In addition, the mode stopped stateful transaction using there must be when finishing when gas, and can be by gas feesIt is sent to miner.It is that, by creating new output, and will export coin basis as miner that gas fees are returned in Quantum ChainTransaction.
The rule of one new block checking common recognition is also created, and ensures that these outputs returned are handed on coin basisExist in easily.Otherwise, miner can select not returning these funds.
For reimbursement purpose, at this moment the sender of transaction funds is considered as the reference output of first input, is only passed throughReplicate output script and return to reimbursement.Now, for reasons of safety, this output script is only pay-to-Pubkeyhash or pay-to-scripthash scripts.When safety needs really, related constraint can be listed.
Only for reference, OP_EXEC_ASSIGN command codes have following form, for distribution of fund contract:
Input:With the order of press-in:
Transaction cryptographic Hash (selectable) for cost;
Version number (VM versions, generally 1);
The limitation gas limit (quantity that maximum gas can be used in commission) of gas;
Gas price (quantum quantity in each gas cell);
Data (need to be sent to the data of intelligent contract);
Intelligent contract address;
Output is (with the order jumped out;)
Spendable (if present expense can be spent);
So, EXEC_ASSIGN example can be included in as follows:
1
10000
100
0xABCD1234...
3d655b14393b55a4dec8ba043bb286afa96af485
EXEC_ASSIGN
If the result that virtual machine is performed is the output abnormality of gas, this output will be spent with redeeming script.From vin[0] the pubkeyhash scripts extracted in .prevout scripts are the input produced during this is merchandised.Version early stage Quantum ChainIn this, only pubkeyhash senders can just be allowed in virtual machine transaction.
Although can result in the execution of virtual machine by the extended formatting that block chain allows, in the virtual machine of ether millMsg.sender value will be arranged to 0, and the output of any gas and the reimbursement of gas will cause contract to keep thisA little funds.
(b) partial refund model
For the other parts of gas model, it is necessary to return the gas not spent.This is to allow the people can to promise to undertake costSubstantial contribution ensures the correct execution of their contract, but their gases that do not use are returned as Quantum Chain and moved backMoney.The return address of gas is sent vin [0] .prevout scripts of transaction described in block chain.By using standardBit coin transaction charge mechanism, gas is sent to contract.
So, new charging model discusses the description for how being traded expense:
Gas_fee=gas_limit*gas_price
Txfee=vin-vout+gas_fee+tx_relay_fee
Refund=txfee-used_gas-tx_relay_fee
Note that this is one single " in the case that credit_price " values can determine transaction priority, make ore depositWork can assess tx_relay_fee and gas_price suggestion.
Since contract is performed, gas label removes (by being multiplied by gas price) from whole expense.In holding for contractAfter the completion of row, the expense gas_fee of gas residue is returned to given gas and returns to script.This should be exported by increasingTo the transaction of coin basis (miner goes to retrieve their block reward using transaction).
Increase to the transaction of coin basis and be output as the pubkeyhash from vin [0] .prevout.In order to obtain gasReimbursement, it is necessary to exist pubkeyhash virtual output go spend.Otherwise, gas reimbursement is preserved (in the defeated of gas in minerUnder the conditions of going out, transmission and the contract of expense coexist).Notice that the possible only one of which ether mill virtual machine contract of each transaction is heldOK, it is impossible to which two contracts, which are performed, all attempts to shared tranaction costs.Multiple EVM that each merchandise, which are performed, can bring some to askTopic.
(c) important gas edge case
Miner must be noted that contract gas/fund returns to script.If gas returns to script, output can cause block to exceedMaximum block scale, then contract transaction will not place into block, and when next block is by digging ore deposit, its execution is certainCan occur.Before attempting to perform contract, miner needs to confirm to have enough positions in candidate block always to provideScript is returned to gas.If it find that reimbursement script is not suitable for current block, if not following this rule then, needs are may result inPerform the contract more than once.If returned without gas fees, in the absence of in order to return to the output of fund.Wrapped in tranaction costsGas_fee containing gas fees is the key of common recognition.If a transaction, which attempts to one meeting of increase, causes bad gas reimbursementBlock, or gas_fee is fewer than txfee, then this transaction is invalid, and the block for possessing transaction is also invalid.Not yetIt is effective to have transaction output script, there is more than one OP_EXEC operations or OP_EXEC_ASSIGN operations.These can be limitedThe ability of script, but recurrence/a variety of executive problems can be made more disposable.Under this approach, static analysis can be easy toGround judges whether script is invalid, rather than needs perform script to determine it.
The preferred embodiments of the invention is the foregoing is only, the claims of the present invention are not limited to.Simultaneously it is described above, for those skilled in the technology concerned it would be appreciated that and implement, therefore other be based on institute of the present inventionThe equivalent change that disclosure is completed, should be included in the covering scope of the claims.
The present invention useful achievement be:The intelligent contract virtual machine realization method of the present invention is ether mill virtual machine (EVM),It can write out more powerful program on ether mill, be responsible for the execution of intelligent contract too in mill block chain, and have benefited fromContract in the too contract performing environment of the network in mill, ether mill is write and performed also therefore becomes very simple.