Tidy up genesis Ledger construction

This commit is contained in:
Vinnie Falco
2015-07-11 14:43:21 -07:00
parent 5bd9cd2ee8
commit 692f224e1c
7 changed files with 68 additions and 132 deletions

View File

@@ -19,7 +19,6 @@
#include <BeastConfig.h>
#include <ripple/app/ledger/Ledger.h>
#include <ripple/basics/contract.h>
#include <ripple/app/ledger/AcceptedLedger.h>
#include <ripple/app/ledger/InboundLedgers.h>
#include <ripple/app/ledger/LedgerMaster.h>
@@ -31,18 +30,20 @@
#include <ripple/app/misc/IHashRouter.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/tx/TransactionMaster.h>
#include <ripple/basics/contract.h>
#include <ripple/basics/Log.h>
#include <ripple/protocol/digest.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/core/Config.h>
#include <ripple/core/DatabaseCon.h>
#include <ripple/core/LoadFeeTrack.h>
#include <ripple/core/JobQueue.h>
#include <ripple/core/SociDB.h>
#include <ripple/json/to_string.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/JsonFields.h>
#include <ripple/protocol/SecretKey.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/core/Config.h>
#include <ripple/core/JobQueue.h>
#include <ripple/core/LoadFeeTrack.h>
#include <ripple/json/to_string.h>
#include <ripple/nodestore/Database.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/types.h>
@@ -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<SLE>
makeGenesisAccount (AccountID const& id,
std::uint64_t drops)
{
std::shared_ptr<SLE> sle =
std::make_shared<SLE>(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 <SHAMap> (SHAMapType::TRANSACTION,
getApp().family(), deprecatedLogs().journal("SHAMap")))
, stateMap_ (std::make_shared <SHAMap> (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<SLE>(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,

View File

@@ -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,

View File

@@ -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<Ledger>(
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<Ledger> (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<Ledger> (true, std::ref (*secondLedger)));
assert (secondLedger->exists(keylet::account(
calcAccountID(rootAddress))));
}
auto const genesis = std::make_shared<Ledger>(
create_genesis, getConfig());
auto const next =
std::make_shared<Ledger>(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<Ledger>(true, *next));
}
Ledger::pointer

View File

@@ -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<Ledger>(
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<Ledger>(
create_genesis, config);
wipe(*ledger);
ledger->rawInsert(sle(1));
ReadView& v0 = *ledger;

View File

@@ -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 <Ledger> (
masterAccountID, startAmount));
Config const config;
auto const ledger =
std::make_shared<Ledger>(
create_genesis, config);
using namespace detail;
TxnSignApiFacade apiFacade (TxnSignApiFacade::noNetOPs, ledger);

View File

@@ -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<Ledger>
genesis();
/** Returns the open ledger.
This is a non-modifiable snapshot of the

View File

@@ -50,26 +50,14 @@ namespace test {
namespace jtx {
std::shared_ptr<Ledger>
Env::genesis()
{
Account const master("master",
generateKeyPair(KeyType::secp256k1,
generateSeed("masterpassphrase")));
auto const ledger =
std::make_shared<Ledger>(
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<Ledger>(
create_genesis, config))
, cachedSLEs_ (std::chrono::seconds(5), clock)
, openLedger (closed_, config, cachedSLEs_, journal)
{