go-ethereum/libevm/register/register_test.go
Arran Schlosberg 35926db4d6
feat: temporary extras (#234)
## Why this should be merged

Registration of extras requires runtime enforcement of payload types. In
production, only a single set of types can be registered and any
attempts at re-registration will panic (by design). Although this makes
production usage safe, it doesn't allow downstream consumers (e.g. data
indexers) to use extras from different chains (e.g. `coreth` _and_
`subnet-evm`) at the same time.

## How this works

1. The `libevm/register.AtMostOnce` type can now be overridden
temporarily.
2. `params`, `core/types`, `core/vm`, and `state` packages introduce
`WithTempRegisteredExtras()` functions.
3. `libevm/temporary.WithRegisteredExtras()` provides "atomic" override
of all extras.

In all cases, the scope of the override is limited to the life of a
single function call.

## How this was tested

Relative to numbered list above:

1. Unit test of new and existing functionality.
2. Integration tests of both packages, demonstrating both payload and
behavioural override.
2025-10-02 17:42:26 +01:00

78 lines
2 KiB
Go

// Copyright 2025 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.
package register
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAtMostOnce(t *testing.T) {
var sut AtMostOnce[int]
assertRegistered := func(t *testing.T, want int) {
t.Helper()
require.True(t, sut.Registered(), "Registered()")
assert.Equal(t, want, sut.Get(), "Get()")
}
const val int = 42
require.NoError(t, sut.Register(val), "Register()")
assertRegistered(t, val)
assert.PanicsWithValue(
t, ErrReRegistration,
func() { sut.MustRegister(0) },
"MustRegister() after Register()",
)
t.Run("TestOnlyClear", func(t *testing.T) {
sut.TestOnlyClear()
require.False(t, sut.Registered(), "Registered()")
t.Run("re-registration", func(t *testing.T) {
sut.MustRegister(val)
assertRegistered(t, val)
})
})
if t.Failed() {
return
}
t.Run("TempOverride", func(t *testing.T) {
t.Run("during", func(t *testing.T) {
sut.TempOverride(val+1, func() {
assertRegistered(t, val+1)
})
})
t.Run("after", func(t *testing.T) {
assertRegistered(t, val)
})
})
t.Run("TempClear", func(t *testing.T) {
t.Run("during", func(t *testing.T) {
sut.TempClear(func() {
assert.False(t, sut.Registered(), "Registered()")
})
})
t.Run("after", func(t *testing.T) {
assertRegistered(t, val)
})
})
}