Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/contract/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the LICENSE.txt file in the project root for more information.
#include "../utils/intconv.h"
#include "../utils/uintconv.h"
#include "../utils/strconv.h"
#include "../utils/utils.h" // libs/json.hpp -> string
#include "../utils/utils.h" // FunctionTypes, libs/json.hpp -> string

/// Namespace for Solidity ABI-related operations.
namespace ABI {
Expand Down
9 changes: 4 additions & 5 deletions src/contract/contractmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ void ContractManager::ethCall(const evmc_message& callInfo, ContractHost* host)
PointerNullifier nullifier(this->host_);
const Address caller(callInfo.sender);
const Functor functor = EVMCConv::getFunctor(callInfo);
/// Call the function on this->createContractFuncs_
// Call the function on this->createContractFuncs_
auto it = this->createContractFuncs_.find(functor);
if (it == this->createContractFuncs_.end()) {
throw DynamicException("ContractManager: Invalid function call");
}
it->second(callInfo,
ContractHost::deriveContractAddress(this->host_->getNonce(caller), caller),
this->contracts_,
this->getContractChainId(),
this->host_);
ContractHost::deriveContractAddress(this->host_->getNonce(caller), caller),
this->contracts_, this->getContractChainId(), this->host_
);
}

Bytes ContractManager::ethCallView(const evmc_message& callInfo, ContractHost* host) const {
Expand Down
20 changes: 10 additions & 10 deletions src/contract/contractmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class ContractManager : public BaseContract {

/// Functions to create contracts.
boost::unordered_flat_map<
Functor,
std::function<
void(const evmc_message&,
const Address&,
boost::unordered_flat_map<Address, std::unique_ptr<BaseContract>, SafeHash>& contracts_,
const uint64_t&,
ContractHost*
)>,
SafeHash
> createContractFuncs_;
Functor,
std::function<void(
const evmc_message&,
const Address&,
boost::unordered_flat_map<Address, std::unique_ptr<BaseContract>, SafeHash>& contracts_,
const uint64_t&,
ContractHost*
)>,
SafeHash
> createContractFuncs_;

/**
* Get all deployed contracts.
Expand Down
126 changes: 115 additions & 11 deletions src/contract/templates/dexv2/dexv2pair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,38 @@ DEXV2Pair::DEXV2Pair(const Address& address, const DB& db
reserve0_(this), reserve1_(this), blockTimestampLast_(this),
price0CumulativeLast_(this), price1CumulativeLast_(this), kLast_(this)
{
this->factory_ = Address(db.get(std::string("factory_"), this->getDBPrefix()));
this->token0_ = Address(db.get(std::string("token0_"), this->getDBPrefix()));
this->token1_ = Address(db.get(std::string("token1_"), this->getDBPrefix()));
this->reserve0_ = UintConv::bytesToUint112(db.get(std::string("reserve0_"), this->getDBPrefix()));
this->reserve1_ = UintConv::bytesToUint112(db.get(std::string("reserve1_"), this->getDBPrefix()));
this->blockTimestampLast_ = UintConv::bytesToUint32(db.get(std::string("blockTimestampLast_"), this->getDBPrefix()));
this->price0CumulativeLast_ = UintConv::bytesToUint256(db.get(std::string("price0CumulativeLast_"), this->getDBPrefix()));
this->price1CumulativeLast_ = UintConv::bytesToUint256(db.get(std::string("price1CumulativeLast_"), this->getDBPrefix()));
this->kLast_ = UintConv::bytesToUint256(db.get(std::string("kLast_"), this->getDBPrefix()));
Hex prefix = Hex::fromBytes(this->getDBPrefix());
Utils::safePrintTest("Loading contract DEXV2Pair from DB with prefix: " + prefix.get());
auto factoryBytes = db.get(std::string("factory_"), this->getDBPrefix());
auto token0Bytes = db.get(std::string("token0_"), this->getDBPrefix());
auto token1Bytes = db.get(std::string("token1_"), this->getDBPrefix());
auto reserve0Bytes = db.get(std::string("reserve0_"), this->getDBPrefix());
auto reserve1Bytes = db.get(std::string("reserve1_"), this->getDBPrefix());
auto blockTimestampLastBytes = db.get(std::string("blockTimestampLast_"), this->getDBPrefix());
auto price0CumulativeLastBytes = db.get(std::string("price0CumulativeLast_"), this->getDBPrefix());
auto price1CumulativeLastBytes = db.get(std::string("price1CumulativeLast_"), this->getDBPrefix());
auto kLastBytes = db.get(std::string("kLast_"), this->getDBPrefix());

Utils::safePrintTest("Factory Bytes: " + Hex::fromBytes(factoryBytes).get());
Utils::safePrintTest("Token0 Bytes: " + Hex::fromBytes(token0Bytes).get());
Utils::safePrintTest("Token1 Bytes: " + Hex::fromBytes(token1Bytes).get());
Utils::safePrintTest("Reserve0 Bytes: " + Hex::fromBytes(reserve0Bytes).get());
Utils::safePrintTest("Reserve1 Bytes: " + Hex::fromBytes(reserve1Bytes).get());
Utils::safePrintTest("BlockTimestampLast Bytes: " + Hex::fromBytes(blockTimestampLastBytes).get());
Utils::safePrintTest("Price0CumulativeLast Bytes: " + Hex::fromBytes(price0CumulativeLastBytes).get());
Utils::safePrintTest("Price1CumulativeLast Bytes: " + Hex::fromBytes(price1CumulativeLastBytes).get());
Utils::safePrintTest("KLast Bytes: " + Hex::fromBytes(kLastBytes).get());


this->factory_ = Address(factoryBytes);
this->token0_ = Address(token0Bytes);
this->token1_ = Address(token1Bytes);
this->reserve0_ = UintConv::bytesToUint112(reserve0Bytes);
this->reserve1_ = UintConv::bytesToUint112(reserve1Bytes);
this->blockTimestampLast_ = UintConv::bytesToUint32(blockTimestampLastBytes);
this->price0CumulativeLast_ = UintConv::bytesToUint256(price0CumulativeLastBytes);
this->price1CumulativeLast_ = UintConv::bytesToUint256(price1CumulativeLastBytes);
this->kLast_ = UintConv::bytesToUint256(kLastBytes);

this->factory_.commit();
this->token0_.commit();
Expand All @@ -47,6 +70,17 @@ DEXV2Pair::DEXV2Pair(const Address& address, const DB& db
this->price0CumulativeLast_.enableRegister();
this->price1CumulativeLast_.enableRegister();
this->kLast_.enableRegister();
Utils::safePrintTest("Loaded from DB DEXV2Pair contract");
Utils::safePrintTest("Factory: " + this->factory_.get().hex().get());
Utils::safePrintTest("Token0: " + this->token0_.get().hex().get());
Utils::safePrintTest("Token1: " + this->token1_.get().hex().get());
Utils::safePrintTest("Reserve0: " + this->reserve0_.get().str());
Utils::safePrintTest("Reserve1: " + this->reserve1_.get().str());
Utils::safePrintTest("BlockTimestampLast: " + std::to_string(this->blockTimestampLast_.get()));
Utils::safePrintTest("Price0CumulativeLast: " + this->price0CumulativeLast_.get().str());
Utils::safePrintTest("Price1CumulativeLast: " + this->price1CumulativeLast_.get().str());
Utils::safePrintTest("KLast: " + this->kLast_.get().str());

}

DEXV2Pair::DEXV2Pair(
Expand All @@ -55,7 +89,14 @@ DEXV2Pair::DEXV2Pair(
factory_(this), token0_(this), token1_(this), reserve0_(this), reserve1_(this),
blockTimestampLast_(this), price0CumulativeLast_(this), price1CumulativeLast_(this), kLast_(this)
{
// Explicitly initialize numbers to 0 to avoid junk values on DB load
this->factory_ = creator;
this->reserve0_ = 0;
this->reserve1_ = 0;
this->blockTimestampLast_ = 0;
this->price0CumulativeLast_ = 0;
this->price1CumulativeLast_ = 0;
this->kLast_ = 0;

this->factory_.commit();
this->token0_.commit();
Expand All @@ -78,6 +119,16 @@ DEXV2Pair::DEXV2Pair(
this->price0CumulativeLast_.enableRegister();
this->price1CumulativeLast_.enableRegister();
this->kLast_.enableRegister();
Utils::safePrintTest("Creating new DEXV2Pair contract");
Utils::safePrintTest("Factory: " + this->factory_.get().hex().get());
Utils::safePrintTest("Token0: " + this->token0_.get().hex().get());
Utils::safePrintTest("Token1: " + this->token1_.get().hex().get());
Utils::safePrintTest("Reserve0: " + this->reserve0_.get().str());
Utils::safePrintTest("Reserve1: " + this->reserve1_.get().str());
Utils::safePrintTest("BlockTimestampLast: " + std::to_string(this->blockTimestampLast_.get()));
Utils::safePrintTest("Price0CumulativeLast: " + this->price0CumulativeLast_.get().str());
Utils::safePrintTest("Price1CumulativeLast: " + this->price1CumulativeLast_.get().str());
Utils::safePrintTest("KLast: " + this->kLast_.get().str());
}

DEXV2Pair::~DEXV2Pair() {};
Expand Down Expand Up @@ -141,6 +192,8 @@ void DEXV2Pair::initialize(const Address& token0, const Address& token1) {
if (this->factory_ != this->getCaller()) throw DynamicException("DEXV2Pair: FORBIDDEN");
this->token0_ = token0;
this->token1_ = token1;
Utils::safePrintTest("Initialized DEXV2Pair contract with token0: " + token0.hex().get() + " and token1: " + token1.hex().get());
Utils::safePrintTest("Initialized Reserves 0: " + this->reserve0_.get().str() + " and Reserves 1: " + this->reserve1_.get().str());
}

std::pair<uint256_t, uint256_t> DEXV2Pair::getReservess() const {
Expand Down Expand Up @@ -256,16 +309,67 @@ void DEXV2Pair::sync() {


DBBatch DEXV2Pair::dump() const {
// We have to dump the tokens as well
DBBatch dbBatch = BaseContract::dump();
DBBatch erc20Batch = ERC20::dump();
for (const auto& dbItem : erc20Batch.getPuts()) dbBatch.push_back(dbItem);
for (const auto& dbItem : erc20Batch.getDels()) dbBatch.delete_key(dbItem);

Utils::safePrintTest("Dumping DEXV2Pair to DB");
uint112_t zeroUint = 0;
uint112_t oneUint = 1;
auto zeroUintBytes = UintConv::uint112ToBytes(zeroUint);
auto oneUintBytes = UintConv::uint112ToBytes(oneUint);
Utils::safePrintTest("Zero Uint: " + zeroUint.str());
Utils::safePrintTest("One Uint: " + oneUint.str());
Utils::safePrintTest("Zero Uint Bytes: " + Hex::fromBytes(zeroUintBytes).get());
Utils::safePrintTest("One Uint Bytes: " + Hex::fromBytes(oneUintBytes).get());

Bytes zeroUintRawBytes;
zeroUintRawBytes.reserve(14);
Bytes oneUintRawBytes;
oneUintRawBytes.reserve(14);
boost::multiprecision::export_bits(zeroUint, std::back_inserter(zeroUintRawBytes), 8);
boost::multiprecision::export_bits(oneUint, std::back_inserter(oneUintRawBytes), 8);
Utils::safePrintTest("Zero Uint Raw Bytes: " + Hex::fromBytes(zeroUintRawBytes).get());
Utils::safePrintTest("One Uint Raw Bytes: " + Hex::fromBytes(oneUintRawBytes).get());

Utils::safePrintTest("Factory: " + this->factory_.get().hex().get());
Utils::safePrintTest("Token0: " + this->token0_.get().hex().get());
Utils::safePrintTest("Token1: " + this->token1_.get().hex().get());
Utils::safePrintTest("Reserve0 Before: " + this->reserve0_.get().str());
Utils::safePrintTest("Reserve1 Before: " + this->reserve1_.get().str());
auto reserve0Bytes = UintConv::uint112ToBytes(this->reserve0_.get());
auto reserve1Bytes = UintConv::uint112ToBytes(this->reserve1_.get());
Utils::safePrintTest("Reserve0 After: " + this->reserve0_.get().str());
Utils::safePrintTest("Reserve1 After: " + this->reserve1_.get().str());
Utils::safePrintTest("reserve0 Bytes: " + Hex::fromBytes(reserve0Bytes).get());
Utils::safePrintTest("reserve1 Bytes: " + Hex::fromBytes(reserve1Bytes).get());

Utils::safePrintTest("BlockTimestampLast: " + std::to_string(this->blockTimestampLast_.get()));
Utils::safePrintTest("Price0CumulativeLast: " + this->price0CumulativeLast_.get().str());
Utils::safePrintTest("Price1CumulativeLast: " + this->price1CumulativeLast_.get().str());
Utils::safePrintTest("KLast: " + this->kLast_.get().str());

dbBatch.push_back(StrConv::stringToBytes("factory_"), this->factory_.get().view(), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("token0_"), this->token0_.get().view(), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("token1_"), this->token1_.get().view(), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("reserve0_"), UintConv::uint112ToBytes(this->reserve0_.get()), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("reserve1_"), UintConv::uint112ToBytes(this->reserve1_.get()), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("reserve0_"), reserve0Bytes, this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("reserve1_"), reserve1Bytes, this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("blockTimestampLast_"), UintConv::uint32ToBytes(this->blockTimestampLast_.get()), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("price0CumulativeLast_"), UintConv::uint256ToBytes(this->price0CumulativeLast_.get()), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("price1CumulativeLast_"), UintConv::uint256ToBytes(this->price1CumulativeLast_.get()), this->getDBPrefix());
dbBatch.push_back(StrConv::stringToBytes("kLast_"), UintConv::uint256ToBytes(this->kLast_.get()), this->getDBPrefix());
const auto& puts = dbBatch.getPuts();
for (const auto& dbItem : puts) {
// Take of the prefix based on this->getDBPrefix().size() from the dbItem.key
Bytes prefixBytes(dbItem.key.begin(), dbItem.key.begin() + this->getDBPrefix().size());
Hex prefix = Hex::fromBytes(prefixBytes);
// Then take dbItem.key + this->getDBPrefix().size() to the end
std::string key(dbItem.key.begin() + this->getDBPrefix().size(), dbItem.key.end());
Hex value = Hex::fromBytes(dbItem.value);
Utils::safePrintTest("Dumping to DB with prefix : " + prefix.get() + " key: " + key + " value: " + value.get());
}
return dbBatch;
}

2 changes: 1 addition & 1 deletion src/contract/templates/erc721test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ERC721Test::ERC721Test(const Address &address, const DB& db)
ERC721Test::ERC721Test(
const std::string &erc721name, const std::string &erc721symbol, const uint64_t& maxTokens,
const Address &address, const Address &creator, const uint64_t &chainId
) : DynamicContract(erc721name, address, creator, chainId),
) : DynamicContract("ERC721Test", address, creator, chainId),
ERC721(erc721name, erc721symbol, address, creator, chainId),
tokenIdCounter_(this, 0), maxTokens_(this, maxTokens), totalSupply_(this, 0)
{
Expand Down
4 changes: 0 additions & 4 deletions src/contract/templates/erc721uristorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ ERC721URIStorage::ERC721URIStorage(const Address& address, const DB& db)
: DynamicContract(address, db),
ERC721(address, db),
_tokenURIs(this) {

for (const auto& dbEntry : db.getBatch(this->getNewPrefix("tokenURIs_"))) {
this->_tokenURIs[Utils::fromBigEndian<uint256_t>(dbEntry.key)] = StrConv::bytesToString(dbEntry.value);
}
Expand All @@ -36,21 +35,18 @@ ERC721URIStorage::ERC721URIStorage(
) : DynamicContract(derivedTypeName, address, creator, chainId),
ERC721(derivedTypeName, erc721_name, erc721_symbol, address, creator, chainId),
_tokenURIs(this) {

ERC721URIStorage::registerContractFunctions();
}

DBBatch ERC721URIStorage::dump() const {
DBBatch batchedOperations = ERC721::dump();

for (auto it = this->_tokenURIs.cbegin(); it != this->_tokenURIs.cend(); ++it) {
batchedOperations.push_back(
Utils::uintToBytes(it->first),
StrConv::stringToBytes(it->second),
this->getNewPrefix("tokenURIs_")
);
}

return batchedOperations;
}

Expand Down
13 changes: 7 additions & 6 deletions src/contract/templates/erc721uristorage.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/*
Copyright (c) [2023-2024] [AppLayer Developers]
This software is distributed under the MIT License.
See the LICENSE.txt file in the project root for more information.
*/

#ifndef ERC721URISTORAGE_H
#define ERC721URISTORAGE_H

#include "erc721.h"


/// Template for an ERC721URIStorage contract.
/// Roughly based on the OpenZeppelin implementation.
class ERC721URIStorage : virtual public ERC721 {
Expand Down Expand Up @@ -33,21 +38,17 @@ class ERC721URIStorage : virtual public ERC721 {
void registerContractFunctions() override;

public:

/**
* ConstructorArguments is a tuple of the contract constructor arguments in the order they appear in the constructor.
*/
using ConstructorArguments = std::tuple<const std::string&, const std::string&>;


/**
* Constructor for loading contract from DB.
* @param address The address where the contract will be deployed.
* @param db Reference to the database object.
*/
ERC721URIStorage(
const Address& address, const DB& db
);
ERC721URIStorage(const Address& address, const DB& db);

/**
* Constructor to be used when creating a new contract.
Expand Down
7 changes: 6 additions & 1 deletion src/contract/templates/nativewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ NativeWrapper::NativeWrapper(
NativeWrapper::~NativeWrapper() = default;

DBBatch NativeWrapper::dump() const {
return BaseContract::dump();
// We need to dump all the data from the parent class as well
DBBatch batch = ERC20::dump();
DBBatch baseDump = BaseContract::dump();
for (const auto& dbItem : baseDump.getPuts()) batch.push_back(dbItem);
for (const auto& dbItem : baseDump.getDels()) batch.delete_key(dbItem);
return batch;
}

void NativeWrapper::registerContractFunctions() {
Expand Down
2 changes: 1 addition & 1 deletion src/contract/templates/pebble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ DBBatch Pebble::dump() const {
for (auto it = this->minters_.cbegin(); it != this->minters_.cend(); ++it) {
batch.push_back(it->first.asBytes(), UintConv::uint8ToBytes(static_cast<uint8_t>(it->second)), this->getNewPrefix("minters_"));
}
batch.push_back(StrConv::stringToBytes("authorized_"), this->authorizer_.get().asBytes(), this->getDBPrefix());
batch.push_back(StrConv::stringToBytes("authorizer_"), this->authorizer_.get().asBytes(), this->getDBPrefix());
return batch;
}

Expand Down
22 changes: 12 additions & 10 deletions src/contract/templates/throwtestA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ See the LICENSE.txt file in the project root for more information.
#include "throwtestA.h"

ThrowTestA::ThrowTestA(
const Address& address, const Address& creator,
const uint64_t& chainId
) : DynamicContract("ThrowTestA", address, creator, chainId) {
const Address& address, const Address& creator, const uint64_t& chainId
) : DynamicContract("ThrowTestA", address, creator, chainId), num_(this) {
this->num_.commit();
registerContractFunctions();
this->num_.enableRegister();
}

ThrowTestA::ThrowTestA(
const Address& address,
const DB& db
) : DynamicContract(address, db) {
ThrowTestA::ThrowTestA(const Address& address, const DB& db) : DynamicContract(address, db) {
this->num_ = UintConv::bytesToUint8(db.get(std::string("num_"), this->getDBPrefix()));
this->num_.commit();
registerContractFunctions();
this->num_.enableRegister();
}

ThrowTestA::~ThrowTestA() { return; }

DBBatch ThrowTestA::dump() const
{
return BaseContract::dump();
DBBatch ThrowTestA::dump() const {
DBBatch dbBatch = BaseContract::dump();
dbBatch.push_back(StrConv::stringToBytes("num_"), UintConv::uint8ToBytes(this->num_.get()), this->getDBPrefix());
return dbBatch;
}

uint8_t ThrowTestA::getNumA() const { return this->num_.get(); }
Expand Down
Loading