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 <kubuxu@protocol.ai>
  • Loading branch information
Jakub Sztandera committed Jun 24, 2020
1 parent 88352c7 commit 4895c89
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 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
4 changes: 2 additions & 2 deletions chain/types/electionproof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func TestElectionLam(t *testing.T) {
}
}

var Res uint64
var Res int64

func BenchmarkWinCounts(b *testing.B) {
totalPower := NewInt(100)
power := NewInt(100)
ep := &ElectionProof{VRFProof: nil}
var res uint64
var res int64

b.ResetTimer()
b.ReportAllocs()
Expand Down
5 changes: 3 additions & 2 deletions chain/validation/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validation

import (
"context"

"golang.org/x/xerrors"

"github.com/filecoin-project/specs-actors/actors/abi"
Expand Down Expand Up @@ -72,8 +73,8 @@ func (a *Applier) ApplyTipSetMessages(epoch abi.ChainEpoch, blocks []vtypes.Bloc
var bms []stmgr.BlockMessages
for _, b := range blocks {
bm := stmgr.BlockMessages{
Miner: b.Miner,
TicketCount: 1,
Miner: b.Miner,
WinCount: 1,
}

for _, m := range b.BLSMessages {
Expand Down

0 comments on commit 4895c89

Please sign in to comment.