Skip to content

Commit

Permalink
Change WinCount to int64, wire it to BlockMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Jakub Sztandera committed Jun 24, 2020
1 parent 88352c7 commit 91831f4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type BlockMessages struct {
Miner address.Address
BlsMessages []types.ChainMsg
SecpkMessages []types.ChainMsg
TicketCount int64
WinCount int64
}

type ExecCallback func(cid.Cid, *types.Message, *vm.ApplyRet) error
Expand Down Expand Up @@ -311,7 +311,7 @@ func (sm *StateManager) computeTipSetState(ctx context.Context, blks []*types.Bl
Miner: b.Miner,
BlsMessages: make([]types.ChainMsg, 0, len(bms)),
SecpkMessages: make([]types.ChainMsg, 0, len(sms)),
TicketCount: 1, //int64(len(b.EPostProof.Proofs)), // TODO fix this
WinCount: b.ElectionProof.WinCount,
}

for _, m := range bms {
Expand Down
4 changes: 2 additions & 2 deletions chain/store/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func (cs *ChainStore) Weight(ctx context.Context, ts *types.TipSet) (types.BigIn

// (wFunction(totalPowerAtTipset(ts)) * sum(ts.blocks[].ElectionProof.WinCount) * wRatio_num * 2^8) / (e * wRatio_den)

totalJ := uint64(0)
totalJ := int64(0)
for _, b := range ts.Blocks() {
totalJ += b.ElectionProof.WinCount
}

eWeight := big.NewInt((log2P * build.WRatioNum))
eWeight = eWeight.Lsh(eWeight, 8)
eWeight = eWeight.Mul(eWeight, new(big.Int).SetUint64(totalJ))
eWeight = eWeight.Mul(eWeight, new(big.Int).SetInt64(totalJ))
eWeight = eWeight.Div(eWeight, big.NewInt(int64(build.BlocksPerEpoch*build.WRatioDen)))

out = out.Add(out, eWeight)
Expand Down
38 changes: 27 additions & 11 deletions chain/types/cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions chain/types/electionproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type ElectionProof struct {
WinCount uint64
WinCount int64
VRFProof []byte
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func lambda(power, totalPower *big.Int) *big.Int {
return lam
}

var MaxWinCount = 3 * build.BlocksPerEpoch
var MaxWinCount = 3 * int64(build.BlocksPerEpoch)

type poiss struct {
lam *big.Int
Expand Down Expand Up @@ -175,7 +175,7 @@ func (p *poiss) next() *big.Int {
// ComputeWinCount uses VRFProof to compute number of wins
// The algorithm is based on Algorand's Sortition with Binomial distribution
// replaced by Poisson distribution.
func (ep *ElectionProof) ComputeWinCount(power BigInt, totalPower BigInt) uint64 {
func (ep *ElectionProof) ComputeWinCount(power BigInt, totalPower BigInt) int64 {
h := blake2b.Sum256(ep.VRFProof)

lhs := BigFromBytes(h[:]).Int // 256bits, assume Q.256 so [0, 1)
Expand All @@ -195,7 +195,7 @@ func (ep *ElectionProof) ComputeWinCount(power BigInt, totalPower BigInt) uint64

p, rhs := newPoiss(lam)

var j uint64
var j int64
for lhs.Cmp(rhs) < 0 && j < MaxWinCount {
rhs = p.next()
j++
Expand Down

0 comments on commit 91831f4

Please sign in to comment.