Skip to content

Commit

Permalink
Update C API to use uint64_t instead of uint256be where it makes sense (
Browse files Browse the repository at this point in the history
#144)

* return uint64_t from get_balance host call in FFI

* use uint64_t for tx_gas_price in FFI

* fix go bindings
  • Loading branch information
poszu authored Oct 12, 2024
1 parent a9cc278 commit 461e631
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 123 deletions.
4 changes: 2 additions & 2 deletions ffi/athcon/bindings/go/athcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (vm *VM) Execute(
recipient, sender Address,
input []byte,
method []byte,
value Bytes32,
value uint64,
code []byte,
) (res Result, err error) {
if len(code) == 0 {
Expand All @@ -183,7 +183,7 @@ func (vm *VM) Execute(
gas: C.int64_t(gas),
recipient: athconAddress(recipient),
sender: athconAddress(sender),
value: athconBytes32(value),
value: C.uint64_t(value),
}
if len(input) > 0 {
// Allocate memory for input data in C.
Expand Down
3 changes: 1 addition & 2 deletions ffi/athcon/bindings/go/athcon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func TestExecuteEmptyCode(t *testing.T) {
defer vm.Destroy()

addr := Address{}
h := Bytes32{}
result, err := vm.Execute(nil, Frontier, Call, 1, 999, addr, addr, nil, nil, h, nil)
result, err := vm.Execute(nil, Frontier, Call, 1, 999, addr, addr, nil, nil, 0, nil)

require.Error(t, err)
require.Empty(t, result.Output)
Expand Down
16 changes: 8 additions & 8 deletions ffi/athcon/bindings/go/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package athcon
bool accountExists(void *ctx, athcon_address *addr);
athcon_bytes32 getStorage(void *ctx, athcon_address *addr, athcon_bytes32 *key);
enum athcon_storage_status setStorage(void *ctx, athcon_address *addr, athcon_bytes32 *key, athcon_bytes32 *val);
athcon_uint256be getBalance(void *ctx, athcon_address *addr);
uint64_t getBalance(void *ctx, athcon_address *addr);
struct athcon_tx_context getTxContext(void *ctx);
athcon_bytes32 getBlockHash(void *ctx, long long int number);
struct athcon_result call(void *ctx, struct athcon_message *msg);
Expand Down Expand Up @@ -68,7 +68,7 @@ func goByteSlice(data *C.uint8_t, size C.size_t) []byte {

// TxContext contains information about current transaction and block.
type TxContext struct {
GasPrice Bytes32
GasPrice uint64
Origin Address
Coinbase Address
BlockHeight int64
Expand All @@ -81,10 +81,10 @@ type HostContext interface {
AccountExists(addr Address) bool
GetStorage(addr Address, key Bytes32) Bytes32
SetStorage(addr Address, key Bytes32, value Bytes32) StorageStatus
GetBalance(addr Address) Bytes32
GetBalance(addr Address) uint64
GetTxContext() TxContext
GetBlockHash(number int64) Bytes32
Call(kind CallKind, recipient Address, sender Address, value Bytes32, input []byte, method []byte, gas int64, depth int) (
Call(kind CallKind, recipient Address, sender Address, value uint64, input []byte, method []byte, gas int64, depth int) (
output []byte, gasLeft int64, createAddr Address, err error)
Spawn(blob []byte) Address
Deploy(code []byte) Address
Expand All @@ -109,9 +109,9 @@ func setStorage(pCtx unsafe.Pointer, pAddr *C.athcon_address, pKey *C.athcon_byt
}

//export getBalance
func getBalance(pCtx unsafe.Pointer, pAddr *C.athcon_address) C.athcon_uint256be {
func getBalance(pCtx unsafe.Pointer, pAddr *C.athcon_address) C.uint64_t {
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)
return athconBytes32(ctx.GetBalance(goAddress(*pAddr)))
return C.uint64_t(ctx.GetBalance(goAddress(*pAddr)))
}

//export getTxContext
Expand All @@ -120,7 +120,7 @@ func getTxContext(pCtx unsafe.Pointer) C.struct_athcon_tx_context {
txContext := ctx.GetTxContext()

return C.struct_athcon_tx_context{
athconBytes32(txContext.GasPrice),
C.uint64_t(txContext.GasPrice),
athconAddress(txContext.Origin),
C.int64_t(txContext.BlockHeight),
C.int64_t(txContext.Timestamp),
Expand All @@ -140,7 +140,7 @@ func call(pCtx unsafe.Pointer, msg *C.struct_athcon_message) C.struct_athcon_res
ctx := (*cgo.Handle)(pCtx).Value().(HostContext)

kind := CallKind(msg.kind)
output, gasLeft, createAddr, err := ctx.Call(kind, goAddress(msg.recipient), goAddress(msg.sender), goHash(msg.value),
output, gasLeft, createAddr, err := ctx.Call(kind, goAddress(msg.recipient), goAddress(msg.sender), uint64(msg.value),
goByteSlice(msg.input_data, msg.input_size), goByteSlice(msg.method_name, msg.method_name_size), int64(msg.gas), int(msg.depth))

statusCode := C.enum_athcon_status_code(0)
Expand Down
14 changes: 5 additions & 9 deletions ffi/athcon/bindings/go/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ func (host *testHostContext) SetStorage(addr Address, key Bytes32, value Bytes32
return StorageAdded
}

func (host *testHostContext) GetBalance(addr Address) Bytes32 {
var b Bytes32
binary.LittleEndian.PutUint32(b[:], 42)
return b
func (host *testHostContext) GetBalance(addr Address) uint64 {
return 42
}

func (host *testHostContext) GetTxContext() TxContext {
Expand All @@ -46,7 +44,7 @@ func (host *testHostContext) GetBlockHash(number int64) Bytes32 {
}

func (host *testHostContext) Call(kind CallKind,
recipient Address, sender Address, value Bytes32, input []byte, method []byte, gas int64, depth int) (
recipient Address, sender Address, value uint64, input []byte, method []byte, gas int64, depth int) (
output []byte, gasLeft int64, createAddr Address, err error) {
return nil, gas, Address{}, nil
}
Expand All @@ -67,8 +65,7 @@ func TestGetBalance(t *testing.T) {

host := &testHostContext{}
addr := Address{}
h := Bytes32{}
result, err := vm.Execute(host, Frontier, Call, 1, 100, addr, addr, nil, nil, h, MINIMAL_TEST_CODE)
result, err := vm.Execute(host, Frontier, Call, 1, 100, addr, addr, nil, nil, 0, MINIMAL_TEST_CODE)
output := result.Output
gasLeft := result.GasLeft

Expand Down Expand Up @@ -97,8 +94,7 @@ func TestCall(t *testing.T) {

host := &testHostContext{}
addr := Address{}
h := Bytes32{}
result, err := vm.Execute(host, Frontier, Call, 1, 10000, addr, addr, []byte{2, 0, 0, 0}, nil, h, RECURSIVE_CALL_TEST)
result, err := vm.Execute(host, Frontier, Call, 1, 10000, addr, addr, []byte{2, 0, 0, 0}, nil, 0, RECURSIVE_CALL_TEST)
output := result.Output

if len(output) != 4 {
Expand Down
20 changes: 9 additions & 11 deletions ffi/athcon/bindings/rust/athcon-client/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub trait HostContext {
fn account_exists(&self, addr: &Address) -> bool;
fn get_storage(&self, addr: &Address, key: &Bytes32) -> Bytes32;
fn set_storage(&mut self, addr: &Address, key: &Bytes32, value: &Bytes32) -> StorageStatus;
fn get_balance(&self, addr: &Address) -> Bytes32;
fn get_tx_context(&self) -> (Bytes32, Address, i64, i64, i64, Bytes32);
fn get_balance(&self, addr: &Address) -> u64;
fn get_tx_context(&self) -> (u64, Address, i64, i64, i64, Bytes32);
fn get_block_hash(&self, number: i64) -> Bytes32;
fn spawn(&mut self, blob: &[u8]) -> Address;
fn deploy(&mut self, blob: &[u8]) -> Address;
Expand All @@ -22,7 +22,7 @@ pub trait HostContext {
kind: MessageKind,
recipient: &Address,
sender: &Address,
value: &Bytes32,
value: u64,
input: &Bytes,
method: &Bytes,
gas: i64,
Expand Down Expand Up @@ -81,12 +81,10 @@ unsafe extern "C" fn set_storage(
unsafe extern "C" fn get_balance(
context: *mut ffi::athcon_host_context,
address: *const ffi::athcon_address,
) -> ffi::athcon_uint256be {
ffi::athcon_uint256be {
bytes: (*(context as *mut ExtendedContext))
.hctx
.get_balance(&(*address).bytes),
}
) -> u64 {
(*(context as *mut ExtendedContext))
.hctx
.get_balance(&(*address).bytes)
}

unsafe extern "C" fn get_tx_context(
Expand All @@ -95,7 +93,7 @@ unsafe extern "C" fn get_tx_context(
let (gas_price, origin, height, timestamp, gas_limit, chain_id) =
(*(context as *mut ExtendedContext)).hctx.get_tx_context();
ffi::athcon_tx_context {
tx_gas_price: athcon_sys::athcon_bytes32 { bytes: gas_price },
tx_gas_price: gas_price,
tx_origin: athcon_sys::athcon_address { bytes: origin },
block_height: height,
block_timestamp: timestamp,
Expand Down Expand Up @@ -161,7 +159,7 @@ pub unsafe extern "C" fn call(
msg.kind,
&msg.recipient.bytes,
&msg.sender.bytes,
&msg.value.bytes,
msg.value,
if !msg.input_data.is_null() && msg.input_size > 0 {
std::slice::from_raw_parts(msg.input_data, msg.input_size)
} else {
Expand Down
4 changes: 2 additions & 2 deletions ffi/athcon/bindings/rust/athcon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl AthconVm {
sender: &Address,
input: &[u8],
method: &[u8],
value: &[u8; 32],
value: u64,
code: &[u8],
) -> (Vec<u8>, i64, StatusCode) {
let ext_ctx = host::ExtendedContext { hctx: ctx };
Expand All @@ -70,7 +70,7 @@ impl AthconVm {
input_size: input.len(),
method_name: method.as_ptr(),
method_name_size: method.len(),
value: ffi::athcon_uint256be { bytes: *value },
value,
code: code.as_ptr(),
code_size: code.len(),
};
Expand Down
19 changes: 6 additions & 13 deletions ffi/athcon/bindings/rust/athcon-client/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,14 @@ impl HostInterface for HostContext {
StorageStatus::ATHCON_STORAGE_MODIFIED
}

fn get_balance(&self, _addr: &Address) -> Bytes32 {
fn get_balance(&self, _addr: &Address) -> u64 {
println!("Host: get_balance");
[0u8; BYTES32_LENGTH]
0
}

fn get_tx_context(&self) -> (Bytes32, Address, i64, i64, i64, Bytes32) {
fn get_tx_context(&self) -> (u64, Address, i64, i64, i64, Bytes32) {
println!("Host: get_tx_context");
(
[0u8; BYTES32_LENGTH],
[0u8; ADDRESS_LENGTH],
0,
0,
0,
[0u8; BYTES32_LENGTH],
)
(0, [0u8; ADDRESS_LENGTH], 0, 0, 0, [0u8; BYTES32_LENGTH])
}

fn get_block_hash(&self, _number: i64) -> Bytes32 {
Expand All @@ -79,7 +72,7 @@ impl HostInterface for HostContext {
kind: MessageKind,
destination: &Address,
sender: &Address,
value: &Bytes32,
value: u64,
input: &Bytes,
method: &Bytes,
gas: i64,
Expand Down Expand Up @@ -161,7 +154,7 @@ fn test_rust_host() {
3u32.to_le_bytes().as_slice(),
// empty method name
&[],
&[0u8; BYTES32_LENGTH],
0,
CONTRACT_CODE,
);
println!("Output: {:?}", hex::encode(&output));
Expand Down
4 changes: 2 additions & 2 deletions ffi/athcon/bindings/rust/athcon-vm/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod tests {
_context: *mut athcon_sys::athcon_host_context,
) -> athcon_sys::athcon_tx_context {
athcon_sys::athcon_tx_context {
tx_gas_price: Uint256::default(),
tx_gas_price: 0,
tx_origin: Address::default(),
block_height: 0,
block_timestamp: 0,
Expand Down Expand Up @@ -123,7 +123,7 @@ mod tests {
input_size: 0,
method_name: std::ptr::null(),
method_name_size: 0,
value: ::athcon_sys::athcon_uint256be::default(),
value: 0,
code: std::ptr::null(),
code_size: 0,
};
Expand Down
Loading

0 comments on commit 461e631

Please sign in to comment.