美文网首页
挖自己的矿:POW工作量证明

挖自己的矿:POW工作量证明

作者: 空白少侠 | 来源:发表于2017-10-29 16:59 被阅读137次

    在上一篇文章中我们Java搭建了一个简单的区块链原型,但是他有个缺陷:加入一个新的区块太简单了。而在真正的区块链系统如比特币中,新的区块加入不是那么简单的,必须经过大量的计算。

    工作量证明(proof of work)

    工作量证明是区块链中的一种共识算法。
    当新的区块想加入到整个区块链的时候,需要经过一个'坎'就是达到区块链设置的某个计算任务(Block Hash算法)。
    起初Block Hash算法用于垃圾邮件的防范

    简述Block Hash算法

    Block Hash算法:给定的一个基本的字符串"Hello, world!",我们给出的工作量要求是,可以在这个字符串后面添加一个叫做nonce的整数值,对变更后(添加nonce)的字符串进行SHA256哈希运算,如果得到的哈希结果(以16进制的形式表示)是以"0000"开头的,则验证通过。为了达到这个工作量证明的目标。我们需要不停的递增nonce值,对得到的新字符串进行SHA256哈希运算。按照这个规则,我们需要经过4251次计算才能找到恰好前4位为0的哈希散列。

    
    "Hello, world!0" => 1312af178c253f84028d480a6adc1e25e81caa44c749ec81976192e2ec934c64
    "Hello, world!1" => e9afc424b79e4f6ab42d99c81156d3a17228d6e1eef4139be78e948a9332a7d8
    "Hello, world!2" => ae37343a357a8297591625e7134cbea22f5928be8ca2a32aa475cf05fd4266b7
    ...
    "Hello, world!4248" => 6e110d98b388e77e9c6f042ac6b497cec46660deef75a55ebc7cfdf65cc0b965
    "Hello, world!4249" => c004190b822f1669cac8dc37e761cb73652e7832fb814565702245cf26ebb9e6
    "Hello, world!4250" => 0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9
    
    

    而0的个数可以认为挖矿难度,不难推算�:若要求0的位数越多那么需要计算的次数就会越多,所消耗的计算资源就会越大。
    以上例子不难�验证

    创建ProofOfWork类:

    添加下面属性

        private Block mBlock;
        private BigInteger mTarget;
        public static int mDiffcult;
    
    
    • mBlock 持有当前进行工作量证明的区块
    • mtarget 计算目标
    • mDiffcult 所指的就是挖矿难度

    而且我们将使用Java的大数类:BigInteger

    �ProofOfWork的构造方法

    
        public ProofOfWork(Block block) {
            this.mBlock = block;
            mTarget = BigInteger.ONE;
            mTarget = mTarget.shiftLeft(256 - ProofOfWork.mDiffcult);
        }
    

    持有需要证明的区块,初始设置为大数型的1 然后使用移位�运算:��将mtarget�左移mDiffcult位

    如:
    mtarget=1,左移10位后 10000000000。

    然后进行数据预处理:

     private String prePareData(int nonce) {
            String result = mBlock.getmPrevBlockHash() +
                    mBlock.getmData() +
                    mBlock.getmTimestamp() +
                    Integer.toHexString(mTarget.intValue()) +
                    Integer.toHexString(nonce);
            return result;
        }
    
    

    接下来就是我们的算法了

       public Map<String, String> run() {
            String hash = "";
            int nonce = 0;
            while (true) {
                String data = prePareData(nonce);
                hash = lib.SHA(data);
                BigInteger hashInt = new BigInteger(hash, 16);
                if (hashInt.compareTo(mTarget) == -1) {
                    break;
                } else {
                    nonce++;
                }
            }
            String validation = mBlock.getmPrevBlockHash() +
                    mBlock.getmData() +
                    mBlock.getmTimestamp() +
                    Integer.toHexString(mTarget.intValue()) +
                    Integer.toHexString(nonce);
            if (lib.SHA(validation).equals(hash)) {
                Map<String, String> map = new HashMap<>(100);
                map.put("nonce", nonce + "");
                map.put("hash", hash);
                return map;
            } else {
                return null;
            }
    
        }
    

    初始化hash和nonce进入死循环:

    对区块进行hash,转换为十六进制hashInt,比较两个大数(其实就是作差)
    若hashInt 小于 mTarget则满足区块链设置的难度要求,跳出循环再验证。

    将hash和nonce标记给当前区块。
    修改Block的构造函数

     public Block(String data, String prehash) {
            this.mData = data;
            this.mPrevBlockHash = prehash;
            this.setHash();
    
            ProofOfWork proofOfWork = new ProofOfWork(this);
            Map<String, String> map = proofOfWork.run();
    
            if (map != null) {
                this.mNonce = Integer.parseInt(map.get("nonce"));
                this.mHash = map.get("hash");
                this.mNum = BlockChain.blockChain.size();
    
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                this.mTimestamp = df.format(new Date());
                System.out.println("|-----------------------------------|");
                System.out.println("编号: \t" + mNum);
                System.out.println("nonce: \t" + mNonce);
                System.out.println("时间:\t" + this.mTimestamp);
                System.out.println("数据:\t" + this.mData);
                System.out.println("父Hash:\t" + this.mPrevBlockHash);
                System.out.println("Hash:\t" + this.mHash);
            }
        }
        private void setHash() {
            String headers = mPrevBlockHash + mData;
            mHash = lib.SHA(headers);
        }
    

    编写测试类

    public class Main {
        public static void main(String[] args) {
            BlockChain.newBlockchain();
            ProofOfWork.mDiffcult = 24;
            int data = 0;
            while (true) {
                BlockChain.addBlock("" + data);
                data++;
            }
        }
    }
    
    
    |-----------------------------------|
    编号:     0
    nonce:  0
    时间: 2017-10-26 19:51:53
    数据: Genesis
    父Hash:  0
    Hash:   1a2cee942b6be3b34bca97e425b59cb76f2ce08d1a575f9dc34921d1b9c3b086
    |-----------------------------------|
    编号:     1
    nonce:  10571159
    时间: 2017-10-26 19:52:41
    数据: 0
    父Hash:  1a2cee942b6be3b34bca97e425b59cb76f2ce08d1a575f9dc34921d1b9c3b086
    Hash:   00000076d9e0adf9fe4d0a66a2ae104437c1b525108a7362464259357eb945e3
    |-----------------------------------|
    编号:     2
    nonce:  9742798
    时间: 2017-10-26 19:53:25
    数据: 1
    父Hash:  00000076d9e0adf9fe4d0a66a2ae104437c1b525108a7362464259357eb945e3
    Hash:   0000003dc7274c9c0e4ac9097a6bfd9c21533f16c78be5cbf81337bb78a7b4fa
    |-----------------------------------|
    编号:     3
    nonce:  15122775
    时间: 2017-10-26 19:54:33
    数据: 2
    父Hash:  0000003dc7274c9c0e4ac9097a6bfd9c21533f16c78be5cbf81337bb78a7b4fa
    Hash:   000000b227546d520ae41a0718f1d9b32026d7559589a90aeb6e3fc8c6a96d50
    |-----------------------------------|
    编号:     4
    nonce:  5794680
    时间: 2017-10-26 19:54:57
    数据: 3
    父Hash:  000000b227546d520ae41a0718f1d9b32026d7559589a90aeb6e3fc8c6a96d50
    Hash:   000000bfe1fc16917d7faa81d3faef75a0eb43d23c7d7d4b090707157f3f295d
    |-----------------------------------|
    编号:     5
    nonce:  20395184
    时间: 2017-10-26 19:56:18
    数据: 4
    父Hash:  000000bfe1fc16917d7faa81d3faef75a0eb43d23c7d7d4b090707157f3f295d
    Hash:   000000508ada2ccfb9bfba1518e4f5a918ac4e591b6956820125bdcae3b1d020
    |-----------------------------------|
    编号:     6
    nonce:  25069127
    时间: 2017-10-26 19:57:56
    数据: 5
    父Hash:  000000508ada2ccfb9bfba1518e4f5a918ac4e591b6956820125bdcae3b1d020
    Hash:   000000ea709f8b4651c22dcefef75459ec491f1f6171056cf71f7cdac313563f
    |-----------------------------------|
    编号:     7
    nonce:  9008228
    时间: 2017-10-26 19:58:32
    数据: 6
    父Hash:  000000ea709f8b4651c22dcefef75459ec491f1f6171056cf71f7cdac313563f
    Hash:   000000b58859d21f8325ab2cffdfef1280abe26cd5b6928bc01d633e7b60fff8
    |-----------------------------------|
    编号:     8
    nonce:  12118155
    时间: 2017-10-26 19:59:20
    数据: 7
    父Hash:  000000b58859d21f8325ab2cffdfef1280abe26cd5b6928bc01d633e7b60fff8
    Hash:   000000d91eccb4c979089dec4a2411875faa38bcf3cb88a7759ef8022d3b0e73
    |-----------------------------------|
    编号:     9
    nonce:  12413162
    时间: 2017-10-26 20:00:09
    数据: 8
    父Hash:  000000d91eccb4c979089dec4a2411875faa38bcf3cb88a7759ef8022d3b0e73
    Hash:   0000009871793ecd17c54aa63ec682f51adaf2cb64e0cc2673252a4de0b37132
    |-----------------------------------|
    编号:     10
    nonce:  39970175
    时间: 2017-10-26 20:02:51
    数据: 9
    父Hash:  0000009871793ecd17c54aa63ec682f51adaf2cb64e0cc2673252a4de0b37132
    Hash:   000000a0f800bca93ae5cb107cc0e464a29ef58c061d94b70a91281db8749b0a
    |-----------------------------------|
    编号:     11
    nonce:  9076542
    时间: 2017-10-26 20:03:27
    数据: 10
    父Hash:  000000a0f800bca93ae5cb107cc0e464a29ef58c061d94b70a91281db8749b0a
    Hash:   0000004b3bf11eea55e2820ba54e73fce2f88a96fa5e309039aed6248a6320ef
    |-----------------------------------|
    编号:     12
    nonce:  7037734
    时间: 2017-10-26 20:03:55
    数据: 11
    父Hash:  0000004b3bf11eea55e2820ba54e73fce2f88a96fa5e309039aed6248a6320ef
    Hash:   000000439f7fe845508221f846a4d0136153edbeb60fa4fb9bc1d4edd4fba12c
    
    
    

    相关文章

      网友评论

          本文标题:挖自己的矿:POW工作量证明

          本文链接:https://www.haomeiwen.com/subject/ecbypxtx.html