Files
Peya/tests/unit_tests/rollup.cpp

115 lines
3.7 KiB
C++
Raw Normal View History

2026-03-18 16:35:23 +01:00
// Copyright (c) 2026, Salvium
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define IN_UNIT_TESTS
#include <cstring>
#include "gtest/gtest.h"
#include "blockchain_db/testdb.h"
#include "cryptonote_core/blockchain.h"
#include "cryptonote_core/blockchain_and_pool.h"
using namespace cryptonote;
namespace
{
class RollupTestDB: public BaseTestDB
{
public:
RollupTestDB() { m_open = true; }
};
Blockchain *init_blockchain_for_tokens(BlockchainAndPool &bap)
{
static const std::pair<uint8_t, uint64_t> hard_forks[] = {
std::make_pair(1, 0),
std::make_pair(HF_VERSION_ENABLE_TOKENS, 1),
std::make_pair((uint8_t)0, (uint64_t)0),
};
static const test_options opts = {
hard_forks,
1000,
};
Blockchain *bc = &bap.blockchain;
const bool ok = bc->init(new RollupTestDB(), network_type::FAKECHAIN, true, &opts, 0, NULL);
EXPECT_TRUE(ok);
return bc;
}
transaction make_rollup_tx(uint64_t amount_burnt, std::initializer_list<uint64_t> fees)
{
transaction tx;
tx.version = TRANSACTION_VERSION_ENABLE_TOKENS;
tx.type = transaction_type::ROLLUP;
tx.source_asset_type = "SAL1";
tx.destination_asset_type = "SAL1";
tx.amount_burnt = amount_burnt;
tx.layer2_rollup_data.version = 1;
uint64_t nonce = 1;
for (const uint64_t fee: fees) {
layer2_rollup_tx_t rollup_tx{};
std::memcpy(&rollup_tx.tx_prefix_hash, &nonce, sizeof(nonce));
const uint64_t key_image_nonce = nonce + 100;
std::memcpy(&rollup_tx.first_key_image, &key_image_nonce, sizeof(key_image_nonce));
rollup_tx.tx_fee = fee;
tx.layer2_rollup_data.txs.push_back(rollup_tx);
++nonce;
}
return tx;
}
} // anonymous namespace
TEST(rollup, rejects_amount_burnt_mismatch)
{
BlockchainAndPool bap;
Blockchain *bc = init_blockchain_for_tokens(bap);
transaction tx = make_rollup_tx(/*amount_burnt=*/5, {2, 4});
tx_verification_context tvc = AUTO_VAL_INIT(tvc);
EXPECT_FALSE(bc->check_tx_type_and_version(tx, tvc));
}
TEST(rollup, accepts_matching_amount_burnt)
{
BlockchainAndPool bap;
Blockchain *bc = init_blockchain_for_tokens(bap);
transaction tx = make_rollup_tx(/*amount_burnt=*/6, {2, 4});
tx_verification_context tvc = AUTO_VAL_INIT(tvc);
EXPECT_TRUE(bc->check_tx_type_and_version(tx, tvc));
}