Skip to content

Commit 94bd6a1

Browse files
committed
fix some bugs
1 parent 10f2a56 commit 94bd6a1

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

algorand.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func NewAlgorand(id PID) *Algorand {
4141

4242
func (alg *Algorand) start() {
4343
alg.quitCh = make(chan struct{})
44-
GetPeerPool().add(alg.peer)
44+
alg.peer.start()
4545
go alg.run()
4646
}
4747

4848
func (alg *Algorand) stop() {
4949
close(alg.quitCh)
50-
GetPeerPool().remove(alg.peer)
50+
alg.peer.stop()
5151
}
5252

5353
// round returns the latest round number.
@@ -210,8 +210,8 @@ func (alg *Algorand) blockProposal(resolveFork bool) *Block {
210210
round := alg.round() + 1
211211
vrf, proof, subusers := alg.sortition(alg.sortitionSeed(round), role(iden, round, PROPOSE), expectedBlockProposers, alg.tokenOwn())
212212
// have been selected.
213+
log.Infof("node %d get %d sub-users in block proposal", alg.id, subusers)
213214
if subusers > 0 {
214-
log.Debugf("node %d has %d sub-users selected as block proposer", alg.id, subusers)
215215
var (
216216
newBlk *Block
217217
proposalType int
@@ -260,7 +260,11 @@ func (alg *Algorand) blockProposal(resolveFork bool) *Block {
260260
}
261261
case <-ticker.C:
262262
// get the block with the highest priority
263-
bhash := alg.peer.getMaxProposal(round).Hash
263+
pp := alg.peer.getMaxProposal(round)
264+
if pp == nil {
265+
continue
266+
}
267+
bhash := pp.Hash
264268
blk := alg.peer.getBlock(bhash)
265269
if blk != nil {
266270
return blk
@@ -521,12 +525,16 @@ func subUsers(expectedNum int, weight uint64, vrf []byte) int {
521525
// hash / 2^hashlen ∉ [ ∑0,j B(k;w,p), ∑0,j+1 B(k;w,p))
522526
hashBig := new(big.Float).SetInt(new(big.Int).SetBytes(vrf))
523527
maxHash := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(2), big.NewInt(common.HashLength), big.NewInt(0)))
524-
for {
528+
for uint64(j) <= weight {
525529
lower := new(big.Float).Mul(big.NewFloat(binomial.CDF(float64(j))), maxHash)
526530
upper := new(big.Float).Mul(big.NewFloat(binomial.CDF(float64(j+1))), maxHash)
527531
if hashBig.Cmp(lower) >= 0 && hashBig.Cmp(upper) < 0 {
528532
break
529533
}
534+
j++
535+
}
536+
if uint64(j) > weight {
537+
j = 0
530538
}
531539
return j
532540
}

common/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
2424
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
2525

2626
func (h Hash) String() string {
27-
return string(h[:])
27+
return h.Hex()
2828
}
2929

3030
func (h Hash) Bytes() []byte {

docs/剖析Algorand.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,6 @@ Algorand论文所模拟的版本,其资源开销如下:
222222
- 如何标记该轮共识区块是`FINAL`还是`TENTATIVE`。
223223
- 达成`TENTATIVE`共识后是否需要额外的流程?(如进行快速的分叉检查?)
224224
3. 每一步投票中,`committee memer`的选举都受到step的影响,这要求sortition算法对各种子参数都具有较为稳定且宽松的结果输出,以避免频繁出现票数总数达不到阈值`T*τ`的情况。
225-
4. 共识流程中多个阶段需要预估超时时间,对于耗时不确定的智能合约运行来说需要加入额外的限制机制
225+
4. 共识流程中多个阶段需要预估超时时间。在耗时不确定的智能合约场景,需要加入额外的限制机制
226226
5. 算法性能受到网络连通性的影响。
227227
6. VRF算法的安全性问题。

main.go

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import (
44
"time"
55
)
66

7-
const (
8-
userAmount = 10
9-
)
10-
117
func main() {
128
var nodes []*Algorand
139
for i := 1; i <= userAmount; i++ {

parameter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import "time"
55
const (
66
// Algorand system parameters
77
totalTokenAmount = 10000
8-
node = 10
9-
tokenPerNode = totalTokenAmount / node
8+
userAmount = 20
9+
tokenPerNode = totalTokenAmount / userAmount
1010

1111
expectedBlockProposers = 26
1212
expectedCommitteeMembers = 2000

peer.go

+9
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ func newPeer(alg *Algorand) *Peer {
2525
algorand: alg,
2626
incomingVotes: make(map[string]*List),
2727
blocks: make(map[common.Hash]*Block),
28+
maxProposals: make(map[uint64]*Proposal),
2829
}
2930
}
3031

32+
func (p *Peer) start() {
33+
GetPeerPool().add(p.algorand.peer)
34+
}
35+
36+
func (p *Peer) stop() {
37+
GetPeerPool().remove(p.algorand.peer)
38+
}
39+
3140
func (p *Peer) ID() PID {
3241
return p.algorand.id
3342
}

0 commit comments

Comments
 (0)