`
lobin
  • 浏览: 425716 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

以太坊Ethereum区块中的Merkle trie:state,transaction,receipt

 
阅读更多

Merkle Tree,也被叫做Merkle trie。

 



 

 



 

 

 

@Test
public void testToBinaryString() {
    for (int i = 0; i < 10; i++) {
        UUID uid = UUID.randomUUID();
        System.out.println(uid.toString());
        String tb = toBinaryString(uid);
        System.out.println(tb);
    }
}

private String toBinaryString(UUID uid) {
    String sid = uid.toString();
    sid = sid.replaceAll("-", "");

    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < sid.length(); k += 2) {
        String ss = sid.substring(k, k + 2);
        String tb = Integer.toBinaryString(Integer.parseInt(ss, 16));
        while (tb.length() < 8) {
            tb = "0" + tb;
        }
        sb.append(tb);
    }
    return sb.toString();
}

 

 

 

 

 

4b4817bf-fdaa-47d0-af49-791e32c59756
01001011010010000001011110111111111111011010101001000111110100001010111101001001011110010001111000110010110001011001011101010110
29ec2da9-ddbe-4f47-86be-09bfb7f24b6b
00101001111011000010110110101001110111011011111001001111010001111000011010111110000010011011111110110111111100100100101101101011
c96464f9-4a1a-4054-93db-5dd6347004b4
11001001011001000110010011111001010010100001101001000000010101001001001111011011010111011101011000110100011100000000010010110100
28869762-70bb-4718-a1a0-17392d6d6dc2
00101000100001101001011101100010011100001011101101000111000110001010000110100000000101110011100100101101011011010110110111000010

 

 

@Test
public void testDistance() {
    UUID uid1 = UUID.fromString("4b4817bf-fdaa-47d0-af49-791e32c59756");
    UUID uid2 = UUID.fromString("29ec2da9-ddbe-4f47-86be-09bfb7f24b6b");

    String b1 = toBinaryString(uid1);
    String b2 = toBinaryString(uid2);
    System.out.println(b1);
    System.out.println(b2);

    String dxor = dxor(uid1, uid2);
    System.out.println(dxor);

    int d = distance(dxor);
    System.out.println(d);
}

private String dxor(UUID n1, UUID n2) {
    String b1 = toBinaryString(n1);
    String b2 = toBinaryString(n2);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < b1.length(); i++) {
        sb.append(b1.charAt(i) == b2.charAt(i) ? '0' : '1');
    }
    return sb.toString();
}

private int distance(String xor) {
    for (int i = 1; i <= xor.length(); i++) {
        if (xor.charAt(i - 1) == '1') {
            return xor.length() - i + 1;
        }
    }
    return 0;
}

 

01001011010010000001011110111111111111011010101001000111110100001010111101001001011110010001111000110010110001011001011101010110
00101001111011000010110110101001110111011011111001001111010001111000011010111110000010011011111110110111111100100100101101101011
01100010101001000011101000010110001000000001010000001000100101110010100111110111011100001010000110000101001101111101110000111101
127

 

1、http://dl.iteye.com/topics/download/b568ee65-ca55-3ae0-bb57-0072258554b0

2、https://github.com/ethereum/wiki/wiki/White-Paper

  • 大小: 42.8 KB
  • 大小: 58.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics