Skip to content

Commit

Permalink
use test-contract-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Jun 24, 2021
1 parent 3bf7e74 commit 944c8d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Binary file removed pytest/testdata/fib.wasm
Binary file not shown.
21 changes: 10 additions & 11 deletions pytest/tests/sanity/rpc_max_gas_burnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,37 @@ def test_max_gas_burnt_view():
config=None,
genesis_config_changes=[],
client_config_changes={
1: {'max_gas_burnt_view': int(1e10)}
1: {'max_gas_burnt_view': int(2e10)}
})

contract_key = nodes[0].signer_key
fib_contract = load_binary_file('testdata/fib.wasm')
contract = load_binary_file(
'../runtime/near-test-contracts/res/test_contract_rs.wasm')

# Deploy the fib smart contract
status = nodes[0].get_status()
latest_block_hash = status['sync_info']['latest_block_hash']
deploy_contract_tx = transaction.sign_deploy_contract_tx(
contract_key, fib_contract, 10,
contract_key, contract, 10,
base58.b58decode(latest_block_hash.encode('utf8')))
deploy_contract_response = nodes[0].send_tx_and_wait(deploy_contract_tx, 10)

def call_fib(node, n):
args = json.dumps({'n': n})
args = base64.b64encode(args.encode('utf-8')).decode('ascii')
res = node.call_function(contract_key.account_id, 'fib', args,
args = base64.b64encode(bytes([n])).decode('ascii')
res = node.call_function(contract_key.account_id, 'fibonacci', args,
timeout=10)
return res.get('result')

# Call view function of the smart contract via the first node. This should
# succeed.
result = call_fib(nodes[0], 10)
result = call_fib(nodes[0], 20)
assert 'result' in result and 'error' not in result, (
'Expected "result" and no "error" in response, got: {}'.format(result))
result = bytes(result['result']).decode('ascii')
assert result == '"55"', (
'Expected result to be "55" but got: {}'.format(result))
n = int.from_bytes(bytes(result['result']), 'little')
assert n == 6765, ('Expected result to be 6765 but got: {}'.format(result))

# Same but against the second node. This should fail because of gas limit.
result = call_fib(nodes[1], 10)
result = call_fib(nodes[1], 20)
assert 'result' not in result and 'error' in result, (
'Expected "error" and no "result" in response, got: {}'.format(result))
error = result['error']
Expand Down
22 changes: 22 additions & 0 deletions runtime/near-test-contracts/test-contract-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,28 @@ pub unsafe fn sum_n() {
value_return(data.len() as u64, data.as_ptr() as u64);
}

/// Calculates Fibonacci numbers in inefficient way. Used to burn gas for the
/// sanity/max_gas_burnt_view.py test.
#[no_mangle]
pub unsafe fn fibonacci() {
input(0);
if register_len(0) != 1 {
panic()
}
let mut n: u8 = 0;
read_register(0, &mut n as *mut u8 as u64);
let data = fib(n).to_le_bytes();
value_return(data.len() as u64, data.as_ptr() as u64);
}

fn fib(n: u8) -> u64 {
if n < 2 {
n as u64
} else {
fib(n - 2) + fib(n - 1)
}
}

#[no_mangle]
pub unsafe fn insert_strings() {
input(0);
Expand Down

0 comments on commit 944c8d7

Please sign in to comment.