Skip to content

Commit

Permalink
sortition: use external sortition package (#5429)
Browse files Browse the repository at this point in the history
  • Loading branch information
cce authored Jun 2, 2023
1 parent 7d8525e commit f66a404
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 833 deletions.
4 changes: 2 additions & 2 deletions data/committee/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/committee/sortition"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/sortition"
)

type (
Expand Down Expand Up @@ -103,7 +103,7 @@ func (cred UnauthenticatedCredential) Verify(proto config.ConsensusParams, m Mem
} else if m.TotalMoney.IsZero() || expectedSelection == 0 || expectedSelection > float64(m.TotalMoney.Raw) {
logging.Base().Panicf("UnauthenticatedCredential.Verify: m.TotalMoney %v, expectedSelection %v", m.TotalMoney.Raw, expectedSelection)
} else if !userMoney.IsZero() {
weight = sortition.Select(userMoney.Raw, m.TotalMoney.Raw, expectedSelection, h)
weight = sortition.Select(userMoney.Raw, m.TotalMoney.Raw, expectedSelection, sortition.Digest(h))
}

if weight == 0 {
Expand Down
2 changes: 1 addition & 1 deletion data/committee/sortition/sortition.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "sortition.h"
#include <boost/math/distributions/binomial.hpp>

uint64_t sortition_binomial_cdf_walk(double n, double p, double ratio, uint64_t money) {
uint64_t sortition_binomial_cdf_walk_old(double n, double p, double ratio, uint64_t money) {
boost::math::binomial_distribution<double> dist(n, p);
for (uint64_t j = 0; j < money; j++) {
// Get the cdf
Expand Down
2 changes: 1 addition & 1 deletion data/committee/sortition/sortition.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Select(money uint64, totalMoney uint64, expectedSize float64, vrfOutput cry
ratio := big.Float{}
cratio, _ := ratio.Quo(&h, maxFloat).Float64()

return uint64(C.sortition_binomial_cdf_walk(C.double(binomialN), C.double(binomialP), C.double(cratio), C.uint64_t(money)))
return uint64(C.sortition_binomial_cdf_walk_old(C.double(binomialN), C.double(binomialP), C.double(cratio), C.uint64_t(money)))
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion data/committee/sortition/sortition.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifdef __cplusplus
extern "C" {
#endif
uint64_t sortition_binomial_cdf_walk(double n, double p, double ratio, uint64_t money);
uint64_t sortition_binomial_cdf_walk_old(double n, double p, double ratio, uint64_t money);
#ifdef __cplusplus
}

Expand Down
60 changes: 60 additions & 0 deletions data/committee/sortition/sortition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ package sortition

import (
"math/rand"
"os"
"strconv"
"testing"

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/algorand/sortition"
"pgregory.net/rapid"
)

func BenchmarkSortition(b *testing.B) {
Expand Down Expand Up @@ -62,3 +68,57 @@ func TestSortitionBasic(t *testing.T) {
t.Errorf("wanted %d selections but got %d, d=%d, maxd=%d", expected, hitcount, d, maxd)
}
}

func TestCompareSortitionImpls(tt *testing.T) {
partitiontest.PartitionTest(tt)

// if TOTAL_MONEY env var is set, parse uint64 from env
var envTotalMoney uint64
if tm := os.Getenv("TOTAL_MONEY"); tm != "" {
if val, err := strconv.ParseUint(tm, 10, 64); err == nil {
envTotalMoney = val
tt.Logf("using TOTAL_MONEY=%d from env", envTotalMoney)
}
}

rapid.Check(tt, func(t *rapid.T) {
// select one of the protocol committee sizes
proto := config.Consensus[protocol.ConsensusCurrentVersion]
expectedSize := rapid.OneOf(
rapid.Just(proto.NumProposers),
rapid.Just(proto.SoftCommitteeSize),
rapid.Just(proto.CertCommitteeSize),
rapid.Just(proto.NextCommitteeSize),
rapid.Just(proto.LateCommitteeSize),
rapid.Just(proto.RedoCommitteeSize),
rapid.Just(proto.DownCommitteeSize),
).Draw(t, "expectedSize").(uint64)
//expectedSize := rapid.Uint64Range(1, totalMoney).Draw(t, "expectedSize").(uint64) // draw random

// total online circulation (must be at least committee size)
var totalMoney uint64
if envTotalMoney != 0 {
totalMoney = envTotalMoney
} else {
const totalMicroAlgos = 10000000000000000
totalMoney = rapid.Uint64Range(expectedSize, totalMicroAlgos).Draw(t, "totalMoney").(uint64)
}

// participating account balance
money := rapid.Uint64Range(1, totalMoney-1).Draw(t, "money").(uint64)

// draw random vrf output
var vrfOutput crypto.Digest
vrfSeed := int64(rapid.Int64Min(0).Draw(t, "vrfSeed").(int64))
rnd := rand.New(rand.NewSource(vrfSeed))
rnd.Read(vrfOutput[:])

tt.Logf("money %d, totalMoney %d, expectedSize %d, vrfSeed %d, vrfOutput %x", money, totalMoney, expectedSize, vrfSeed, vrfOutput)
selectedLocal := Select(money, totalMoney, float64(expectedSize), vrfOutput)
selectedExtern := sortition.Select(money, totalMoney, float64(expectedSize), sortition.Digest(vrfOutput))
if selectedLocal != selectedExtern {
t.Fatalf("different results (money %d, totalMoney %d, expectedSize %d, vrfOutput %x): local %d, extern %d",
money, totalMoney, expectedSize, vrfOutput, selectedLocal, selectedExtern)
}
})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/algorand/graphtrace v0.1.0
github.com/algorand/msgp v1.1.53
github.com/algorand/oapi-codegen v1.12.0-algorand.0
github.com/algorand/sortition v1.0.0
github.com/algorand/websocket v1.4.6
github.com/aws/aws-sdk-go v1.33.0
github.com/consensys/gnark-crypto v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/algorand/msgp v1.1.53 h1:D6HKLyvLE6ltfsf8Apsrc+kqYb/CcOZEAfh1DpkPrNg=
github.com/algorand/msgp v1.1.53/go.mod h1:5K3d58/poT5fPmtiwuQft6GjgSrVEM46KoXdLrID8ZU=
github.com/algorand/oapi-codegen v1.12.0-algorand.0 h1:W9PvED+wAJc+9EeXPONnA+0zE9UhynEqoDs4OgAxKhk=
github.com/algorand/oapi-codegen v1.12.0-algorand.0/go.mod h1:tIWJ9K/qrLDVDt5A1p82UmxZIEGxv2X+uoujdhEAL48=
github.com/algorand/sortition v1.0.0 h1:PJiZtdSTBm4nArQrZXBnhlljHXhuyAXRJBqVWowQu3E=
github.com/algorand/sortition v1.0.0/go.mod h1:23CZwAbTWPv0bBsq+Php/2J6Y/iXDyzlfcZyepeY5Fo=
github.com/algorand/websocket v1.4.6 h1:I0kV4EYwatuUrKtNiwzYYgojgwh6pksDmlqntKG2Woc=
github.com/algorand/websocket v1.4.6/go.mod h1:HJmdGzFtnlUQ4nTzZP6WrT29oGYf1t6Ybi64vROcT+M=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
Expand Down
1 change: 1 addition & 0 deletions tools/block-generator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/algorand/go-sumhash v0.1.0 // indirect
github.com/algorand/msgp v1.1.53 // indirect
github.com/algorand/oapi-codegen v1.12.0-algorand.0 // indirect
github.com/algorand/sortition v1.0.0 // indirect
github.com/algorand/websocket v1.4.6 // indirect
github.com/aws/aws-sdk-go v1.33.0 // indirect
github.com/consensys/gnark-crypto v0.7.0 // indirect
Expand Down
Loading

0 comments on commit f66a404

Please sign in to comment.