Skip to content

Commit

Permalink
enable fraud service to full & light nodes and extend abci.FraudProof…
Browse files Browse the repository at this point in the history
… with go-fraud Proof interface (cosmos#891)

As part of go-fraud library implementation, this PR adds:
* ProofService to full and light nodes
* Extends abci.FraudProof interface with go-fraud Proof interface

Fixes cosmos#892 cosmos#893 cosmos#894

---------

Co-authored-by: Ganesha Upadhyaya <gupadhyaya@Ganeshas-MacBook-Pro-2.local>
  • Loading branch information
gupadhyaya and Ganesha Upadhyaya committed May 10, 2023
1 parent d6d030a commit 5bf92b8
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 27 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ A modular framework for rollups, with an ABCI-compatible client interface.
[![Go Report Card](https://goreportcard.com/badge/github.com/rollkit/rollkit)](https://goreportcard.com/report/github.com/rollkit/rollkit)
[![codecov](https://codecov.io/gh/rollkit/rollkit/branch/main/graph/badge.svg?token=CWGA4RLDS9)](https://codecov.io/gh/rollkit/rollkit)
[![GoDoc](https://godoc.org/github.com/rollkit/rollkit?status.svg)](https://godoc.org/github.com/rollkit/rollkit)
[![Twitter Follow](https://img.shields.io/twitter/follow/CelestiaOrg?style=social)](https://twitter.com/CelestiaOrg)

## Building From Source

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/celestiaorg/go-cnc v0.3.0
github.com/celestiaorg/go-fraud v0.1.0
github.com/celestiaorg/go-header v0.2.6
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/go-kit/kit v0.12.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/celestiaorg/go-cnc v0.3.0 h1:eAVPNHGpx+2sBO7NZyQ1+VW8rzf6W4FQDlSq6aqSTsM=
github.com/celestiaorg/go-cnc v0.3.0/go.mod h1:zYzvHudSd1iNPuHBMyvZ1YvWou5aT9JXgtch9Tkaf70=
github.com/celestiaorg/go-fraud v0.1.0 h1:v6mZvlmf2J5ELZfPnrtmmOvKbaYIUs/erDWPO8NbZyY=
github.com/celestiaorg/go-fraud v0.1.0/go.mod h1:yoNM35cKMAkt5Mi/Qx3Wi9bnPilLi8n6RpHZVglTUDs=
github.com/celestiaorg/go-header v0.2.6 h1:f1Mlyu+EfDpkuzO3SWU5dow+ga2vLQ7hNuvsOe//z64=
github.com/celestiaorg/go-header v0.2.6/go.mod h1:i9OpY70+PJ1xPw1IgMfF0Pk6vBD6VWPmjY3bgubJBcU=
github.com/celestiaorg/go-libp2p-messenger v0.2.0 h1:/0MuPDcFamQMbw9xTZ73yImqgTO3jHV7wKHvWD/Irao=
Expand Down
62 changes: 43 additions & 19 deletions node/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"fmt"

"github.com/celestiaorg/go-fraud/fraudserv"
"github.com/celestiaorg/go-header"
ds "github.com/ipfs/go-datastore"
ktds "github.com/ipfs/go-datastore/keytransform"
"github.com/libp2p/go-libp2p/core/crypto"
Expand All @@ -32,6 +34,7 @@ import (
"github.com/rollkit/rollkit/state/txindex"
"github.com/rollkit/rollkit/state/txindex/kv"
"github.com/rollkit/rollkit/store"
"github.com/rollkit/rollkit/types"
)

// prefixes used in KV store to separate main node data from DALC data
Expand Down Expand Up @@ -76,7 +79,9 @@ type FullNode struct {
BlockIndexer indexer.BlockIndexer
IndexerService *txindex.IndexerService

hExService *HeaderExchangeService
hExService *HeaderExchangeService
fraudService *fraudserv.ProofService
proofServiceFactory ProofServiceFactory

// keep context here only because of API compatibility
// - it's used in `OnStart` (defined in service.Service interface)
Expand Down Expand Up @@ -161,27 +166,38 @@ func newFullNode(
return nil, fmt.Errorf("HeaderExchangeService initialization error: %w", err)
}

fraudProofFactory := NewProofServiceFactory(
client,
func(ctx context.Context, u uint64) (header.Header, error) {
return headerExchangeService.headerStore.GetByHeight(ctx, u)
},
mainKV,
true,
types.StateFraudProofType,
)

ctx, cancel := context.WithCancel(ctx)

node := &FullNode{
proxyApp: proxyApp,
eventBus: eventBus,
genesis: genesis,
conf: conf,
P2P: client,
blockManager: blockManager,
dalc: dalc,
Mempool: mp,
mempoolIDs: mpIDs,
incomingTxCh: make(chan *p2p.GossipMessage),
Store: s,
TxIndexer: txIndexer,
IndexerService: indexerService,
BlockIndexer: blockIndexer,
hExService: headerExchangeService,
ctx: ctx,
cancel: cancel,
DoneBuildingBlock: doneBuildingChannel,
proxyApp: proxyApp,
eventBus: eventBus,
genesis: genesis,
conf: conf,
P2P: client,
blockManager: blockManager,
dalc: dalc,
Mempool: mp,
mempoolIDs: mpIDs,
incomingTxCh: make(chan *p2p.GossipMessage),
Store: s,
TxIndexer: txIndexer,
IndexerService: indexerService,
BlockIndexer: blockIndexer,
hExService: headerExchangeService,
proofServiceFactory: fraudProofFactory,
ctx: ctx,
cancel: cancel,
DoneBuildingBlock: doneBuildingChannel,
}

node.BaseService = *service.NewBaseService(logger, "Node", node)
Expand Down Expand Up @@ -269,6 +285,14 @@ func (n *FullNode) OnStart() error {
if err = n.dalc.Start(); err != nil {
return fmt.Errorf("error while starting data availability layer client: %w", err)
}

// since p2p pubsub and host are required to create ProofService,
// we have to delay the construction until Start and use the help of ProofServiceFactory
n.fraudService = n.proofServiceFactory.CreateProofService()
if err = n.fraudService.Start(n.ctx); err != nil {
return fmt.Errorf("error while starting fraud exchange service: %w", err)
}

if n.conf.Aggregator {
n.Logger.Info("working in aggregator mode", "block time", n.conf.BlockTime)
go n.blockManager.AggregationLoop(n.ctx, n.conf.LazyAggregator)
Expand Down
2 changes: 1 addition & 1 deletion node/header_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func newP2PExchange(
) (*goheaderp2p.Exchange[*types.SignedHeader], error) {
opts = append(opts,
goheaderp2p.WithNetworkID[goheaderp2p.ClientParameters](network),
goheaderp2p.WithChainID[goheaderp2p.ClientParameters](chainID),
goheaderp2p.WithChainID(chainID),
)
return goheaderp2p.NewExchange[*types.SignedHeader](host, peers, conngater, opts...)
}
Expand Down
33 changes: 27 additions & 6 deletions node/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/celestiaorg/go-fraud/fraudserv"
"github.com/celestiaorg/go-header"
ds "github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p/core/crypto"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -17,6 +19,7 @@ import (
"github.com/rollkit/rollkit/config"
"github.com/rollkit/rollkit/p2p"
"github.com/rollkit/rollkit/store"
"github.com/rollkit/rollkit/types"
)

var _ Node = &LightNode{}
Expand All @@ -28,7 +31,9 @@ type LightNode struct {

proxyApp proxy.AppConns

hExService *HeaderExchangeService
hExService *HeaderExchangeService
fraudService *fraudserv.ProofService
proofServiceFactory ProofServiceFactory

ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -67,14 +72,25 @@ func newLightNode(
return nil, fmt.Errorf("HeaderExchangeService initialization error: %w", err)
}

fraudProofFactory := NewProofServiceFactory(
client,
func(ctx context.Context, u uint64) (header.Header, error) {
return headerExchangeService.headerStore.GetByHeight(ctx, u)
},
datastore,
true,
types.StateFraudProofType,
)

ctx, cancel := context.WithCancel(ctx)

node := &LightNode{
P2P: client,
proxyApp: proxyApp,
hExService: headerExchangeService,
cancel: cancel,
ctx: ctx,
P2P: client,
proxyApp: proxyApp,
hExService: headerExchangeService,
proofServiceFactory: fraudProofFactory,
cancel: cancel,
ctx: ctx,
}

node.P2P.SetTxValidator(node.falseValidator())
Expand Down Expand Up @@ -103,6 +119,11 @@ func (ln *LightNode) OnStart() error {
return fmt.Errorf("error while starting header exchange service: %w", err)
}

ln.fraudService = ln.proofServiceFactory.CreateProofService()
if err := ln.fraudService.Start(ln.ctx); err != nil {
return fmt.Errorf("error while starting fraud exchange service: %w", err)
}

return nil
}

Expand Down
38 changes: 38 additions & 0 deletions node/proof_service_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package node

import (
"github.com/celestiaorg/go-fraud"
"github.com/celestiaorg/go-fraud/fraudserv"
"github.com/ipfs/go-datastore"

"github.com/rollkit/rollkit/p2p"
)

type ProofServiceFactory struct {
client *p2p.Client
getter fraud.HeaderFetcher
ds datastore.Datastore
syncerEnabled bool
proofType fraud.ProofType
}

func NewProofServiceFactory(c *p2p.Client, getter fraud.HeaderFetcher, ds datastore.Datastore, syncerEnabled bool, proofType fraud.ProofType) ProofServiceFactory {
return ProofServiceFactory{
client: c,
getter: getter,
ds: ds,
syncerEnabled: syncerEnabled,
proofType: proofType,
}
}

func (factory *ProofServiceFactory) CreateProofService() *fraudserv.ProofService {
return fraudserv.NewProofService(
factory.client.PubSub(),
factory.client.Host(),
factory.getter,
factory.ds,
factory.syncerEnabled,
factory.proofType.String(),
)
}
47 changes: 47 additions & 0 deletions types/state_fraud_proof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package types

import (
"github.com/celestiaorg/go-header"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/celestiaorg/go-fraud"
)

// Implements Proof interface from https://github.com/celestiaorg/go-fraud/

const StateFraudProofType fraud.ProofType = "state-fraud"

type StateFraudProof struct {
abci.FraudProof
}

func init() {
fraud.Register(&StateFraudProof{})
}

func (fp *StateFraudProof) Type() fraud.ProofType {
return StateFraudProofType
}

func (fp *StateFraudProof) HeaderHash() []byte {
return fp.FraudulentBeginBlock.Hash
}

func (fp *StateFraudProof) Height() uint64 {
return uint64(fp.BlockHeight)
}

func (fp *StateFraudProof) Validate(header.Header) error {
// TODO (ganesh): fill this later
return nil
}

func (fp *StateFraudProof) MarshalBinary() (data []byte, err error) {
return fp.Marshal()
}

func (fp *StateFraudProof) UnmarshalBinary(data []byte) error {
return fp.Unmarshal(data)
}

var _ fraud.Proof = &StateFraudProof{}

0 comments on commit 5bf92b8

Please sign in to comment.