使用 go 实现 Delegated Proof of Stake 机制

DPoS的伪代码实现

for round i //分成很多个round,round无限持续dlist_i = get N delegates sort by votes //根据投票结果选出得票率最高的N个受托人dlist_i = shuffle(dlist_i) //随机改变顺序loop //round完了,退出循环slot = global_time_offset / block_intervalpos = slot % Nif dlist_i[pos] exists in this node //delegate在这个节点generateBlock(keypair of dlist_i[pos]) //产生blockelseskip

DPoS 使用 go 语言实现

package mainimport ("time""crypto/sha256""encoding/hex""fmt"
)type Block struct {Index     intTimestamp stringBPM       intHash      stringPrevHash  stringDelegate string
}// 生成区块
func generateBlock(oldBlock Block, BPM int, address string) (Block, error) {var newBlock Blockt := time.Now()newBlock.Index = oldBlock.Index + 1newBlock.Timestamp = t.String()newBlock.BPM = BPMnewBlock.PrevHash = oldBlock.HashnewBlock.Hash = calculateBlockHash(newBlock)newBlock.Delegate = addressreturn newBlock, nil
}var Blockchain []Block// 受托人
var delegates = []string{"001","002","003","004","005"}// 当前的 delegates 的索引
var indexDelegate int// 生成Hash字符串
func calculateHash(s string) string {h := sha256.New()h.Write([]byte(s))hashed := h.Sum(nil)return hex.EncodeToString(hashed)
}//生成区块的 Hash 值
func calculateBlockHash(block Block) string {record := string(block.Index) + block.Timestamp + string(block.BPM) + block.PrevHashreturn calculateHash(record)
}func isBlockValid(newBlock, oldBlock Block) bool {if oldBlock.Index+1 != newBlock.Index {return false}if oldBlock.Hash != newBlock.PrevHash {return false}if calculateBlockHash(newBlock) != newBlock.Hash {return false}return true
}func main() {lenc:=len(delegates)var firstBlock BlockfirstBlock.Hash = ""firstBlock.PrevHash=""firstBlock.Index=1var i = 0for {time.Sleep(time.Second)var newblock,_ = generateBlock(firstBlock,1,delegates[i])fmt.Println(newblock,delegates[i])i++i= i%lenc}
}

// 创建初始区块
t := time.Now()
genesisBlock := Block{}
genesisBlock = Block{0, t.String(), 0, calculateBlockHash(genesisBlock), “”, “”}
Blockchain = append(Blockchain, genesisBlock)

indexDelegate++countDlegate := len(delegates)for indexDelegate < countDlegate {// 3秒生成区块time.Sleep(time.Second * 3)fmt.Println(indexDelegate)// 创建新的区块rand.Seed(int64(time.Now().Unix()))bpm := rand.Intn(100)oldLastIndex := Blockchain[len(Blockchain)-1]newBlock, err := generateBlock(oldLastIndex, bpm, delegates[indexDelegate])if err != nil {log.Println(err)continue}fmt.Printf("Blockchain....%v\n",newBlock)if isBlockValid(newBlock, oldLastIndex) {Blockchain = append(Blockchain,newBlock)}indexDelegate = (indexDelegate + 1)% countDlegateif indexDelegate == 0 {// 更换受托人的排列顺序delegates = randDelegates(delegates)}
}

}
“`

使用 go 实现 Delegated Proof of Stake 机制相关推荐

  1. DPoS即股权授权证明(Delegated Proof of Stake)

    公链通常会遇到可扩展性问题,为了解决这些问题,一些区块链(如Lisk,EOS,Steem,BitShares和Ark)采用了股权授权证明(DPoS)共识机制.DPoS力求快速的进行交易和创建区块,同时 ...

  2. 使用 go 实现 Proof of Stake 共识机制

    使用 go 实现 Proof of Stake 共识机制 什么是 Proof of Stake 在PoW中,节点之间通过hash的计算力来竞赛以获取下一个区块的记账权,而在PoS中,块是已经铸造好的, ...

  3. 【译】The missing explanation of Proof of Stake Version 3

    在每一个加密货币中,都必须有一些共识机制来保持整个分布式网络的同步. 当比特币首次推出时,它推出了工作证明(PoW)系统. PoW是通过反复加密散列一块数据(块头)来完成的. 由于单向哈希如何工作. ...

  4. 权益证明问题 —— Proof of Stake FAQ

    ethereum wiki 中的汉语繁体翻译实在是读不通顺,还不如看英文,顺便翻译下.原文:Proof of Stake FAQ 什么是权益证明 权益证明(Proof of Stake,PoS)是一种 ...

  5. web3:区块链共识机制系列-POS(Proof of Stake)股权证明算法

    web3相关学习一并收录至该博客:web3学习博客目录大全 前情衔接:web3:区块链常见的几大共识机制及优缺点 目录 前言 算法公式与原理 算法公式 运作原理 以Peer Coin为例 缺陷 优点 ...

  6. PoS即股权证明(Proof of Stake)

    随着以太坊从原来的工作量证明(PoW)机制逐步向股权证明(PoS)过度,股权证明受到了越来越多的关注.为了更好地理解股权证明,我们需要先了解一下工作量证明的基本概念. 工作量证明 工作量证明是一个挖矿 ...

  7. 权益证明(Proof Of Stake)——Go语言实验

    权益证明,它提出来币龄的概念,币龄 = 持有的货币数 * 持有时间,比如说我有100币,持有了30天,那么我的币龄就是3000.币龄越大的节点呢获取记账权(也就是生成区块)的概率越大.每次记完账之后, ...

  8. 公信宝区块链技术和应用白皮书

    公信宝区块链技术和应用白皮书(V2.0.0) I.摘要 大数据时代已经来临,万物互联的时代也已不远,人类社会生产.获得和处理数据的能力已经远超过去.通过对数据的合理应用,我们对世界的认识.对商业和社会 ...

  9. EOS智能合约与DApp开发入门教程

    EOS的是Block.One主导研发的一个区块链底层公链系统,它专门为支撑商业去中心化应用(Decentralized Application)而设计,其代码开源. 比特币被称为区块链1.0,因为它开 ...

最新文章

  1. 关于新冠肺炎的一切|回形针
  2. linux 分区克隆软件 partclone 简介
  3. 从cpan上安装perl模块
  4. matlab三参数拟合函数,数据拟合,有三个参数,提示拟合参数太多,谢谢您啦!...
  5. 异常 —— throws
  6. 网狐棋牌(三) 调度引擎初步分析
  7. windows10 下安装、配置、启动mysql
  8. C++之智能指针std::shared_ptr简单使用和理解
  9. html请求接口_软件测试学习教程——LoadRunner实现接口测试
  10. SpringCloud 从菜鸟到大牛之二 服务注册与发现 Sping Cloud Eureka
  11. php接口返回一个数组怎末写_PHP写api接口怎么写啊,有什么具体的例子吗?
  12. 苹果系统备份文件服务器地址,IOS备份到tftp服务器和升级IOS
  13. JDE学习report和from总结
  14. c 中空格的asc码表_泰格豪雅卡莱拉系列计时码表,车迷心中的永远的“白月光”...
  15. servlet+jsp面试题
  16. fstab文件详解,mount挂载参数
  17. Pspice for TI取消默认打开方式
  18. echarts社区地图、echart地图
  19. 关于IDEA的一些常用的快捷键整合,赶紧进来KK......
  20. WIN7 中 SMTP服务器的配置

热门文章

  1. git push--解决 /etc/ssh/ssh_config: Bad configuration option: permitrootlogin 问题
  2. 数字电路设计——优先级需求处理与显示
  3. 半梦半醒之间-林清玄
  4. 软件测试工程师又一大挑战:大数据测试
  5. dcm4che依赖下载不了
  6. python运行mcmc为何老出错_为什么我的metropolis算法(mcmc)的python实现这么慢?
  7. RelativeSource={RelativeSource TemplatedParent}
  8. 内网域渗透分析工具BloodHound
  9. collectl 命令收集描述当前系统状态的数据
  10. 淮海工学院计算机考试报名,淮海工学院-江苏计算机等级考试网.DOC