Skip to content

Commit 9296bf2

Browse files
committed
test: Add test coverage for new v1 event types
1 parent 3ff5283 commit 9296bf2

11 files changed

+502
-5
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package v1
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/attestantio/go-eth2-client/spec/phase0"
8+
"github.com/ethpandaops/contributoor/internal/events/mock"
9+
"github.com/ethpandaops/ethwallclock"
10+
xatuethv1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1"
11+
"github.com/ethpandaops/xatu/pkg/proto/xatu"
12+
"github.com/prysmaticlabs/go-bitfield"
13+
"github.com/sirupsen/logrus"
14+
"github.com/stretchr/testify/require"
15+
"go.uber.org/mock/gomock"
16+
)
17+
18+
func TestAttestationEvent_Decorated(t *testing.T) {
19+
ctrl := gomock.NewController(t)
20+
defer ctrl.Finish()
21+
22+
var (
23+
now = time.Now()
24+
slot = uint64(123)
25+
sourceEpoch = uint64(2)
26+
targetEpoch = uint64(3)
27+
mockBeacon = mock.NewMockBeaconDataProvider(ctrl)
28+
mockSlot = ethwallclock.NewSlot(slot, now.Add(-10*time.Second), now)
29+
mockEpoch = ethwallclock.NewEpoch(targetEpoch, now.Add(-5*time.Minute), now)
30+
mockSource = ethwallclock.NewEpoch(sourceEpoch, now.Add(-10*time.Minute), now)
31+
mockTarget = ethwallclock.NewEpoch(targetEpoch, now.Add(-5*time.Minute), now)
32+
blockRoot = phase0.Root{0x1} // Simple root for testing
33+
sourceRoot = phase0.Root{0x2} // Simple root for testing
34+
targetRoot = phase0.Root{0x3} // Simple root for testing
35+
signature = phase0.BLSSignature{} // Zero signature
36+
aggrBits = bitfield.Bitlist([]byte{0x01, 0x01}) // Single bit set
37+
)
38+
39+
mockBeacon.EXPECT().GetSlot(slot).Return(mockSlot)
40+
mockBeacon.EXPECT().GetEpochFromSlot(slot).Return(mockEpoch)
41+
mockBeacon.EXPECT().GetEpoch(targetEpoch).Return(mockTarget)
42+
mockBeacon.EXPECT().GetEpochFromSlot(sourceEpoch).Return(mockSource)
43+
mockBeacon.EXPECT().GetValidatorIndex(
44+
phase0.Epoch(targetEpoch),
45+
phase0.Slot(slot),
46+
phase0.CommitteeIndex(1),
47+
uint64(0),
48+
).Return(phase0.ValidatorIndex(789), nil)
49+
50+
event := NewAttestationEvent(
51+
logrus.New(),
52+
mockBeacon,
53+
&xatu.Meta{
54+
Client: &xatu.ClientMeta{},
55+
},
56+
&phase0.Attestation{
57+
AggregationBits: aggrBits,
58+
Data: &phase0.AttestationData{
59+
Slot: phase0.Slot(slot),
60+
Index: phase0.CommitteeIndex(1),
61+
BeaconBlockRoot: blockRoot,
62+
Source: &phase0.Checkpoint{
63+
Epoch: phase0.Epoch(sourceEpoch),
64+
Root: sourceRoot,
65+
},
66+
Target: &phase0.Checkpoint{
67+
Epoch: phase0.Epoch(targetEpoch),
68+
Root: targetRoot,
69+
},
70+
},
71+
Signature: signature,
72+
},
73+
now,
74+
)
75+
76+
var (
77+
decorated = event.Decorated()
78+
metaData = decorated.Meta.Client.GetEthV1EventsAttestationV2()
79+
additionalData = decorated.GetEthV1EventsAttestationV2()
80+
)
81+
82+
// Assert event.
83+
require.NotNil(t, decorated)
84+
require.Equal(t, xatu.Event_BEACON_API_ETH_V1_EVENTS_ATTESTATION_V2.String(), event.Type())
85+
86+
// Assert additional data.
87+
require.NotNil(t, additionalData)
88+
require.Equal(t, "0x0101", additionalData.AggregationBits)
89+
require.Equal(t, xatuethv1.TrimmedString(signature.String()), additionalData.Signature)
90+
91+
// Assert attestation data.
92+
data := additionalData.Data
93+
require.NotNil(t, data)
94+
require.Equal(t, slot, data.Slot.Value)
95+
require.Equal(t, uint64(1), data.Index.Value)
96+
require.Equal(t, blockRoot.String(), data.BeaconBlockRoot)
97+
98+
// Assert source checkpoint.
99+
source := data.Source
100+
require.NotNil(t, source)
101+
require.Equal(t, sourceEpoch, source.Epoch.Value)
102+
require.Equal(t, sourceRoot.String(), source.Root)
103+
104+
// Assert target checkpoint.
105+
target := data.Target
106+
require.NotNil(t, target)
107+
require.Equal(t, targetEpoch, target.Epoch.Value)
108+
require.Equal(t, targetRoot.String(), target.Root)
109+
110+
// Assert metadata.
111+
require.NotNil(t, metaData)
112+
require.Equal(t, slot, metaData.Slot.Number.Value)
113+
require.Equal(t, targetEpoch, metaData.Epoch.Number.Value)
114+
115+
// Assert source metadata.
116+
require.NotNil(t, metaData.Source)
117+
require.Equal(t, sourceEpoch, metaData.Source.Epoch.Number.Value)
118+
119+
// Assert target metadata.
120+
require.NotNil(t, metaData.Target)
121+
require.Equal(t, targetEpoch, metaData.Target.Epoch.Number.Value)
122+
123+
// Assert attesting validator (since we have a single bit set).
124+
require.NotNil(t, metaData.AttestingValidator)
125+
require.Equal(t, uint64(0), metaData.AttestingValidator.CommitteeIndex.Value)
126+
require.Equal(t, uint64(789), metaData.AttestingValidator.Index.Value)
127+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package v1
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
8+
"github.com/attestantio/go-eth2-client/spec/deneb"
9+
"github.com/attestantio/go-eth2-client/spec/phase0"
10+
"github.com/ethpandaops/contributoor/internal/events/mock"
11+
"github.com/ethpandaops/ethwallclock"
12+
xatuethv1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1"
13+
"github.com/ethpandaops/xatu/pkg/proto/xatu"
14+
"github.com/sirupsen/logrus"
15+
"github.com/stretchr/testify/require"
16+
"go.uber.org/mock/gomock"
17+
)
18+
19+
func TestBlobSidecarEvent_Decorated(t *testing.T) {
20+
ctrl := gomock.NewController(t)
21+
defer ctrl.Finish()
22+
23+
var (
24+
now = time.Now()
25+
slot = uint64(123)
26+
mockBeacon = mock.NewMockBeaconDataProvider(ctrl)
27+
mockSlot = ethwallclock.NewSlot(slot, now.Add(-10*time.Second), now)
28+
mockEpoch = ethwallclock.NewEpoch(3, now.Add(-5*time.Minute), now)
29+
blockRoot = phase0.Root{0x1} // Simple root for testing
30+
kzgCommitment = deneb.KZGCommitment{} // Zero commitment
31+
versionedHash = deneb.VersionedHash{} // Zero hash
32+
index = deneb.BlobIndex(1)
33+
)
34+
35+
mockBeacon.EXPECT().GetSlot(slot).Return(mockSlot)
36+
mockBeacon.EXPECT().GetEpochFromSlot(slot).Return(mockEpoch)
37+
38+
event := NewBlobSidecarEvent(
39+
logrus.New(),
40+
mockBeacon,
41+
&xatu.Meta{
42+
Client: &xatu.ClientMeta{},
43+
},
44+
&eth2v1.BlobSidecarEvent{
45+
Slot: phase0.Slot(slot),
46+
BlockRoot: blockRoot,
47+
Index: index,
48+
KZGCommitment: kzgCommitment,
49+
VersionedHash: versionedHash,
50+
},
51+
now,
52+
)
53+
54+
var (
55+
decorated = event.Decorated()
56+
metaData = decorated.Meta.Client.GetEthV1EventsBlobSidecar()
57+
additionalData = decorated.GetEthV1EventsBlobSidecar()
58+
)
59+
60+
// Assert event.
61+
require.NotNil(t, decorated)
62+
require.Equal(t, xatu.Event_BEACON_API_ETH_V1_EVENTS_BLOB_SIDECAR.String(), event.Type())
63+
64+
// Assert additional data.
65+
require.NotNil(t, additionalData)
66+
require.Equal(t, slot, additionalData.Slot.Value)
67+
require.Equal(t, uint64(1), additionalData.Index.Value)
68+
require.Equal(t, blockRoot.String(), additionalData.BlockRoot)
69+
require.Equal(t, xatuethv1.KzgCommitmentToString(kzgCommitment), additionalData.KzgCommitment)
70+
require.Equal(t, xatuethv1.VersionedHashToString(versionedHash), additionalData.VersionedHash)
71+
72+
// Assert metadata.
73+
require.NotNil(t, metaData)
74+
require.Equal(t, slot, metaData.Slot.Number.Value)
75+
require.Equal(t, uint64(3), metaData.Epoch.Number.Value)
76+
}

internal/events/v1/block.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func (e *BlockEvent) Data() interface{} {
4747
}
4848

4949
func (e *BlockEvent) Decorated() *xatu.DecoratedEvent {
50-
//TODO(@matty): Populate event data.
5150
decorated := &xatu.DecoratedEvent{
5251
Meta: e.Meta(),
5352
Event: &xatu.Event{

internal/events/v1/block_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package v1
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
8+
"github.com/attestantio/go-eth2-client/spec/phase0"
9+
"github.com/ethpandaops/contributoor/internal/events/mock"
10+
"github.com/ethpandaops/ethwallclock"
11+
"github.com/ethpandaops/xatu/pkg/proto/xatu"
12+
"github.com/sirupsen/logrus"
13+
"github.com/stretchr/testify/require"
14+
"go.uber.org/mock/gomock"
15+
)
16+
17+
func TestBlockEvent_Decorated(t *testing.T) {
18+
ctrl := gomock.NewController(t)
19+
defer ctrl.Finish()
20+
21+
var (
22+
now = time.Now()
23+
slot = uint64(123)
24+
mockBeacon = mock.NewMockBeaconDataProvider(ctrl)
25+
mockSlot = ethwallclock.NewSlot(slot, now.Add(-10*time.Second), now)
26+
mockEpoch = ethwallclock.NewEpoch(3, now.Add(-5*time.Minute), now)
27+
block = phase0.Root{0x1} // Simple root for testing
28+
)
29+
30+
mockBeacon.EXPECT().GetSlot(slot).Return(mockSlot)
31+
mockBeacon.EXPECT().GetEpochFromSlot(slot).Return(mockEpoch)
32+
33+
event := NewBlockEvent(
34+
logrus.New(),
35+
mockBeacon,
36+
&xatu.Meta{
37+
Client: &xatu.ClientMeta{},
38+
},
39+
&eth2v1.BlockEvent{
40+
Slot: phase0.Slot(slot),
41+
Block: block,
42+
ExecutionOptimistic: true,
43+
},
44+
now,
45+
)
46+
47+
var (
48+
decorated = event.Decorated()
49+
metaData = decorated.Meta.Client.GetEthV1EventsBlockV2()
50+
additionalData = decorated.GetEthV1EventsBlockV2()
51+
)
52+
53+
// Assert event.
54+
require.NotNil(t, decorated)
55+
require.Equal(t, xatu.Event_BEACON_API_ETH_V1_EVENTS_BLOCK_V2.String(), event.Type())
56+
57+
// Assert additional data.
58+
require.NotNil(t, additionalData)
59+
require.Equal(t, slot, additionalData.Slot.Value)
60+
require.Equal(t, block.String(), additionalData.Block)
61+
require.True(t, additionalData.ExecutionOptimistic)
62+
63+
// Assert metadata.
64+
require.NotNil(t, metaData)
65+
require.Equal(t, slot, metaData.Slot.Number.Value)
66+
require.Equal(t, uint64(3), metaData.Epoch.Number.Value)
67+
}

internal/events/v1/chain_reorg.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func (e *ChainReorgEvent) Data() interface{} {
4747
}
4848

4949
func (e *ChainReorgEvent) Decorated() *xatu.DecoratedEvent {
50-
//TODO(@matty): Populate event data.
5150
decorated := &xatu.DecoratedEvent{
5251
Meta: e.Meta(),
5352
Event: &xatu.Event{
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package v1
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
8+
"github.com/attestantio/go-eth2-client/spec/phase0"
9+
"github.com/ethpandaops/contributoor/internal/events/mock"
10+
"github.com/ethpandaops/ethwallclock"
11+
"github.com/ethpandaops/xatu/pkg/proto/xatu"
12+
"github.com/sirupsen/logrus"
13+
"github.com/stretchr/testify/require"
14+
"go.uber.org/mock/gomock"
15+
)
16+
17+
func TestChainReorgEvent_Decorated(t *testing.T) {
18+
ctrl := gomock.NewController(t)
19+
defer ctrl.Finish()
20+
21+
var (
22+
now = time.Now()
23+
slot = uint64(123)
24+
mockBeacon = mock.NewMockBeaconDataProvider(ctrl)
25+
mockSlot = ethwallclock.NewSlot(slot, now.Add(-10*time.Second), now)
26+
mockEpoch = ethwallclock.NewEpoch(3, now.Add(-5*time.Minute), now)
27+
oldRoot = phase0.Root{0x1} // Simple root for testing
28+
newRoot = phase0.Root{0x2} // Simple root for testing
29+
depth = uint64(2)
30+
)
31+
32+
mockBeacon.EXPECT().GetSlot(slot).Return(mockSlot)
33+
mockBeacon.EXPECT().GetEpochFromSlot(slot).Return(mockEpoch)
34+
35+
event := NewChainReorgEvent(
36+
logrus.New(),
37+
mockBeacon,
38+
&xatu.Meta{
39+
Client: &xatu.ClientMeta{},
40+
},
41+
&eth2v1.ChainReorgEvent{
42+
Slot: phase0.Slot(slot),
43+
Depth: depth,
44+
OldHeadBlock: oldRoot,
45+
NewHeadBlock: newRoot,
46+
OldHeadState: oldRoot,
47+
NewHeadState: newRoot,
48+
Epoch: phase0.Epoch(3),
49+
},
50+
now,
51+
)
52+
53+
var (
54+
decorated = event.Decorated()
55+
metaData = decorated.Meta.Client.GetEthV1EventsChainReorgV2()
56+
additionalData = decorated.GetEthV1EventsChainReorgV2()
57+
)
58+
59+
// Assert event.
60+
require.NotNil(t, decorated)
61+
require.Equal(t, xatu.Event_BEACON_API_ETH_V1_EVENTS_CHAIN_REORG_V2.String(), event.Type())
62+
63+
// Assert additional data.
64+
require.NotNil(t, additionalData)
65+
require.Equal(t, slot, additionalData.Slot.Value)
66+
require.Equal(t, depth, additionalData.Depth.Value)
67+
require.Equal(t, oldRoot.String(), additionalData.OldHeadBlock)
68+
require.Equal(t, newRoot.String(), additionalData.NewHeadBlock)
69+
require.Equal(t, oldRoot.String(), additionalData.OldHeadState)
70+
require.Equal(t, newRoot.String(), additionalData.NewHeadState)
71+
require.Equal(t, uint64(3), additionalData.Epoch.Value)
72+
73+
// Assert metadata.
74+
require.NotNil(t, metaData)
75+
require.Equal(t, slot, metaData.Slot.Number.Value)
76+
require.Equal(t, uint64(3), metaData.Epoch.Number.Value)
77+
}

internal/events/v1/contribution_and_proof.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func (e *ContributionAndProofEvent) Data() interface{} {
4747
}
4848

4949
func (e *ContributionAndProofEvent) Decorated() *xatu.DecoratedEvent {
50-
//TODO(@matty): Populate event data.
5150
decorated := &xatu.DecoratedEvent{
5251
Meta: e.Meta(),
5352
Event: &xatu.Event{

0 commit comments

Comments
 (0)