Skip to content

Commit

Permalink
Merge branch 'feat/adr-fraud-handling' of github.com-faulty:dymension…
Browse files Browse the repository at this point in the history
…xyz/dymint into feat/adr-fraud-handling
  • Loading branch information
Faulty Tolly committed Sep 20, 2024
2 parents 94923c0 + cd804f3 commit 835dcae
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 610 deletions.
6 changes: 3 additions & 3 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/dymensionxyz/dymint/fraud"
"github.com/dymensionxyz/gerr-cosmos/gerrc"

errorsmod "cosmossdk.io/errors"

Expand All @@ -16,7 +16,7 @@ import (
func (m *Manager) applyBlockWithFraudHandling(block *types.Block, commit *types.Commit, blockMetaData types.BlockMetaData, validate bool) error {
if validate {
if err := m.validateBlockBeforeApply(block, commit); err != nil {
if errors.Is(err, fraud.ErrFraud) {
if errors.Is(err, gerrc.ErrFault) {
m.FraudHandler.HandleFault(context.Background(), err)
} else if err != nil {
m.blockCache.Delete(block.Header.Height)
Expand All @@ -28,7 +28,7 @@ func (m *Manager) applyBlockWithFraudHandling(block *types.Block, commit *types.
}

if err := m.applyBlock(block, commit, blockMetaData); err != nil {
if errors.Is(err, fraud.ErrFraud) {
if errors.Is(err, gerrc.ErrFault) {
m.FraudHandler.HandleFault(context.Background(), err)
}
return fmt.Errorf("apply block: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions block/fraud.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
uevent "github.com/dymensionxyz/dymint/utils/event"
)

// FraudHandler is an interface that defines a method to handle faults.
// Contract: should not be blocking.
type FraudHandler interface {
// HandleFault handles a fault that occurred in the system.
// The fault is passed as an error type.
HandleFault(ctx context.Context, fault error)
}

// FreezeHandler is used to handle faults coming from executing and validating blocks.
type FreezeHandler struct {
m *Manager
Expand Down
10 changes: 5 additions & 5 deletions fraud/types_test.go → block/fraud_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fraud_test
package block_test

import (
"errors"
"testing"

"github.com/dymensionxyz/dymint/fraud"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

type mockError struct {
Expand All @@ -17,19 +17,19 @@ func (m mockError) Error() string {
}

func (mockError) Unwrap() error {
return fraud.ErrFraud
return gerrc.ErrFault
}

func TestErrorIsErrFault(t *testing.T) {
err := mockError{name: "test", data: "test"}

if !errors.Is(err, fraud.ErrFraud) {
if !errors.Is(err, gerrc.ErrFault) {
t.Error("Expected Is to return true")
}

anotherErr := errors.New("some error")

if errors.Is(anotherErr, fraud.ErrFraud) {
if errors.Is(anotherErr, gerrc.ErrFault) {
t.Error("Expected Is to return false")
}
}
3 changes: 1 addition & 2 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/dymensionxyz/dymint/config"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/fraud"
"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/p2p"
"github.com/dymensionxyz/dymint/settlement"
Expand Down Expand Up @@ -78,7 +77,7 @@ type Manager struct {
TargetHeight atomic.Uint64

// Fraud handler
FraudHandler fraud.Handler
FraudHandler FraudHandler
}

// NewManager creates new block Manager.
Expand Down
20 changes: 10 additions & 10 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"errors"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -31,7 +32,6 @@ import (

"github.com/dymensionxyz/dymint/config"
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/fraud"
blockmocks "github.com/dymensionxyz/dymint/mocks/github.com/dymensionxyz/dymint/block"
fraudmocks "github.com/dymensionxyz/dymint/mocks/github.com/dymensionxyz/dymint/fraud"
"github.com/dymensionxyz/dymint/node/events"
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestApplyCachedBlocks_WithFraudCheck(t *testing.T) {
mockExecutor.On("GetAppInfo").Return(&abci.ResponseInfo{
LastBlockHeight: int64(0),
}, nil)
mockExecutor.On("ExecuteBlock", mock.Anything, mock.Anything).Return(nil, fraud.ErrFraud)
mockExecutor.On("ExecuteBlock", mock.Anything, mock.Anything).Return(nil, gerrc.ErrFault)

// Check that handle fault is called
manager.FraudHandler = block.NewFreezeHandler(manager)
Expand Down Expand Up @@ -273,8 +273,8 @@ func TestApplyCachedBlocks_WithFraudCheck(t *testing.T) {
case receivedEvent := <-fraudEventReceived:
if receivedEvent.Error == nil {
t.Error("there should be an error in the event")
} else if !errors.Is(receivedEvent.Error, fraud.ErrFraud) {
t.Errorf("Unexpected error received, expected: %v, got: %v", fraud.ErrFraud, receivedEvent.Error)
} else if !errors.Is(receivedEvent.Error, gerrc.ErrFault) {
t.Errorf("Unexpected error received, expected: %v, got: %v", gerrc.ErrFault, receivedEvent.Error)
}
case <-time.After(5 * time.Second):
t.Error("timeout waiting for fraud event")
Expand Down Expand Up @@ -344,14 +344,14 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {
}, nil)
mockExecutor.On("UpdateStateAfterInitChain", mock.Anything, mock.Anything).Return(nil)
mockExecutor.On("UpdateMempoolAfterInitChain", mock.Anything).Return(nil)
mockExecutor.On("ExecuteBlock", mock.Anything, mock.Anything).Return(nil, fraud.ErrFraud)
mockExecutor.On("ExecuteBlock", mock.Anything, mock.Anything).Return(nil, gerrc.ErrFault)

// Check that handle fault is called
mockFraudHandler := &fraudmocks.MockHandler{}
manager.FraudHandler = mockFraudHandler

mockFraudHandler.On("HandleFault", mock.Anything, mock.MatchedBy(func(err error) bool {
return errors.Is(err, fraud.ErrFraud)
return errors.Is(err, gerrc.ErrFault)
})).Return(nil)

// Initially sync target is 0
Expand All @@ -367,7 +367,7 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {
go func() {
errChan <- manager.Start(ctx)
err := <-errChan
require.True(t, errors.Is(err, fraud.ErrFraud))
require.True(t, errors.Is(err, gerrc.ErrFault))
}()
<-ctx.Done()
assert.Equal(t, batch.EndHeight(), manager.LastSubmittedHeight.Load())
Expand Down Expand Up @@ -775,14 +775,14 @@ func TestManager_ProcessNextDABatch_FraudHandling(t *testing.T) {
mockExecutor.On("GetAppInfo").Return(&abci.ResponseInfo{
LastBlockHeight: int64(batch.EndHeight()),
}, nil)
mockExecutor.On("ExecuteBlock", mock.Anything, mock.Anything).Return(nil, fraud.ErrFraud)
mockExecutor.On("ExecuteBlock", mock.Anything, mock.Anything).Return(nil, gerrc.ErrFault)

// Check that handle fault is called
mockFraudHandler := &fraudmocks.MockHandler{}
manager.FraudHandler = mockFraudHandler

mockFraudHandler.On("HandleFault", mock.Anything, mock.MatchedBy(func(err error) bool {
return errors.Is(err, fraud.ErrFraud)
return errors.Is(err, gerrc.ErrFault)
})).Return(nil)

app.On("Commit", mock.Anything).Return(abci.ResponseCommit{Data: commitHash[:]}).Once()
Expand All @@ -795,7 +795,7 @@ func TestManager_ProcessNextDABatch_FraudHandling(t *testing.T) {
err = manager.ProcessNextDABatch(daResultSubmitBatch.SubmitMetaData)

// Verify
require.True(errors.Is(err, fraud.ErrFraud))
require.True(errors.Is(err, gerrc.ErrFault))
mockExecutor.AssertExpectations(t)
mockFraudHandler.AssertExpectations(t)
}
Expand Down
13 changes: 0 additions & 13 deletions fraud/handler.go

This file was deleted.

7 changes: 0 additions & 7 deletions fraud/types.go

This file was deleted.

Loading

0 comments on commit 835dcae

Please sign in to comment.