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

Commit

Permalink
feat(state): Plugin System (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Devon Bear <itsdevbear@berachain.com>
  • Loading branch information
calbera and Devon Bear authored Feb 3, 2023
1 parent d318cda commit 3e3fb92
Show file tree
Hide file tree
Showing 54 changed files with 3,807 additions and 3,731 deletions.
3 changes: 1 addition & 2 deletions eth/core/precompile/container/stateful_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"reflect"

"github.com/berachain/stargazer/eth/core/precompile/container"
"github.com/berachain/stargazer/eth/core/state"
coretypes "github.com/berachain/stargazer/eth/core/types"
"github.com/berachain/stargazer/eth/core/vm"
"github.com/berachain/stargazer/lib/common"
Expand All @@ -48,7 +47,7 @@ var _ = Describe("Stateful Container", func() {
ctx = testutil.NewContext()
sc = container.NewStateful(&mockStateful{&mockBase{}}, mockIdsToMethods)
empty = container.NewStateful(nil, nil)
sdb = &mockSdb{&state.StateDB{}, 0}
sdb = &mockSdb{nil, 0}
})

Describe("Test Required Gas", func() {
Expand Down
33 changes: 33 additions & 0 deletions eth/core/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,44 @@
package state

import (
"math/big"

coretypes "github.com/berachain/stargazer/eth/core/types"
"github.com/berachain/stargazer/lib/common"
libtypes "github.com/berachain/stargazer/lib/types"
)

// `StatePlugin` is a plugin which tracks the accounts (balances, nonces, codes, states) in the
// native vm. This also handles removing suicided accounts.
type StatePlugin interface { //nolint:revive // vibes.
libtypes.Controllable[string]

CreateAccount(common.Address)
// Exist reports whether the given account exists in state.
// Notably this should also return true for suicided accounts.
Exist(common.Address) bool

GetBalance(common.Address) *big.Int
SubBalance(common.Address, *big.Int)
AddBalance(common.Address, *big.Int)
TransferBalance(common.Address, common.Address, *big.Int)

GetNonce(common.Address) uint64
SetNonce(common.Address, uint64)

GetCodeHash(common.Address) common.Hash
GetCode(common.Address) []byte
SetCode(common.Address, []byte)
GetCodeSize(common.Address) int

GetCommittedState(common.Address, common.Hash) common.Hash
GetState(common.Address, common.Hash) common.Hash
SetState(common.Address, common.Hash, common.Hash)
ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) error

DeleteSuicides([]common.Address)
}

// `LogsPlugin` defines the interface for tracking logs created during a state transition.
type LogsPlugin interface {
// `LogsPlugin` implements `libtypes.Controllable`.
Expand Down
2 changes: 1 addition & 1 deletion eth/core/state/plugin/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
// `initJournalCapacity` is the initial capacity of the plugins' journals.
initJournalCapacity = 32
// `refundRegistryKey` is the registry key for the refund plugin.
refundRegistryKey = "refund"
refundRegistryKey = `refund`
// `logsRegistryKey` is the registry key for the logs plugin.
logsRegistryKey = `logs`
// `maxUint` is used to check that a uint does not underflow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package cachekv
package mock

import libtypes "github.com/berachain/stargazer/lib/types"
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

// Compile-time assertion that `cacheValue` implements `types.Cloneable`.
var _ libtypes.Cloneable[*cacheValue] = (*cacheValue)(nil)
//go:generate moq -out ./logs.mock.go -pkg mock ../../ LogsPlugin

// `cacheValue` represents a cached value in the cachekv store.
// If dirty is true, it indicates the cached value is different from the underlying value.
type cacheValue struct {
value []byte
dirty bool
}

// `newCacheValue` creates a new `cacheValue` object with the given `value` and `dirty` flag.
func newCacheValue(v []byte, d bool) *cacheValue {
return &cacheValue{
value: v,
dirty: d,
// `NewEmptyLogsPlugin` returns an empty `LogsPluginMock`.
func NewEmptyLogsPlugin() *LogsPluginMock {
// make and configure a mocked state.LogsPlugin
return &LogsPluginMock{
AddLogFunc: func(log *types.Log) {
panic("mock out the AddLog method")
},
BuildLogsAndClearFunc: func(hash1 common.Hash, hash2 common.Hash, v1 uint, v2 uint) []*types.Log {
panic("mock out the BuildLogsAndClear method")
},
FinalizeFunc: func() {
// no-op
},
RegistryKeyFunc: func() string {
return "emptylogs"
},
RevertToSnapshotFunc: func(n int) {
// no-op
},
SnapshotFunc: func() int {
// no-op
return 0
},
}
}

// `Clone` implements `types.Cloneable`.
func (cv *cacheValue) Clone() *cacheValue {
// Return a new cacheValue with the same value and dirty flag
return newCacheValue(append([]byte(nil), cv.value...), cv.dirty)
}
Loading

0 comments on commit 3e3fb92

Please sign in to comment.