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

Version 0.4: Better display of multiple contract calls. Many enhancements and corrections. #1980

Merged
merged 17 commits into from
Jun 10, 2024
Merged
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
5 changes: 5 additions & 0 deletions bvm/ManagerStd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ namespace bvm2 {
m_Pending.m_pBlocker = nullptr;
}

void ManagerStd::Reset()
{
OnReset();
}

void ManagerStd::StartRun(uint32_t iMethod)
{
OnReset();
Expand Down
2 changes: 1 addition & 1 deletion bvm/ManagerStd.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ namespace beam::bvm2 {
void Comm_OnNewMsg(const Blob&, Comm::Channel&);
void Comm_OnNewMsg();

protected:
void SelectContext(bool bDependent, uint32_t nChargeNeeded) override;
bool get_HdrAt(Block::SystemState::Full&) override;
void VarsEnum(const Blob& kMin, const Blob& kMax, IReadVars::Ptr&) override;
Expand Down Expand Up @@ -92,6 +91,7 @@ namespace beam::bvm2 {
// results
std::ostringstream m_Out;

void Reset();
void StartRun(uint32_t iMethod);
};
} // namespace
Expand Down
113 changes: 80 additions & 33 deletions bvm/bvm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ namespace bvm2 {

uint32_t ProcessorContract::get_WasmVersion()
{
return IsPastFork(6) ? 1 : 0;
return IsPastFork_<6>() ? 1 : 0;
}

void Processor::Compiler::Compile(ByteBuffer& res, const Blob& src, Kind kind, Wasm::Compiler::DebugInfo* pDbgInfo /* = nullptr */)
Expand Down Expand Up @@ -1342,11 +1342,22 @@ namespace bvm2 {
}
BVM_METHOD_HOST(LoadVar)
{
Blob key(pKey, nKey);
VarKey vk;
SetVarKeyFromShader(vk, nType, Blob(pKey, nKey), false);

if (Kind::Contract == get_Kind())
{
((ProcessorPlusEnv_Contract*) this)->SetVarKeyFromShader(vk, nType, key, false);
key = vk.ToBlob();
}
else
{
// impose the same restriction for apps
Exc::Test(key.n <= Limits::VarKeySize);
}

Blob res;
LoadVar(vk.ToBlob(), res);
LoadVar(key, res);

memcpy(pVal, res.p, std::min(nVal, res.n));
return res.n;
Expand All @@ -1370,38 +1381,62 @@ namespace bvm2 {
}
BVM_METHOD_HOST(LoadVarEx)
{
Exc::Test(IsPastHF4());
Blob key(pKey, nKey);
VarKey vk;

std::setmin(nKeyBufSize, Limits::VarKeySize);
Exc::Test(nKey <= nKeyBufSize);
bool bContract = (Kind::Contract == get_Kind());
if (bContract)
{
Exc::Test(IsPastFork_<4>());

VarKey vk;
SetVarKeyFromShader(vk, nType, Blob(pKey, nKey), false);
std::setmin(nKeyBufSize, Limits::VarKeySize);
Exc::Test(nKey <= nKeyBufSize);

((ProcessorPlusEnv_Contract*) this)->SetVarKeyFromShader(vk, nType, key, false);
key = vk.ToBlob();
}
else
{
// impose the same restriction for apps
Exc::Test(key.n <= Limits::VarKeySize);
}

Blob key = vk.ToBlob(), res;
Blob res;
LoadVarEx(key, res,
!!(Shaders::KeySearchFlags::Exact & nSearchFlag),
!!(Shaders::KeySearchFlags::Bigger & nSearchFlag));

if (key.n > ContractID::nBytes)
if (bContract)
{
// make sure we're still in the context of the contract vars
auto pK = (const uint8_t*) key.p;
const auto& cid = m_FarCalls.m_Stack.back().m_Cid;

if (!memcmp(pK, cid.m_pData, cid.nBytes) && (nType == pK[cid.nBytes]))
// make sure it's still the current contract+type
bool bMatch = (key.n > ContractID::nBytes);
if (bMatch)
{
nKey = key.n - (ContractID::nBytes + 1);
memcpy(pKey, pK + ContractID::nBytes + 1, std::min(nKey, nKeyBufSize));
auto pK = (const uint8_t*)key.p;
const auto& cid = ((ProcessorPlusEnv_Contract*) this)->m_FarCalls.m_Stack.back().m_Cid;

memcpy(pVal, res.p, std::min(nVal, res.n));
nVal = res.n;
return;
if (!memcmp(pK, cid.m_pData, cid.nBytes) && (nType == pK[cid.nBytes]))
{
// ok
key.n -= (ContractID::nBytes + 1);
((const uint8_t*&) key.p) += (ContractID::nBytes + 1);
}
else
bMatch = false;
}

if (!bMatch)
{
key.n = 0;
res.n = 0;
}
}

nKey = 0;
nVal = 0;
nKey = key.n;
memcpy(pKey, key.p, std::min(nKey, nKeyBufSize));

memcpy(pVal, res.p, std::min(nVal, res.n));
nVal = res.n;
}

BVM_METHOD(SaveVar)
Expand All @@ -1411,12 +1446,24 @@ namespace bvm2 {
}
BVM_METHOD_HOST(SaveVar)
{
TestVarSize(nVal);

Blob key(pKey, nKey);
VarKey vk;
SetVarKeyFromShader(vk, nType, Blob(pKey, nKey), true);

return SaveVarInternal(vk.ToBlob(), Blob(pVal, nVal));
if (Kind::Contract == get_Kind())
{
((ProcessorPlusEnv_Contract*) this)->TestVarSize(nVal);
((ProcessorPlusEnv_Contract*) this)->TestCanWrite();

((ProcessorPlusEnv_Contract*) this)->SetVarKeyFromShader(vk, nType, key, true);
key = vk.ToBlob();
}
else
{
Exc::Test(key.n <= Limits::VarKeySize);
Exc::Test(nVal <= Limits::VarSize_4);
}

return SaveVar(key, Blob(pVal, nVal));
}

BVM_METHOD(EmitLog)
Expand Down Expand Up @@ -1445,7 +1492,7 @@ namespace bvm2 {
auto nCalleeStackMax = m_Stack.m_BytesCurrent;
uint8_t* pArgsPtr = nullptr;

bool bPastHF6 = IsPastFork(6);
bool bPastHF6 = IsPastFork_<6>();
if (!bPastHF6)
{
nFlags &= 0xff; // before HF6 it was uint8_t
Expand Down Expand Up @@ -1559,7 +1606,7 @@ namespace bvm2 {

BVM_METHOD_HOST(UpdateShader)
{
Exc::Test(IsPastHF4());
Exc::Test(IsPastFork_<4>());
TestCanWrite();

TestVarSize(nVal);
Expand Down Expand Up @@ -1793,7 +1840,7 @@ namespace bvm2 {
BVM_METHOD(get_ForkHeight)
{
if (Kind::Contract == get_Kind())
Exc::Test(IsPastFork(5));
Exc::Test(IsPastFork_<5>());

const Rules& r = Rules::get();
return (iFork >= _countof(r.pForks)) ? MaxHeight : r.pForks[iFork].m_Height;
Expand All @@ -1805,7 +1852,7 @@ namespace bvm2 {
{
if (Kind::Contract == get_Kind())
{
Exc::Test(IsPastFork(6));
Exc::Test(IsPastFork_<6>());
DischargeUnits(Limits::Cost::LoadVar);
}

Expand Down Expand Up @@ -2056,7 +2103,7 @@ namespace bvm2 {
BVM_METHOD(HashClone)
{
if (Kind::Contract == get_Kind())
Exc::Test(IsPastHF4());
Exc::Test(IsPastFork_<4>());

return CloneHash(m_DataProcessor.FindStrict(pHash));
}
Expand Down Expand Up @@ -2235,7 +2282,7 @@ namespace bvm2 {
BVM_METHOD(Secp_Point_ExportEx)
{
if (Kind::Contract == get_Kind())
Exc::Test(IsPastHF4());
Exc::Test(IsPastFork_<4>());

DischargeUnits(Limits::Cost::Secp_Point_Export);
m_Secp.m_Point.FindStrict(p).m_Val.Export(get_AddrAsW<ECC::Point::Storage>(res));
Expand Down Expand Up @@ -3876,7 +3923,7 @@ namespace bvm2 {
// Shader aux
void ProcessorContract::AddRemoveShader(const ContractID& cid, const Blob* pCode)
{
AddRemoveShader(cid, pCode, IsPastHF4());
AddRemoveShader(cid, pCode, IsPastFork_<4>());
}

void ProcessorContract::AddRemoveShader(const ContractID& cid, const Blob* pCode, bool bFireEvent)
Expand Down
25 changes: 12 additions & 13 deletions bvm/bvm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,19 @@ namespace bvm2 {

const HeightPos* FromWasmOpt(Wasm::Word pPos, HeightPos& buf);

bool IsPastFork(uint32_t iFork)
template <uint32_t iFork>
bool IsPastFork_()
{
assert(iFork < _countof(Rules::get().pForks));
return get_Height() + 1 >= Rules::get().pForks[iFork].m_Height;
// current height does not include the current being-interpreted block
return Rules::get().IsPastFork_<iFork>(get_Height() + 1);
}

bool IsPastHF4() {
// current height does not include the current being-interpreted block
return IsPastFork(4);
virtual void LoadVar(const Blob&, Blob& res) { res.n = 0; } // res is temporary
virtual void LoadVarEx(Blob& key, Blob& res, bool bExact, bool bBigger) {
key.n = 0;
res.n = 0;
}
virtual uint32_t SaveVar(const Blob&, const Blob& val) { return 0; }

public:

Expand Down Expand Up @@ -334,6 +337,8 @@ namespace bvm2 {
{
protected:

friend struct ProcessorPlusEnv;

void SetVarKey(VarKey&);
void SetVarKey(VarKey&, uint8_t nTag, const Blob&);
void SetVarKeyFromShader(VarKey&, uint8_t nTag, const Blob&, bool bW);
Expand Down Expand Up @@ -400,14 +405,8 @@ namespace bvm2 {
virtual void DischargeUnits(uint32_t size) override;
virtual uint32_t get_WasmVersion() override;

virtual void LoadVar(const Blob&, Blob& res) { res.n = 0; } // res is temporary
virtual uint32_t SaveVar(const Blob&, const Blob& val) { return 0; }
virtual uint32_t OnLog(const Blob&, const Blob& val) { return 0; }

virtual void LoadVarEx(Blob& key, Blob& res, bool bExact, bool bBigger) {
key.n = 0;
res.n = 0;
}

virtual Asset::ID AssetCreate(const Asset::Metadata&, const PeerID&, Amount& valDeposit) { return 0; }
virtual bool AssetEmit(Asset::ID, const PeerID&, AmountSigned) { return false; }
Expand All @@ -433,7 +432,7 @@ namespace bvm2 {

void TestVarSize(uint32_t n)
{
uint32_t nMax = IsPastHF4() ? Limits::VarSize_4 : Limits::VarSize_0;
uint32_t nMax = IsPastFork_<4>() ? Limits::VarSize_4 : Limits::VarSize_0;
Exc::Test(n <= nMax);
}

Expand Down
6 changes: 3 additions & 3 deletions bvm/bvm2_opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@
macro(0x19, void , StackFree) \
macro(0x1A, void* , Heap_Alloc) \
macro(0x1B, void , Heap_Free) \
macro(0x20, uint32_t , LoadVar) \
macro(0x21, uint32_t , SaveVar) \
macro(0x27, void , LoadVarEx) \
macro(0x28, void , Halt) \
macro(0x2A, uint32_t , get_AssetInfo) \
macro(0x2B, void , HashWrite) \
Expand Down Expand Up @@ -550,14 +553,11 @@
macro(0xB0, uint8_t , VerifyBeamHashIII) \

#define BVMOpsAll_Contract(macro) \
macro(0x20, uint32_t , LoadVar) \
macro(0x21, uint32_t , SaveVar) \
macro(0x22, uint32_t , EmitLog) \
macro(0x23, void , CallFar) \
macro(0x24, uint32_t , get_CallDepth) \
macro(0x25, void , get_CallerCid) \
macro(0x26, void , UpdateShader) \
macro(0x27, void , LoadVarEx) \
macro(0x29, void , AddSig) \
macro(0x30, void , FundsLock) \
macro(0x31, void , FundsUnlock) \
Expand Down
23 changes: 2 additions & 21 deletions bvm/ethash_service/shaders_ethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,9 @@

#pragma once

#include "../bvm2.h"

namespace Shaders
{
using PubKey = ECC::Point;
using Secp_point_data = ECC::Point;
using Secp_point_dataEx = ECC::Point::Storage;
using Secp_scalar_data = ECC::Scalar;
using AssetID = beam::Asset::ID;
using ContractID = ECC::uintBig;
using HashValue = ECC::uintBig;
using beam::Amount;
using beam::Height;
using beam::Timestamp;
using beam::HeightPos;

template<bool bToShader, typename T>
inline void ConvertOrd(T& x)
{
if constexpr (bToShader)
x = beam::ByteOrder::to_le(x);
else
x = beam::ByteOrder::from_le(x);
}

#include "bvm/Shaders/Ethash.h"
}
2 changes: 1 addition & 1 deletion bvm/wasm_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ namespace Wasm {
#endif // WASM_INTERPRETER_DEBUG

Processor()
:m_Instruction(Reader::Mode::Emulate_x86)
:m_Instruction(Reader::Mode::Standard)
{
}

Expand Down
Loading