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 Miner address.Address
BlsMessages []types.ChainMsg BlsMessages []types.ChainMsg
SecpkMessages []types.ChainMsg SecpkMessages []types.ChainMsg
TicketCount int64 WinCount int64
} }
type ExecCallback func(cid.Cid, *types.Message, *vm.ApplyRet) error 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, Miner: b.Miner,
BlsMessages: make([]types.ChainMsg, 0, len(bms)), BlsMessages: make([]types.ChainMsg, 0, len(bms)),
SecpkMessages: make([]types.ChainMsg, 0, len(sms)), 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 { 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) // (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() { for _, b := range ts.Blocks() {
totalJ += b.ElectionProof.WinCount totalJ += b.ElectionProof.WinCount
} }
eWeight := big.NewInt((log2P * build.WRatioNum)) eWeight := big.NewInt((log2P * build.WRatioNum))
eWeight = eWeight.Lsh(eWeight, 8) 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))) eWeight = eWeight.Div(eWeight, big.NewInt(int64(build.BlocksPerEpoch*build.WRatioDen)))
out = out.Add(out, eWeight) out = out.Add(out, eWeight)

View File

@ -518,10 +518,15 @@ func (t *ElectionProof) MarshalCBOR(w io.Writer) error {
scratch := make([]byte, 9) scratch := make([]byte, 9)
// t.WinCount (uint64) (uint64) // t.WinCount (int64) (int64)
if t.WinCount >= 0 {
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.WinCount)); err != nil { if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.WinCount)); err != nil {
return err return err
}
} else {
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.WinCount-1)); err != nil {
return err
}
} }
// t.VRFProof ([]uint8) (slice) // 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") 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 { if err != nil {
return err return err
} }
if maj != cbg.MajUnsignedInt { switch maj {
return fmt.Errorf("wrong type for uint64 field") 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) // t.VRFProof ([]uint8) (slice)

View File

@ -8,7 +8,7 @@ import (
) )
type ElectionProof struct { type ElectionProof struct {
WinCount uint64 WinCount int64
VRFProof []byte VRFProof []byte
} }
@ -105,7 +105,7 @@ func lambda(power, totalPower *big.Int) *big.Int {
return lam return lam
} }
var MaxWinCount = 3 * build.BlocksPerEpoch var MaxWinCount = 3 * int64(build.BlocksPerEpoch)
type poiss struct { type poiss struct {
lam *big.Int lam *big.Int
@ -175,7 +175,7 @@ func (p *poiss) next() *big.Int {
// ComputeWinCount uses VRFProof to compute number of wins // ComputeWinCount uses VRFProof to compute number of wins
// The algorithm is based on Algorand's Sortition with Binomial distribution // The algorithm is based on Algorand's Sortition with Binomial distribution
// replaced by Poisson 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) h := blake2b.Sum256(ep.VRFProof)
lhs := BigFromBytes(h[:]).Int // 256bits, assume Q.256 so [0, 1) 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) p, rhs := newPoiss(lam)
var j uint64 var j int64
for lhs.Cmp(rhs) < 0 && j < MaxWinCount { for lhs.Cmp(rhs) < 0 && j < MaxWinCount {
rhs = p.next() rhs = p.next()
j++ j++

View File

@ -110,13 +110,13 @@ func TestElectionLam(t *testing.T) {
} }
} }
var Res uint64 var Res int64
func BenchmarkWinCounts(b *testing.B) { func BenchmarkWinCounts(b *testing.B) {
totalPower := NewInt(100) totalPower := NewInt(100)
power := NewInt(100) power := NewInt(100)
ep := &ElectionProof{VRFProof: nil} ep := &ElectionProof{VRFProof: nil}
var res uint64 var res int64
b.ResetTimer() b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()

View File

@ -2,6 +2,7 @@ package validation
import ( import (
"context" "context"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/actors/abi" "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 var bms []stmgr.BlockMessages
for _, b := range blocks { for _, b := range blocks {
bm := stmgr.BlockMessages{ bm := stmgr.BlockMessages{
Miner: b.Miner, Miner: b.Miner,
TicketCount: 1, WinCount: 1,
} }
for _, m := range b.BLSMessages { for _, m := range b.BLSMessages {