Skip to content

Commit

Permalink
include fraud checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Faulty Tolly committed Sep 11, 2024
1 parent 2da988f commit b42b2a8
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
48 changes: 48 additions & 0 deletions fraud/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fraud

import (
"context"
"errors"
"fmt"

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

type Fault struct {
Underlying error

// Block defines the block in which the fault happened.
Block *types.Block
}

func (f Fault) Error() string {
return fmt.Sprintf("fraud error: %s", f.Underlying)
}

func (f Fault) Is(err error) bool {
fault, ok := (err).(Fault)
if !ok {
return false
}
return errors.Is(fault.Underlying, f.Underlying)
}

func (f Fault) Unwrap() error {
return f.Underlying
}

func (f Fault) GovProposalContent() string {
return f.Error()
}

func NewFault(err error, block *types.Block) Fault {
return Fault{Underlying: err, Block: block}
}

func IsFault(err error) bool {
return errors.As(err, &Fault{})
}

type Handler interface {
HandleFault(ctx context.Context, fault Fault)
}
103 changes: 103 additions & 0 deletions fraud/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package fraud_test

import (
"context"
"errors"
"testing"

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

func TestFault_Error(t *testing.T) {
underlyingErr := errors.New("test error")
fault := fraud.NewFault(underlyingErr, nil)

expected := "fraud error: test error"
if fault.Error() != expected {
t.Errorf("Expected error message '%s', got '%s'", expected, fault.Error())
}
}

func TestFault_Is(t *testing.T) {
underlyingErr := errors.New("test error")
fault1 := fraud.NewFault(underlyingErr, nil)
fault2 := fraud.NewFault(underlyingErr, nil)
otherErr := errors.New("other error")

if !fault1.Is(fault2) {
t.Error("Expected fault1.Is(fault2) to be true")
}

if fault1.Is(otherErr) {
t.Error("Expected fault1.Is(otherErr) to be false")
}
}

func TestFault_Unwrap(t *testing.T) {
underlyingErr := errors.New("test error")
fault := fraud.NewFault(underlyingErr, nil)

if fault.Unwrap() != underlyingErr {
t.Error("Expected Unwrap() to return the underlying error")
}
}

func TestFault_GovProposalContent(t *testing.T) {
underlyingErr := errors.New("test error")
fault := fraud.NewFault(underlyingErr, nil)

expected := "fraud error: test error"
if fault.GovProposalContent() != expected {
t.Errorf("Expected GovProposalContent '%s', got '%s'", expected, fault.GovProposalContent())
}
}

func TestNewFault(t *testing.T) {
underlyingErr := errors.New("test error")
block := &types.Block{}
fault := fraud.NewFault(underlyingErr, block)

if fault.Underlying != underlyingErr {
t.Error("Expected Underlying error to match")
}

if fault.Block != block {
t.Error("Expected Block to match")
}
}

func TestIsFault(t *testing.T) {
underlyingErr := errors.New("test error")
fault := fraud.NewFault(underlyingErr, nil)

isFault := fraud.IsFault(fault)
if !isFault {
t.Error("Expected IsFault to return true for a Fault")
}

isFault = fraud.IsFault(errors.New("not a fault"))
if isFault {
t.Error("Expected IsFault to return false for a non-Fault error")
}
}

type mockHandler struct {
called bool
}

func (m *mockHandler) HandleFault(ctx context.Context, fault fraud.Fault) {
m.called = true
}

func TestHandler(t *testing.T) {
handler := &mockHandler{}
ctx := context.Background()
fault := fraud.NewFault(errors.New("test error"), nil)

handler.HandleFault(ctx, fault)

if !handler.called {
t.Error("Expected HandleFault to be called")
}
}

0 comments on commit b42b2a8

Please sign in to comment.