Change WinCount to int64, wire it to BlockMessage

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-06-24 19:44:05 +02:00
parent 88352c74fc
commit 4895c895aa
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
6 changed files with 40 additions and 23 deletions

View File

@ -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
@ -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 {

View File

@ -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)

View File

@ -518,10 +518,15 @@ func (t *ElectionProof) MarshalCBOR(w io.Writer) error {
scratch := make([]byte, 9)
// t.WinCount (uint64) (uint64)
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.WinCount)); err != nil {
return err
// t.WinCount (int64) (int64)
if t.WinCount >= 0 {
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.WinCount)); err != nil {
return err
}
} else {
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.WinCount-1)); err != nil {
return err
}
}
// t.VRFProof ([]uint8) (slice)
@ -555,19 +560,30 @@ func (t *ElectionProof) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input had wrong number of fields")
}
// t.WinCount (uint64) (uint64)
// t.WinCount (int64) (int64)
{
maj, extra, err = cbg.CborReadHeaderBuf(br, scratch)
maj, extra, err := cbg.CborReadHeaderBuf(br, scratch)
var extraI int64
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
switch maj {
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative oveflow")
}
extraI = -1 - extraI
default:
return fmt.Errorf("wrong type for int64 field: %d", maj)
}
t.WinCount = uint64(extra)
t.WinCount = int64(extraI)
}
// t.VRFProof ([]uint8) (slice)

View File

@ -8,7 +8,7 @@ import (
)
type ElectionProof struct {
WinCount uint64
WinCount int64
VRFProof []byte
}
@ -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
@ -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)
@ -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++

View File

@ -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()

View File

@ -2,6 +2,7 @@ package validation
import (
"context"
"golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/actors/abi"
@ -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 {