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

Commit

Permalink
refator(statetypes): Types -> Storage (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Bear authored Feb 2, 2023
1 parent 46daaf6 commit ce96084
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 207 deletions.
7 changes: 0 additions & 7 deletions lib/utils/unsafe_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@
package utils_test

import (
"testing"

"github.com/berachain/stargazer/lib/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestUtilsPkg(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "lib/utils")
}

var _ = Describe("UnsafeStrToBytes", func() {
When("given a valid string", func() {
It("should return a byte array with the same content", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// 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 types_test
package utils_test

import (
"testing"
Expand All @@ -21,7 +21,7 @@ import (
. "github.com/onsi/gomega"
)

func TestStateTypes(t *testing.T) {
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "x/evm/plugins/state/types")
RunSpecs(t, "lib/utils")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

syntax = "proto3";

package stargazer.core.state.types.v1;
package stargazer.core.state.storage.v1;

option go_package = "github.com/berachain/stargazer/x/evm/plugins/state/types";
option go_package = "github.com/berachain/stargazer/x/evm/plugins/state/storage";

// `State` represents a single key/value pair of evm state data.
message State {
// `Slot` represents a single key/value pair of evm state data.
message Slot {
// `key` is the stored key.
string key = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// 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 types
package constants

const EvmStoreKey = "evm"
const EvmNamespace = "evm"
const (
EvmStoreKey = "evm"
EvmNamespace = "evm"
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// 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 types
package state

import (
coretypes "github.com/berachain/stargazer/eth/core/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// 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 types
package state

import (
"github.com/berachain/stargazer/lib/common"
Expand All @@ -34,12 +34,12 @@ func AddressStoragePrefix(address common.Address) []byte {
return bz
}

// `StateKeyFor` defines the full key under which an account state is stored.
func StateKeyFor(address common.Address, key common.Hash) []byte {
// `KeyForSlot` defines the full key under which an account state is stored.
func KeyForSlot(address common.Address, slot common.Hash) []byte {
bz := make([]byte, 1+common.AddressLength+common.HashLength)
copy(bz, []byte{keyPrefixStorage})
copy(bz[1:], address[:])
copy(bz[1+common.AddressLength:], key[:])
copy(bz[1+common.AddressLength:], slot[:])
return bz
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// 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 types
package state

import (
"github.com/berachain/stargazer/lib/common"
Expand All @@ -34,7 +34,7 @@ var _ = Describe("StateKeyFor", func() {
It("returns a storage key for a given account and storage slot", func() {
address := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678")
slot := common.HexToHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef")
key := StateKeyFor(address, slot)
key := KeyForSlot(address, slot)
Expect(key).To(HaveLen(1 + common.AddressLength + common.HashLength))
Expect(key[0]).To(Equal(keyPrefixStorage))
Expect(key[1 : 1+common.AddressLength]).To(Equal(address.Bytes()))
Expand Down
46 changes: 23 additions & 23 deletions x/evm/plugins/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/berachain/stargazer/lib/common"
"github.com/berachain/stargazer/lib/crypto"
"github.com/berachain/stargazer/lib/utils"
"github.com/berachain/stargazer/x/evm/constants"
"github.com/berachain/stargazer/x/evm/plugins/state/store/cachekv"
"github.com/berachain/stargazer/x/evm/plugins/state/store/cachemulti"
"github.com/berachain/stargazer/x/evm/plugins/state/types"
)

var (
Expand Down Expand Up @@ -78,8 +78,8 @@ type StateDB struct { //nolint: revive // we like the vibe.
ethStore cachekv.StateDBCacheKVStore

// keepers used for balance and account information.
ak types.AccountKeeper
bk types.BankKeeper
ak AccountKeeper
bk BankKeeper

// Any error that occurs during an sdk module read or write is
// memoized here and is eventually be returned by `Commit`.
Expand Down Expand Up @@ -113,10 +113,10 @@ type StateDB struct { //nolint: revive // we like the vibe.
}

// returns a *StateDB using the MultiStore belonging to ctx.
func NewStateDB(
func NewSlotDB(
ctx sdk.Context,
ak types.AccountKeeper,
bk types.BankKeeper,
ak AccountKeeper,
bk BankKeeper,
storeKey storetypes.StoreKey,
evmDenom string,
) *StateDB {
Expand Down Expand Up @@ -155,7 +155,7 @@ func (sdb *StateDB) CreateAccount(addr common.Address) {
sdb.ak.SetAccount(sdb.ctx, acc)

// initialize the code hash to empty
sdb.ethStore.Set(types.CodeHashKeyFor(addr), emptyCodeHashBytes)
sdb.ethStore.Set(CodeHashKeyFor(addr), emptyCodeHashBytes)
}

// =============================================================================
Expand Down Expand Up @@ -190,7 +190,7 @@ func (sdb *StateDB) Reset(ctx sdk.Context) {
// sdb.accessList = newAccessList()
// sdb.suicides = make([]common.Address, 0)
// TODO: unghetto this
*sdb = *NewStateDB(ctx, sdb.ak, sdb.bk, sdb.storeKey, sdb.evmDenom)
*sdb = *NewSlotDB(ctx, sdb.ak, sdb.bk, sdb.storeKey, sdb.evmDenom)
}

// =============================================================================
Expand All @@ -210,13 +210,13 @@ func (sdb *StateDB) AddBalance(addr common.Address, amount *big.Int) {
coins := sdk.NewCoins(sdk.NewCoin(sdb.evmDenom, sdk.NewIntFromBigInt(amount)))

// Mint the coins to the evm module account
if err := sdb.bk.MintCoins(sdb.ctx, types.EvmNamespace, coins); err != nil {
if err := sdb.bk.MintCoins(sdb.ctx, constants.EvmNamespace, coins); err != nil {
sdb.setErrorUnsafe(err)
return
}

// Send the coins from the evm module account to the destination address.
if err := sdb.bk.SendCoinsFromModuleToAccount(sdb.ctx, types.EvmNamespace, addr[:], coins); err != nil {
if err := sdb.bk.SendCoinsFromModuleToAccount(sdb.ctx, constants.EvmNamespace, addr[:], coins); err != nil {
sdb.setErrorUnsafe(err)
}
}
Expand All @@ -227,13 +227,13 @@ func (sdb *StateDB) SubBalance(addr common.Address, amount *big.Int) {
coins := sdk.NewCoins(sdk.NewCoin(sdb.evmDenom, sdk.NewIntFromBigInt(amount)))

// Send the coins from the source address to the evm module account.
if err := sdb.bk.SendCoinsFromAccountToModule(sdb.ctx, addr[:], types.EvmNamespace, coins); err != nil {
if err := sdb.bk.SendCoinsFromAccountToModule(sdb.ctx, addr[:], constants.EvmNamespace, coins); err != nil {
sdb.setErrorUnsafe(err)
return
}

// Burn the coins from the evm module account.
if err := sdb.bk.BurnCoins(sdb.ctx, types.EvmNamespace, coins); err != nil {
if err := sdb.bk.BurnCoins(sdb.ctx, constants.EvmNamespace, coins); err != nil {
sdb.setErrorUnsafe(err)
return
}
Expand Down Expand Up @@ -288,7 +288,7 @@ func (sdb *StateDB) SetNonce(addr common.Address, nonce uint64) {
// the code hash of account.
func (sdb *StateDB) GetCodeHash(addr common.Address) common.Hash {
if sdb.ak.HasAccount(sdb.ctx, addr[:]) {
if ch := sdb.ethStore.Get(types.CodeHashKeyFor(addr)); ch != nil {
if ch := sdb.ethStore.Get(CodeHashKeyFor(addr)); ch != nil {
return common.BytesToHash(ch)
}
return emptyCodeHash
Expand All @@ -306,20 +306,20 @@ func (sdb *StateDB) GetCode(addr common.Address) []byte {
if (codeHash == common.Hash{}) || codeHash == emptyCodeHash {
return nil
}
return sdb.ethStore.Get(types.CodeKeyFor(codeHash))
return sdb.ethStore.Get(CodeKeyFor(codeHash))
}

// SetCode implements the `GethStateDB` interface by setting the code hash and
// code for the given account.
func (sdb *StateDB) SetCode(addr common.Address, code []byte) {
codeHash := crypto.Keccak256Hash(code)

sdb.ethStore.Set(types.CodeHashKeyFor(addr), codeHash[:])
sdb.ethStore.Set(CodeHashKeyFor(addr), codeHash[:])
// store or delete code
if len(code) == 0 {
sdb.ethStore.Delete(types.CodeKeyFor(codeHash))
sdb.ethStore.Delete(CodeKeyFor(codeHash))
} else {
sdb.ethStore.Set(types.CodeKeyFor(codeHash), code)
sdb.ethStore.Set(CodeKeyFor(codeHash), code)
}
}

Expand Down Expand Up @@ -380,15 +380,15 @@ func (sdb *StateDB) getStateFromStore(
store storetypes.KVStore,
addr common.Address, slot common.Hash,
) common.Hash {
if value := store.Get(types.StateKeyFor(addr, slot)); value != nil {
if value := store.Get(KeyForSlot(addr, slot)); value != nil {
return common.BytesToHash(value)
}
return common.Hash{}
}

// `SetState` implements the `GethStateDB` interface by setting the state of an
// address.
func (sdb *StateDB) SetState(addr common.Address, key, value common.Hash) {
func (sdb *StateDB) SetState(addr common.Address, slot, value common.Hash) {
// For performance reasons, we don't check to ensure the account exists before we execute.
// This is reasonably safe since under normal operation, SetState is only ever called by the
// SSTORE opcode in the EVM, which will only ever be called on an account that exists, since
Expand All @@ -399,12 +399,12 @@ func (sdb *StateDB) SetState(addr common.Address, key, value common.Hash) {

// If empty value is given, delete the state entry.
if len(value) == 0 || (value == common.Hash{}) {
sdb.ethStore.Delete(types.StateKeyFor(addr, key))
sdb.ethStore.Delete(KeyForSlot(addr, slot))
return
}

// Set the state entry.
sdb.ethStore.Set(types.StateKeyFor(addr, key), value[:])
sdb.ethStore.Set(KeyForSlot(addr, slot), value[:])
}

// =============================================================================
Expand Down Expand Up @@ -511,7 +511,7 @@ func (sdb *StateDB) ForEachStorage(
addr common.Address,
cb func(key, value common.Hash) bool,
) error {
it := sdk.KVStorePrefixIterator(sdb.ethStore, types.AddressStoragePrefix(addr))
it := sdk.KVStorePrefixIterator(sdb.ethStore, AddressStoragePrefix(addr))
defer it.Close()

for ; it.Valid(); it.Next() {
Expand Down Expand Up @@ -553,7 +553,7 @@ func (sdb *StateDB) Commit() error {
}

// clear the codehash from this account
sdb.ethStore.Delete(types.CodeHashKeyFor(suicidalAddr))
sdb.ethStore.Delete(CodeHashKeyFor(suicidalAddr))

// remove auth account
sdb.ak.RemoveAccount(sdb.ctx, acct)
Expand Down
20 changes: 9 additions & 11 deletions x/evm/plugins/state/statedb_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import (

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

// "github.com/berachain/stargazer/eth/params".
"github.com/berachain/stargazer/x/evm/plugins/state/types"
)

type StateDBFactory struct { //nolint:revive // the vibes are good.
// Cosmos Keeper References
ak types.AccountKeeper
bk types.BankKeeper
ak AccountKeeper
bk BankKeeper

// evmStoreKey is the store key for the EVM store.
evmStoreKey storetypes.StoreKey
Expand All @@ -36,13 +34,13 @@ type StateDBFactory struct { //nolint:revive // the vibes are good.
// evmDenom params.Retriever[params.EVMDenom]
}

// NewStateDBFactory returns a new StateDBFactory instance.
func NewStateDBFactory(
ak types.AccountKeeper,
bk types.BankKeeper,
// NewSlotDBFactory returns a new StateDBFactory instance.
func NewSlotDBFactory(
ak AccountKeeper,
bk BankKeeper,
evmStoreKey storetypes.StoreKey,
// evmDenom params.Retriever[params.EVMDenom],
logFactory types.EthereumLogFactory,
logFactory EthereumLogFactory,
) *StateDBFactory {
return &StateDBFactory{
ak: ak,
Expand All @@ -53,8 +51,8 @@ func NewStateDBFactory(
}
}

// BuildNewStateDB returns a new StateDB instance.
// BuildNewSlotDB returns a new StateDB instance.
func (sdf *StateDBFactory) BuildStateDB(ctx context.Context) *StateDB {
return NewStateDB(sdk.UnwrapSDKContext(ctx), sdf.ak, sdf.bk, sdf.evmStoreKey, "abera")
return NewSlotDB(sdk.UnwrapSDKContext(ctx), sdf.ak, sdf.bk, sdf.evmStoreKey, "abera")
// sdf.evmDenom.Get(ctx), sdf.er)
}
Loading

0 comments on commit ce96084

Please sign in to comment.