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

Commit

Permalink
[epociask/no-issue-withdrawal-enforce] State key and contract event i…
Browse files Browse the repository at this point in the history
…nvariant unit tests
  • Loading branch information
Ethen Pociask committed Jun 28, 2023
1 parent bb41869 commit 95c62c1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 19 deletions.
21 changes: 11 additions & 10 deletions internal/core/id_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package core
package core_test

import (
"testing"

"github.com/base-org/pessimism/internal/core"
"github.com/stretchr/testify/assert"
)

func Test_Component_ID(t *testing.T) {

expectedPID := ComponentPID([4]byte{1, 1, 1, 1})
actualID := MakeCUUID(1, 1, 1, 1)
expectedPID := core.ComponentPID([4]byte{1, 1, 1, 1})
actualID := core.MakeCUUID(1, 1, 1, 1)

assert.Equal(t, expectedPID, actualID.PID)

Expand All @@ -20,10 +21,10 @@ func Test_Component_ID(t *testing.T) {
}

func Test_Pipeline_ID(t *testing.T) {
expectedID := PipelinePID([9]byte{1, 1, 1, 1, 1, 1, 1, 1, 1})
actualID := MakePUUID(1,
MakeCUUID(1, 1, 1, 1),
MakeCUUID(1, 1, 1, 1))
expectedID := core.PipelinePID([9]byte{1, 1, 1, 1, 1, 1, 1, 1, 1})
actualID := core.MakePUUID(1,
core.MakeCUUID(1, 1, 1, 1),
core.MakeCUUID(1, 1, 1, 1))

assert.Equal(t, expectedID, actualID.PID)

Expand All @@ -34,12 +35,12 @@ func Test_Pipeline_ID(t *testing.T) {
}

func Test_InvSession_ID(t *testing.T) {
expectedID := InvSessionPID([3]byte{1, 2, 1})
actualID := MakeSUUID(1, 2, 1)
expectedID := core.InvSessionPID([3]byte{1, 2, 1})
actualID := core.MakeSUUID(1, 2, 1)

assert.Equal(t, expectedID, actualID.PID)

expectedStr := "layer1:live:unknown"
expectedStr := "layer1:live:balance_enforcement"
actualStr := actualID.PID.String()

assert.Equal(t, expectedStr, actualStr)
Expand Down
25 changes: 25 additions & 0 deletions internal/core/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core_test

import (
"testing"

"github.com/base-org/pessimism/internal/core"
"github.com/stretchr/testify/assert"
)

func Test_StateKey(t *testing.T) {
sk := core.MakeStateKey(0, "testId", false)

puuid := core.NilPUUID()
puuid.PID[0] = 1

// Successfully set the key
err := sk.SetPUUID(puuid)
assert.NoError(t, err)
assert.Contains(t, sk.String(), puuid.String())

pUUID2 := core.NilPUUID()

err = sk.SetPUUID(pUUID2)
assert.Error(t, err, "cannot set puuid more than once")
}
1 change: 0 additions & 1 deletion internal/engine/registry/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func Test_Balance_Invalidate(t *testing.T) {
})

// No invalidation

testData1 := core.TransitData{
Type: core.AccountBalance,
Value: float64(3),
Expand Down
85 changes: 77 additions & 8 deletions internal/engine/registry/event_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,103 @@ package registry_test
import (
"testing"

"github.com/base-org/pessimism/internal/core"
"github.com/base-org/pessimism/internal/engine/registry"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
)

func Test_Event_Log_Invariant(t *testing.T) {
var tests = []struct {
name string
cfgConstruct func() *registry.EventInvConfig
function func(t *testing.T, cfg *registry.EventInvConfig)
name string
function func(t *testing.T, cfg *registry.EventInvConfig)
}{
{
name: "Successful Invalidation",
function: func(t *testing.T, cfg *registry.EventInvConfig) {
ei := registry.NewEventInvariant(&registry.EventInvConfig{
Address: "0x123",
Address: "0x0000000000000000000000000000000000000420",
ContractName: "0x69",
Sigs: []string{"0x420"},
})

hash := crypto.Keccak256Hash([]byte("0x420"))

td := &registry.TransitData{
Type: registry.EventLog,
Address: "0x123",
td := core.TransitData{
Type: core.EventLog,
Address: common.HexToAddress("0x0000000000000000000000000000000000000420"),
Value: types.Log{
Topics: []common.Hash{
Topics: []common.Hash{hash},
},
}

outcome, invalid, err := ei.Invalidate(td)

assert.NoError(t, err)
assert.True(t, invalid)
assert.NotNil(t, outcome)
},
},
{
name: "Error Invalidation Due to Mismatched Addresses",
function: func(t *testing.T, cfg *registry.EventInvConfig) {
ei := registry.NewEventInvariant(&registry.EventInvConfig{
Address: "0x0000000000000000000000000000000000000420",
ContractName: "0x69",
Sigs: []string{"0x420"},
})

hash := crypto.Keccak256Hash([]byte("0x420"))

td := core.TransitData{
Type: core.EventLog,
Address: common.HexToAddress("0x0000000000000000000000000000000000000069"),
Value: types.Log{
Topics: []common.Hash{hash},
},
}

outcome, invalid, err := ei.Invalidate(td)

assert.Error(t, err)
assert.False(t, invalid)
assert.Nil(t, outcome)
},
},
{
name: "No Invalidation Due to Missing Signature",
function: func(t *testing.T, cfg *registry.EventInvConfig) {
ei := registry.NewEventInvariant(&registry.EventInvConfig{
Address: "0x0000000000000000000000000000000000000420",
ContractName: "0x69",
Sigs: []string{"0x424"},
})

hash := crypto.Keccak256Hash([]byte("0x420"))

td := core.TransitData{
Type: core.EventLog,
Address: common.HexToAddress("0x0000000000000000000000000000000000000420"),
Value: types.Log{
Topics: []common.Hash{hash},
},
}

outcome, invalid, err := ei.Invalidate(td)

assert.NoError(t, err)
assert.False(t, invalid)
assert.Nil(t, outcome)
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.function(t, nil)
})
}

}

0 comments on commit 95c62c1

Please sign in to comment.