-
Notifications
You must be signed in to change notification settings - Fork 65
/
events.go
96 lines (81 loc) · 2.88 KB
/
events.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package p2p
import (
"fmt"
"github.com/dymensionxyz/dymint/p2p/pb"
"github.com/dymensionxyz/dymint/types"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
)
/* -------------------------------------------------------------------------- */
/* Event types */
/* -------------------------------------------------------------------------- */
const (
// EventTypeKey is a reserved composite key for event name.
EventTypeKey = "p2p.event"
)
// Define the event types
const (
EventNewGossipedBlock = "NewGossipedBlock"
)
/* -------------------------------------------------------------------------- */
/* Event Data */
/* -------------------------------------------------------------------------- */
// GossipedBlock defines the struct of the event data for the GossipedBlock
type GossipedBlock struct {
// Block is the block that was gossiped
Block types.Block
// Commit is the commit that was gossiped
Commit types.Commit
}
// MarshalBinary encodes GossipedBlock into binary form and returns it.
func (e *GossipedBlock) MarshalBinary() ([]byte, error) {
return e.ToProto().Marshal()
}
// UnmarshalBinary decodes binary form of GossipedBlock into object.
func (e *GossipedBlock) UnmarshalBinary(data []byte) error {
var pbGossipedBlock pb.GossipedBlock
err := pbGossipedBlock.Unmarshal(data)
if err != nil {
return err
}
err = e.FromProto(&pbGossipedBlock)
return err
}
// ToProto converts Data into protobuf representation and returns it.
func (e *GossipedBlock) ToProto() *pb.GossipedBlock {
return &pb.GossipedBlock{
Block: e.Block.ToProto(),
Commit: e.Commit.ToProto(),
}
}
// FromProto fills GossipedBlock with data from its protobuf representation.
func (e *GossipedBlock) FromProto(other *pb.GossipedBlock) error {
if err := e.Block.FromProto(other.Block); err != nil {
return err
}
if err := e.Commit.FromProto(other.Commit); err != nil {
return err
}
return nil
}
// Validate run basic validation on the gossiped block
func (e *GossipedBlock) Validate() error {
if err := e.Block.ValidateBasic(); err != nil {
return err
}
if err := e.Commit.ValidateBasic(); err != nil {
return err
}
return nil
}
/* -------------------------------------------------------------------------- */
/* Queries */
/* -------------------------------------------------------------------------- */
var (
// EventQueryNewNewGossipedBlock is the query used for getting EventNewGossipedBlock
EventQueryNewNewGossipedBlock = QueryForEvent(EventNewGossipedBlock)
)
// QueryForEvent returns a query for the given event.
func QueryForEvent(eventType string) tmpubsub.Query {
return tmquery.MustParse(fmt.Sprintf("%s='%s'", EventTypeKey, eventType))
}