Skip to content

Commit

Permalink
code-hygiene: Move fraud proofs to feature branch (cosmos#1092)
Browse files Browse the repository at this point in the history
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview

Closes: cosmos#1094

Moved the existing fraud proof related code to this
[branch](https://github.com/rollkit/rollkit/tree/with_abci_fraud_proofs).

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [x] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords

---------

Co-authored-by: Diego <31937514+Ferret-san@users.noreply.github.com>
Co-authored-by: Ganesha Upadhyaya <gupadhyaya@Ganeshas-MacBook-Pro-2.local>
  • Loading branch information
3 people authored Jul 25, 2023
1 parent efdb3e3 commit 06e9b98
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 655 deletions.
45 changes: 1 addition & 44 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync/atomic"
"time"

"github.com/celestiaorg/go-fraud/fraudserv"
abci "github.com/cometbft/cometbft/abci/types"
cmcrypto "github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/merkle"
Expand Down Expand Up @@ -120,7 +119,7 @@ func NewManager(
conf.DABlockTime = defaultDABlockTime
}

exec := state.NewBlockExecutor(proposerAddress, conf.NamespaceID, genesis.ChainID, mempool, proxyApp, conf.FraudProofs, eventBus, logger)
exec := state.NewBlockExecutor(proposerAddress, conf.NamespaceID, genesis.ChainID, mempool, proxyApp, eventBus, logger)
if s.LastBlockHeight+1 == genesis.InitialHeight {
res, err := exec.InitChain(genesis)
if err != nil {
Expand Down Expand Up @@ -246,45 +245,6 @@ func (m *Manager) AggregationLoop(ctx context.Context, lazy bool) {
}
}

func (m *Manager) SetFraudProofService(fraudProofServ *fraudserv.ProofService) {
m.executor.SetFraudProofService(fraudProofServ)
}

func (m *Manager) ProcessFraudProof(ctx context.Context, cancel context.CancelFunc) {
defer cancel()
// subscribe to state fraud proof
sub, err := m.executor.FraudService.Subscribe(types.StateFraudProofType)
if err != nil {
m.logger.Error("failed to subscribe to fraud proof gossip", "error", err)
return
}
defer sub.Cancel()

// blocks until a valid fraud proof is received via subscription
// sub.Proof is a blocking call that only returns on proof received or context ended
proof, err := sub.Proof(ctx)
if err != nil {
m.logger.Error("failed to receive gossiped fraud proof", "error", err)
return
}

// only handle the state fraud proofs for now
fraudProof, ok := proof.(*types.StateFraudProof)
if !ok {
m.logger.Error("unexpected type received for state fraud proof", "error", err)
return
}
m.logger.Debug("fraud proof received",
"block height", fraudProof.BlockHeight,
"pre-state app hash", fraudProof.PreStateAppHash,
"expected valid app hash", fraudProof.ExpectedValidAppHash,
"length of state witness", len(fraudProof.StateWitness),
)

// halt chain
m.logger.Info("verified fraud proof, halting chain")
}

// SyncLoop is responsible for syncing blocks.
//
// SyncLoop processes headers gossiped in P2p network to know what's the latest block height,
Expand All @@ -307,9 +267,6 @@ func (m *Manager) SyncLoop(ctx context.Context, cancel context.CancelFunc) {
m.retrieveCond.Signal()

err := m.trySyncNextBlock(ctx, daHeight)
if err != nil && err.Error() == fmt.Errorf("failed to ApplyBlock: %w", state.ErrFraudProofGenerated).Error() {
return
}
if err != nil {
m.logger.Info("failed to sync next block", "error", err)
}
Expand Down
4 changes: 0 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
flagDABlockTime = "rollkit.da_block_time"
flagDAStartHeight = "rollkit.da_start_height"
flagNamespaceID = "rollkit.namespace_id"
flagFraudProofs = "rollkit.experimental_insecure_fraud_proofs"
flagLight = "rollkit.light"
flagTrustedHash = "rollkit.trusted_hash"
flagLazyAggregator = "rollkit.lazy_aggregator"
Expand Down Expand Up @@ -55,7 +54,6 @@ type BlockManagerConfig struct {
// DAStartHeight allows skipping first DAStartHeight-1 blocks when querying for blocks.
DAStartHeight uint64 `mapstructure:"da_start_height"`
NamespaceID types.NamespaceID `mapstructure:"namespace_id"`
FraudProofs bool `mapstructure:"fraud_proofs"`
}

// GetViperConfig reads configuration parameters from Viper instance.
Expand All @@ -70,7 +68,6 @@ func (nc *NodeConfig) GetViperConfig(v *viper.Viper) error {
nc.BlockTime = v.GetDuration(flagBlockTime)
nc.LazyAggregator = v.GetBool(flagLazyAggregator)
nsID := v.GetString(flagNamespaceID)
nc.FraudProofs = v.GetBool(flagFraudProofs)
nc.Light = v.GetBool(flagLight)
bytes, err := hex.DecodeString(nsID)
if err != nil {
Expand All @@ -94,7 +91,6 @@ func AddFlags(cmd *cobra.Command) {
cmd.Flags().Duration(flagDABlockTime, def.DABlockTime, "DA chain block time (for syncing)")
cmd.Flags().Uint64(flagDAStartHeight, def.DAStartHeight, "starting DA block height (for syncing)")
cmd.Flags().BytesHex(flagNamespaceID, def.NamespaceID[:], "namespace identifies (8 bytes in hex)")
cmd.Flags().Bool(flagFraudProofs, def.FraudProofs, "enable fraud proofs (experimental & insecure)")
cmd.Flags().Bool(flagLight, def.Light, "run light client")
cmd.Flags().String(flagTrustedHash, def.TrustedHash, "initial trusted hash to start the header exchange service")
}
2 changes: 0 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestViperAndCobra(t *testing.T) {
assert.NoError(cmd.Flags().Set(flagDAConfig, `{"json":true}`))
assert.NoError(cmd.Flags().Set(flagBlockTime, "1234s"))
assert.NoError(cmd.Flags().Set(flagNamespaceID, "0102030405060708"))
assert.NoError(cmd.Flags().Set(flagFraudProofs, "false"))

nc := DefaultNodeConfig
assert.NoError(nc.GetViperConfig(v))
Expand All @@ -36,5 +35,4 @@ func TestViperAndCobra(t *testing.T) {
assert.Equal(`{"json":true}`, nc.DAConfig)
assert.Equal(1234*time.Second, nc.BlockTime)
assert.Equal(types.NamespaceID{1, 2, 3, 4, 5, 6, 7, 8}, nc.NamespaceID)
assert.Equal(false, nc.FraudProofs)
}
1 change: 0 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var DefaultNodeConfig = NodeConfig{
BlockManagerConfig: BlockManagerConfig{
BlockTime: 30 * time.Second,
NamespaceID: types.NamespaceID{},
FraudProofs: false,
},
DALayer: "mock",
DAConfig: "",
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ go 1.20

require (
cosmossdk.io/math v1.0.1
github.com/celestiaorg/go-fraud v0.1.2
github.com/celestiaorg/go-header v0.2.12
github.com/celestiaorg/nmt v0.17.0
github.com/celestiaorg/rsmt2d v0.9.0
github.com/celestiaorg/utils v0.1.0
github.com/cometbft/cometbft v0.37.0
github.com/cometbft/cometbft v0.37.1
github.com/creachadair/taskgroup v0.3.2
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/go-kit/kit v0.12.0
Expand Down Expand Up @@ -42,6 +41,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/celestiaorg/go-fraud v0.1.2 // indirect
github.com/celestiaorg/go-libp2p-messenger v0.2.0 // indirect
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand Down Expand Up @@ -188,7 +188,6 @@ require (
)

replace (
github.com/cometbft/cometbft => github.com/rollkit/cometbft v0.37.1
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4
google.golang.org/grpc => google.golang.org/grpc v1.33.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20211130200136-a8f946100490/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/cometbft/cometbft v0.37.1 h1:KLxkQTK2hICXYq21U2hn1W5hOVYUdQgDQ1uB+90xPIg=
github.com/cometbft/cometbft v0.37.1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/cometbft/cometbft-db v0.8.0 h1:vUMDaH3ApkX8m0KZvOFFy9b5DZHBAjsnEuo9AKVZpjo=
github.com/cometbft/cometbft-db v0.8.0/go.mod h1:6ASCP4pfhmrCBpfk01/9E1SI29nD3HfVHrY4PG8x5c0=
github.com/containerd/cgroups v0.0.0-20201119153540-4cbc285b3327/go.mod h1:ZJeTFisyysqgcCdecO57Dj79RfL0LNeGiFUqLYQRYLE=
Expand Down Expand Up @@ -1366,8 +1368,6 @@ github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rollkit/celestia-openrpc v0.1.1 h1:Ub8ydB1c0qDQJmYL+s7i5u9YiQyINxw88AL1S8A+Lig=
github.com/rollkit/celestia-openrpc v0.1.1/go.mod h1:Or8y0vuAzJu3SLOahIOt96+y4Vxaf74vrPFkUJl0oj0=
github.com/rollkit/cometbft v0.37.1 h1:Nj8Tg3MQoinOI1HmESlqoAgViCAlBb6I2GmAnZoytlQ=
github.com/rollkit/cometbft v0.37.1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE=
Expand Down
42 changes: 0 additions & 42 deletions mocks/Application.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 21 additions & 50 deletions node/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ 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 @@ -34,7 +32,6 @@ 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 @@ -79,10 +76,8 @@ type FullNode struct {
BlockIndexer indexer.BlockIndexer
IndexerService *txindex.IndexerService

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

// keep context here only because of API compatibility
// - it's used in `OnStart` (defined in service.Service interface)
Expand Down Expand Up @@ -167,16 +162,6 @@ 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,
genesis.ChainID,
)

blockExchangeService, err := NewBlockExchangeService(ctx, mainKV, conf, genesis, client, logger.With("module", "BlockExchangeService"))
if err != nil {
return nil, fmt.Errorf("BlockExchangeService initialization error: %w", err)
Expand All @@ -185,26 +170,25 @@ func newFullNode(
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,
bExService: blockExchangeService,
proofServiceFactory: fraudProofFactory,
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,
bExService: blockExchangeService,
ctx: ctx,
cancel: cancel,
DoneBuildingBlock: doneBuildingChannel,
}

node.BaseService = *service.NewBaseService(logger, "Node", node)
Expand Down Expand Up @@ -296,24 +280,12 @@ func (n *FullNode) OnStart() error {
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.AddVerifier(types.StateFraudProofType, VerifierFn(n.proxyApp)); err != nil {
return fmt.Errorf("error while registering verifier for fraud service: %w", err)
}
if err = n.fraudService.Start(n.ctx); err != nil {
return fmt.Errorf("error while starting fraud exchange service: %w", err)
}
n.blockManager.SetFraudProofService(n.fraudService)

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)
go n.headerPublishLoop(n.ctx)
go n.blockPublishLoop(n.ctx)
}
go n.blockManager.ProcessFraudProof(n.ctx, n.cancel)
go n.blockManager.RetrieveLoop(n.ctx)
go n.blockManager.SyncLoop(n.ctx, n.cancel)
return nil
Expand Down Expand Up @@ -341,7 +313,6 @@ func (n *FullNode) OnStop() {
err = multierr.Append(err, n.P2P.Close())
err = multierr.Append(err, n.hExService.Stop())
err = multierr.Append(err, n.bExService.Stop())
err = multierr.Append(err, n.fraudService.Stop(n.ctx))
n.Logger.Error("errors while stopping node:", "errors", err)
}

Expand Down
5 changes: 0 additions & 5 deletions node/full_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,6 @@ func TestTx(t *testing.T) {
mockApp.On("Commit", mock.Anything).Return(abci.ResponseCommit{})
mockApp.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
mockApp.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
mockApp.On("GetAppHash", mock.Anything).Return(abci.ResponseGetAppHash{})
mockApp.On("GenerateFraudProof", mock.Anything).Return(abci.ResponseGenerateFraudProof{})

err = rpc.node.Start()
require.NoError(err)
Expand Down Expand Up @@ -842,8 +840,6 @@ func createApp(require *require.Assertions, vKeyToRemove cmcrypto.PrivKey, wg *s
app.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
app.On("BeginBlock", mock.Anything).Return(abci.ResponseBeginBlock{})
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{})
app.On("GetAppHash", mock.Anything).Return(abci.ResponseGetAppHash{})
app.On("GenerateFraudProof", mock.Anything).Return(abci.ResponseGenerateFraudProof{})

pbValKey, err := encoding.PubKeyToProto(vKeyToRemove.PubKey())
require.NoError(err)
Expand Down Expand Up @@ -934,7 +930,6 @@ func TestMempool2Nodes(t *testing.T) {
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{})
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{})
app.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
app.On("GetAppHash", mock.Anything).Return(abci.ResponseGetAppHash{})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
Loading

0 comments on commit 06e9b98

Please sign in to comment.