Skip to content

Commit ce43197

Browse files
Add gossip fraud proof capability over p2p
1 parent 3c4318f commit ce43197

10 files changed

Lines changed: 856 additions & 306 deletions

File tree

‎block/manager.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type Manager struct {
5959
HeaderOutCh chan *types.Header
6060
HeaderInCh chan *types.Header
6161

62+
FraudProofCh chan *types.FraudProof
63+
6264
syncTarget uint64
6365
blockInCh chan newBlockEvent
6466
syncCache map[uint64]*types.Block
@@ -230,6 +232,9 @@ func (m *Manager) SyncLoop(ctx context.Context) {
230232
}
231233
delete(m.syncCache, currentHeight+1)
232234
}
235+
case fraudProof := <-m.FraudProofCh:
236+
m.logger.Debug("fraud proof received", "Block Height", fraudProof.BlockHeight)
237+
// TODO(manav): Set up a new cosmos app here
233238
case <-ctx.Done():
234239
return
235240
}

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
go.uber.org/multierr v1.8.0
2727
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2
2828
google.golang.org/grpc v1.46.2
29+
google.golang.org/protobuf v1.28.0
2930
)
3031

3132
require (
@@ -187,7 +188,6 @@ require (
187188
golang.org/x/tools v0.1.10 // indirect
188189
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
189190
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
190-
google.golang.org/protobuf v1.28.0 // indirect
191191
gopkg.in/ini.v1 v1.66.4 // indirect
192192
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
193193
gopkg.in/yaml.v2 v2.4.0 // indirect

‎go.sum‎

Lines changed: 0 additions & 268 deletions
Large diffs are not rendered by default.

‎node/node.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func NewNode(ctx context.Context, conf config.NodeConfig, p2pKey crypto.PrivKey,
153153

154154
node.P2P.SetTxValidator(node.newTxValidator())
155155
node.P2P.SetHeaderValidator(node.newHeaderValidator())
156+
node.P2P.SetFraudProofValidator(node.newFraudProofValidator())
156157

157158
return node, nil
158159
}
@@ -321,6 +322,23 @@ func (n *Node) newHeaderValidator() p2p.GossipValidator {
321322
}
322323
}
323324

325+
// newFraudProofValidator returns a pubsub validator that runs basic checks and forwards
326+
// the deserialized header for further processing
327+
func (n *Node) newFraudProofValidator() p2p.GossipValidator {
328+
return func(fraudProofMsg *p2p.GossipMessage) bool {
329+
n.Logger.Debug("fraud proof received", "from", fraudProofMsg.From, "bytes", len(fraudProofMsg.Data))
330+
var fraudProof types.FraudProof
331+
err := fraudProof.UnmarshalBinary(fraudProofMsg.Data)
332+
if err != nil {
333+
n.Logger.Error("failed to deserialize fraud proof", "error", err)
334+
return false
335+
}
336+
// TODO(manav): Add validation checks for fraud proof here
337+
n.blockManager.FraudProofCh <- &fraudProof
338+
return true
339+
}
340+
}
341+
324342
func createAndStartIndexerService(
325343
conf config.NodeConfig,
326344
kvStore store.KVStore,

‎p2p/client.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838

3939
// headerTopicSuffix is added after namespace to create pubsub topic for block header gossiping.
4040
headerTopicSuffix = "-header"
41+
42+
fraudProofTopicSuffix = "-fraudProof"
4143
)
4244

4345
// Client is a P2P client, implemented with libp2p.
@@ -60,6 +62,9 @@ type Client struct {
6062
headerGossiper *Gossiper
6163
headerValidator GossipValidator
6264

65+
fraudProofGossiper *Gossiper
66+
fraudProofValidator GossipValidator
67+
6368
// cancel is used to cancel context passed to libp2p functions
6469
// it's required because of discovery.Advertise call
6570
cancel context.CancelFunc
@@ -138,6 +143,7 @@ func (c *Client) Close() error {
138143
return multierr.Combine(
139144
c.txGossiper.Close(),
140145
c.headerGossiper.Close(),
146+
c.fraudProofGossiper.Close(),
141147
c.dht.Close(),
142148
c.host.Close(),
143149
)
@@ -165,6 +171,17 @@ func (c *Client) SetHeaderValidator(validator GossipValidator) {
165171
c.headerValidator = validator
166172
}
167173

174+
// GossipHeader sends a fraud proof to the P2P network.
175+
func (c *Client) GossipFraudProof(ctx context.Context, fraudProof []byte) error {
176+
c.logger.Debug("Gossiping fraud proof", "len", len(fraudProof))
177+
return c.fraudProofGossiper.Publish(ctx, fraudProof)
178+
}
179+
180+
// SetFraudProofValidator sets the callback function, that will be invoked after a fraud proof is received from P2P network.
181+
func (c *Client) SetFraudProofValidator(validator GossipValidator) {
182+
c.fraudProofValidator = validator
183+
}
184+
168185
func (c *Client) Addrs() []multiaddr.Multiaddr {
169186
return c.host.Addrs()
170187
}
@@ -316,6 +333,13 @@ func (c *Client) setupGossiping(ctx context.Context) error {
316333
}
317334
go c.headerGossiper.ProcessMessages(ctx)
318335

336+
c.fraudProofGossiper, err = NewGossiper(c.host, ps, c.getFraudProofTopic(), c.logger,
337+
WithValidator(c.fraudProofValidator))
338+
if err != nil {
339+
return err
340+
}
341+
go c.fraudProofGossiper.ProcessMessages(ctx)
342+
319343
return nil
320344
}
321345

@@ -356,3 +380,7 @@ func (c *Client) getTxTopic() string {
356380
func (c *Client) getHeaderTopic() string {
357381
return c.getNamespace() + headerTopicSuffix
358382
}
383+
384+
func (c *Client) getFraudProofTopic() string {
385+
return c.getNamespace() + fraudProofTopicSuffix
386+
}

‎proto/optimint/optimint.proto‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,17 @@ message Block {
7474
Data data = 2;
7575
Commit last_commit = 3;
7676
}
77+
78+
message FraudProof {
79+
uint64 block_height = 1;
80+
StateWitness state_witness = 2;
81+
}
82+
83+
message StateWitness {
84+
repeated WitnessData witness_data = 1;
85+
}
86+
87+
message WitnessData {
88+
bytes key = 1;
89+
bytes value = 2;
90+
}

‎types/block.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,17 @@ type Signature []byte
8989
type IntermediateStateRoots struct {
9090
RawRootsList [][]byte
9191
}
92+
93+
type FraudProof struct {
94+
BlockHeight uint64
95+
StateWitness StateWitness
96+
}
97+
98+
type StateWitness struct {
99+
WitnessData []WitnessData
100+
}
101+
102+
type WitnessData struct {
103+
Key []byte
104+
Value []byte
105+
}

0 commit comments

Comments
 (0)