2020-03-22 00:23:37 +00:00
|
|
|
package attestationutil_test
|
|
|
|
|
|
|
|
import (
|
2020-06-09 22:40:48 +00:00
|
|
|
"context"
|
2020-03-22 00:23:37 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-05-12 20:55:17 +00:00
|
|
|
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-22 00:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/attestationutil"
|
2020-06-09 22:40:48 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-18 16:31:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-10-08 23:20:18 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-03-22 00:23:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAttestingIndices(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
bf bitfield.Bitfield
|
2021-02-23 00:14:50 +00:00
|
|
|
committee []types.ValidatorIndex
|
2020-03-22 00:23:37 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2020-03-22 23:19:38 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []uint64
|
2021-01-20 03:00:52 +00:00
|
|
|
err string
|
2020-03-22 00:23:37 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Full committee attested",
|
|
|
|
args: args{
|
2020-03-22 23:19:38 +00:00
|
|
|
bf: bitfield.Bitlist{0b1111},
|
2021-02-23 00:14:50 +00:00
|
|
|
committee: []types.ValidatorIndex{0, 1, 2},
|
2020-03-22 00:23:37 +00:00
|
|
|
},
|
|
|
|
want: []uint64{0, 1, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Partial committee attested",
|
|
|
|
args: args{
|
2020-03-22 23:19:38 +00:00
|
|
|
bf: bitfield.Bitlist{0b1101},
|
2021-02-23 00:14:50 +00:00
|
|
|
committee: []types.ValidatorIndex{0, 1, 2},
|
2020-03-22 00:23:37 +00:00
|
|
|
},
|
|
|
|
want: []uint64{0, 2},
|
|
|
|
},
|
2021-01-20 03:00:52 +00:00
|
|
|
{
|
|
|
|
name: "Invalid bit length",
|
|
|
|
args: args{
|
|
|
|
bf: bitfield.Bitlist{0b11111},
|
2021-02-23 00:14:50 +00:00
|
|
|
committee: []types.ValidatorIndex{0, 1, 2},
|
2021-01-20 03:00:52 +00:00
|
|
|
},
|
|
|
|
err: "bitfield length 4 is not equal to committee length 3",
|
|
|
|
},
|
2020-03-22 00:23:37 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-01-20 03:00:52 +00:00
|
|
|
got, err := attestationutil.AttestingIndices(tt.args.bf, tt.args.committee)
|
|
|
|
if tt.err == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, tt.want, got)
|
|
|
|
} else {
|
|
|
|
require.ErrorContains(t, tt.err, err)
|
|
|
|
}
|
2020-03-22 00:23:37 +00:00
|
|
|
})
|
|
|
|
}
|
2020-03-22 01:42:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 22:40:48 +00:00
|
|
|
func TestIsValidAttestationIndices(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-08-25 15:23:06 +00:00
|
|
|
name string
|
|
|
|
att *eth.IndexedAttestation
|
|
|
|
wantedErr string
|
2020-06-09 22:40:48 +00:00
|
|
|
}{
|
2021-01-20 21:46:13 +00:00
|
|
|
{
|
|
|
|
name: "Indices should not be nil",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
},
|
|
|
|
wantedErr: "nil or missing indexed attestation data",
|
|
|
|
},
|
2020-06-09 22:40:48 +00:00
|
|
|
{
|
|
|
|
name: "Indices should be non-empty",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
2020-08-27 18:13:32 +00:00
|
|
|
Signature: make([]byte, 96),
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
2020-08-25 15:23:06 +00:00
|
|
|
wantedErr: "expected non-empty",
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Greater than max validators per committee",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
AttestingIndices: make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee+1),
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
2020-08-27 18:13:32 +00:00
|
|
|
Signature: make([]byte, 96),
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
2020-08-25 15:23:06 +00:00
|
|
|
wantedErr: "indices count exceeds",
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Needs to be sorted",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{3, 2, 1},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
2020-08-27 18:13:32 +00:00
|
|
|
Signature: make([]byte, 96),
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
2020-08-25 15:23:06 +00:00
|
|
|
wantedErr: "not uniquely sorted",
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid indices",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{1, 2, 3},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
2020-08-27 18:13:32 +00:00
|
|
|
Signature: make([]byte, 96),
|
2020-06-09 22:40:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-08 23:20:18 +00:00
|
|
|
{
|
|
|
|
name: "Valid indices with length of 2",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{1, 2},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid indices with length of 1",
|
|
|
|
att: ð.IndexedAttestation{
|
|
|
|
AttestingIndices: []uint64{1},
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
},
|
|
|
|
},
|
2020-06-09 22:40:48 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := attestationutil.IsValidAttestationIndices(context.Background(), tt.att)
|
2020-08-25 15:23:06 +00:00
|
|
|
if tt.wantedErr != "" {
|
|
|
|
assert.ErrorContains(t, tt.wantedErr, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
2020-06-09 22:40:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 01:42:51 +00:00
|
|
|
func BenchmarkAttestingIndices_PartialCommittee(b *testing.B) {
|
2020-07-08 09:57:50 +00:00
|
|
|
bf := bitfield.Bitlist{0b11111111, 0b11111111, 0b10000111, 0b11111111, 0b100}
|
2021-02-23 00:14:50 +00:00
|
|
|
committee := []types.ValidatorIndex{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34}
|
2020-03-22 01:42:51 +00:00
|
|
|
|
2020-07-08 09:57:50 +00:00
|
|
|
b.ResetTimer()
|
2020-03-22 01:42:51 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2021-01-20 03:00:52 +00:00
|
|
|
_, err := attestationutil.AttestingIndices(bf, committee)
|
|
|
|
require.NoError(b, err)
|
2020-03-22 01:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 20:55:17 +00:00
|
|
|
|
2020-10-08 23:20:18 +00:00
|
|
|
func BenchmarkIsValidAttestationIndices(b *testing.B) {
|
|
|
|
indices := make([]uint64, params.BeaconConfig().MaxValidatorsPerCommittee)
|
|
|
|
for i := 0; i < len(indices); i++ {
|
|
|
|
indices[i] = uint64(i)
|
|
|
|
}
|
|
|
|
att := ð.IndexedAttestation{
|
|
|
|
AttestingIndices: indices,
|
|
|
|
Data: ð.AttestationData{
|
|
|
|
Target: ð.Checkpoint{},
|
|
|
|
},
|
|
|
|
Signature: make([]byte, 96),
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if err := attestationutil.IsValidAttestationIndices(context.Background(), att); err != nil {
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 20:55:17 +00:00
|
|
|
func TestAttDataIsEqual(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
attData1 *eth.AttestationData
|
|
|
|
attData2 *eth.AttestationData
|
|
|
|
equal bool
|
|
|
|
}
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
name: "same",
|
|
|
|
attData1: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attData2: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
equal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "diff slot",
|
|
|
|
attData1: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attData2: ð.AttestationData{
|
|
|
|
Slot: 4,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "diff block",
|
|
|
|
attData1: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("good block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attData2: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "diff source root",
|
|
|
|
attData1: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attData2: ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("bad source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-18 16:31:42 +00:00
|
|
|
assert.Equal(t, tt.equal, attestationutil.AttDataIsEqual(tt.attData1, tt.attData2))
|
2020-06-17 13:50:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckPtIsEqual(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
checkPt1 *eth.Checkpoint
|
|
|
|
checkPt2 *eth.Checkpoint
|
|
|
|
equal bool
|
|
|
|
}
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
name: "same",
|
|
|
|
checkPt1: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
checkPt2: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
equal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "diff epoch",
|
|
|
|
checkPt1: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
checkPt2: ð.Checkpoint{
|
|
|
|
Epoch: 5,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
equal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "diff root",
|
|
|
|
checkPt1: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
checkPt2: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("bad source"),
|
|
|
|
},
|
|
|
|
equal: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-18 16:31:42 +00:00
|
|
|
assert.Equal(t, tt.equal, attestationutil.CheckPointIsEqual(tt.checkPt1, tt.checkPt2))
|
2020-05-12 20:55:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAttDataIsEqual(b *testing.B) {
|
|
|
|
attData1 := ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
attData2 := ð.AttestationData{
|
|
|
|
Slot: 5,
|
|
|
|
CommitteeIndex: 2,
|
|
|
|
BeaconBlockRoot: []byte("great block"),
|
|
|
|
Source: ð.Checkpoint{
|
|
|
|
Epoch: 4,
|
|
|
|
Root: []byte("good source"),
|
|
|
|
},
|
|
|
|
Target: ð.Checkpoint{
|
|
|
|
Epoch: 10,
|
|
|
|
Root: []byte("good target"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Run("fast", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-07-18 16:31:42 +00:00
|
|
|
assert.Equal(b, true, attestationutil.AttDataIsEqual(attData1, attData2))
|
2020-05-12 20:55:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Run("proto.Equal", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-07-18 16:31:42 +00:00
|
|
|
assert.Equal(b, true, attestationutil.AttDataIsEqual(attData1, attData2))
|
2020-05-12 20:55:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|