Ejemplo n.º 1
0
        // 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);
        }
Ejemplo n.º 2
0
        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)
        }
Ejemplo n.º 3
0
        private bool CheckMerkleRoot(LedgerStateFinal ledgerState, SignedLedger ledger)
        {
            var hash = GetMerkleRootHash(ledgerState, ledger.GetVersion());

            return(ledger.Ledger.MerkleHash.Equals(hash));
        }