-
Notifications
You must be signed in to change notification settings - Fork 60
/
events.go
85 lines (72 loc) · 2.61 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
package p2p
import (
"github.com/dymensionxyz/dymint/p2p/pb"
"github.com/dymensionxyz/dymint/types"
uevent "github.com/dymensionxyz/dymint/utils/event"
)
/* -------------------------------------------------------------------------- */
/* Event types */
/* -------------------------------------------------------------------------- */
const (
// EventTypeKey is a reserved composite key for event name.
EventTypeKey = "p2p.event"
)
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 */
/* -------------------------------------------------------------------------- */
// EventQueryNewNewGossipedBlock is the query used for getting EventNewGossipedBlock
var EventQueryNewNewGossipedBlock = uevent.QueryFor(EventTypeKey, EventNewGossipedBlock)