2020-06-23 04:00:38 +00:00
|
|
|
package testing
|
2020-06-18 20:56:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2020-09-22 11:49:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
2020-06-18 20:56:23 +00:00
|
|
|
)
|
|
|
|
|
2020-06-23 04:00:38 +00:00
|
|
|
// BitlistWithAllBitsSet creates list of bitlists with all bits set.
|
2020-10-12 08:11:05 +00:00
|
|
|
func BitlistWithAllBitsSet(length uint64) bitfield.Bitlist {
|
2020-06-18 20:56:23 +00:00
|
|
|
b := bitfield.NewBitlist(length)
|
|
|
|
for i := uint64(0); i < length; i++ {
|
|
|
|
b.SetBitAt(i, true)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:00:38 +00:00
|
|
|
// BitlistsWithSingleBitSet creates list of bitlists with a single bit set in each.
|
2020-10-12 08:11:05 +00:00
|
|
|
func BitlistsWithSingleBitSet(n, length uint64) []bitfield.Bitlist {
|
2020-06-18 20:56:23 +00:00
|
|
|
lists := make([]bitfield.Bitlist, n)
|
|
|
|
for i := uint64(0); i < n; i++ {
|
|
|
|
b := bitfield.NewBitlist(length)
|
|
|
|
b.SetBitAt(i%length, true)
|
|
|
|
lists[i] = b
|
|
|
|
}
|
|
|
|
return lists
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:00:38 +00:00
|
|
|
// BitlistsWithMultipleBitSet creates list of bitlists with random n bits set.
|
|
|
|
func BitlistsWithMultipleBitSet(t testing.TB, n, length, count uint64) []bitfield.Bitlist {
|
2020-09-22 11:49:58 +00:00
|
|
|
seed := timeutils.Now().UnixNano()
|
2020-06-18 20:56:23 +00:00
|
|
|
t.Logf("bitlistsWithMultipleBitSet random seed: %v", seed)
|
|
|
|
rand.Seed(seed)
|
|
|
|
lists := make([]bitfield.Bitlist, n)
|
|
|
|
for i := uint64(0); i < n; i++ {
|
|
|
|
b := bitfield.NewBitlist(length)
|
|
|
|
keys := rand.Perm(int(length))
|
|
|
|
for _, key := range keys[:count] {
|
|
|
|
b.SetBitAt(uint64(key), true)
|
|
|
|
}
|
|
|
|
lists[i] = b
|
|
|
|
}
|
|
|
|
return lists
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:00:38 +00:00
|
|
|
// MakeAttestationsFromBitlists creates list of bitlists from list of attestations.
|
2020-10-12 08:11:05 +00:00
|
|
|
func MakeAttestationsFromBitlists(bl []bitfield.Bitlist) []*ethpb.Attestation {
|
2020-06-18 20:56:23 +00:00
|
|
|
atts := make([]*ethpb.Attestation, len(bl))
|
|
|
|
for i, b := range bl {
|
|
|
|
atts[i] = ðpb.Attestation{
|
|
|
|
AggregationBits: b,
|
|
|
|
Data: nil,
|
|
|
|
Signature: bls.NewAggregateSignature().Marshal(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return atts
|
|
|
|
}
|