连载:Qtum量子链设计文档(四):POS机制设计和实现

量子链Qtum2018-06-14 10:51:33  阅读 -评论 0

连载:Qtum量子链设计文档(四):POS机制设计和实现点击上方蓝字关注QTUM公众号,了解最新行业资讯

连载:Qtum量子链设计文档(四):POS机制设计和实现

Qtum原始设计文档汇总(4)-- PoS & MPoS(互惠权益证明共识机制)

Qtum量子链是首个基于UTXO模型的,采用PoS共识机制且兼容多种虚拟机(包括以太坊EVM)的智能合约平台。在前面几篇文章中,我们回顾了Qtum关于UTXO,EVM,AAL等相关设计文档。本章将介绍Qtum采用的共识机制MPoS(互惠权益证明机制)。

MPoS在PoS3.0版本的基础上,修改了区块奖励的返还方式,增强了安全性,从而使PoS机制能够真正用于图灵完备的智能合约系统中。

Qtum自发布以来,不少开发者和钱包的使用者都问过类似的问题:什么叫staking?为什么stake的币要等500个区块才能继续staking?为什么矿工只得到了0.4QTUM的奖励?……

这些其实都和本章介绍的MPoS共识机制有关。

以下截取部分早期Qtum开发团队针对PoS和MPoS的相关原始设计文档(附中文翻译)(ps:文档中QTUM-<#>或QTUMCORE-<#>为内部设计文档编号):

QTUMCORE-4:Add PoS block verification and mining Description:After this story the Qtum blockchain should be capable of verifying basic PoS v3 blocks, and the wallet should be able to mine these blocks similar to PoW blocks. There is not a network rule as of yet for only allowing PoS blocks at this point, so the chain will accept either PoW and PoS blocks as valid. Both block types should use the same default bitcoin standard difficulty. Thing to ensure: stake modifier changes every block   Coinage does not change PoS values No decentralized checkpoints (still in design) Make sure that these statements remain true: don't enforce tx fee as consensus rule static pos reward (make it just 1 token for now, will change later) stake modifier v2 - The purpose of stake modifier is to prevent a txout (coin) owner from computing future proof-of-stake generated by txout at the time of transaction confirmation. Verification of the block was changed, now you need only previous block, so all other blocks can be deleted and space saved (you need last 500 blocks to stake) pruning capabilities should be in future major release compatibility with BIP66 only last 500 blocks are needed to verify proof of stake No coin age, instead just a coin confirmation requirement (500 blocks is fine for now) References: https://nm.reddit.com/r/blackcoin/comments/3x9yy6/claims_of_an_unprecedented_pos_algo_by/cy358dg/ https://bravenewcoin.com/assets/Whitepapers/blackcoin-pos-protocol-v2-whitepaper.pdf task:添加PoS区块验证和挖矿功能 描述:该任务完成后,Qtum区块应该能够验证基本的PoS v3区块,并且钱包能像挖PoW区块一样进行PoS挖矿。目前还没有规定是只允许PoS区块,因此对于PoW和PoS区块都将被认为是有效的。这两种区块类型应该使用相同的默认的比特币标准难度。 注意确保以下内容: 每个区块的stake modifier都会发生改变 coinage(币龄)不会改变PoS哈希值 无去中心化的checkpoints(仍然在设计) 注意确保以下事实成立: •   不强制将交易费用作为共识规则 •   静态的PoS奖励(暂时将它的值设置为1个token,后面会进行修改) •   stake modifier v2 -- stake modifier的目的是防止txout(币)owner计算未来的在交易确认的时候txout生成的PoS。修改了区块的验证方式,现在你只需要上一个(包含用于stake的txout的)区块,其他区块可以删除,未来版本中可以有删除不必要区块以节省存储空间的能力(需要最后500个区块来stake)。 •   和BIP66兼容 •   只需要最近的500个区块用于验证PoS •   没有币龄,只需要币达到一定的确认数(目前500个区块是可以的)就可以进行PoS挖矿。 参考: https://nm.reddit.com/r/blackcoin/comments/3x9yy6/claims_of_an_unprecedented_pos_algo_by/cy358dg/ https://bravenewcoin.com/assets/Whitepapers/blackcoin-pos-protocol-v2-whitepaper.pdf

QTUMCORE-16: Change mainnet to only allow PoS after a certain blockheight Description: None Task:修改主网,使得在一定的区块高度之后只允许PoS

上述两个任务首次将PoS共识机制加入到Qtum区块链中,描述了关于PoS区块的验证及生成的最基本规则,同时增加了在一定区块之后只允许PoS挖矿的共识。

QTUMCORE-27: Consensus/chain parameters for PoS Description: The consensus provide parameters that will affect the creation of new blocks in the network. The PoS limit need to be added in order to determine the initial difficulty for PoS. The halving interval is 4 years and there will be 7 halving intervals. The last PoW block need to be defined and set to 5000 for the main net. The reward for PoW block is 20000 coins, the reward for PoS block is 4 coins. Task:PoS的共识参数 描述:共识提供一些参数,这些参数会影响网络中新区块的创建。 为了决定PoS的初始难度,需要添加PoS限制。 挖矿奖励减半的时间间隔是4年,并且将会发生7次减半。 最后一个PoW区块需要定义,对于主网络该参数设置为5000。PoW区块的奖励是2万个QTUM,PoS区块奖励是4个QTUM。

上述任务规定了PoS区块的难度以及区块奖励的共识参数。

QTUMCORE-28: Update of block/block-header parameters for PoS Description: The general rule for the header is to keep it as small as possible. Four parameters are needed for PoS in order to work: Block signature, that is the signature of the whole block, signed by the block creator Block type (Proof Of Work or Proof Of Stake) Previous stake location, need in order to confirm the block validity, mandatory for PoS Staking time, the time when the staking transaction was created, mandatory for PoS Block signature must be a parameter. The parameter need to be in the header due to the segregated witness which split the block delivery into header and transactions that can be required from any user. The other parameters can be extracted from the PoS transaction. Whether or not to include those 3 parameters in the header too can be discussed. The block header is downloaded before the whole block, not including those parameters in the header will prevent doing PoS checks to the header before downloading the whole block. Blackcoin download the whole block before the checks and put those parameters into the Block. Task:更新PoS的区块/区块头参数 描述:对于区块头,一般原则是使它尽量的小。 为了PoS能够工作,需要以下4种参数: •   区块签名,这是整个区块的签名,由区块的创建者进行签名 •   区块类型(PoW还是PoS) •   前一个stake位置,需要这个参数是为了确认区块的有效性,该参数对于PoS是必须的 •   staking时间,staking交易创建的时间,该参数对于PoS是必须的 区块签名是一个必要参数。该参数必须在区块头中,因为隔离验证(segregated witness)将区块传递分为区块头和任何用户都能请求的交易数据。 其他参数可以从PoS交易中提取。可以讨论一下区块头中是否要包含另外3个参数。区块头是在整个区块之前被下载的,在下载整个区块之前,区块头中不包含那些参数会阻碍区块头的PoS检查。Blackcoin在检查前下载整个区块,并将那些参数放入区块中。

PoS区块与原本的PoW区块在区块头信息上不同,上述任务具体描述并实现了PoS区块头特有的一些参数。

QTUMCORE-29: Proof of Work/Stake kernel update Description: The source code for PoW and PoS kernel is kept into the files: pos.h pos.cpp pow.h pow.cpp We need to decide if we will keep it or use different structure for organization of the code for PoW and PoS kernel. The version of the stake modifier that will be used is the second version. Block difficulty, masks, modifiers, time-stamps are all used hare, so this is important parts for PoS. Task:PoW/PoS内核更新 描述:PoW和PoS内核的源代码在下列文件中: •   pos.h •   pos.cpp •   pow.h •   pow.cpp 我们需要决定是保留它,还是使用不同的架构来组织PoW和PoS内核的代码。 将使用的stake modifier版本为第二个版本。 区块difficulty,masks,modifier,time-stamps(时间戳)这里都使用了,这些都是PoS的重要部分。

上述任务描述了Qtum对PoW、PoS内核代码的更新。

QTUMCORE-33:Add time-stamp in Transaction for PoS Description:The time-stamp is the time that the transaction has been created. It is used for PoS to avoid including transactions in the block that have time bigger then the block time. Transaction with bigger time will eventually be include in the chain when the value for the time become less then the one from the block time. The other uses for the parameter is to determine the staking time for the block (the time of PoS transaction). Task:为PoS添加交易的time-stamp(时间戳) 描述:时间戳是交易被创建的时间。在PoS中,它用于避免在区块中包含那些交易时间晚于区块时间的交易。那些交易时间晚于区块生成时间的交易最终会被包含在后续的区块中。 该参数还可以用于确定区块的staking时间(PoS交易的时间)。

上述任务为交易加上时间戳,从而避免了交易的生成时间晚于区块生成时间。

QTUMCORE-34: Signing PoS block Description: The creator of new PoS block need to sign the block so the other miners can verify that the block is created from the miner and check its validity. According to PoS 3, the first transaction is empty in case of PoS. The second transaction is the PoS transaction in the block. The block signature is empty in case of PoW - the first transaction in the block is not empty for PoW. The block version should be bumped to 2 for all PoS blocks Task:对PoS区块进行签名 描述:新的PoS区块的创建者需要对该区块进行签名,以便其他的矿工能够验证该区块是由这个矿工创建的并验证区块的有效性。 根据PoS 3,PoS中的第一个交易是空的。区块中的第二个交易是PoS交易。 在PoW的情况下,区块签名是空的 -- 对于PoW,区块的第一个交易不是空的。 对于所有的PoS区块,区块的版本应该为2。

上述任务描述了PoS区块与PoW区块的不同之处,并规定了如何对PoS区块进行签名。

QTUMCORE-35: Creating new PoS block Description: The first transaction in PoS block is empty. The second transaction in PoS block contain the PoS transaction. The Coins are considered as available if the transaction outputs that create them are matured. List of available coins for staking need to be created, UTXO from the list will be chosen and used to create the staking transaction. The staking coins are used to create the PoS transaction. It is special type of transaction that the output is bigger then the input, that mean if the input coins were 1000, the output coins will be 1001 coins due to the reward. Normal transaction have outputs that are smaller or equal to the inputs, PoS transaction is an exception from that rule. If the stake is big, for PoS big stake is considered more then 200 coins, then the output will contain two entries, for example: Input is 1000, outputs are 500.5 and 500.5. This parameter should be easily adjusted later (as it might need to be) After creating the PoS transaction, the block is filled with transactions and is signed. Task:创建新的PoS区块 描述:PoS区块的第一个交易是空的。PoS区块的第二个交易包含PoS交易。 如果创建币的交易输出已经成熟,那么这些币被认为是可用于staking的。用于staking的可用币的列表需要被创建,列表中的UTXO将被选择并用于创建staking交易。 Staking币用于创建PoS交易。这是一种特殊的交易类型,输出可以比输入大,这意味着如果输入的币为1000个,由于挖矿奖励,输出的币可以为1001个。正常的交易的输出是小于或等于输入的,PoS交易则是一个例外。 如果stake很大,对于PoS,大的stake可以考虑为超过200个币,那么输出将包括2条记录。例如,输入为1000,输出为500.5和500.5。这个参数后面应该很容易调整(可能需要这样做)。 创建PoS交易后,再向区块中加入其它交易并进行签名。

上述任务描述了新PoS区块的生成规则,并描述了coinstake这种特殊交易,用于奖励区块生成者。

QTUMCORE-36: Accepting blocks for PoS Description: The block header need to be checked when received. The whole block need to be checked before accepting it (when the block was created by the miner or received from other chains ...). When processing PoS blocks we need to handle duplicate hashes and orphan blocks. Task:接受PoS区块 描述:接受区块时需要检查区块头。 在接受区块前整个区块都需要检查(当区块被矿工创建或从其他节点接收时…) 处理PoS区块时,我们需要处理重复哈希和孤块问题。

新区块生成后需要全网广播,上述任务描述了当节点接收到来自网络中的新区块时,应该检查区块,并对特殊情况进行处理。

QTUMCORE-37: Coins serialization and transaction undo for PoS Description: The coins are saved when a transaction is considered as accepted (the block it pushed to the main chain) so they will not be able to be double spent. The formula for saving coins need to be updated in order to provide flag to the coins that belong to CoinStake transaction. They will not be spendable until they mature. The top of the chain might change if someone create a block that have more work. So in that case undo is needed to the transactions of the previous block. Update of the formula will be needed to include the CoinStake transactions too in that process. Task:币序列化以及PoS交易撤销 描述:当交易被认为是可接受的时,币被保存(即被写到主链上),因此这些币不能被双花。 保存币的公式需要更新,以便可以给属于CoinStake交易的币提供flag。这些币在成熟前是不能花费的。 如果有人创建了一个具有更多工作量的区块,链的顶部可能会改变。因此在这种情况下,需要撤销前一个区块的交易。在这个过程中,公式的更新需要包含CoinStake交易。

在交易被接受之后,它就会被写到最长主链上。上述任务描述了用于stake的QTUM在成熟之前是无法被使用的。当分叉出现更长主链时,需要注意撤销交易,这时会包括coinstake交易的撤销。

QTUMCORE-38:RPC update for PoS information Information about the PoS parameters need to be provide like the Difficultly, the type of mining used in the Block (Proof Of Work or Proof Of Stake), the reward for the PoS transaction. Specifically change to match Blackcoin: {code:javascript} earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getinfo {    "version" : "v1.2.2-1-g843ca04",    "protocolversion" : 60018,    "walletversion" : 60000,    "balance" : 0.00000000,    "newmint" : 0.00000000,    "stake" : 0.00000000,    "blocks" : 24972,    "timeoffset" : 0,    "moneysupply" : 74478849.88019848,    "connections" : 2,    "proxy" : "",    "ip" : "0.0.0.0",    "difficulty" : {        "proof-of-work" : 242.36806749,        "proof-of-stake" : 0.49991990    },    "testnet" : false,    "keypoololdest" : 1490022927,    "keypoolsize" : 101,    "paytxfee" : 0.00010000,    "mininput" : 0.00000000,    "errors" : "" } earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getdifficulty {    "proof-of-work" : 242.36806749,    "proof-of-stake" : 0.26817940 } earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getmininginfo {    "blocks" : 36985,    "currentblocksize" : 0,    "currentblocktx" : 0,    "difficulty" : {        "proof-of-work" : 242.36806749,        "proof-of-stake" : 0.36442549,        "search-interval" : 0    },    "blockvalue" : 1000000000000,    "netmhashps" : 0.00000000,    "netstakeweight" : 21304538.19162334,    "errors" : "",    "pooledtx" : 0,    "stakeweight" : {        "minimum" : 0,        "maximum" : 0,        "combined" : 0    },    "stakeinterest" : 1000000,    "testnet" : false } earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getstakesubsidy error: {"code":-1,"message":"getstakesubsidy <hex string>\nReturns proof-of-stake subsidy value for the specified coinstake."} earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getsubsidy 1000000000000 earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getstakinginfo {    "enabled" : true,    "staking" : false,    "errors" : "",    "currentblocksize" : 0,    "currentblocktx" : 0,    "pooledtx" : 0,    "difficulty" : 0.22945304,    "search-interval" : 0,    "weight" : 0,    "netstakeweight" : 16341449,    "expectedtime" : 0 } {code} task:与PoS信息相关的RPC更新 描述:PoS相关的参数和信息可以通过新的RPC调用获得,例如difficulty,区块中使用的挖矿类型(PoW或PoS),PoS交易的奖励。 为匹配Blackcoin具体的修改如下: {code:javascript} earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getinfo {    "version" : "v1.2.2-1-g843ca04",    "protocolversion" : 60018,    "walletversion" : 60000,    "balance" : 0.00000000,    "newmint" : 0.00000000,    "stake" : 0.00000000,    "blocks" : 24972,    "timeoffset" : 0,    "moneysupply" : 74478849.88019848,    "connections" : 2,    "proxy" : "",    "ip" : "0.0.0.0",    "difficulty" : {        "proof-of-work" : 242.36806749,        "proof-of-stake" : 0.49991990    },    "testnet" : false,    "keypoololdest" : 1490022927,    "keypoolsize" : 101,    "paytxfee" : 0.00010000,    "mininput" : 0.00000000,    "errors" : "" } earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getdifficulty {    "proof-of-work" : 242.36806749,    "proof-of-stake" : 0.26817940 } earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getmininginfo {    "blocks" : 36985,    "currentblocksize" : 0,    "currentblocktx" : 0,    "difficulty" : {        "proof-of-work" : 242.36806749,        "proof-of-stake" : 0.36442549,        "search-interval" : 0    },    "blockvalue" : 1000000000000,    "netmhashps" : 0.00000000,    "netstakeweight" : 21304538.19162334,    "errors" : "",    "pooledtx" : 0,    "stakeweight" : {        "minimum" : 0,        "maximum" : 0,        "combined" : 0    },    "stakeinterest" : 1000000,    "testnet" : false } earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getstakesubsidy error: {"code":-1,"message":"getstakesubsidy <hex string>\nReturns proof-of-stake subsidy value for the specified coinstake."} earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getsubsidy 1000000000000 earlz@earlz-ubdev:~/blackcoin/src$ ./blackcoind getstakinginfo {    "enabled" : true,    "staking" : false,    "errors" : "",    "currentblocksize" : 0,    "currentblocktx" : 0,    "pooledtx" : 0,    "difficulty" : 0.22945304,    "search-interval" : 0,    "weight" : 0,    "netstakeweight" : 16341449,    "expectedtime" : 0 } {code}

在实现了基本的PoS共识机制后,需要提供RPC接口供外部获取PoS相关信息。上述任务或增加或增强了对应RPC调用,使外部能够获取准确的PoS相关信息。

QTUMCORE-61:Implement Mutualized Proof Of Stake Block Reward System Description:Mutualized Proof Of Stake (MPoS) is a new reward system designed to make DoS attacks on Qtum more expensive for the attacker. Goals 1. Prevent malicious miners from attacking the network for free by constructing expensive to validate blocks, and then receiving all of the gas fees back to themselves through the mining process 2. Help to make it more difficult and expensive for an attacker to DoS the network Procedure 1. When a staker mines a block, he receives only a small portion of the PoS reward and gas fees. The rest of the reward and fees are shared with 9 other people. 2. When a staker mines a block, his stake script (staketx.vout[0]) is registered to receive a share of the reward, lasting 10 blocks, 500 blocks from when the block was mined 3. Thus, every block there will be 10 reward recipients. The creator of the block, and 9 "mutual stakers". 4. After 9 blocks of shared rewards, the staker's script will be removed, and another will be added to replace it 5. If a stake script has mined more than 1 block in a 10 block period, then there can be a case where he receives 2x the share. However, once the earliest stake script instance exceeds 510 blocks from it's mined block, it is dropped and the reward drops to normal. Identical stake scripts should not be combined into a single UTXO, the rewards should be duplicated 6. In order to prevent a malicious or careless staker from mining contracts that have either a free, or very cheap gas price, a "Minimum Gas Price DGP" feature will be created in a later story, so that a minimum gas price can be made. This would be increased in times of exploits and crisis, and decreased in times where the gas price becomes repressive and makes using contracts too expensive. Example 1. There is a staker, X, who mines blocks 1000 2. X receives 1/10th of the block reward and gas fees for the block. The staking transaction includes at least 9 other outputs from the other mutual stakers 3. X mines another block at height 1004 4. X receives 1/10th of the block reward and gas fees for the block. The staking transaction includes at least 9 other outputs from the other mutual stakers 5. X then doesn't mine anymore blocks for the duration of the example 6. At blocks 1500, 1501, 1502, and 14503, X receives a UTXO for each block for 1/10th of the block reward and gas fees for the created blocks, along with the block creator and 8 other stakers 7. At blocks 1504-1509, X receives 2 UTXOs for each block for 1/10th of the block reward and gas fees for the created blocks, along with the block creator and 7 other stakers (even though X is a single staker, he receives 2 UTXOs for mining 2 blocks in a small span of time) 8. At blocks 1510-1513, X receives 1 UTXO for each block for 1/10th of the block reward and gas fees for the created blocks, along with the block creator and 8 other stakers. 9. X's MPoS period is complete, he will not receive anymore MPoS rewards until he mines another block Consensus Rules The block reward sent to the MPoS stakers must come from the staking block Thus, the stake transaction must contain at least 10 outputs, 1 for the creator, and 9 for the MPoS stakers The first 10 outputs of the stake transaction are consensus critical and must be in exactly the correct order based on the MPoS staker blockheight and with the creator being the first output. The stake transaction can contain additional outputs, such as for splitting a large staking UTXO into multiple UTXOs. After the 10th output, there should not be any special consensus rules for the outputs so long as the output value does not cause the transaction to exceed the input value+block reward+fees The first 500 blocks of PoS does not use MPoS, and so the block creator gets the full reward without using this logic Note that the 0th output of the staking transaction should be empty (so that it is detected as a staking transaction). This story breaks some of the "mutlisig staking" (misleading name) functionality from Blackcoin. We can fix this in a later story Task:实现互惠权益证明(MPoS)区块奖励系统 描述:互惠权益证明 (MPoS)是一种新的奖励系统,设计它的目的是使得攻击者对Qtum进行DoS攻击的代价更高。 目标 1.  防止恶意的矿工构造(对其它节点来说)很难验证的区块,然后在挖矿的过程接收回所有的gas费用这种方式来对网络进行攻击 2.  使得攻击者对网络进行DoS攻击变得更困难,代价更高。 流程 1.  当staker(PoS矿工)挖掘一个区块,他只获得PoS奖励和gas费用的一小部分。奖励以及费用的其余部分和其他9个人共享 2.  当staker挖掘到一个区块,他的stake脚本((staketx.vout[0])被注册用于接收一份奖励,从该区块被挖掘后的500个区块开始,持续10个区块。 3.  这样,每个区块将会有10个挖矿奖励的接受者。区块的创建者,以及9个“互惠的”staker 4.  在9个共享奖励的区块之后,staker的脚本将被删除,且另一个staker的脚本将会添加进来取代它。 5.  在一个10个区块的周期内,若staker挖掘出的区块多于1个,那么会出现该staker接受2份奖励的场景。然而,最早的stake脚本实例从它挖掘出的区块开始,一旦超过了510个区块,它就会被删除,并且奖励也会下降到正常水平。相同的stake脚本不应该合并成一个单一的UTXO,而应该保持重复的多个UTXO。 6.  为了防止恶意或粗心的staker对免费的或者gas price很便宜的合约进行挖矿,在后面的任务中将会创建一个“最小的gas price DGP”特性,这样就会有一个最小的gas price。在出现漏洞和危机的时候,gas price将可以增加,当gas price变得很高并且使得合约的使用变得很昂贵时,gas price也可以降低。 示例 1.  有一个staker,X,他挖掘出了第1000个区块 2.  X接收了该区块的奖励和gas费用总和的十分之一。Staking交易包括了来自其他互惠的staker的至少9个输出 3.  X在区块高度为1004时又挖到了一个区块 4.  X接收该区块的区块奖励和gas费用总和的十分之一。Staking交易包括了来自其他互惠的staker的至少9个输出 5.  在该示例期间,X没有再挖到任何区块 6.  在区块1500,1501,1502和1503中,X在每个区块中接收一个UTXO,获得创建区块的区块奖励和gas费用总和的十分之一,和区块的创建者以及其他8个staker一起分享区块奖励和gas费用。 7.  在区块1504-1509中,X在每个区块接收2个UTXO,获取创建区块的区块奖励和gas费用总和的十分之二,和区块的创建者以及其他7个staker一起分享区块奖励和gas费用(虽然X只是一个staker,但是他接收了2个UTXO,因为他在一个小的时间跨度内挖掘出了两个区块) 8.  在区块1510-1513中,X在每个区块上接收1个UTXO,获得创建区块的区块奖励和gas费用总和的十分之一,和区块的创建者以及其他8个staker一起分享区块奖励和gas费用。 9.  X的MPoS过程完成了,他不会再接收到任何MPoS奖励,除非他挖到了其他区块。 共识规则 •   发送给MPoS staker的区块奖励必须来自于staking区块 •   stake交易必须包含至少10个输出(outputs),1个给区块创建者,9个给MPoS staker •   stake交易的第1个10个outputs是关系到共识的,必须准确地按照MPoS staker挖矿的区块高度正确地排列,且创建者为第一个output。 •   stake交易可以包含额外的outputs,例如,将一个大的staking UTXO分成多个UTXO。在第10个output后,对于output不应该有任何特殊的共识规则,只要交易的output值不会超过输入值+区块奖励+费用。 •   前500个PoS区块不使用MPoS,因此区块的创建者获得全部的奖励,不需要使用前面的规则 注意,staking交易的第0个output应该为空(通过这点,判断交易为staking交易)。该任务破坏了Blackcoin的一些“multisig(多重签名) staking”功能。在后面的任务中,我们会解决这个问题。

QTUMCORE-67:Accepting blocks for MPoS Description:The whole block need to be checked before accepting it (when the block was created by the miner or received from other chains ...). The block need to be checked that satisfy the MPoS rules: The reward is shared with 9 other blocks creator, so there are 10 recipients. The recipients are the correct one. The recipient is creator to block and not older then 510 blocks. The outputs are in the right order. Task:接受MPoS的区块 描述:在接受区块之前,整个区块需要被检查(当该区块被矿工创建出来或者从其他节点接收时……) 需要检查区块,以满足MPoS规则: •   区块奖励和9个其他的区块创建者共享,因此有10个接收者 •   接收者是正确的 •   接收者是区块的创建者,且创建的区块编号相比该区块不会早于510个 •   outputs的顺序正确

在此任务之前,Qtum已完全实现了标准的PoS3.0共识机制。但PoS机制在用于支持图灵完备的智能合约系统时会遇到一些问题,主要体现在攻击者可以通过构造对于其他节点来说非常难以验证的合约交易来达到攻击的目的,而攻击者由于有先验知识,可以回收全部的gas费用,从而实现零成本或低成本的攻击。 鉴于此,上述任务提出了PoS共识机制的改进 -- MPoS,即互惠权益共识机制。任务中详细描述了其工作原理及激励机制。MPoS大大增加了恶意节点的攻击成本,这也是Qtum量子链在创建以来能够稳定运行的基础。

QTUMCORE-68:Creating new MPoS block Description:The block miner will submit script for receiving reward 500 blocks from the block creation lasting for 10 blocks. The reward will be divided into 10 outputs, 9 of them for the recipients, the other of the outputs need to be preserved because correspond to the recipients. The recipients miners that previously created blocks and submitted script for receiving reward after 500 blocks with length of 10 blocks. If a recipient created more blocks in the period of 10 blocks, then will receive more of the outputs when the block reward is divided. {code} Block creation parameter: BlockReward = 4; TxFee = 2; Refund = 1; Input0 = 160; PoS outputs: Output0 = NULL; Output1 = 165; Output2 = 1; PoS outputs: Output0 = NULL; Output1 = 160.5; Output2 = 0.5; Output3 = 0.5; Output4 = 0.5; Output5 = 0.5; Output6 = 0.5; Output7 = 0.5; Output8 = 0.5; Output9 = 0.5; Output10 = 0.5; Output11 = 1; {code} Task:创建新的MPoS区块 描述:矿工将提交脚本,用于接收从区块创建开始的500个区块后的区块奖励,并持续10个区块。 挖矿奖励被分为10个outputs,其中的9个给recipients(接收者),另一个output需要保留,因为要和recipients对应。 Recipients矿工之前创建了区块并且提交了脚本用于接收500个区块后长度为10个区块的奖励。 如果recipient在一个10个区块的周期内创建了更多的区块,那么它将在区块奖励被分割时获得更多的outputs。 {code} Block creation parameter: BlockReward = 4; TxFee = 2; Refund = 1; Input0 = 160; PoS outputs: Output0 = NULL; Output1 = 165; Output2 = 1; PoS outputs: Output0 = NULL; Output1 = 160.5; Output2 = 0.5; Output3 = 0.5; Output4 = 0.5; Output5 = 0.5; Output6 = 0.5; Output7 = 0.5; Output8 = 0.5; Output9 = 0.5; Output10 = 0.5; Output11 = 1; {code}

上述任务描述了MPoS区块是如何生成的。

QTUMCORE-69:Make PoS staker only process transactions after a stake is found Description:The staker currently uses similar block construction techniques as for PoW blocks. This results in transactions being processed multiple times, which is not ideal, especially for contract transactions. It should be rewritten so that it only processes and adds transactions to the block after a stake has been found Task:PoS staker只有在发现了stake之后才处理交易 描述:目前,Staker使用和PoW区块类似的区块构造技术,这会导致交易被多次处理,这是不合理的,尤其是对合约交易。应该进行重写,使得只有在发现了stake之后,才处理交易以及添加交易到区块中。

为了避免许多重复的操作,上述任务提出了先发现stake,后添加交易到区块的解决方案。

QYUMCORE-74:Make PoS staker able to properly measure a contract's resource usage in a block Description:Right now the PoS staker logic is fairly simple. It simply counts sigops and the overall size of the transaction to measure how much a transaction will cause a block to "fill up". This logic works fine for simple Bitcoin based transactions, but does not handle more advance use cases for smart contracts. For example, there was a recent bug which caused the staker to create an invalid block due to a contract creating too many sigops in a consensing tx. This could cause the entire network to stop functioning, as every staker will try to add this transaction to the block, and then will generate an invalid block, and the staker is not smart enough to recover. So, we need to ensure that when adding a contract to a belock, that not only is it's own transaction size counted, but also any AAL created transactions as a result of that contract. Additionally, the staker logic does not account for the gas limit per block, and so a very expensive transaction could be created that would cause the block to always go beyond the allowed gas amount, creating an invalid block. First, we need a light-weight version of CheckBlock that does not verify signatures, nor execute contracts. Then, we need to change the staker logic to something like this: {code:java} while(!mempool.end){  tx=mempool.tx;  block.add(mempool.tx);  if(tx.IsCallOrCreate()){    //..execute contract    result=tx.executionResult;    block.add(result); //add condensing tx and gas refund vout  }  if(!CheckBlockLight(block)){ //check block but don't execute contracts again or check signatures again.    count++;    block.remove(tx); //remove this tx    block.remove(result); //remove condensing tx and gas refund vout from contract exec  }  mempool.tx.next(); //iterate to next item in mempool  if(count>=50){    break; //tried adding 50 different transactions to mempool but all failed, so publish the block as-is } {code} Note there is some logic in the staker for "package" transactions. This is a special set of transactions in Bitcoin that allows for a transaction to be given a higher transaction fee after it's already been broadcast. It works by spending an (unconfirmed) output in the low fee tx and in the spending tx giving a high fee. This functionality is not needed for contract transactions. Task:使PoS staker能够正确地测量区块中合约的资源使用情况 描述:现在PoS staker的逻辑还很简单。它只计算sigops和交易的总大小来测量一个交易导致一个区块“填充”的程度。这种逻辑适用于简单地基于比特币的交易,但是不能处理更先进的智能合约使用场景。例如,最近有一个会导致staker创建无效区块的bug,原因是合约在consensing交易中创建了太多的sigops。这可能导致整个网络停止运行,因为每个staker将试图将该交易添加到区块中,然后生成无效的区块,并且staker没有足够的智能到能够解决这个问题。 因此,我们需要确保将合约添加到区块时,不仅要计算自己的交易大小,还要计算由该合约导致的任何账户抽象层创建的交易。另外,staker的逻辑并不考虑每个区块的gas limit,因此一个非常昂贵的交易也可能被创建,这会导致区块总是超出允许的gas数量,从而创建一个无效的区块。 首先,我们需要一个CheckBlock的轻量版本,它不验证签名,也不运行合约。 然后,我们需要将staker的逻辑改为如下: {code:java} while(!mempool.end){  tx=mempool.tx;  block.add(mempool.tx);  if(tx.IsCallOrCreate()){    //..execute contract    result=tx.executionResult;    block.add(result); //add condensing tx and gas refund vout  }  if(!CheckBlockLight(block)){ //check block but don't execute contracts again or check signatures again.    count++;    block.remove(tx); //remove this tx    block.remove(result); //remove condensing tx and gas refund vout from contract exec  }  mempool.tx.next(); //iterate to next item in mempool  if(count>=50){    break; //tried adding 50 different transactions to mempool but all failed, so publish the block as-is } {code} 注意,在staker中有一些“打包(package)”交易的逻辑。在比特币中这是一组特殊的交易,它允许交易在被广播后获得更高的交易费用。它的工作方式是在低费用的交易上花费一个(未确认的)output并且在需要花费的交易上付很高的费用。合约交易中不需要此功能。

上述任务旨在令区块生成者能够正确的估计合约所使用的区块资源,并设置了gas limit,避免由于验证过于复杂的恶意合约而导致整个网络阻塞。

小结

MPoS共识机制的原理与PoS基本一致,因此它能保证Qtum网络足够去中心化。同时,由于其改进了系统的激励机制,使恶意合约的攻击成本大大提高,因此整个网络变得更加安全与稳定。MPoS是Qtum网络稳定运行至今的重要基石。Qtum也希望通过本章的介绍,帮助开发者和Qtum钱包的使用者更深入地理解该共识机制的运行原理,从而解除他们开发和使用上的疑问。

连载:Qtum量子链设计文档(四):POS机制设计和实现

关注Qtum量子链(qtumchain)公众号,回复关键字查阅Qtum量子链相关资料,以下是部分文档关键字

回复:‘白皮书’,查看《Qtum量子链白皮书,设计原理,实现方案,及应用》

回复:‘未来’,查看《Qtum量子链未来2年技术路线规划-简略版》

回复:‘指南’,查看《首篇Qtum量子链区块链开发指南系列面世》

回复:‘专访’,查看《Nasdaq专访Qtum:区块链会成为世界最大的信任服务商》

回复:‘文档’,查看英文版本《Qtum量子链实现文档》

回复:‘中文文档’,查看中文版本《Qtum量子链实现文档》

连载:Qtum量子链设计文档(四):POS机制设计和实现

连载:Qtum量子链设计文档(四):POS机制设计和实现

声明:链世界登载此文仅出于分享区块链知识,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不构成投资建议。投资者据此操作,风险自担。此文如侵犯到您的合法权益,请联系我们kefu@lianshijie.com

参与讨论 (0 人参与讨论)

相关推荐

区块链的常识之,什么是区块链股份授权证明机制DPoS?

区块链的常识之,什么是区块链股份授权证明机制DPoS?

股份授权证明机制(DPOS)是一种新的保障网络安全的共识机制。它在尝试解决传统的PoW机制和PoS机制问题的同时,还能通过实施科技式的民主抵消中心化所带来的负面效应。DPoS 是一种区块链的共识算法, 2014年4月由Bitshares 的首席开发者 Dan Larimer (现为EOS CTO)提出并应用。当时Dan观察到比特币系统共识算法POW的一些问题:比如矿池导致算力越来越集中、电力耗费过

我们能设计一个国产灰度公司吗?搜搜币觉得可以

我们能设计一个国产灰度公司吗?搜搜币觉得可以

本周灰度比特币信托基金增持比特币20336.39枚比特币持有比特币总量达.526764.52枚,增持数量排名基金第一。增持数量排名第二位的是3iQ比特币基金,本周增持3104.92枚比特币,持有总量达13765.6枚比特币。排名第三位的是ETC Group,共增持895.92枚比特币,持有总量达8312.56枚比特币。据统计,增持数量排名前三位的基金本.周共增持24336.63枚比特币,三者共持有

浅谈波卡平行链消息传递机制 HRMP

而 Relaychain 做为 Polkadot 多链系统的中枢, 则为 Parachain 提供了一致性安全保证及跨链消息路由机制。HRMP便是其当前的解决方案。XCMP 是 Parachain 之间消息传递的终极方案, 允许 Parachain 之间直接传递信息且只占用 Relaychain 最小的资源。

Bebt交易所:深入分析自动化做市商发展掣肘与设计改进

Bebt交易所:深入分析自动化做市商发展掣肘与设计改进

在基于区块链的分布式系统 (如 Ethereum) 上重构一个新的金融世界时,必须认识到区块链世界与链下世界相比,有着完全不同的动态属性。最值得注意的是,链上并非连续计时,而是通过区块来量化时间的流逝。但因为它受到区块大小的限制,这又导致了延迟问题和计算能力的限制。由于这些结构上的差异,分布式金融的设计者应该具有与中心化世界的设计者完全不同的思路。例如,由于区块链的成本和技术基础设施,做市商在基于

TrustToken携手Chainlink预言机和Armanino为TUSD稳定币推出储备证明机制

在Armanino和Chainlink的支持下,TUSD成为了全球首个推出链上实时证明的稳定币。TrustToken目前正与独立的实时证明提供商Armanino联手,通过领先的预言机网络Chainlink向链上传输可信数据。TUSD链上储备证明是行业首个针对法币担保稳定币的储备证明机制。

比特币向左,以太坊向右,POW和POS你站哪一边?

最近以太坊2.0发布升级进度的消息,其创始人Vitalik发文详述以太坊从PoW转向PoS的三大关键原因,引起加密货币社区的广泛讨论。

Thorchain:运行原理和代币机制

在Thorchain上有四种角色:流动性提供者、兑换者、套利者以及节点运营者。Thorchain处理每个观察到的交易,并收集签名者。通过TSS,Thorchain可以提供较高层级的安全性,在生成新地址和签名交易时相应降低风险。

谁是更好的去中心化云存储?Filecoin vs Storj设计要点和实测PK

而真正能算得上在去中心化云存储赛道主战场上正面PK的重量级选手,其实只有Filecoin和Storj。

麦妖榜
更新日期 2019-09-03
排名用户贡献值
1牛市来了30910
2BitettFan24187
3等待的宿命23810
4区块大康20369
5六叶树20310
6linjm122719429
7天下无双16192
8lizhen00215280
9让时间淡忘14586
10yelanyi050511349
返回顶部 ↑