zoukankan      html  css  js  c++  java
  • 以太坊PoW

    ethash

    ethash(eth+hash)是以太坊设计的挖矿算法,为了实现ASIC-resistance,ethash依赖于对内存资源的访问,是一种memory-hard函数。同时为了支持轻节点对区块进行验证,专门设计了cache和dataset一小一大的数据集,小的cache用于验证,大的dataset用于挖矿。

    ethash伪代码

    • 通过seed计算cache
      每隔30000个块会重新生成seed值(对原来的seed重新求哈希),并用新的seed生成新的cache;
      cache初始大小为16M,每隔30000个块重新生成时增加初始大小的1/128——128K
    def mkcache(cache_size, seed)
        o = [hash(seed)]
        for i in range(1, cache_size)
            o.append(hash(o[-1]))
        return o
    
    • 通过cache生成第i个dataset的值
      这个dataset叫做DAG,初始大小是1G,也是每隔30000个块更新,同样增加初始大小的1/128——8M;
      先通过cache中的第i%cache_size元素生成初始的mix,因为两个不同的dataset元素可以对应同一个cache中的元素,为了保证每隔初始的mix都不同,将i也参与了哈希计算;
      随后循环256次,每次通过get_int_from_item来根据当前的mix值求得下一个要访问的cache元素的下标,用这个cache元素和mix通过make_item来求得新的mix值,注意到由于初始的mix值都不同,所以访问cache序列也是不同的;
    def calc_dataset_item(cache, i)
        cache_size = cache.size
        mix = hash(cache[i % cache_size] ^ i)
        for j in range(256)
            cache_index = get_int_from_item(mix)
            mix = make_item(mix, cache[cache_index % cache_size])
        return hash(mix)
    
    • 计算出整个dataset
      通过不断调用上面的calc_dataset_item函数来一次生成dataset中全部full_size个元素;
    def calc_dataset(full_size, cache)
        return [calc_dataset_item(cache, i) for i in range(full_size)]
    
    • 挖矿算法
      分为全节点和轻节点,全节点负责挖矿,轻节点负责验证;
      先通过header和nonce求出一个初始的mix,然后进入64次循环,根据当前的mix值求出要访问的dataset的元素的下标,然后根据这个下标访问dataset中两个连续的值,最后返回mix的哈希值,用来和target比较。
    def hashimoto_full(header, nonce, full_size, dataset)
        mix = hash(header, nonce)
        for i in range(64)
            dataset_index = get_int_from_item(mix) % full_size
            mix = make_item(mix, dataset[dataset_index])
            mix = make_item(mix, dataset[dataset_index + 1])
        return hash(mix)
    
    def hashimoto_light(header, nonce, full_size, cache)
        mix = hash(header, nonce)
        for i in range(64)
            dataset_index = get_int_from_item(mix) % full_size
            mix = make_item(mix, calc_dataset_item(cache, dataset_index))
            mix = make_item(mix, calc_dataset_item(cache, dataset_index + 1))
        return hash(mix)
    
  • 相关阅读:
    js下载doxc 文件示例和部分后缀对应的content-type 总结
    使用react-app-rewired和customize-cra对默认webpack自定义配置
    koa2使用es7 的装饰器decorator
    vue history 模式打包部署在域名的二级目录的配置指南
    linux 安装 node 环境
    javascript 正则表达式之分组与前瞻匹配详解
    vue的$emit 与$on父子组件与兄弟组件的之间通信
    mysql 的基本操作总结--增删改查
    mysql 常用的时间日期函数小结
    小程序封装request请求,统一API
  • 原文地址:https://www.cnblogs.com/HachikoT/p/13636352.html
Copyright © 2011-2022 走看看