Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainlib in browser #315

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ if(BUILD_NATIVE)
file(APPEND ${CMAKE_BINARY_DIR}/CTestTestfile.cmake "subdirs(\"native\")\n")
endif()

if(DEFINED WASI_SDK_PREFIX OR DEFINED ENV{EMSDK})
add_subdirectory(external/boost)
endif()

if(DEFINED WASI_SDK_PREFIX)
set(EDEN_ATOMIC_ASSETS_ACCOUNT atomicassets CACHE STRING "The account holding the atomicassets contract")
set(EDEN_ATOMIC_MARKET_ACCOUNT atomicmarket CACHE STRING "The account holding the atomicmarket contract")
Expand All @@ -43,13 +47,34 @@ if(DEFINED WASI_SDK_PREFIX)
-DFORCE_COLORED_OUTPUT=${FORCE_COLORED_OUTPUT}
-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
-DBOOST_EXTRACTED=${BOOST_EXTRACTED}
)
file(APPEND ${CMAKE_BINARY_DIR}/CTestTestfile.cmake "subdirs(\"wasm\")\n")
ExternalProject_Add_StepTargets(wasm test)
else()
message(WARNING "WASI_SDK_PREFIX isn't defined; skipping wasm")
endif()

if(DEFINED ENV{EMSDK})
ExternalProject_Add(emscripten
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/emscripten
BINARY_DIR emscripten
CMAKE_COMMAND emcmake
INSTALL_COMMAND ""
BUILD_ALWAYS 1
TEST_EXCLUDE_FROM_MAIN 1
CMAKE_ARGS
cmake
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_FIND_ROOT_PATH=${CMAKE_CURRENT_BINARY_DIR}/emscripten/deps
-DBOOST_EXTRACTED=${BOOST_EXTRACTED}
-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
)
file(APPEND ${CMAKE_BINARY_DIR}/CTestTestfile.cmake "subdirs(\"emscripten\")\n")
ExternalProject_Add_StepTargets(emscripten test)
endif()

########## TS PACKAGES ####################
if(NOT DEFINED SKIP_TS OR NOT SKIP_TS)
add_custom_target(yarn ALL
Expand Down
174 changes: 174 additions & 0 deletions emscripten/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
cmake_minimum_required(VERSION 3.16.3)
cmake_policy(VERSION 3.16)
project(emscripten)

set(IS_EMSCRIPTEN YES)
get_filename_component(ROOT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/.. ABSOLUTE)
get_filename_component(ROOT_BINARY_DIR ${CMAKE_BINARY_DIR}/.. ABSOLUTE)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output" TRUE)
if(${FORCE_COLORED_OUTPUT})
add_compile_options(-fcolor-diagnostics)
endif()

set(DEP_PREFIX ${CMAKE_BINARY_DIR}/deps)

set(DEP_URL https://github.com/eoscommunity/Eden/releases/download/deps)
set(GMP_ARCHIVE gmp-6.2.1.tar.zst)
set(GMP_EXTRACTED gmp-6.2.1)
set(OPENSSL_ARCHIVE openssl-1.1.1k.tar.gz)
set(OPENSSL_EXTRACTED openssl-1.1.1k)
set(ZLIB_ARCHIVE zlib-1.2.11.tar.gz)
set(ZLIB_EXTRACTED zlib-1.2.11)

function(download url archive)
if(NOT EXISTS ${archive})
message("Downloading ${url}")
file(DOWNLOAD ${url} ${archive}
STATUS download_status
TIMEOUT 60
SHOW_PROGRESS
TLS_VERIFY ON)
list(POP_BACK download_status BOOST_DOWNLOAD_STATUS_MSG)
if(NOT download_status EQUAL 0)
file(REMOVE ${archive})
message(FATAL_ERROR "Download ${url} failed. ${BOOST_DOWNLOAD_STATUS_MSG}")
endif()
endif()
endfunction()

# Dependancy builds are done at generation time so cmake's find_* commands can find them

if(NOT EXISTS ${DEP_PREFIX}/lib/libz.a)
download(${DEP_URL}/${ZLIB_ARCHIVE} ${DEP_PREFIX}/${ZLIB_ARCHIVE})
message("building zlib")
execute_process(
WORKING_DIRECTORY ${DEP_PREFIX}
COMMAND bash -c "\
rm -rf ${ZLIB_EXTRACTED} \
&& tar xf ${ZLIB_ARCHIVE} \
&& cd ${ZLIB_EXTRACTED} \
&& emconfigure ./configure --prefix=${DEP_PREFIX} --static \
&& emmake make -j \
&& emmake make -j install \
"
)
endif()

if(NOT EXISTS ${DEP_PREFIX}/lib/libgmp.so)
download(${DEP_URL}/${GMP_ARCHIVE} ${DEP_PREFIX}/${GMP_ARCHIVE})
message("building gmp")
execute_process(
WORKING_DIRECTORY ${DEP_PREFIX}
COMMAND bash -c "\
rm -rf ${GMP_EXTRACTED} \
&& tar xf ${GMP_ARCHIVE} \
&& cd ${GMP_EXTRACTED} \
&& emconfigure ./configure --prefix=${DEP_PREFIX} --host none --disable-assembly \
&& emmake make -j \
&& emmake make -j install \
&& ln -sf libgmp.a ../lib/libgmp.so \
"
)
endif()

if(NOT EXISTS ${DEP_PREFIX}/lib/libssl.a)
download(${DEP_URL}/${OPENSSL_ARCHIVE} ${DEP_PREFIX}/${OPENSSL_ARCHIVE})
message("building openssl")
execute_process(
WORKING_DIRECTORY ${DEP_PREFIX}
COMMAND bash -c "\
rm -rf ${OPENSSL_EXTRACTED} \
&& tar xf ${OPENSSL_ARCHIVE} \
&& cd ${OPENSSL_EXTRACTED} \
&& emconfigure bash -c \
\"CROSS_COMPILE= ./config --prefix=${DEP_PREFIX} no-asm no-afalgeng no-shared\" \
&& make -j build_libs \
&& make install_dev \
"
)
endif()

if(NOT EXISTS ${DEP_PREFIX}/include/boost)
message("building boost")
execute_process(
WORKING_DIRECTORY ${BOOST_EXTRACTED}
COMMAND bash -c "\
ZLIB_LIBRARY_PATH=${DEP_PREFIX}/lib \
ZLIB_INCLUDE=${DEP_PREFIX}/include \
./b2 \
--prefix=${DEP_PREFIX} \
--build-dir=${DEP_PREFIX}/build-boost \
toolset=emscripten \
variant=release \
runtime-link=static \
link=static \
--with-chrono \
--with-date_time \
--with-filesystem \
--with-iostreams \
--with-program_options \
--with-system \
--with-test \
install \
"
)
endif()

set(Boost_USE_STATIC_RUNTIME ON)
set(ECC_IMPL secp256k1)
set(SKIP_FC_TESTS YES)
option(ENABLE_TOOLS "enable building of tools" OFF)
set(EOSIO_ROOT_KEY "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV")

add_compile_options(-fexceptions -pthread)
add_link_options(-fexceptions -pthread)

add_subdirectory(../external/eos/libraries/builtins builtins)
add_subdirectory(../external/eos/libraries/chain chain)
add_subdirectory(../external/eos/libraries/chainbase chainbase)
add_subdirectory(../external/eos/libraries/eos-vm eos-vm)
add_subdirectory(../external/eos/libraries/fc fc)
add_subdirectory(../external/eos/libraries/version version)
add_subdirectory(../external/eos/libraries/wasm-jit wasm-jit)
add_subdirectory(../libraries/cltestlib cltestlib)

add_library(chain INTERFACE)
target_link_libraries(chain INTERFACE eosio_chain)

add_executable(cltester test.cpp)
target_link_options(cltester PRIVATE -L${DEP_PREFIX}/lib)
target_link_libraries(cltester cltestlib)
target_link_options(cltester PRIVATE
-gsource-map
--source-map-base ./
-flto
--embind
-sMODULARIZE=1
-sEXPORT_NAME="createCLTester"
-sDISABLE_EXCEPTION_CATCHING=0
-sEXIT_RUNTIME=0
-sALLOW_MEMORY_GROWTH=1
-sDEMANGLE_SUPPORT=1
-sSTACK_OVERFLOW_CHECK=0
#-sASSERTIONS=1
-Wno-pthreads-mem-growth
)
add_custom_command(TARGET cltester
POST_BUILD
COMMAND mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/node_modules
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/node_modules .
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/package.json .
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/src .
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/webpack.config.js .
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/yarn.lock .
COMMAND ln -sf ${CMAKE_CURRENT_SOURCE_DIR}/test.html .
COMMAND yarn
COMMAND npx webpack
)

configure_file(test.html ${CMAKE_CURRENT_BINARY_DIR}/test.html COPYONLY)
15 changes: 15 additions & 0 deletions emscripten/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "cltester-js",
"version": "0.0.1",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"buffer": "^6.0.3",
"copy-webpack-plugin": "^9.0.0",
"eosjs": "^22.0.0",
"webpack": "5",
"webpack-cli": "^4.7.2",
"xterm": "^4.13.0",
"xterm-addon-fit": "^0.5.0"
}
}
67 changes: 67 additions & 0 deletions emscripten/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Api, RpcError } from 'eosjs';
import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';

const term = new Terminal();
const fitAddon = new FitAddon();
term.loadAddon(fitAddon);
term.open(document.getElementById('terminal'));
fitAddon.fit();
window.addEventListener('resize', () => fitAddon.fit());
function writeTerm(s) {
term.write(s.replace(/\r\n?|\n/g, '\r\n'));
}

let testerOptions = {
printErr(s) { writeTerm(s + '\n'); }
};

const defaultPubKey = "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV";
const defaultPrivKey = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3";
const defaultSignatureProvider = new JsSignatureProvider([defaultPrivKey]);


function simpleAuth(key = defaultPubKey) {
return {
threshold: 1,
keys: [{ key, weight: 1 }],
accounts: [],
waits: []
};
}

function createAccount(name, owner = simpleAuth(), active = simpleAuth()) {
return {
account: 'eosio',
name: 'newaccount',
authorization: [{ actor: 'eosio', permission: 'active' }],
data: { creator: 'eosio', name, owner, active },
};
}

(async () => {
try {
await (async () => {
let t = await createCLTester(testerOptions);
let c = await t.createChain();
await c.finishBlock();
writeTerm(JSON.stringify(await c.get_info(), null, 4) + '\n');
writeTerm(JSON.stringify(await c.get_block(1), null, 4) + '\n');
writeTerm(JSON.stringify(await c.get_block(2), null, 4) + '\n');
let api = new Api({ rpc: c, signatureProvider: defaultSignatureProvider });
writeTerm(JSON.stringify(await api.transact({
actions: [
createAccount('alice'),
createAccount('bob'),
createAccount('sue'),
]
}, { useLastIrreversible: true, expireSeconds: 1 }), null, 4) + '\n');
await c.finishBlock();
// while (true)
// await c.finishBlock();
})();
} catch (e) {
writeTerm(e + '\n');
}
})();
1 change: 1 addition & 0 deletions emscripten/test-pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Module['printErr'] = Module['print']
Loading