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

Commit

Permalink
Minor API changes (ethereum-optimism#16)
Browse files Browse the repository at this point in the history
* refactor: var rename

* refactor: use uint64 for timestamp and gas limit

* refactor: alias api to cc_api for consistency

* refactor: var renames

* refactor: var and alias name changes

* build: wasm test binaries
  • Loading branch information
therealbytes committed Sep 2, 2023
1 parent 8e48b3d commit aaf2a70
Show file tree
Hide file tree
Showing 26 changed files with 315 additions and 316 deletions.
12 changes: 6 additions & 6 deletions concrete/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ var _ StateDB = &CommitSafeStateDB{}
type EVM interface {
StateDB() StateDB
BlockHash(block *big.Int) common.Hash
BlockTimestamp() *big.Int
BlockTimestamp() uint64
BlockGasLimit() uint64
BlockNumber() *big.Int
BlockDifficulty() *big.Int
BlockGasLimit() *big.Int
BlockCoinbase() common.Address
}

Expand Down Expand Up @@ -268,21 +268,21 @@ func (s *EphemeralStorage) GetPreimageSize(hash common.Hash) int {
var _ Storage = (*EphemeralStorage)(nil)

type Block interface {
Timestamp() *big.Int
Timestamp() uint64
GasLimit() uint64
Number() *big.Int
Difficulty() *big.Int
GasLimit() *big.Int
Coinbase() common.Address
}

type FullBlock struct {
evm EVM
}

func (b *FullBlock) Timestamp() *big.Int { return b.evm.BlockTimestamp() }
func (b *FullBlock) Timestamp() uint64 { return b.evm.BlockTimestamp() }
func (b *FullBlock) GasLimit() uint64 { return b.evm.BlockGasLimit() }
func (b *FullBlock) Number() *big.Int { return b.evm.BlockNumber() }
func (b *FullBlock) Difficulty() *big.Int { return b.evm.BlockDifficulty() }
func (b *FullBlock) GasLimit() *big.Int { return b.evm.BlockGasLimit() }
func (b *FullBlock) Coinbase() common.Address { return b.evm.BlockCoinbase() }

var _ Block = (*FullBlock)(nil)
Expand Down
18 changes: 9 additions & 9 deletions concrete/api/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ type Array interface {
Push(value common.Hash)
Pop() common.Hash
Swap(i, j int)
GetReference(key int) Reference
GetMap(key int) Mapping
GetArray(key int) Array
GetReference(index int) Reference
GetMap(index int) Mapping
GetArray(index int) Array
}

type array struct {
Expand Down Expand Up @@ -250,24 +250,24 @@ func (a *array) Swap(i, j int) {
a.Set(j, iVal)
}

func (a *array) GetReference(key int) Reference {
func (a *array) GetReference(index int) Reference {
return &reference{
ds: a.ds,
key: a.key(key),
key: a.key(index),
}
}

func (a *array) GetMap(key int) Mapping {
func (a *array) GetMap(index int) Mapping {
return &mapping{
ds: a.ds,
id: a.key(key),
id: a.key(index),
}
}

func (a *array) GetArray(key int) Array {
func (a *array) GetArray(index int) Array {
return &array{
ds: a.ds,
id: a.key(key),
id: a.key(index),
}
}

Expand Down
104 changes: 52 additions & 52 deletions concrete/api/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
cc_api "github.com/ethereum/go-ethereum/concrete/api"
"github.com/ethereum/go-ethereum/concrete/api"
"github.com/stretchr/testify/require"
)

var statedbs = []struct {
name string
constructor func() cc_api.StateDB
constructor func() api.StateDB
readOnly bool
commitSafe bool
}{
{
name: "StateDB",
constructor: func() cc_api.StateDB {
constructor: func() api.StateDB {
return NewMockStateDB()
},
readOnly: false,
commitSafe: false,
},
{
name: "ReadOnlyStateDB",
constructor: func() cc_api.StateDB {
return cc_api.NewReadOnlyStateDB(NewMockStateDB())
constructor: func() api.StateDB {
return api.NewReadOnlyStateDB(NewMockStateDB())
},
readOnly: true,
commitSafe: true,
},
{
name: "CommitSafeStateDB",
constructor: func() cc_api.StateDB {
return cc_api.NewCommitSafeStateDB(NewMockStateDB())
constructor: func() api.StateDB {
return api.NewCommitSafeStateDB(NewMockStateDB())
},
readOnly: false,
commitSafe: true,
Expand All @@ -58,85 +58,85 @@ var statedbs = []struct {

var statedbMethods = []struct {
name string
call func(statedb cc_api.StateDB)
call func(statedb api.StateDB)
readOnly bool
commitSafe bool
}{
{
name: "SetPersistentState",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.SetPersistentState(common.Address{}, common.Hash{}, common.Hash{})
},
readOnly: false,
commitSafe: false,
},
{
name: "SetEphemeralState",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.SetEphemeralState(common.Address{}, common.Hash{}, common.Hash{})
},
readOnly: false,
commitSafe: true,
},
{
name: "AddPersistentPreimage",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.AddPersistentPreimage(common.Hash{}, []byte{})
},
readOnly: false,
commitSafe: true,
},
{
name: "AddEphemeralPreimage",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.AddEphemeralPreimage(common.Hash{}, []byte{})
},
readOnly: false,
commitSafe: true,
},
{
name: "GetPersistentState",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.GetPersistentState(common.Address{}, common.Hash{})
},
readOnly: true,
commitSafe: true,
},
{
name: "GetEphemeralState",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.GetEphemeralState(common.Address{}, common.Hash{})
},
readOnly: true,
commitSafe: true,
},
{
name: "GetPersistentPreimage",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.GetPersistentPreimage(common.Hash{})
},
readOnly: true,
commitSafe: true,
},
{
name: "GetPersistentPreimageSize",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.GetPersistentPreimageSize(common.Hash{})
},
readOnly: true,
commitSafe: true,
},
{
name: "GetEphemeralPreimage",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.GetEphemeralPreimage(common.Hash{})
},
readOnly: true,
commitSafe: true,
},
{
name: "GetEphemeralPreimageSize",
call: func(statedb cc_api.StateDB) {
call: func(statedb api.StateDB) {
statedb.GetEphemeralPreimageSize(common.Hash{})
},
readOnly: true,
Expand Down Expand Up @@ -164,29 +164,29 @@ func TestStateDB(t *testing.T) {

var evms = []struct {
name string
constructor func() cc_api.EVM
constructor func() api.EVM
statedbType interface{}
}{
{
name: "EVM",
constructor: func() cc_api.EVM {
constructor: func() api.EVM {
return NewMockEVM(NewMockStateDB())
},
statedbType: &MockStateDB{},
},
{
name: "ReadOnlyEVM",
constructor: func() cc_api.EVM {
return cc_api.NewReadOnlyEVM(NewMockEVM(NewMockStateDB()))
constructor: func() api.EVM {
return api.NewReadOnlyEVM(NewMockEVM(NewMockStateDB()))
},
statedbType: &cc_api.ReadOnlyStateDB{},
statedbType: &api.ReadOnlyStateDB{},
},
{
name: "CommitSafeEVM",
constructor: func() cc_api.EVM {
return cc_api.NewCommitSafeEVM(NewMockEVM(NewMockStateDB()))
constructor: func() api.EVM {
return api.NewCommitSafeEVM(NewMockEVM(NewMockStateDB()))
},
statedbType: &cc_api.CommitSafeStateDB{},
statedbType: &api.CommitSafeStateDB{},
},
}

Expand All @@ -204,18 +204,18 @@ func TestEVM(t *testing.T) {

var storages = []struct {
name string
constructor func() cc_api.Storage
constructor func() api.Storage
}{
{
name: "PersistentStorage",
constructor: func() cc_api.Storage {
return cc_api.NewPersistentStorage(NewMockStateDB(), common.Address{})
constructor: func() api.Storage {
return api.NewPersistentStorage(NewMockStateDB(), common.Address{})
},
},
{
name: "EphemeralStorage",
constructor: func() cc_api.Storage {
return cc_api.NewEphemeralStorage(NewMockStateDB(), common.Address{})
constructor: func() api.Storage {
return api.NewEphemeralStorage(NewMockStateDB(), common.Address{})
},
},
}
Expand All @@ -232,44 +232,44 @@ func TestAPIStorage(t *testing.T) {

var apis = []struct {
name string
constructor func() cc_api.API
constructor func() api.API
readOnly bool
stateOnly bool
}{
{
name: "API",
constructor: func() cc_api.API {
constructor: func() api.API {
statedb := NewMockStateDB()
evm := NewMockEVM(statedb)
return cc_api.New(evm, common.Address{})
return api.New(evm, common.Address{})
},
readOnly: false,
stateOnly: false,
},
{
name: "StateAPI",
constructor: func() cc_api.API {
constructor: func() api.API {
statedb := NewMockStateDB()
return cc_api.NewStateAPI(statedb, common.Address{})
return api.NewStateAPI(statedb, common.Address{})
},
readOnly: false,
stateOnly: true,
},
{
name: "ReadOnlyAPI",
constructor: func() cc_api.API {
constructor: func() api.API {
statedb := NewMockStateDB()
evm := cc_api.NewReadOnlyEVM(NewMockEVM(statedb))
return cc_api.New(evm, common.Address{})
evm := api.NewReadOnlyEVM(NewMockEVM(statedb))
return api.New(evm, common.Address{})
},
readOnly: true,
stateOnly: false,
},
{
name: "ReadOnlyStateAPI",
constructor: func() cc_api.API {
statedb := cc_api.NewReadOnlyStateDB(NewMockStateDB())
return cc_api.NewStateAPI(statedb, common.Address{})
constructor: func() api.API {
statedb := api.NewReadOnlyStateDB(NewMockStateDB())
return api.NewStateAPI(statedb, common.Address{})
},
readOnly: true,
stateOnly: true,
Expand All @@ -285,18 +285,18 @@ func TestStateAPI(t *testing.T) {
)
for _, specs := range apis {
t.Run(specs.name, func(t *testing.T) {
api := specs.constructor()
r.NotNil(api.StateDB(), "StateDB should not be nil")
r.NotNil(api.Ephemeral(), "Ephemeral should not be nil")
r.NotNil(api.Persistent(), "Persistent should not be nil")
API := specs.constructor()
r.NotNil(API.StateDB(), "StateDB should not be nil")
r.NotNil(API.Ephemeral(), "Ephemeral should not be nil")
r.NotNil(API.Persistent(), "Persistent should not be nil")
if specs.stateOnly {
r.Nil(api.EVM(), "EVM should be nil")
r.Panics(func() { api.BlockHash(big.NewInt(0)) }, "BlockHash should panic")
r.Panics(func() { api.Block() }, "Block should panic")
r.Nil(API.EVM(), "EVM should be nil")
r.Panics(func() { API.BlockHash(big.NewInt(0)) }, "BlockHash should panic")
r.Panics(func() { API.Block() }, "Block should panic")
} else {
r.NotNil(api.EVM(), "EVM should not be nil")
r.NotPanics(func() { api.BlockHash(big.NewInt(0)) }, "BlockHash should not panic")
r.NotPanics(func() { api.Block() }, "Block should not panic")
r.NotNil(API.EVM(), "EVM should not be nil")
r.NotPanics(func() { API.BlockHash(big.NewInt(0)) }, "BlockHash should not panic")
r.NotPanics(func() { API.Block() }, "Block should not panic")
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions concrete/api/test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ func NewMockEVM(db api.StateDB) api.EVM {

func (m *mockEVM) StateDB() api.StateDB { return m.db }
func (m *mockEVM) BlockHash(block *big.Int) common.Hash { return common.Hash{} }
func (m *mockEVM) BlockTimestamp() *big.Int { return common.Big0 }
func (m *mockEVM) BlockTimestamp() uint64 { return 0 }
func (m *mockEVM) BlockGasLimit() uint64 { return 0 }
func (m *mockEVM) BlockNumber() *big.Int { return common.Big0 }
func (m *mockEVM) BlockDifficulty() *big.Int { return common.Big0 }
func (m *mockEVM) BlockGasLimit() *big.Int { return common.Big0 }
func (m *mockEVM) BlockCoinbase() common.Address { return common.Address{} }

var _ api.EVM = &mockEVM{}
Loading

0 comments on commit aaf2a70

Please sign in to comment.