From 692f224e1c5e83754680610f6fb73c6601eebcf2 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 11 Jul 2015 14:43:21 -0700 Subject: [PATCH] Tidy up genesis Ledger construction --- src/ripple/app/ledger/Ledger.cpp | 63 ++++++++++----------------- src/ripple/app/ledger/Ledger.h | 19 +++++--- src/ripple/app/main/Application.cpp | 60 ++++++++----------------- src/ripple/ledger/tests/View_test.cpp | 10 ++++- src/ripple/rpc/tests/JSONRPC.test.cpp | 23 ++-------- src/ripple/test/jtx/Env.h | 9 ---- src/ripple/test/jtx/impl/Env.cpp | 16 +------ 7 files changed, 68 insertions(+), 132 deletions(-) diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 606a65bf1..4adc0e271 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -31,18 +30,20 @@ #include #include #include +#include #include -#include #include +#include #include +#include +#include #include +#include +#include #include #include +#include #include -#include -#include -#include -#include #include #include #include @@ -54,6 +55,8 @@ namespace ripple { +create_genesis_t const create_genesis {}; + class Ledger::txs_iter_impl : public txs_type::iter_base { @@ -110,51 +113,29 @@ public: //------------------------------------------------------------------------------ -/* Create the "genesis" account root. - The genesis account root contains all the XRP - that will ever exist in the system. - @param id The AccountID of the account root - @param drops The number of drops to start with -*/ -static -std::shared_ptr -makeGenesisAccount (AccountID const& id, - std::uint64_t drops) -{ - std::shared_ptr sle = - std::make_shared(ltACCOUNT_ROOT, - getAccountRootIndex(id)); - sle->setAccountID (sfAccount, id); - sle->setFieldAmount (sfBalance, drops); - sle->setFieldU32 (sfSequence, 1); - return sle; -} - -// VFALCO This constructor could be eliminating by providing -// a free function createGenesisLedger, call the -// other constructor with appropriate parameters, and -// then create the master account / flush dirty. -// -Ledger::Ledger (AccountID const& masterAccountID, - std::uint64_t balanceInDrops) +Ledger::Ledger (create_genesis_t, Config const& config) : mImmutable (false) , txMap_ (std::make_shared (SHAMapType::TRANSACTION, getApp().family(), deprecatedLogs().journal("SHAMap"))) , stateMap_ (std::make_shared (SHAMapType::STATE, getApp().family(), deprecatedLogs().journal("SHAMap"))) - // VFALCO Needs audit - , fees_(getFees(*this, getConfig())) + , fees_ (getFees(*this, getConfig())) { - // first ledger info_.seq = 1; - info_.drops = balanceInDrops; + info_.drops = SYSTEM_CURRENCY_START; info_.closeTimeResolution = ledgerDefaultTimeResolution; - auto const sle = makeGenesisAccount( - masterAccountID, balanceInDrops); - WriteLog (lsTRACE, Ledger) - << "root account: " << sle->getJson(0); + auto const id = calcAccountID( + generateKeyPair(KeyType::secp256k1, + generateSeed("masterpassphrase")).first); + auto const sle = std::make_shared(keylet::account(id)); + sle->setFieldU32 (sfSequence, 1); + sle->setAccountID (sfAccount, id); + sle->setFieldAmount (sfBalance, info_.drops); rawInsert(sle); stateMap_->flushDirty (hotACCOUNT_NODE, info_.seq); + updateHash(); + setClosed(); + setAccepted(); } Ledger::Ledger (uint256 const& parentHash, diff --git a/src/ripple/app/ledger/Ledger.h b/src/ripple/app/ledger/Ledger.h index 8b2269f09..625c3c1a3 100644 --- a/src/ripple/app/ledger/Ledger.h +++ b/src/ripple/app/ledger/Ledger.h @@ -40,6 +40,9 @@ class TransactionMaster; class SqliteStatement; +struct create_genesis_t { }; +extern create_genesis_t const create_genesis; + /** Holds a ledger. The ledger is composed of two SHAMaps. The state map holds all of the @@ -80,13 +83,19 @@ public: Ledger (Ledger const&) = delete; Ledger& operator= (Ledger const&) = delete; - /** Construct the genesis ledger. + /** Create the Genesis ledger. - @param masterAccountID The public of the account that - will hold `balanceInDrops` XRP in drops. + The Genesis ledger contains a single account whose + AccountID is generated with a Generator using the seed + computed from the string "masterpassphrase" and ordinal + zero. + + The account has an XRP balance equal to the total amount + of XRP in the system. No more XRP than the amount which + starts in this account can ever exist, with amounts + used to pay fees being destroyed. */ - Ledger (AccountID const& masterAccountID, - std::uint64_t balanceInDrops); + Ledger (create_genesis_t, Config const& config); // Used for ledgers loaded from JSON files Ledger (uint256 const& parentHash, uint256 const& transHash, diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index cbb6824f8..fc058b9d2 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -756,7 +756,7 @@ public: { m_journal.info << "Starting new Ledger"; - startNewLedger (); + startGenesisLedger (); } else if (startUp == Config::LOAD || startUp == Config::LOAD_FILE || @@ -777,10 +777,12 @@ public: if (!getConfig ().RUN_STANDALONE) m_networkOPs->needNetworkLedger (); - startNewLedger (); + startGenesisLedger (); } else - startNewLedger (); + { + startGenesisLedger (); + } m_orderBookDB.setup (getApp().getLedgerMaster ().getCurrentLedger ()); @@ -1031,7 +1033,7 @@ public: private: void updateTables (); - void startNewLedger (); + void startGenesisLedger (); Ledger::pointer getLastFullLedger(); bool loadOldLedger ( std::string const& ledgerID, bool replay, bool isFilename); @@ -1041,45 +1043,19 @@ private: //------------------------------------------------------------------------------ -void ApplicationImp::startNewLedger () +void ApplicationImp::startGenesisLedger () { - // New stuff. - RippleAddress rootSeedMaster = RippleAddress::createSeedGeneric ("masterpassphrase"); - RippleAddress rootGeneratorMaster = RippleAddress::createGeneratorPublic (rootSeedMaster); - RippleAddress rootAddress = RippleAddress::createAccountPublic (rootGeneratorMaster, 0); - - // Print enough information to be able to claim root account. - m_journal.info << "Root master seed: " << rootSeedMaster.humanSeed (); - m_journal.info << "Root account: " << toBase58(calcAccountID(rootAddress)); - - { - auto const masterAccountID = - calcAccountID(generateKeyPair( - KeyType::secp256k1, - generateSeed("masterpassphrase")).first); - - auto firstLedger = std::make_shared( - masterAccountID, SYSTEM_CURRENCY_START); - assert (firstLedger->exists(keylet::account( - calcAccountID(rootAddress)))); - // TODO(david): Add any default amendments - // TODO(david): Set default fee/reserve - firstLedger->getHash(); // updates the hash - firstLedger->setClosed (); - firstLedger->setAccepted (); - m_ledgerMaster->pushLedger (firstLedger); - - Ledger::pointer secondLedger = std::make_shared (true, std::ref (*firstLedger)); - secondLedger->setClosed (); - secondLedger->setAccepted (); - - m_networkOPs->setLastCloseTime (secondLedger->info().closeTime); - openLedger_.emplace(secondLedger, getConfig(), - cachedSLEs_, deprecatedLogs().journal("OpenLedger")); - m_ledgerMaster->pushLedger (secondLedger, std::make_shared (true, std::ref (*secondLedger))); - assert (secondLedger->exists(keylet::account( - calcAccountID(rootAddress)))); - } + auto const genesis = std::make_shared( + create_genesis, getConfig()); + auto const next = + std::make_shared(true, *genesis); + next->setClosed (); + next->setAccepted (); + m_networkOPs->setLastCloseTime (next->info().closeTime); + openLedger_.emplace(next, getConfig(), + cachedSLEs_, deprecatedLogs().journal("OpenLedger")); + m_ledgerMaster->pushLedger (next, + std::make_shared(true, *next)); } Ledger::pointer diff --git a/src/ripple/ledger/tests/View_test.cpp b/src/ripple/ledger/tests/View_test.cpp index 8e644a03b..a70fec193 100644 --- a/src/ripple/ledger/tests/View_test.cpp +++ b/src/ripple/ledger/tests/View_test.cpp @@ -146,8 +146,11 @@ class View_test void testLedger() { + Config const config; using namespace jtx; - auto const ledger = Env::genesis(); + auto const ledger = + std::make_shared( + create_genesis, config); wipe(*ledger); ReadView& v = *ledger; succ(v, 0, boost::none); @@ -388,7 +391,10 @@ class View_test // ApplyView on that, then another ApplyView, // erase the item, apply. { - auto const ledger = Env::genesis(); + Config const config; + auto const ledger = + std::make_shared( + create_genesis, config); wipe(*ledger); ledger->rawInsert(sle(1)); ReadView& v0 = *ledger; diff --git a/src/ripple/rpc/tests/JSONRPC.test.cpp b/src/ripple/rpc/tests/JSONRPC.test.cpp index e017d6511..47d61f9af 100644 --- a/src/ripple/rpc/tests/JSONRPC.test.cpp +++ b/src/ripple/rpc/tests/JSONRPC.test.cpp @@ -910,25 +910,10 @@ class JSONRPC_test : public beast::unit_test::suite public: void testAutoFillFees () { - std::string const secret = "masterpassphrase"; - RippleAddress rootSeedMaster - = RippleAddress::createSeedGeneric (secret); - - RippleAddress rootGeneratorMaster - = RippleAddress::createGeneratorPublic (rootSeedMaster); - - RippleAddress rootAddress - = RippleAddress::createAccountPublic (rootGeneratorMaster, 0); - - std::uint64_t startAmount (100000); - - auto const masterAccountID = - calcAccountID(generateKeyPair( - KeyType::secp256k1, - generateSeed("masterpassphrase")).first); - - Ledger::pointer ledger (std::make_shared ( - masterAccountID, startAmount)); + Config const config; + auto const ledger = + std::make_shared( + create_genesis, config); using namespace detail; TxnSignApiFacade apiFacade (TxnSignApiFacade::noNetOPs, ledger); diff --git a/src/ripple/test/jtx/Env.h b/src/ripple/test/jtx/Env.h index efaa633d7..122a546e3 100644 --- a/src/ripple/test/jtx/Env.h +++ b/src/ripple/test/jtx/Env.h @@ -147,15 +147,6 @@ public: Env (beast::unit_test::suite& test_); - /** Creates a genesis ledger. - - This is called by the implementation but provided - as a public member for interested callers. - */ - static - std::shared_ptr - genesis(); - /** Returns the open ledger. This is a non-modifiable snapshot of the diff --git a/src/ripple/test/jtx/impl/Env.cpp b/src/ripple/test/jtx/impl/Env.cpp index b0c5cbe3e..7a3457ed1 100644 --- a/src/ripple/test/jtx/impl/Env.cpp +++ b/src/ripple/test/jtx/impl/Env.cpp @@ -50,26 +50,14 @@ namespace test { namespace jtx { -std::shared_ptr -Env::genesis() -{ - Account const master("master", - generateKeyPair(KeyType::secp256k1, - generateSeed("masterpassphrase"))); - auto const ledger = - std::make_shared( - master.id(), SYSTEM_CURRENCY_START); - ledger->setClosed(); - return ledger; -} - // VFALCO Could wrap the log in a Journal here Env::Env (beast::unit_test::suite& test_) : test(test_) , master("master", generateKeyPair( KeyType::secp256k1, generateSeed("masterpassphrase"))) - , closed_ (genesis()) + , closed_ (std::make_shared( + create_genesis, config)) , cachedSLEs_ (std::chrono::seconds(5), clock) , openLedger (closed_, config, cachedSLEs_, journal) {