Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: block production misbehavior detection #1071

Merged
merged 53 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
b42b2a8
include fraud checking
Sep 11, 2024
bf0e7eb
update test
Sep 11, 2024
c7939c7
first check on mockFraud Handler
Sep 12, 2024
7fb5faa
fraud check done
Sep 15, 2024
bb1ebfb
add fraud check on p2p layer
Sep 16, 2024
1bb5ffe
even received check
Sep 16, 2024
11c6703
fix tests
Sep 16, 2024
242863e
add validation for block timestamp fraud
Sep 16, 2024
07277e1
include test for block validation and fraud
Sep 17, 2024
2c147dd
include fraud test errors
Sep 17, 2024
a45ef32
fix tests
Sep 17, 2024
c58d16d
fix units
Sep 17, 2024
427abd7
remove generic handler
Sep 17, 2024
32fb103
fix linter
Sep 17, 2024
58cf2a8
reordering imports by hand
Sep 17, 2024
a9ca873
pr comments 1
Sep 19, 2024
4963924
fix pr suggestions
Sep 19, 2024
94923c0
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Sep 19, 2024
b913735
move some packages
Sep 19, 2024
338ad82
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Sep 19, 2024
cd804f3
remove unused file
Sep 19, 2024
835dcae
Merge branch 'feat/adr-fraud-handling' of github.com-faulty:dymension…
Sep 20, 2024
f88b299
export methods
Sep 20, 2024
48c862f
update comments
Sep 20, 2024
d54bdb3
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Sep 27, 2024
e3ffab8
fix test
Sep 30, 2024
7ba65a8
fix linter
Sep 30, 2024
921ff1e
block height invalid, fraud
Oct 2, 2024
d5f89ef
update last block height comparison
Oct 2, 2024
a1a8d01
add last header block hash
Oct 2, 2024
c623b40
add validations
Oct 7, 2024
3378be8
add sequencer hash
Oct 7, 2024
465ef3f
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Oct 7, 2024
99abc4e
include next sequencer hash
Oct 8, 2024
6408d12
include tmsignature validate
Oct 8, 2024
5191dda
linter
Oct 8, 2024
ec1a825
add missing lib
Oct 16, 2024
e5a767d
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Oct 16, 2024
dec030c
add to state serialization
Oct 16, 2024
a978587
include param to last header hash to UpdateStateFromApp
Oct 16, 2024
1ae1c11
Add fraud handler to manager.
Oct 16, 2024
fc85b29
Fix units
Oct 17, 2024
806fc8c
solve some issues based on the PR comments
Oct 17, 2024
710f523
comment applyBlockWithFraudHandling
Oct 17, 2024
40e5f8b
remove comment
Oct 17, 2024
684c515
simplify apply block with block fraud handling
Oct 17, 2024
decd83e
cleanup the code and duplications on validate
Oct 17, 2024
e740570
update comments
Oct 17, 2024
ad473d1
expected heights
Oct 17, 2024
2e27ce7
updated fraud info
Oct 17, 2024
aa74bec
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Oct 22, 2024
166ef41
update branch with validator set
Oct 22, 2024
adffd84
move err invalid signature fraud
Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ packages:
github.com/dymensionxyz/dymint/p2p:
interfaces:
GetProposerI:
github.com/dymensionxyz/dymint/block:
interfaces:
ExecutorI:
github.com/dymensionxyz/dymint/fraud:
interfaces:
Handler:




Expand Down
20 changes: 18 additions & 2 deletions block/block.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package block

import (
"context"
"errors"
"fmt"
"github.com/dymensionxyz/dymint/fraud"

errorsmod "cosmossdk.io/errors"

"github.com/dymensionxyz/dymint/types"
)

// applyBlockWithFraudHandling calls applyBlock and handles fraud errors.
// Contract: block and commit must be validated before calling this function!
func (m *Manager) applyBlockWithFraudHandling(block *types.Block, commit *types.Commit, blockMetaData types.BlockMetaData) error {
err := m.applyBlock(block, commit, blockMetaData)
if errors.Is(err, fraud.ErrFraud) {
m.FraudHandler.HandleFault(context.Background(), err)
return fmt.Errorf("apply block: %w", err)
} else if err != nil {
return fmt.Errorf("apply block: %w", err)
}

return nil
}

// applyBlock applies the block to the store and the abci app.
// Contract: block and commit must be validated before calling this function!
// steps: save block -> execute block with app -> update state -> commit block to app -> update state's height and commit result.
Expand Down Expand Up @@ -152,12 +169,11 @@ func (m *Manager) attemptApplyCachedBlocks() error {
return fmt.Errorf("block not valid at height %d, dropping it: err:%w", cachedBlock.Block.Header.Height, err)
}

err := m.applyBlock(cachedBlock.Block, cachedBlock.Commit, types.BlockMetaData{Source: cachedBlock.Source})
err := m.applyBlockWithFraudHandling(cachedBlock.Block, cachedBlock.Commit, types.BlockMetaData{Source: cachedBlock.Source})
if err != nil {
return fmt.Errorf("apply cached block: expected height: %d: %w", expectedHeight, err)
}
m.logger.Info("Block applied", "height", expectedHeight)

}

return nil
Expand Down
14 changes: 13 additions & 1 deletion block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ import (
// default minimum block max size allowed. not specific reason to set it to 10K, but we need to avoid no transactions can be included in a block.
const minBlockMaxBytes = 10000

type ExecutorI interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont need I suffix in go

Copy link
Contributor

@danwt danwt Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and interfaces should be defined in consumer package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm it is already

InitChain(genesis *tmtypes.GenesisDoc, valset []*tmtypes.Validator) (*abci.ResponseInitChain, error)
CreateBlock(height uint64, lastCommit *types.Commit, lastHeaderHash, nextSeqHash [32]byte, state *types.State, maxBlockDataSizeBytes uint64) *types.Block
Commit(state *types.State, block *types.Block, resp *tmstate.ABCIResponses) ([]byte, int64, error)
GetAppInfo() (*abci.ResponseInfo, error)
ExecuteBlock(state *types.State, block *types.Block) (*tmstate.ABCIResponses, error)
UpdateStateAfterInitChain(s *types.State, res *abci.ResponseInitChain)
UpdateMempoolAfterInitChain(s *types.State)
UpdateStateAfterCommit(s *types.State, resp *tmstate.ABCIResponses, appHash []byte, height uint64)
UpdateProposerFromBlock(s *types.State, block *types.Block) bool
}

// Executor creates and applies blocks and maintains state.
type Executor struct {
localAddress []byte
Expand All @@ -34,7 +46,7 @@ type Executor struct {

// NewExecutor creates new instance of BlockExecutor.
// localAddress will be used in sequencer mode only.
func NewExecutor(localAddress []byte, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConns, eventBus *tmtypes.EventBus, logger types.Logger) (*Executor, error) {
func NewExecutor(localAddress []byte, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConns, eventBus *tmtypes.EventBus, logger types.Logger) (ExecutorI, error) {
be := Executor{
localAddress: localAddress,
chainID: chainID,
Expand Down
21 changes: 21 additions & 0 deletions block/fraud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package block

import (
"context"
"github.com/dymensionxyz/dymint/node/events"
uevent "github.com/dymensionxyz/dymint/utils/event"
)

type FreezeHandler struct {
m *Manager
}
danwt marked this conversation as resolved.
Show resolved Hide resolved

func (f FreezeHandler) HandleFault(ctx context.Context, fault error) {
uevent.MustPublish(context.TODO(), f.m.Pubsub, &events.DataHealthStatus{Error: fault}, events.HealthStatusList)
faultytolly marked this conversation as resolved.
Show resolved Hide resolved
}

func NewFreezeHandler(manager *Manager) *FreezeHandler {
return &FreezeHandler{
m: manager,
}
}
15 changes: 12 additions & 3 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"errors"
"fmt"
"github.com/dymensionxyz/dymint/fraud"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -45,7 +46,7 @@
// Store and execution
Store store.Store
State *types.State
Executor *Executor
Executor ExecutorI

// Clients and servers
Pubsub *pubsub.Server
Expand Down Expand Up @@ -77,6 +78,9 @@

// TargetHeight holds the value of the current highest block seen from either p2p (probably higher) or the DA
TargetHeight atomic.Uint64

// Fraud handler
FraudHandler fraud.Handler
}

// NewManager creates new block Manager.
Expand Down Expand Up @@ -116,6 +120,7 @@
blockCache: &Cache{
cache: make(map[uint64]types.CachedBlock),
},
FraudHandler: nil, // TODO: create a default handler
}

err = m.LoadStateOnInit(store, genesis, logger)
Expand All @@ -137,6 +142,10 @@
return m, nil
}

func (m *Manager) SetFraudHandler(handler fraud.Handler) {
m.FraudHandler = handler
}
faultytolly marked this conversation as resolved.
Show resolved Hide resolved

// Start starts the block manager.
func (m *Manager) Start(ctx context.Context) error {
// Check if InitChain flow is needed
Expand Down Expand Up @@ -171,8 +180,8 @@
}()

// P2P Sync. Subscribe to P2P received blocks events
go uevent.MustSubscribe(ctx, m.Pubsub, "applyGossipedBlocksLoop", p2p.EventQueryNewGossipedBlock, m.onReceivedBlock, m.logger)
go uevent.MustSubscribe(ctx, m.Pubsub, "applyBlockSyncBlocksLoop", p2p.EventQueryNewBlockSyncBlock, m.onReceivedBlock, m.logger)
go uevent.MustSubscribe(ctx, m.Pubsub, "applyGossipedBlocksLoop", p2p.EventQueryNewGossipedBlock, m.OnReceivedBlock, m.logger)

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
go uevent.MustSubscribe(ctx, m.Pubsub, "applyBlockSyncBlocksLoop", p2p.EventQueryNewBlockSyncBlock, m.OnReceivedBlock, m.logger)

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism
return nil
}

Expand Down
Loading
Loading