Skip to content

Commit

Permalink
go: Add support for EIP-2929
Browse files Browse the repository at this point in the history
Implement Go bindings for access_account() and access_storage()
Host methods.
  • Loading branch information
yperbasis authored and chfast committed Mar 29, 2021
1 parent c91f6d6 commit 5304359
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bindings/go/evmc/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
const struct evmc_host_interface evmc_go_host = {
(evmc_account_exists_fn)accountExists,
(evmc_access_account_fn)accessAccount,
(evmc_access_storage_fn)accessStorage,
(evmc_get_storage_fn)getStorage,
(evmc_set_storage_fn)setStorage,
(evmc_get_balance_fn)getBalance,
Expand Down Expand Up @@ -46,6 +48,8 @@ static inline void go_exported_functions_type_checks()
(void)tx_context;
struct evmc_result result;
(void)result;
enum evmc_access_status access_status;
(void)access_status;
enum evmc_storage_status storage_status;
(void)storage_status;
bool bool_flag;
Expand All @@ -55,6 +59,14 @@ static inline void go_exported_functions_type_checks()
bool_flag = account_exists_fn(context, address);
bool_flag = accountExists(context, address);

evmc_access_account_fn access_account_fn = NULL;
access_status = access_account_fn(context, address);
access_status = accessAccount(context, address);

evmc_access_storage_fn access_storage_fn = NULL;
access_status = access_storage_fn(context, address, &bytes32);
access_status = accessStorage(context, address, &bytes32);

evmc_get_storage_fn get_storage_fn = NULL;
bytes32 = get_storage_fn(context, address, &bytes32);
bytes32 = getStorage(context, address, &bytes32);
Expand Down
21 changes: 21 additions & 0 deletions bindings/go/evmc/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const (
Create2 CallKind = C.EVMC_CREATE2
)

type AccessStatus int

const (
ColdAccess AccessStatus = C.EVMC_ACCESS_COLD
WarmAccess AccessStatus = C.EVMC_ACCESS_WARM
)

type StorageStatus int

const (
Expand Down Expand Up @@ -73,6 +80,8 @@ type TxContext struct {

type HostContext interface {
AccountExists(addr Address) bool
AccessAccount(addr Address) AccessStatus
AccessStorage(addr Address, key Hash) AccessStatus
GetStorage(addr Address, key Hash) Hash
SetStorage(addr Address, key Hash, value Hash) StorageStatus
GetBalance(addr Address) Hash
Expand All @@ -94,6 +103,18 @@ func accountExists(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.bool {
return C.bool(ctx.AccountExists(goAddress(*pAddr)))
}

//export accessAccount
func accessAccount(pCtx unsafe.Pointer, pAddr *C.evmc_address) C.enum_evmc_access_status {
ctx := getHostContext(uintptr(pCtx))
return C.enum_evmc_access_status(ctx.AccessAccount(goAddress(*pAddr)))
}

//export accessStorage
func accessStorage(pCtx unsafe.Pointer, pAddr *C.evmc_address, pKey *C.evmc_bytes32) C.enum_evmc_access_status {
ctx := getHostContext(uintptr(pCtx))
return C.enum_evmc_access_status(ctx.AccessStorage(goAddress(*pAddr), goHash(*pKey)))
}

//export getStorage
func getStorage(pCtx unsafe.Pointer, pAddr *C.struct_evmc_address, pKey *C.evmc_bytes32) C.evmc_bytes32 {
ctx := getHostContext(uintptr(pCtx))
Expand Down
8 changes: 8 additions & 0 deletions bindings/go/evmc/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func (host *testHostContext) AccountExists(addr Address) bool {
return false
}

func (host *testHostContext) AccessAccount(addr Address) AccessStatus {
return ColdAccess
}

func (host *testHostContext) AccessStorage(addr Address, key Hash) AccessStatus {
return ColdAccess
}

func (host *testHostContext) GetStorage(addr Address, key Hash) Hash {
return Hash{}
}
Expand Down

0 comments on commit 5304359

Please sign in to comment.