Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into update-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Mart committed Jul 24, 2023
2 parents 3052048 + 0d9c79b commit 9ae10a0
Show file tree
Hide file tree
Showing 66 changed files with 1,783 additions and 699 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ubuntu-2004.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ jobs:
steps:
- name: Timestamp
id: ccache_cache_timestamp
shell: cmake -P {0}
run: |
string(TIMESTAMP current_date "%Y-%m-%d-%H-%M-%S" UTC)
message("::set-output name=timestamp::${current_date}")
run: echo timestamp=`date -u +"%Y-%m-%d-%H-%M-%S"` >>$GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@v3
with:
submodules: false
fetch-depth: 0
- name: Preserve ccache
uses: actions/cache@v1.1.0
uses: actions/cache@v3
with:
path: .caches
key: $ubuntu-20.04-caches-${{ steps.ccache_cache_timestamp.outputs.timestamp }}
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/ubuntu-2204.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ jobs:
steps:
- name: Timestamp
id: ccache_cache_timestamp
shell: cmake -P {0}
run: |
string(TIMESTAMP current_date "%Y-%m-%d-%H-%M-%S" UTC)
message("::set-output name=timestamp::${current_date}")
run: echo timestamp=`date -u +"%Y-%m-%d-%H-%M-%S"` >>$GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@v3
with:
submodules: false
fetch-depth: 0
- name: Preserve ccache
uses: actions/cache@v1.1.0
uses: actions/cache@v3
with:
path: .caches
key: $ubuntu-22.04-caches-${{ steps.ccache_cache_timestamp.outputs.timestamp }}
Expand Down
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ if(DEFINED WASI_SDK_PREFIX)
install(DIRECTORY ${WASI_SDK_PREFIX}/ USE_SOURCE_PERMISSIONS DESTINATION . COMPONENT WASI EXCLUDE_FROM_ALL)
elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
ExternalProject_Add(wasi-sdk
URL https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-19/wasi-sdk-19.0-linux.tar.gz
URL_HASH SHA256=d900abc826eec1955b9afd250e7cc2496338abbf6c440d86a313c06e42083fa1
URL https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz
URL_HASH SHA256=7030139d495a19fbeccb9449150c2b1531e15d8fb74419872a719a7580aad0f9
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
Expand Down Expand Up @@ -233,6 +233,7 @@ if(BUILD_RUST)
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/AuthInviteSys.wasm ${BOOT_IMAGE}
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/CommonSys.wasm ${BOOT_IMAGE}
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/CoreFractalSys.wasm ${BOOT_IMAGE}
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/CpuSys.wasm ${BOOT_IMAGE}
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/ExploreSys.wasm ${BOOT_IMAGE}
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/FractalSys.wasm ${BOOT_IMAGE}
COMMAND cp -a ${CMAKE_CURRENT_BINARY_DIR}/InviteSys.wasm ${BOOT_IMAGE}
Expand Down
1 change: 1 addition & 0 deletions doc/psidk/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Web Services](services/cpp-service/reference/web-services.md)
- [Magic Numbers](services/cpp-service/reference/magic-numbers.md)
- [Native Functions](services/cpp-service/reference/native-functions.md)
- [System Functions](services/cpp-service/reference/system.md)
- [CMake Support](services/cpp-service/reference/cmake.md)
- [Rust Services](services/rust-service.md)
- [Basic Service](services/rust-service/basic.md)
Expand Down
1 change: 1 addition & 0 deletions doc/psidk/src/services/cpp-service/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
- [Web Services](reference/web-services.md)
- [Magic Numbers](reference/magic-numbers.md)
- [Native Functions](reference/native-functions.md)
- [System Functions](reference/system.md)
- [CMake Support](reference/cmake.md)
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ This is the set of raw native functions (wasm imports). They are available for s

- [psibase::raw::abortMessage]
- [psibase::raw::call]
- [psibase::raw::clockTimeGet]
- [psibase::raw::getCurrentAction]
- [psibase::raw::getKey]
- [psibase::raw::getResult]
Expand All @@ -100,6 +101,7 @@ This is the set of raw native functions (wasm imports). They are available for s

{{#cpp-doc ::psibase::raw::abortMessage}}
{{#cpp-doc ::psibase::raw::call}}
{{#cpp-doc ::psibase::raw::clockTimeGet}}
{{#cpp-doc ::psibase::raw::getCurrentAction}}
{{#cpp-doc ::psibase::raw::getKey}}
{{#cpp-doc ::psibase::raw::getResult}}
Expand Down
32 changes: 32 additions & 0 deletions doc/psidk/src/services/cpp-service/reference/system.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# System Functions

Some parts of the C++ standard library that interact with the host system are restricted or unavailable in services.

## I/O

Only stdout and stderr are available. They both write to a single merged stream which is in included the transaction trace. stdin is not supported.

Supported APIs:
- `write`
- `printf` family
- `std::cout`, `std::cerr`, `std::clog`

## Clocks

UTC, monotonic, and CPU clocks are available to subjective services. The monotonic clock is only guaranteed to be monotonic within a block. The CPU clock measures CPU time spent on the current transaction.

Supported APIs:
- `clock_gettime` with `CLOCK_REALITME`, `CLOCK_MONOTONIC`, or `CLOCK_PROCESS_CPUTIME_ID`
- `clock`
- `time`
- `std::chrono::system_clock`
- `std::chrono::monotonic_clock`
- `std::chrono::high_resolution_clock`

## Unavailable Functionality

- Filesystem
- Nondeterministic random numbers
- Concurrency
- Exceptions
- `setjmp`/`longjmp`
8 changes: 8 additions & 0 deletions libraries/net/test/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <services/system/AccountSys.hpp>
#include <services/system/AuthAnySys.hpp>
#include <services/system/CpuSys.hpp>
#include <services/system/ProducerSys.hpp>
#include <services/system/TransactionSys.hpp>
#include <services/system/VerifyEcSys.hpp>
Expand Down Expand Up @@ -80,6 +81,11 @@ void boot(BlockContext* ctx, const Consensus& producers, bool ec)
.flags = TransactionSys::serviceFlags,
.code = readWholeFile("TransactionSys.wasm"),
},
{
.service = CpuSys::service,
.flags = CpuSys::serviceFlags,
.code = readWholeFile("CpuSys.wasm"),
},
{
.service = AccountSys::service,
.flags = 0,
Expand Down Expand Up @@ -219,6 +225,8 @@ BlockMessage makeBlock(const BlockInfo& info,
}
}
trx.subjectiveData.emplace();
trx.subjectiveData->emplace_back();
trx.subjectiveData->push_back(psio::to_frac(std::chrono::nanoseconds(100000)));
m.push(TransactionInfo{trx});
}
newBlock.block.transactions = std::move(trxs);
Expand Down
8 changes: 7 additions & 1 deletion libraries/psibase/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function(add suffix)

if(DEFINED IS_WASM)
target_link_libraries(psibase${suffix} PUBLIC wasm-base${suffix})
target_mapped_include_directories(psibase${suffix} PUBLIC common/include)
target_mapped_include_directories(psibase${suffix} PUBLIC common/include wasm32-wasi)

add_library(simple-malloc-objects${suffix} OBJECT service/src/simple_malloc.cpp)
target_link_libraries(simple-malloc-objects${suffix} PUBLIC wasm-base${suffix})
Expand Down Expand Up @@ -42,6 +42,7 @@ function(add suffix)
target_link_libraries(psibase-service-wasi-polyfill${suffix} PUBLIC wasm-base${suffix})
target_sources(psibase-service-wasi-polyfill${suffix} PRIVATE
service/src/ossl.c
service/src/time.c
service/src/wasi-polyfill/__wasi_clock_time_get.cpp
service/src/wasi-polyfill/__wasi_environ_get.cpp
service/src/wasi-polyfill/__wasi_environ_sizes_get.cpp
Expand Down Expand Up @@ -109,6 +110,7 @@ function(add suffix)
tester/src/testUtils.cpp
service/src/intrinsic.cpp
service/src/ossl.c
service/src/time.c
tester/src/tester_intrinsics.cpp
tester/src/wasi_polyfill/__wasi_args_get.cpp
tester/src/wasi_polyfill/__wasi_args_sizes_get.cpp
Expand Down Expand Up @@ -150,6 +152,7 @@ function(add suffix)
../../services/system/ProducerSys/include
../../services/system/AuthEcSys/include
../../services/system/AuthAnySys/include
../../services/system/CpuSys/include
../../services/system/ProxySys/include
../../services/system/SetCodeSys/include
../../services/system/TransactionSys/include
Expand Down Expand Up @@ -191,8 +194,10 @@ function(add suffix)
native/src/TransactionContext.cpp
native/src/useTriedent.cpp
native/src/VerifyProver.cpp
native/src/Watchdog.cpp
)

add_subdirectory(native/tests)
add_subdirectory(common/tests)
endif()
endfunction()
Expand All @@ -205,6 +210,7 @@ endif()
if(IS_WASM)
install(DIRECTORY common/include/psibase TYPE INCLUDE COMPONENT libpsibase FILES_MATCHING PATTERN "*.hpp")
install(DIRECTORY tester/include/psibase TYPE INCLUDE COMPONENT libpsibase FILES_MATCHING PATTERN "*.hpp")
install(DIRECTORY wasm32-wasi TYPE INCLUDE COMPONENT libpsibase FILES_MATCHING PATTERN "*.h")
install(PROGRAMS sdk/psidk-cmake-args TYPE BIN COMPONENT libpsibase)
install(FILES sdk/psibase-config.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/psidk COMPONENT libpsibase)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libc++abi-replacements.a ${CMAKE_CURRENT_BINARY_DIR}/libc++abi-replacements-debug.a TYPE LIB COMPONENT libpsibase)
Expand Down
21 changes: 21 additions & 0 deletions libraries/psibase/common/include/psibase/nativeFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ namespace psibase
/// Otherwise returns `-1` and clears result. Use [getResult] to get result
/// and [getKey] to get found key.
PSIBASE_NATIVE(kvMax) uint32_t kvMax(DbId db, const char* key, uint32_t keyLen);

/// Gets the current value of a clock in nanoseconds.
///
/// This function is non-deterministic and is only available in subjective services.
///
/// The following clocks are supported
/// - `__WASI_CLOCKID_REALTIME` returns wall-clock time since the unix epoch.
/// - `__WASI_CLOCKID_MONOTONIC` returns monotonic time since an unspecified epoch.
/// All uses of CLOCK_MONOTONIC within the same block use the same epoch.
/// - `__WASI_CLOCKID_PROCESS_CPUTIME_ID` measures CPU time spent executing the
/// current transaction.
///
/// Returns 0 on success or an error code on failure.
///
/// Errors:
/// - `EINVAL`: the clock id is not supported
int32_t clockTimeGet(uint32_t id, uint64_t* time);

/// Sets the transaction timer to expire a given number of nanoseconds
/// after the beginning of the current transaction.
PSIBASE_NATIVE(setMaxTransactionTime) void setMaxTransactionTime(uint64_t ns);
} // namespace raw

/// Get result
Expand Down
3 changes: 2 additions & 1 deletion libraries/psibase/native/include/psibase/NativeFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <psibase/ExecutionContext.hpp>

#include <eosio/vm/argument_proxy.hpp>
#include <eosio/vm/span.hpp>
#include <psibase/nativeTables.hpp>

Expand Down Expand Up @@ -37,7 +38,7 @@ namespace psibase
uint32_t getKey(eosio::vm::span<char> dest);
void writeConsole(eosio::vm::span<const char> str);
void abortMessage(eosio::vm::span<const char> str);
uint64_t getBillableTime();
int32_t clockTimeGet(uint32_t id, eosio::vm::argument_proxy<uint64_t*> time);
void setMaxTransactionTime(uint64_t nanoseconds);
uint32_t getCurrentAction();
uint32_t call(eosio::vm::span<const char> data);
Expand Down
9 changes: 6 additions & 3 deletions libraries/psibase/native/include/psibase/SystemContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

namespace psibase
{
struct WatchdogManager;

struct SystemContext
{
SharedDatabase sharedDatabase;
WasmCache wasmCache;
std::vector<ExecutionMemory> executionMemories;
SharedDatabase sharedDatabase;
WasmCache wasmCache;
std::vector<ExecutionMemory> executionMemories;
std::shared_ptr<WatchdogManager> watchdogManager;

void setNumMemories(size_t n)
{
Expand Down
25 changes: 12 additions & 13 deletions libraries/psibase/native/include/psibase/TransactionContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ namespace psibase
// Might be redundant elsewhere?
struct TransactionContext
{
BlockContext& blockContext;
const SignedTransaction& signedTransaction;
TransactionTrace& transactionTrace;
ConfigRow config;
KvResourceMap kvResourceDeltas;
std::uint32_t remainingStack = 0;
const std::chrono::steady_clock::time_point startTime;
std::chrono::steady_clock::duration databaseTime;
bool allowDbRead;
bool allowDbWrite;
bool allowDbReadSubjective;
std::vector<std::vector<char>> subjectiveData;
size_t nextSubjectiveRead = 0;
BlockContext& blockContext;
const SignedTransaction& signedTransaction;
TransactionTrace& transactionTrace;
ConfigRow config;
KvResourceMap kvResourceDeltas;
std::uint32_t remainingStack = 0;
std::chrono::steady_clock::duration databaseTime;
bool allowDbRead;
bool allowDbWrite;
bool allowDbReadSubjective;
std::vector<std::vector<char>> subjectiveData;
size_t nextSubjectiveRead = 0;

TransactionContext(BlockContext& blockContext,
const SignedTransaction& signedTransaction,
Expand Down
68 changes: 68 additions & 0 deletions libraries/psibase/native/include/psibase/Watchdog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once

#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>

#include <time.h>

namespace psibase
{

struct CpuClock
{
using rep = std::int64_t;
using period = std::ratio<1, 1000000000>;
using duration = std::chrono::duration<rep, period>;
using time_point = std::chrono::time_point<CpuClock>;
static constexpr bool is_steady = false;
static time_point now(clockid_t clock = CLOCK_THREAD_CPUTIME_ID);
};

class Watchdog;

class WatchdogManager
{
public:
WatchdogManager();
~WatchdogManager();

private:
friend class Watchdog;
void add(Watchdog* wd);
void remove(Watchdog* wd);
void run();
std::mutex mutex;
std::condition_variable cond;
std::vector<Watchdog*> children;
bool done = false;
std::thread worker;
};

class Watchdog
{
public:
Watchdog(WatchdogManager& m, std::function<void()> f);
~Watchdog();
void setLimit(CpuClock::duration dur);
void pause();
void resume();
CpuClock::duration elapsed();
void interrupt();

private:
friend class WatchdogManager;
// Called with manager mutex held
CpuClock::duration wait_duration();
WatchdogManager* manager;
bool paused;
CpuClock::duration elapsedOrStart;
CpuClock::duration limit = CpuClock::duration::max();
clockid_t cpuclock;
std::function<void()> handler;
};

} // namespace psibase
Loading

0 comments on commit 9ae10a0

Please sign in to comment.