Skip to content

Commit

Permalink
Use soroban-test-wasms crate for test vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Sep 2, 2022
1 parent 3bfbca2 commit d89c499
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
13 changes: 13 additions & 0 deletions src/crypto/ByteSlice.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <xdrpp/message.h>
#include <xdrpp/types.h>

#ifdef ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
#include "rust/RustBridge.h"
#endif

namespace stellar
{

Expand Down Expand Up @@ -71,6 +75,15 @@ class ByteSlice
: mData(bytes.data()), mSize(bytes.size())
{
}
#ifdef ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
ByteSlice(Bytes const& buf) : mData(buf.vec.data()), mSize(buf.vec.size())
{
}
ByteSlice(XDRBuf const& buf)
: mData(buf.data->data()), mSize(buf.data->size())
{
}
#endif
ByteSlice(char const* str) : ByteSlice((void const*)str, strlen(str))
{
}
Expand Down
12 changes: 12 additions & 0 deletions src/rust/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,15 @@ pub(crate) fn preflight_host_function(
pfc.with_cb(|cb| cb.set_result_mem_bytes(mem_bytes))?;
Ok(())
}

// Accessors for test wasms, compiled into soroban-test-wasms crate.
pub(crate) fn get_test_wasm_add_i32() -> Result<Bytes, Box<dyn Error>> {
Ok(Bytes {
vec: soroban_test_wasms::ADD_I32.iter().cloned().collect(),
})
}
pub(crate) fn get_test_wasm_contract_data() -> Result<Bytes, Box<dyn Error>> {
Ok(Bytes {
vec: soroban_test_wasms::CONTRACT_DATA.iter().cloned().collect(),
})
}
6 changes: 6 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ mod rust_bridge {
cb: UniquePtr<PreflightCallbacks>,
) -> Result<()>;
fn init_logging(maxLevel: LogLevel) -> Result<()>;

// Accessors for test wasms, compiled into soroban-test-wasms crate.
fn get_test_wasm_add_i32() -> Result<Bytes>;
fn get_test_wasm_contract_data() -> Result<Bytes>;
}

// And the extern "C++" block declares C++ stuff we're going to import to
Expand Down Expand Up @@ -96,6 +100,8 @@ mod b64;
use b64::{from_base64, to_base64};

mod contract;
use contract::get_test_wasm_add_i32;
use contract::get_test_wasm_contract_data;
use contract::get_xdr_hashes;
use contract::invoke_host_function;
use contract::preflight_host_function;
Expand Down
71 changes: 25 additions & 46 deletions src/transactions/test/InvokeHostFunctionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "test/test.h"
#include "transactions/SignatureUtils.h"
#include "transactions/TransactionUtils.h"
#include "util/XDRCereal.h"
#include "xdr/Stellar-contract.h"
#include "xdr/Stellar-ledger-entries.h"
#include <autocheck/autocheck.hpp>
Expand All @@ -27,44 +28,6 @@
using namespace stellar;
using namespace stellar::txtest;

std::vector<uint8_t>
get_testdata(std::string const& filename)
{
std::filesystem::path fnPath("testdata");
fnPath /= filename;
std::ifstream in(fnPath);
REQUIRE(in);
in.exceptions(std::ios::badbit);
return std::vector<uint8_t>{std::istreambuf_iterator<char>(in), {}};
}

// Example WASM files are, for the time being, compiled from the SDK repo
// and then copied into this repo. They need to be regenerated anytime
// the environment interface version number changes, at minimum.
//
// To regenerate, check out the SDK, install a nightly toolchain with
// the rust-src component (to enable the 'tiny' build) using the following:
//
// $ rustup component add rust-src --toolchain nightly
//
// clang-format off
// then do:
// $ make -C $SDK build
// $ cp $SDK/target-tiny/wasm32-unknown-unknown/release/example_*.wasm $CORE/src/testdata/
// clang-format on

std::vector<uint8_t>
get_example_i32_wasm()
{
return get_testdata("example_add_i32.wasm");
}

std::vector<uint8_t>
get_example_contract_data_wasm()
{
return get_testdata("example_contract_data.wasm");
}

template <typename T>
SCVal
makeBinary(T begin, T end)
Expand All @@ -75,6 +38,17 @@ makeBinary(T begin, T end)
return val;
}

template <typename T>
SCVal
makeContract(T begin, T end)
{
SCVal val(SCValType::SCV_OBJECT);
val.obj().activate().type(stellar::SCO_CONTRACT_CODE);
val.obj()->contractCode().type(stellar::SCCONTRACT_CODE_WASM);
val.obj()->contractCode().wasm().assign(begin, end);
return val;
}

static SCVal
makeI32(int32_t i32)
{
Expand All @@ -92,9 +66,9 @@ makeSymbol(std::string const& str)
}

static LedgerKey
createContract(Application& app, std::vector<uint8_t> const& contract,
uint256 const& salt, PublicKey const& pub, Signature const& sig,
bool expectSuccess, bool expectEntry)
createContract(Application& app, Bytes const& contract, uint256 const& salt,
PublicKey const& pub, Signature const& sig, bool expectSuccess,
bool expectEntry)
{
HashIDPreimage preImage;
preImage.type(ENVELOPE_TYPE_CONTRACT_ID_FROM_ED25519);
Expand All @@ -108,7 +82,9 @@ createContract(Application& app, std::vector<uint8_t> const& contract,
auto& ihf = op.body.invokeHostFunctionOp();
ihf.function = HOST_FN_CREATE_CONTRACT;

auto contractBin = makeBinary(contract.begin(), contract.end());
auto contractCodeObj =
makeContract(contract.vec.begin(), contract.vec.end());
auto contractBin = makeBinary(contract.vec.begin(), contract.vec.end());
ihf.parameters = {contractBin, makeBinary(salt.begin(), salt.end()),
makeBinary(pub.ed25519().begin(), pub.ed25519().end()),
makeBinary(sig.begin(), sig.end())};
Expand Down Expand Up @@ -136,14 +112,17 @@ createContract(Application& app, std::vector<uint8_t> const& contract,
LedgerTxn ltx2(app.getLedgerTxnRoot());
auto ltxe2 = loadContractData(ltx2, contractID, wasmKey);
REQUIRE((bool)ltxe2 == expectEntry);
// FIXME: it's a little weird that we put a contractBin in and get a
// contractCodeObj out. This is probably a residual error from before an API
// change. See https://github.com/stellar/rs-soroban-env/issues/369
REQUIRE((!expectEntry ||
ltxe2.current().data.contractData().val == contractBin));
ltxe2.current().data.contractData().val == contractCodeObj));

return lk;
}

static LedgerKey
deployContract(Application& app, std::vector<uint8_t> const& contract)
deployContract(Application& app, Bytes const& contract)
{
uint256 salt = sha256("salt");
auto key = SecretKey::fromSeed(sha256("a1"));
Expand All @@ -167,8 +146,8 @@ TEST_CASE("invoke host function", "[tx][contract]")
auto app = createTestApplication(clock, getTestConfig());
auto root = TestAccount::createRoot(*app);

auto const addI32Wasm = get_example_i32_wasm();
auto const contractDataWasm = get_example_contract_data_wasm();
auto const addI32Wasm = rust_bridge::get_test_wasm_add_i32();
auto const contractDataWasm = rust_bridge::get_test_wasm_contract_data();

SECTION("add i32")
{
Expand Down

5 comments on commit d89c499

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from MonsieurNicolas
at graydon@d89c499

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging graydon/stellar-core/check-sha256-of-xdr-files = d89c499 into auto

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graydon/stellar-core/check-sha256-of-xdr-files = d89c499 merged ok, testing candidate = 364b2f7

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 364b2f7

Please sign in to comment.