Skip to content

Commit

Permalink
new blockchain hooks test sc
Browse files Browse the repository at this point in the history
  • Loading branch information
laurci committed Jul 19, 2024
1 parent f232172 commit c8c1c16
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/contracts/new-blockchain-hooks/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
rustc --target wasm32-unknown-unknown -O --crate-type=cdylib src/lib.rs -o output/new-blockchain-hooks.wasm
Binary file not shown.
51 changes: 51 additions & 0 deletions test/contracts/new-blockchain-hooks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![no_std]
#![no_main]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[no_mangle]
pub extern "C" fn test_round_time() {
unsafe {
let result = getRoundTime();
let result: [u8; 1] = [(result & 0xff) as u8];
finish(result.as_ref().as_ptr(), 1);
};
}

#[no_mangle]
pub extern "C" fn test_epoch_start_block_time_stamp() {
unsafe {
let result = epochStartBlockTimeStamp();
let result: [u8; 1] = [(result & 0xff) as u8];
finish(result.as_ref().as_ptr(), 1);
};
}

#[no_mangle]
pub extern "C" fn test_epoch_start_block_nonce() {
unsafe {
let result = epochStartBlockNonce();
let result: [u8; 1] = [(result & 0xff) as u8];
finish(result.as_ref().as_ptr(), 1);
};
}

#[no_mangle]
pub extern "C" fn test_epoch_start_block_round() {
unsafe {
let result = epochStartBlockRound();
let result: [u8; 1] = [(result & 0xff) as u8];
finish(result.as_ref().as_ptr(), 1);
};
}

extern {
fn finish(data: *const u8, len: i32);
fn getRoundTime() -> i64;
fn epochStartBlockTimeStamp() -> i64;
fn epochStartBlockNonce() -> i64;
fn epochStartBlockRound() -> i64;
}
131 changes: 131 additions & 0 deletions vmhost/hosttest/newBloclchainHooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package hostCoretest

import (
"math/big"
"testing"

vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-go/testcommon"

"github.com/stretchr/testify/require"
)

func TestNewBlockchainHooks_GetRoundTime(t *testing.T) {
code := testcommon.GetTestSCCode("new-blockchain-hooks", "../../")

blockchainHook := testcommon.BlockchainHookStubForCall(code, nil)
blockchainHook.RoundTimeCalled = func() uint64 {
return 32
}

host := testcommon.NewTestHostBuilder(t).
WithBlockchainHook(blockchainHook).
Build()
defer func() {
host.Reset()
}()

input := testcommon.DefaultTestContractCallInput()
input.GasProvided = 100000
input.Function = "test_round_time"
input.CallValue = big.NewInt(64)

vmOutput, err := host.RunSmartContractCall(input)
require.Nil(t, err)

require.Equal(t, vmcommon.Ok, vmOutput.ReturnCode)
require.Len(t, vmOutput.ReturnData, 1)
require.Equal(t, "", vmOutput.ReturnMessage)

require.Equal(t, []byte{32}, vmOutput.ReturnData[0])
}

func TestNewBlockchainHooks_EpochStartBlockTimeStamp(t *testing.T) {
code := testcommon.GetTestSCCode("new-blockchain-hooks", "../../")

blockchainHook := testcommon.BlockchainHookStubForCall(code, nil)
blockchainHook.EpochStartBlockTimeStampCalled = func() uint64 {
return 31
}

host := testcommon.NewTestHostBuilder(t).
WithBlockchainHook(blockchainHook).
Build()
defer func() {
host.Reset()
}()

input := testcommon.DefaultTestContractCallInput()
input.GasProvided = 100000
input.Function = "test_epoch_start_block_time_stamp"
input.CallValue = big.NewInt(64)

vmOutput, err := host.RunSmartContractCall(input)
require.Nil(t, err)

require.Equal(t, vmcommon.Ok, vmOutput.ReturnCode)
require.Len(t, vmOutput.ReturnData, 1)
require.Equal(t, "", vmOutput.ReturnMessage)

require.Equal(t, []byte{31}, vmOutput.ReturnData[0])
}

func TestNewBlockchainHooks_EpochStartBlockNonce(t *testing.T) {
code := testcommon.GetTestSCCode("new-blockchain-hooks", "../../")

blockchainHook := testcommon.BlockchainHookStubForCall(code, nil)
blockchainHook.EpochStartBlockNonceCalled = func() uint64 {
return 30
}

host := testcommon.NewTestHostBuilder(t).
WithBlockchainHook(blockchainHook).
Build()
defer func() {
host.Reset()
}()

input := testcommon.DefaultTestContractCallInput()
input.GasProvided = 100000
input.Function = "test_epoch_start_block_nonce"
input.CallValue = big.NewInt(64)

vmOutput, err := host.RunSmartContractCall(input)
require.Nil(t, err)

require.Equal(t, vmcommon.Ok, vmOutput.ReturnCode)
require.Len(t, vmOutput.ReturnData, 1)
require.Equal(t, "", vmOutput.ReturnMessage)

require.Equal(t, []byte{30}, vmOutput.ReturnData[0])
}

func TestNewBlockchainHooks_EpochStartBlockRound(t *testing.T) {
code := testcommon.GetTestSCCode("new-blockchain-hooks", "../../")

blockchainHook := testcommon.BlockchainHookStubForCall(code, nil)
blockchainHook.EpochStartBlockRoundCalled = func() uint64 {
return 29
}

host := testcommon.NewTestHostBuilder(t).
WithBlockchainHook(blockchainHook).
Build()
defer func() {
host.Reset()
}()

input := testcommon.DefaultTestContractCallInput()
input.GasProvided = 100000
input.Function = "test_epoch_start_block_round"
input.CallValue = big.NewInt(64)

vmOutput, err := host.RunSmartContractCall(input)
require.Nil(t, err)

require.Equal(t, vmcommon.Ok, vmOutput.ReturnCode)
require.Len(t, vmOutput.ReturnData, 1)
require.Equal(t, "", vmOutput.ReturnMessage)

require.Equal(t, []byte{29}, vmOutput.ReturnData[0])
}

0 comments on commit c8c1c16

Please sign in to comment.