diff --git a/chain/stmgr/stmgr.go b/chain/stmgr/stmgr.go index 917b5ca26df..a4e60648551 100644 --- a/chain/stmgr/stmgr.go +++ b/chain/stmgr/stmgr.go @@ -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 { diff --git a/chain/store/weight.go b/chain/store/weight.go index ab663f70008..eb385f816e9 100644 --- a/chain/store/weight.go +++ b/chain/store/weight.go @@ -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) diff --git a/chain/types/cbor_gen.go b/chain/types/cbor_gen.go index 23527e69b80..9932b186380 100644 --- a/chain/types/cbor_gen.go +++ b/chain/types/cbor_gen.go @@ -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) diff --git a/chain/types/electionproof.go b/chain/types/electionproof.go index 1e12d1182bf..b8879b27c88 100644 --- a/chain/types/electionproof.go +++ b/chain/types/electionproof.go @@ -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++