Add atomic conserved energy and elemental inventory ledger
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#include "evo/accounting/ledger.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace evo::accounting {
|
||||
namespace {
|
||||
bool add(std::uint64_t& value, std::uint64_t amount) noexcept {
|
||||
if (amount > std::numeric_limits<std::uint64_t>::max() - value) {
|
||||
return false;
|
||||
}
|
||||
value += amount;
|
||||
return true;
|
||||
}
|
||||
bool accumulate(Inventory& total, const Inventory& amount) noexcept {
|
||||
if (!add(total.energy.value, amount.energy.value)) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t index = 0; index < total.matter.size(); ++index) {
|
||||
if (!add(total.matter[index].value, amount.matter[index].value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool contains(const Inventory& balance, const Inventory& amount) noexcept {
|
||||
if (balance.energy < amount.energy) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t index = 0; index < balance.matter.size(); ++index) {
|
||||
if (balance.matter[index] < amount.matter[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<Ledger, LedgerError> Ledger::create(std::span<const Inventory> opening) {
|
||||
if (opening.empty() || opening.size() > max_reservoirs) {
|
||||
return std::unexpected{LedgerError::reservoir_limit};
|
||||
}
|
||||
Ledger ledger;
|
||||
for (const auto& balance : opening) {
|
||||
if (!accumulate(ledger.totals_, balance)) {
|
||||
return std::unexpected{LedgerError::overflow};
|
||||
}
|
||||
}
|
||||
ledger.balances_.assign(opening.begin(), opening.end());
|
||||
return ledger;
|
||||
}
|
||||
|
||||
std::expected<void, LedgerError> Ledger::transfer(std::uint64_t action,
|
||||
std::span<const Transfer> legs) {
|
||||
if (action <= last_action_) {
|
||||
return std::unexpected{LedgerError::stale_action};
|
||||
}
|
||||
if (legs.empty() || legs.size() > max_transfers) {
|
||||
return std::unexpected{LedgerError::transfer_limit};
|
||||
}
|
||||
auto candidate = balances_;
|
||||
for (const auto& leg : legs) {
|
||||
if (leg.from.value == 0 || leg.to.value == 0 || leg.from.value > candidate.size() ||
|
||||
leg.to.value > candidate.size()) {
|
||||
return std::unexpected{LedgerError::invalid_reservoir};
|
||||
}
|
||||
if (leg.from.value == leg.to.value) {
|
||||
return std::unexpected{LedgerError::same_reservoir};
|
||||
}
|
||||
auto& source = candidate[leg.from.value - 1];
|
||||
auto& destination = candidate[leg.to.value - 1];
|
||||
if (!contains(source, leg.amount)) {
|
||||
return std::unexpected{LedgerError::insufficient_inventory};
|
||||
}
|
||||
// Global totals bound every destination; keep the local check explicit.
|
||||
if (!accumulate(destination, leg.amount)) {
|
||||
return std::unexpected{LedgerError::overflow};
|
||||
}
|
||||
source.energy.value -= leg.amount.energy.value;
|
||||
for (std::size_t index = 0; index < source.matter.size(); ++index) {
|
||||
source.matter[index].value -= leg.amount.matter[index].value;
|
||||
}
|
||||
}
|
||||
balances_.swap(candidate);
|
||||
last_action_ = action;
|
||||
return {};
|
||||
}
|
||||
} // namespace evo::accounting
|
||||
Reference in New Issue
Block a user