|
| 1 | +#include <nano/lib/blockbuilders.hpp> |
| 2 | +#include <nano/lib/blocks.hpp> |
| 3 | +#include <nano/lib/numbers.hpp> |
| 4 | +#include <nano/lib/work.hpp> |
| 5 | +#include <nano/secure/common.hpp> |
| 6 | +#include <nano/secure/ledger.hpp> |
| 7 | +#include <nano/secure/ledger_set_any.hpp> |
| 8 | +#include <nano/secure/storage_weighted_work.hpp> |
| 9 | +#include <nano/test_common/ledger_context.hpp> |
| 10 | + |
| 11 | +#include <gtest/gtest.h> |
| 12 | + |
| 13 | +#include <limits> |
| 14 | + |
| 15 | +namespace |
| 16 | +{ |
| 17 | +std::shared_ptr<nano::block> send_to (nano::account const & destination) |
| 18 | +{ |
| 19 | + nano::block_builder builder; |
| 20 | + nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits<unsigned>::max () }; |
| 21 | + return builder.state () |
| 22 | + .account (nano::dev::genesis_key.pub) |
| 23 | + .previous (nano::dev::genesis->hash ()) |
| 24 | + .representative (nano::dev::genesis_key.pub) |
| 25 | + .balance (nano::dev::constants.genesis_amount - 1) |
| 26 | + .link (destination) |
| 27 | + .sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) |
| 28 | + .work (*pool.generate (nano::dev::genesis->hash ())) |
| 29 | + .build (); |
| 30 | +} |
| 31 | +} |
| 32 | + |
| 33 | +// A send to a not-yet-opened account is storage-weighted: its required work is the base |
| 34 | +// requirement scaled by the multiplier, and at multiplier 1 it collapses to the base. |
| 35 | +TEST (storage_weighted_work, new_account_is_weighted) |
| 36 | +{ |
| 37 | + auto ctx = nano::test::ledger_empty (); |
| 38 | + auto & ledger = ctx.ledger (); |
| 39 | + nano::keypair fresh; |
| 40 | + auto block = send_to (fresh.pub); |
| 41 | + auto transaction = ledger.tx_begin_read (); |
| 42 | + |
| 43 | + ASSERT_TRUE (nano::block_adds_new_account (ledger, transaction, *block)); |
| 44 | + |
| 45 | + uint64_t const base = ledger.work.threshold_base (block->work_version ()); |
| 46 | + auto weighted = nano::evaluate_storage_weighted_work (ledger, transaction, *block, 8.0); |
| 47 | + ASSERT_TRUE (weighted.creates_new_account); |
| 48 | + ASSERT_EQ (base, weighted.base_threshold); |
| 49 | + ASSERT_EQ (nano::difficulty::from_multiplier (8.0, base), weighted.required_threshold); |
| 50 | + ASSERT_GT (weighted.required_threshold, weighted.base_threshold); |
| 51 | + |
| 52 | + // Multiplier 1 must not raise the requirement, and the block's own work satisfies it. |
| 53 | + auto unweighted = nano::evaluate_storage_weighted_work (ledger, transaction, *block, 1.0); |
| 54 | + ASSERT_EQ (base, unweighted.required_threshold); |
| 55 | + ASSERT_TRUE (unweighted.satisfies); |
| 56 | +} |
| 57 | + |
| 58 | +// A send to an already-opened account carries no storage weight, whatever the multiplier. |
| 59 | +TEST (storage_weighted_work, existing_account_not_weighted) |
| 60 | +{ |
| 61 | + auto ctx = nano::test::ledger_empty (); |
| 62 | + auto & ledger = ctx.ledger (); |
| 63 | + // Genesis account already exists in the ledger. |
| 64 | + auto block = send_to (nano::dev::genesis_key.pub); |
| 65 | + auto transaction = ledger.tx_begin_read (); |
| 66 | + |
| 67 | + ASSERT_FALSE (nano::block_adds_new_account (ledger, transaction, *block)); |
| 68 | + |
| 69 | + auto weighted = nano::evaluate_storage_weighted_work (ledger, transaction, *block, 8.0); |
| 70 | + ASSERT_FALSE (weighted.creates_new_account); |
| 71 | + ASSERT_EQ (weighted.base_threshold, weighted.required_threshold); |
| 72 | + ASSERT_DOUBLE_EQ (1.0, weighted.weight_multiplier); |
| 73 | + ASSERT_TRUE (weighted.satisfies); |
| 74 | +} |
| 75 | + |
| 76 | +// A multiplier below 1 can never lower the base requirement. |
| 77 | +TEST (storage_weighted_work, multiplier_floor) |
| 78 | +{ |
| 79 | + auto ctx = nano::test::ledger_empty (); |
| 80 | + auto & ledger = ctx.ledger (); |
| 81 | + nano::keypair fresh; |
| 82 | + auto block = send_to (fresh.pub); |
| 83 | + auto transaction = ledger.tx_begin_read (); |
| 84 | + |
| 85 | + auto result = nano::evaluate_storage_weighted_work (ledger, transaction, *block, 0.25); |
| 86 | + ASSERT_EQ (result.base_threshold, result.required_threshold); |
| 87 | + ASSERT_DOUBLE_EQ (1.0, result.weight_multiplier); |
| 88 | +} |
0 commit comments