// we finalize the ledger and create a new immutable ledger state private void Finalize(SignedLedger signed, LedgerPostState state) { var ledgerState = state.Finalize(HasherFactory.CreateHasher(signed.GetVersion())); if (!CheckMerkleRoot(ledgerState, signed)) { throw new Exception("Merkle root is not valid"); } LiveService.PersistenceManager.Save(new SignedLedgerState(signed, state.GetLedgerStateChange())); LedgerState = ledgerState; LastLedger = signed; OnNewLedger(LastLedger); }
private void InitializeLedger(SignedLedger lastLedger) { var accounts = new Trie <Account>(Address.RAW_SIZE); foreach (var account in LiveService.AccountManager.GetAccounts()) { accounts.CreateOrUpdate(account.Key.ToRawBytes(), old => { if (old != null) { throw new Exception("The ledger's account states are duplicated !"); } return(account.Value); }); } // TODO compute hash accounts.ComputeHash(HasherFactory.CreateHasher(lastLedger.GetVersion())); LedgerState = new LedgerStateFinal(accounts); LastLedger = lastLedger; // Debug.Assert(SignedLedgerValidator.Validate(this.lastLedger) == LedgerValidationStatus.Ok, "Last Ledger is not valid"); // Most likely not enough signatures (see quorum) }
private bool CheckMerkleRoot(LedgerStateFinal ledgerState, SignedLedger ledger) { var hash = GetMerkleRootHash(ledgerState, ledger.GetVersion()); return(ledger.Ledger.MerkleHash.Equals(hash)); }