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

Bump expiration op #3775

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
36 changes: 23 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/protocol-next/xdr
6 changes: 3 additions & 3 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rustc-simple-version = "0.1.0"
version = "0.0.16"
git = "https://github.com/stellar/rs-soroban-env"
package = "soroban-env-host"
rev = "e6e02dc4b5cf946028e51603883bd81ca38ce519"
rev = "44009d67f6a7b85c0aafc363a8539fc8217bcb9a"

# This copy of the soroban host is _optional_ and only enabled during protocol
# transitions. When transitioning from protocol N to N+1, the `curr` copy
Expand All @@ -51,11 +51,11 @@ optional = true
version = "0.0.16"
git = "https://github.com/stellar/rs-soroban-env"
package = "soroban-env-host"
rev = "16bdf376278e1f6322beebad0a11e3c484b1eef9"
rev = "e6e02dc4b5cf946028e51603883bd81ca38ce519"

[dependencies.soroban-test-wasms]
git = "https://github.com/stellar/rs-soroban-env"
rev = "e6e02dc4b5cf946028e51603883bd81ca38ce519"
rev = "44009d67f6a7b85c0aafc363a8539fc8217bcb9a"

[dependencies.cargo-lock]
git = "https://github.com/rustsec/rustsec"
Expand Down
12 changes: 6 additions & 6 deletions src/rust/src/host-dep-tree-curr.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
soroban-env-host 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=e6e02dc4b5cf946028e51603883bd81ca38ce519#e6e02dc4b5cf946028e51603883bd81ca38ce519
soroban-env-host 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=44009d67f6a7b85c0aafc363a8539fc8217bcb9a#44009d67f6a7b85c0aafc363a8539fc8217bcb9a
├── stellar-strkey 0.0.7 git+https://github.com/stellar/rs-stellar-strkey?rev=e6ba45c60c16de28c7522586b80ed0150157df73#e6ba45c60c16de28c7522586b80ed0150157df73
│ ├── thiserror 1.0.40 checksum:978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac
│ │ └── thiserror-impl 1.0.40 checksum:f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f
Expand All @@ -25,14 +25,14 @@ soroban-env-host 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=e6e02d
│ │ └── downcast-rs 1.2.0 checksum:9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650
│ ├── smallvec 1.10.0 checksum:a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0
│ └── intx 0.1.0 checksum:f6f38a50a899dc47a6d0ed5508e7f601a2e34c3a85303514b5d137f3c10a0c75
├── soroban-native-sdk-macros 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=e6e02dc4b5cf946028e51603883bd81ca38ce519#e6e02dc4b5cf946028e51603883bd81ca38ce519
├── soroban-native-sdk-macros 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=44009d67f6a7b85c0aafc363a8539fc8217bcb9a#44009d67f6a7b85c0aafc363a8539fc8217bcb9a
│ ├── syn 2.0.18 checksum:32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e
│ ├── quote 1.0.28 checksum:1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488
│ ├── proc-macro2 1.0.60 checksum:dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406
│ └── itertools 0.10.5 checksum:b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473
│ └── either 1.8.1 checksum:7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91
├── soroban-env-common 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=e6e02dc4b5cf946028e51603883bd81ca38ce519#e6e02dc4b5cf946028e51603883bd81ca38ce519
│ ├── stellar-xdr 0.0.16 git+https://github.com/stellar/rs-stellar-xdr?rev=f7b43ea2a4a36a87ebde2cb6050ea4bd7540df21#f7b43ea2a4a36a87ebde2cb6050ea4bd7540df21
├── soroban-env-common 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=44009d67f6a7b85c0aafc363a8539fc8217bcb9a#44009d67f6a7b85c0aafc363a8539fc8217bcb9a
│ ├── stellar-xdr 0.0.16 git+https://github.com/stellar/rs-stellar-xdr?rev=dfb98415a08abe2c8a05a44ec237d53164198215#dfb98415a08abe2c8a05a44ec237d53164198215
│ │ ├── hex 0.4.3 checksum:7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70
│ │ ├── crate-git-revision 0.0.4 checksum:f998aef136a4e7833b0e4f0fc0939a59c40140b28e0ffbf524ad84fb2cc568c8
│ │ │ ├── serde_json 1.0.97 checksum:bdf3bf93142acad5821c99197022e170842cdbc1c30482b98750c688c640842a
Expand All @@ -48,10 +48,10 @@ soroban-env-host 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=e6e02d
│ │ └── base64 0.13.1 checksum:9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8
│ ├── static_assertions 1.1.0 checksum:a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f
│ ├── soroban-wasmi 0.30.0-soroban git+https://github.com/stellar/wasmi?rev=1a2bc7f#1a2bc7f3801c565c2dab22021255a164c05a7f76
│ ├── soroban-env-macros 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=e6e02dc4b5cf946028e51603883bd81ca38ce519#e6e02dc4b5cf946028e51603883bd81ca38ce519
│ ├── soroban-env-macros 0.0.16 git+https://github.com/stellar/rs-soroban-env?rev=44009d67f6a7b85c0aafc363a8539fc8217bcb9a#44009d67f6a7b85c0aafc363a8539fc8217bcb9a
│ │ ├── thiserror 1.0.40 checksum:978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac
│ │ ├── syn 2.0.18 checksum:32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e
│ │ ├── stellar-xdr 0.0.16 git+https://github.com/stellar/rs-stellar-xdr?rev=f7b43ea2a4a36a87ebde2cb6050ea4bd7540df21#f7b43ea2a4a36a87ebde2cb6050ea4bd7540df21
│ │ ├── stellar-xdr 0.0.16 git+https://github.com/stellar/rs-stellar-xdr?rev=dfb98415a08abe2c8a05a44ec237d53164198215#dfb98415a08abe2c8a05a44ec237d53164198215
│ │ ├── serde_json 1.0.97 checksum:bdf3bf93142acad5821c99197022e170842cdbc1c30482b98750c688c640842a
│ │ ├── serde 1.0.164 checksum:9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d
│ │ ├── quote 1.0.28 checksum:1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488
Expand Down
162 changes: 162 additions & 0 deletions src/transactions/BumpFootprintExpirationOpFrame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2023 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#ifdef ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
#include "transactions/BumpFootprintExpirationOpFrame.h"

namespace stellar
{

struct BumpFootprintExpirationMetrics
{
medida::MetricsRegistry& mMetrics;

size_t mLedgerWriteByte{0};

BumpFootprintExpirationMetrics(medida::MetricsRegistry& metrics)
: mMetrics(metrics)
{
}

~BumpFootprintExpirationMetrics()
{
mMetrics
.NewMeter({"soroban", "bump-fprint-exp-op", "write-ledger-byte"},
"byte")
.Mark(mLedgerWriteByte);
}
};

BumpFootprintExpirationOpFrame::BumpFootprintExpirationOpFrame(
Operation const& op, OperationResult& res, TransactionFrame& parentTx)
: OperationFrame(op, res, parentTx)
, mBumpFootprintExpirationOp(mOperation.body.bumpFootprintExpirationOp())
{
}

bool
BumpFootprintExpirationOpFrame::isOpSupported(LedgerHeader const& header) const
{
return header.ledgerVersion >= 20;
}

bool
BumpFootprintExpirationOpFrame::doApply(AbstractLedgerTxn& ltx)
{
throw std::runtime_error(
"BumpFootprintExpirationOpFrame::doApply needs Config");
}

bool
BumpFootprintExpirationOpFrame::doApply(Application& app,
AbstractLedgerTxn& ltx,
Hash const& sorobanBasePrngSeed)
{
BumpFootprintExpirationMetrics metrics(app.getMetrics());

auto const& resources = mParentTx.sorobanResources();
auto const& footprint = resources.footprint;

UnorderedMap<LedgerKey, uint32_t> originalExpirations;

for (auto const& lk : footprint.readOnly)
{
// When we move to use EXPIRATION_EXTENSIONS, this should become a
// loadWithoutRecord, and the metrics should be updated.
auto ltxe = ltx.load(lk);
if (ltxe)
{
auto keySize = xdr::xdr_size(lk);
auto entrySize = xdr::xdr_size(ltxe.current());
metrics.mLedgerWriteByte += keySize;
metrics.mLedgerWriteByte += entrySize;

// Divide the limit by 2 for the metadata check since the previous
// state is also included in the meta.
if (resources.extendedMetaDataSizeBytes / 2 <
metrics.mLedgerWriteByte ||
resources.readBytes < metrics.mLedgerWriteByte ||
resources.writeBytes < metrics.mLedgerWriteByte)
{
innerResult().code(
BUMP_FOOTPRINT_EXPIRATION_RESOURCE_LIMIT_EXCEEDED);
return false;
}

originalExpirations.emplace(LedgerEntryKey(ltxe.current()),
getExpirationLedger(ltxe.current()));

auto ledgerSeq = ltx.loadHeader().current().ledgerSeq;
uint32_t bumpTo = UINT32_MAX;
if (UINT32_MAX - ledgerSeq >
mBumpFootprintExpirationOp.ledgersToExpire())
{
bumpTo =
ledgerSeq + mBumpFootprintExpirationOp.ledgersToExpire();
}

if (getExpirationLedger(ltxe.current()) < bumpTo)
{
setExpirationLedger(ltxe.current(), bumpTo);
}
}
}

mParentTx.pushInitialExpirations(std::move(originalExpirations));

innerResult().code(BUMP_FOOTPRINT_EXPIRATION_SUCCESS);
return true;
}

bool
BumpFootprintExpirationOpFrame::doCheckValid(SorobanNetworkConfig const& config,
uint32_t ledgerVersion)
{
auto const& footprint = mParentTx.sorobanResources().footprint;
if (!footprint.readWrite.empty())
{
innerResult().code(BUMP_FOOTPRINT_EXPIRATION_MALFORMED);
return false;
}

for (auto const& lk : footprint.readOnly)
{
if (!isSorobanEntry(lk))
{
innerResult().code(BUMP_FOOTPRINT_EXPIRATION_MALFORMED);
return false;
}
}

if (mBumpFootprintExpirationOp.ledgersToExpire() >
config.stateExpirationSettings().maxEntryExpiration)
{
innerResult().code(BUMP_FOOTPRINT_EXPIRATION_MALFORMED);
return false;
}

return true;
}

bool
BumpFootprintExpirationOpFrame::doCheckValid(uint32_t ledgerVersion)
{
throw std::runtime_error(
"BumpFootprintExpirationOpFrame::doCheckValid needs Config");
}

void
BumpFootprintExpirationOpFrame::insertLedgerKeysToPrefetch(
UnorderedSet<LedgerKey>& keys) const
{
}

bool
BumpFootprintExpirationOpFrame::isSoroban() const
{
return true;
}
}

#endif // ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
51 changes: 51 additions & 0 deletions src/transactions/BumpFootprintExpirationOpFrame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

// Copyright 2023 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#ifdef ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
#include "transactions/OperationFrame.h"
#include "xdr/Stellar-transaction.h"

namespace stellar
{
class AbstractLedgerTxn;

class BumpFootprintExpirationOpFrame : public OperationFrame
{
BumpFootprintExpirationResult&
innerResult()
{
return mResult.tr().bumpFootprintExpirationResult();
}

BumpFootprintExpirationOp const& mBumpFootprintExpirationOp;

public:
BumpFootprintExpirationOpFrame(Operation const& op, OperationResult& res,
TransactionFrame& parentTx);

bool isOpSupported(LedgerHeader const& header) const override;

bool doApply(AbstractLedgerTxn& ltx) override;
bool doApply(Application& app, AbstractLedgerTxn& ltx,
Hash const& sorobanBasePrngSeed) override;

bool doCheckValid(SorobanNetworkConfig const& config,
uint32_t ledgerVersion) override;
bool doCheckValid(uint32_t ledgerVersion) override;

void
insertLedgerKeysToPrefetch(UnorderedSet<LedgerKey>& keys) const override;

static BumpFootprintExpirationResultCode
getInnerCode(OperationResult const& res)
{
return res.tr().bumpFootprintExpirationResult().code();
}

virtual bool isSoroban() const override;
};
}
#endif // ENABLE_NEXT_PROTOCOL_VERSION_UNSAFE_FOR_PRODUCTION
Loading