Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
[epociask/unsafe-withdrawal-heuristic] Fixed failing unit/e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethen Pociask committed Oct 26, 2023
1 parent f68d4da commit 20fa54f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 51 deletions.
4 changes: 2 additions & 2 deletions e2e/heuristic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ func TestUnsafeWithdrawalDetection(t *testing.T) {
return height.Uint64() > proveReceipt.BlockNumber.Uint64(), nil
}))

// Ensure Pessimism has detected what it considers a "faulty" withdrawal
// Ensure Pessimism has detected what it considers an unsafe withdrawal
alerts := ts.TestSlackSvr.SlackAlerts()
require.Equal(t, 3, len(alerts), "expected 3 alerts")
assert.Contains(t, alerts[0].Text, "withdrawal_enforcement", "expected alert to be for withdrawal_enforcement")
assert.Contains(t, alerts[0].Text, "unsafe_withdrawal", "expected alert to be for withdrawal_enforcement")
assert.Contains(t, alerts[0].Text, fakeAddr.String(), "expected alert to be for dummy L2ToL1MessagePasser")
assert.Contains(t, alerts[0].Text, alertMsg, "expected alert to have alert message")
}
Expand Down
1 change: 0 additions & 1 deletion e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type SysTestSuite struct {
Close func()

// Mocked services
ctrl *gomock.Controller
TestSlackSvr *TestSlackServer
TestPagerDutyServer *TestPagerDutyServer
TestIndexerClient *mocks.MockIndexerClient
Expand Down
25 changes: 13 additions & 12 deletions internal/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/base-org/pessimism/internal/core"
"github.com/base-org/pessimism/internal/engine"
"github.com/base-org/pessimism/internal/engine/heuristic"
"github.com/base-org/pessimism/internal/mocks"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,35 +44,35 @@ func Test_HardCodedEngine(t *testing.T) {
td := core.TransitData{}

ts.mockHeuristic.EXPECT().Assess(td).
Return(nil, false, testErr()).Times(1)
Return(heuristic.NoActivations(), testErr()).Times(1)

ts.mockHeuristic.EXPECT().SUUID().
Return(core.NilSUUID()).Times(1)

outcome, activated := ts.re.Execute(context.Background(), td, ts.mockHeuristic)
assert.Nil(t, outcome)
assert.False(t, activated)
as := ts.re.Execute(context.Background(), td, ts.mockHeuristic)
assert.Nil(t, as)

}},
{
name: "Successful Activation",
test: func(t *testing.T, ts *testSuite) {
td := core.TransitData{}

expectedOut := &core.Activation{
Message: "20 inch blade on the Impala",
}
expectedOut := heuristic.NewActivationSet().Add(
&heuristic.Activation{
Message: "20 inch blade on the Impala",
})

ts.mockHeuristic.EXPECT().Assess(td).
Return(expectedOut, true, nil).Times(1)
Return(expectedOut, nil).Times(1)

ts.mockHeuristic.EXPECT().SUUID().
Return(core.NilSUUID()).Times(1)

outcome, activated := ts.re.Execute(context.Background(), td, ts.mockHeuristic)
assert.NotNil(t, outcome)
assert.True(t, activated)
assert.Equal(t, expectedOut, outcome)
as := ts.re.Execute(context.Background(), td, ts.mockHeuristic)
assert.NotNil(t, as)
assert.True(t, as.Activated())
assert.Equal(t, expectedOut, as)
}},
}

Expand Down
12 changes: 6 additions & 6 deletions internal/engine/registry/balance_enforce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ func Test_Balance_Assess(t *testing.T) {
Value: float64(3),
}

_, activated, err := bi.Assess(testData1)
as, err := bi.Assess(testData1)
assert.NoError(t, err)
assert.False(t, activated)
assert.False(t, as.Activated())

// Upper bound activation
testData2 := core.TransitData{
Type: core.AccountBalance,
Value: float64(6),
}

_, activated, err = bi.Assess(testData2)
as, err = bi.Assess(testData2)
assert.NoError(t, err)
assert.True(t, activated)
assert.True(t, as.Activated())

// Lower bound activation
testData3 := core.TransitData{
Type: core.AccountBalance,
Value: float64(0.1),
}

_, activated, err = bi.Assess(testData3)
as, err = bi.Assess(testData3)
assert.NoError(t, err)
assert.True(t, activated)
assert.True(t, as.Activated())
}
16 changes: 7 additions & 9 deletions internal/engine/registry/contract_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func Test_Event_Log_Heuristic(t *testing.T) {
},
}

outcome, activated, err := ei.Assess(td)
as, err := ei.Assess(td)

assert.NoError(t, err)
assert.True(t, activated)
assert.NotNil(t, outcome)
assert.NotNil(t, as)
assert.True(t, as.Activated())
},
},
{
Expand All @@ -63,11 +63,10 @@ func Test_Event_Log_Heuristic(t *testing.T) {
},
}

outcome, activated, err := ei.Assess(td)
as, err := ei.Assess(td)

assert.Error(t, err)
assert.False(t, activated)
assert.Nil(t, outcome)
assert.Nil(t, as)
},
},
{
Expand All @@ -90,11 +89,10 @@ func Test_Event_Log_Heuristic(t *testing.T) {
},
}

outcome, activated, err := ei.Assess(td)
as, err := ei.Assess(td)

assert.NoError(t, err)
assert.False(t, activated)
assert.Nil(t, outcome)
assert.NotNil(t, as)
},
},
}
Expand Down
16 changes: 7 additions & 9 deletions internal/engine/registry/fault_detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ func Test_FaultDetector(t *testing.T) {
Value: testLog,
}

outcome, pass, err := ts.fd.Assess(td)
assert.Nil(t, outcome)
assert.False(t, pass)
as, err := ts.fd.Assess(td)
assert.Nil(t, as)
assert.Error(t, err)

},
Expand All @@ -117,10 +116,9 @@ func Test_FaultDetector(t *testing.T) {
Value: testLog,
}

outcome, pass, err := ts.fd.Assess(td)
assert.Nil(t, outcome)
assert.False(t, pass)
as, err := ts.fd.Assess(td)
assert.Error(t, err)
assert.Nil(t, as)

},
},
Expand Down Expand Up @@ -153,9 +151,9 @@ func Test_FaultDetector(t *testing.T) {
Value: testLog,
}

outcome, pass, err := ts.fd.Assess(td)
assert.NotNil(t, outcome)
assert.True(t, pass)
as, err := ts.fd.Assess(td)
assert.NotNil(t, as)
assert.True(t, as.Activated())
assert.NoError(t, err)

},
Expand Down
24 changes: 12 additions & 12 deletions internal/engine/registry/withdrawal_safety.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ func (wi *WithdrawalSafetyHeuristic) Assess(td core.TransitData) (*heuristic.Act
// TODO - Update withdrawal decoding to convert to big.Int instead of string
corrWithdrawal := withdrawals[0]

// 4. Perform invariant analysis using the withdrawal metadata

// 4.1 Check if the proven withdrawal amount is greater than the OptimismPortal value
portalWEI, err := wi.l1Client.BalanceAt(context.Background(), common.HexToAddress(wi.cfg.L1PortalAddress), big.NewInt(int64(log.BlockNumber)))
portalWEI, err := wi.l1Client.BalanceAt(context.Background(), common.HexToAddress(wi.cfg.L1PortalAddress),
big.NewInt(int64(log.BlockNumber)))
if err != nil {
return nil, err
}
Expand All @@ -162,6 +160,7 @@ func (wi *WithdrawalSafetyHeuristic) Assess(td core.TransitData) (*heuristic.Act
return nil, err
}

// 4. Perform invariant analysis using the withdrawal metadata
invariants := []func() (bool, string){
// 4.1
// Check if the proven withdrawal amount is greater than the OptimismPortal value
Expand All @@ -172,7 +171,7 @@ func (wi *WithdrawalSafetyHeuristic) Assess(td core.TransitData) (*heuristic.Act
// Check if the proven withdrawal amount is greater than 5% of the OptimismPortal value
func() (bool, string) {
return p_common.PercentOf(withdrawalETH, portalETH).Cmp(big.NewFloat(5)) == 1, `

Check failure on line 173 in internal/engine/registry/withdrawal_safety.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 5, in <argument> detected (gomnd)
A withdraw was proven that is within 5% of the Optimism Portal balance`
A withdraw was proven that is >= 5% of the Optimism Portal balance`
},
// 4.3
// Ensure the proven withdrawal exists in the L2ToL1MessagePasser storage
Expand All @@ -184,13 +183,14 @@ func (wi *WithdrawalSafetyHeuristic) Assess(td core.TransitData) (*heuristic.Act
as := heuristic.NewActivationSet()
for _, inv := range invariants {
if success, msg := inv(); success {
as = as.Add(&heuristic.Activation{
TimeStamp: time.Now(),
Message: fmt.Sprintf(unsafeWithdrawalMsg, msg,
wi.cfg.L1PortalAddress, wi.cfg.L2ToL1Address,
wi.SUUID(), log.TxHash.Hex(), corrWithdrawal.TransactionHash,
withdrawalWEI),
})
as = as.Add(
&heuristic.Activation{
TimeStamp: time.Now(),
Message: fmt.Sprintf(unsafeWithdrawalMsg, msg,
wi.cfg.L1PortalAddress, wi.cfg.L2ToL1Address,
wi.SUUID(), log.TxHash.Hex(), corrWithdrawal.TransactionHash,
withdrawalWEI),
})
}
}

Expand Down

0 comments on commit 20fa54f

Please sign in to comment.