|
|
@ -62,7 +62,7 @@ func generateRandomSlots(num int) []SlotPurchase { |
|
|
|
key := shard.BLSPublicKey{} |
|
|
|
key := shard.BLSPublicKey{} |
|
|
|
key.FromLibBLSPublicKey(secretKey.GetPublicKey()) |
|
|
|
key.FromLibBLSPublicKey(secretKey.GetPublicKey()) |
|
|
|
stake := numeric.NewDecFromBigInt(big.NewInt(int64(stakeGen.Int63n(maxStakeGen)))) |
|
|
|
stake := numeric.NewDecFromBigInt(big.NewInt(int64(stakeGen.Int63n(maxStakeGen)))) |
|
|
|
randomSlots = append(randomSlots, SlotPurchase{addr, key, stake}) |
|
|
|
randomSlots = append(randomSlots, SlotPurchase{addr, key, stake, stake}) |
|
|
|
} |
|
|
|
} |
|
|
|
return randomSlots |
|
|
|
return randomSlots |
|
|
|
} |
|
|
|
} |
|
|
@ -71,15 +71,15 @@ func TestMedian(t *testing.T) { |
|
|
|
copyPurchases := append([]SlotPurchase{}, testingPurchases...) |
|
|
|
copyPurchases := append([]SlotPurchase{}, testingPurchases...) |
|
|
|
sort.SliceStable(copyPurchases, |
|
|
|
sort.SliceStable(copyPurchases, |
|
|
|
func(i, j int) bool { |
|
|
|
func(i, j int) bool { |
|
|
|
return copyPurchases[i].Stake.LTE(copyPurchases[j].Stake) |
|
|
|
return copyPurchases[i].RawStake.LTE(copyPurchases[j].RawStake) |
|
|
|
}) |
|
|
|
}) |
|
|
|
numPurchases := len(copyPurchases) / 2 |
|
|
|
numPurchases := len(copyPurchases) / 2 |
|
|
|
if len(copyPurchases)%2 == 0 { |
|
|
|
if len(copyPurchases)%2 == 0 { |
|
|
|
expectedMedian = copyPurchases[numPurchases-1].Stake.Add( |
|
|
|
expectedMedian = copyPurchases[numPurchases-1].RawStake.Add( |
|
|
|
copyPurchases[numPurchases].Stake, |
|
|
|
copyPurchases[numPurchases].RawStake, |
|
|
|
).Quo(two) |
|
|
|
).Quo(two) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
expectedMedian = copyPurchases[numPurchases].Stake |
|
|
|
expectedMedian = copyPurchases[numPurchases].RawStake |
|
|
|
} |
|
|
|
} |
|
|
|
med := Median(testingPurchases) |
|
|
|
med := Median(testingPurchases) |
|
|
|
if !med.Equal(expectedMedian) { |
|
|
|
if !med.Equal(expectedMedian) { |
|
|
@ -90,9 +90,9 @@ func TestMedian(t *testing.T) { |
|
|
|
func TestEffectiveStake(t *testing.T) { |
|
|
|
func TestEffectiveStake(t *testing.T) { |
|
|
|
for _, val := range testingPurchases { |
|
|
|
for _, val := range testingPurchases { |
|
|
|
expectedStake := numeric.MaxDec( |
|
|
|
expectedStake := numeric.MaxDec( |
|
|
|
numeric.MinDec(numeric.OneDec().Add(c).Mul(expectedMedian), val.Stake), |
|
|
|
numeric.MinDec(numeric.OneDec().Add(c).Mul(expectedMedian), val.RawStake), |
|
|
|
numeric.OneDec().Sub(c).Mul(expectedMedian)) |
|
|
|
numeric.OneDec().Sub(c).Mul(expectedMedian)) |
|
|
|
calculatedStake := effectiveStake(expectedMedian, val.Stake) |
|
|
|
calculatedStake := effectiveStake(expectedMedian, val.RawStake) |
|
|
|
if !expectedStake.Equal(calculatedStake) { |
|
|
|
if !expectedStake.Equal(calculatedStake) { |
|
|
|
t.Errorf( |
|
|
|
t.Errorf( |
|
|
|
"Expected: %s, Got: %s", expectedStake.String(), calculatedStake.String(), |
|
|
|
"Expected: %s, Got: %s", expectedStake.String(), calculatedStake.String(), |
|
|
|