Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

chain/owasm: Increase wasm gas limit and consume gas when reading / writing #2150

Merged
merged 3 commits into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@

### Owasm

- (feat) [\#2150](https://github.com/bandprotocol/bandchain/pull/2150) Increase wasm gas limit and consume gas when reading / writing.

### Oracle Binary Encoding (OBI)

- (impv) [#1947](https://github.com/bandprotocol/bandchain/pull/2065) Remove obi.js build process
Expand Down
4 changes: 2 additions & 2 deletions chain/x/oracle/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
MaxCompiledWasmCodeSize = 1 * 1024 * 1024 // 1MB
MaxDataSize = 1 * 1024 // 1kB

WasmPrepareGas = 100000
WasmExecuteGas = 100000
WasmPrepareGas = 1000000
WasmExecuteGas = 5000000
)

// nolint
Expand Down
Binary file modified go-owasm/api/libgo_owasm.dylib
Binary file not shown.
Binary file modified go-owasm/api/libgo_owasm.so
Binary file not shown.
12 changes: 8 additions & 4 deletions go-owasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn run(code: &[u8], gas_limit: u32, span_size: i64, is_prepare: bool, env: Env)
"read_calldata" => func!(|ctx: &mut Ctx, ptr: i64| -> Result<i64, Error> {
let vm: &mut vm::VMLogic = unsafe { &mut *(ctx.data as *mut vm::VMLogic) };
let span_size = vm.get_span_size();
vm.consume_gas(span_size as u32)?;
require_mem_range(ctx.memory(0).size().bytes().0, (ptr + span_size) as usize)?;
let mut mem: Vec<u8> = Vec::with_capacity(span_size as usize);
let mut calldata = Span::create_writable(mem.as_mut_ptr(), span_size as usize);
Expand All @@ -183,6 +184,7 @@ fn run(code: &[u8], gas_limit: u32, span_size: i64, is_prepare: bool, env: Env)
if len > vm.get_span_size() {
return Err(Error::SpanTooSmallError);
}
vm.consume_gas(len as u32)?;
require_mem_range(ctx.memory(0).size().bytes().0, (ptr + len) as usize)?;
let data: Vec<u8> = ctx.memory(0).view()[ptr as usize..(ptr + len) as usize].iter().map(|cell| cell.get()).collect();
vm.set_return_data(&data)
Expand All @@ -204,6 +206,7 @@ fn run(code: &[u8], gas_limit: u32, span_size: i64, is_prepare: bool, env: Env)
if len > vm.get_span_size() {
return Err(Error::SpanTooSmallError);
}
vm.consume_gas(len as u32)?;
require_mem_range(ctx.memory(0).size().bytes().0, (ptr + len) as usize)?;
let data: Vec<u8> = ctx.memory(0).view()[ptr as usize..(ptr + len) as usize].iter().map(|cell| cell.get()).collect();
vm.ask_external_data(eid, did, &data)
Expand All @@ -214,10 +217,11 @@ fn run(code: &[u8], gas_limit: u32, span_size: i64, is_prepare: bool, env: Env)
}),
"read_external_data" => func!(|ctx: &mut Ctx, eid: i64, vid: i64, ptr: i64| -> Result<i64, Error> {
let vm: &mut vm::VMLogic = unsafe { &mut *(ctx.data as *mut vm::VMLogic) };
let span_size = vm.get_span_size() as usize;
let mut mem: Vec<u8> = Vec::with_capacity(span_size);
let mut data = Span::create_writable(mem.as_mut_ptr(), span_size);
// TODO: span_size bound check
let span_size = vm.get_span_size();
vm.consume_gas(span_size as u32)?;
require_mem_range(ctx.memory(0).size().bytes().0, (ptr + span_size) as usize)?;
let mut mem: Vec<u8> = Vec::with_capacity(span_size as usize);
let mut data = Span::create_writable(mem.as_mut_ptr(), span_size as usize);
vm.get_external_data(eid, vid, &mut data)?;
for (idx, byte) in data.read().iter().enumerate() {
ctx.memory(0).view()[ptr as usize + idx].set(*byte)
Expand Down