Пример #1
0
        CoreBlock GenerateBlock(short issuer, int revision, long timestamp)
        {
            if (_blockInfo.ServiceBlocks.Count > 0)
            {
                var removed = new HashSet <int>();

                foreach (var serviceBlockTransaction in _blockInfo.ServiceBlocks.Values)
                {
                    var chainInfo = GetChainInfo(serviceBlockTransaction.ServiceBlock.ChainId);

                    var totalRevenue    = chainInfo.GetTotalAccountRevenue(timestamp);
                    var totalPayout     = chainInfo.TotalAccountPayout;
                    var availablePayout = totalRevenue - totalPayout;

                    var invalidBalance = false;
                    foreach (var serviceTransaction in serviceBlockTransaction.ServiceBlock.Transactions)
                    {
                        if (serviceTransaction.TransactionType == ServiceTransactionTypes.Purchase)
                        {
                            var purchaseTransaction = serviceTransaction as PurchaseServiceTransaction;
                            var buyer = GetCoreAccount(purchaseTransaction.AccountId);

                            if (!buyer.CanPurchase(purchaseTransaction.Price))
                            {
                                invalidBalance = true;
                                removed.Add(serviceBlockTransaction.ServiceBlock.ChainId);
                                break;
                            }
                        }
                        else if (serviceTransaction.TransactionType == ServiceTransactionTypes.RequestRevenue)
                        {
                            var revenue = serviceTransaction as RequestRevenueServiceTransaction;

                            availablePayout -= revenue.PayoutAmount;

                            if (availablePayout < 0)
                            {
                                invalidBalance = true;
                                break;
                            }
                        }
                    }

                    if (!invalidBalance)
                    {
                        var serviceAccount = GetCoreAccount(chainInfo.AccountId);

                        foreach (var serviceTransaction in serviceBlockTransaction.ServiceBlock.Transactions)
                        {
                            if (serviceTransaction.TransactionType == ServiceTransactionTypes.Purchase)
                            {
                                var purchaseTransaction = serviceTransaction as PurchaseServiceTransaction;
                                var buyer = GetCoreAccount(purchaseTransaction.AccountId);

                                buyer.Purchase(purchaseTransaction.Price, serviceAccount);

                                _blockInfo.AccountUpdates.Purchases.Add(purchaseTransaction);
                                _blockInfo.AccountUpdates.AffectedAccounts.Add(serviceTransaction.AccountId);
                                _blockInfo.AccountUpdates.AffectedAccounts.Add(purchaseTransaction.ReceiverAccountId);
                                _blockInfo.AccountUpdates.AffectedAccounts.Add(chainInfo.AccountId);
                            }
                            else if (serviceTransaction.TransactionType == ServiceTransactionTypes.Join)
                            {
                                _blockInfo.AccountUpdates.Joins.Add(serviceTransaction as JoinServiceTransaction);
                                _blockInfo.AccountUpdates.AffectedAccounts.Add(serviceTransaction.AccountId);
                            }
                            else if (serviceTransaction.TransactionType == ServiceTransactionTypes.RequestRevenue)
                            {
                                var revenue        = serviceTransaction as RequestRevenueServiceTransaction;
                                var networkAccount = GetCoreAccount(CoreAccount.NetworkAccountId);
                                var account        = GetCoreAccount(serviceTransaction.AccountId);

                                networkAccount.RemoveFromTranfser(revenue.PayoutAmount);
                                account.AddFromTransfer(revenue.PayoutAmount);

                                _blockInfo.AccountUpdates.Revenues.Add(revenue);
                                _blockInfo.AccountUpdates.AffectedAccounts.Add(serviceTransaction.AccountId);
                                _blockInfo.AccountUpdates.AffectedAccounts.Add(CoreAccount.NetworkAccountId);
                            }
                        }
                    }
                }

                foreach (var remove in removed)
                {
                    _blockInfo.ServiceBlocks.Remove(remove);
                }
            }

            if (!_blockInfo.HasTransaction || !(_coreChain.LastProcessedBlockId == _lastBlock.BlockId) || !(_blockState.BlockId == _lastBlock.BlockId))
            {
                return(null);
            }

            var coreChain = GetChainInfo(CoreChain.CoreChainId);

            var state = coreChain.LastState;
            var nextCoreTransactionId = state.LastTransactionId + 1;
            var blockId = _lastBlock.BlockId + 1;

            _blockInfo.Sort();

            var coreOperations   = new List <CoreOperation>();
            var coreTransactions = new List <CoreTransaction>();

            foreach (var newAccount in _blockInfo.NewAccounts)
            {
                newAccount.Operation.UpdateOperationId(nextCoreTransactionId);
                newAccount.Transaction.MetaData.SetTransactionId(nextCoreTransactionId);

                var account = GetCoreAccount(newAccount.Operation.AccountId);
                account.LastTransactionId = nextCoreTransactionId;

                ++nextCoreTransactionId;
                coreOperations.Add(newAccount.Operation);
                coreTransactions.Add(newAccount.Transaction);
            }

            foreach (var newChain in _blockInfo.Chains)
            {
                newChain.Operation.UpdateOperationId(nextCoreTransactionId);
                newChain.Transaction.MetaData.SetTransactionId(nextCoreTransactionId);

                var account = GetCoreAccount(newChain.Operation.AccountId);
                newChain.Operation.PreviousAccountTransactionId = account.LastTransactionId;
                account.LastTransactionId = nextCoreTransactionId;

                ++nextCoreTransactionId;
                coreOperations.Add(newChain.Operation);
                coreTransactions.Add(newChain.Transaction);
            }

            if (_blockInfo.AccountUpdates.HasUpdates)
            {
                _blockInfo.AccountUpdates.Operation.UpdateOperationId(nextCoreTransactionId);

                foreach (var accountId in _blockInfo.AccountUpdates.AffectedAccounts)
                {
                    var account = GetCoreAccount(accountId);
                    _blockInfo.AccountUpdates.Operation.AddAccount(accountId, account.LastTransactionId, account.HeleusCoins);

                    account.LastTransactionId = nextCoreTransactionId;
                }

                foreach (var transaction in _blockInfo.AccountUpdates.Transactions)
                {
                    _blockInfo.AccountUpdates.Operation.AddTransfer(transaction.AccountId, transaction.ReceiverAccountId, transaction.Amount, transaction.Reason, transaction.Timestamp);
                    transaction.MetaData.SetTransactionId(nextCoreTransactionId);

                    coreTransactions.Add(transaction);
                }

                foreach (var transaction in _blockInfo.AccountUpdates.Purchases)
                {
                    _blockInfo.AccountUpdates.Operation.AddPurchase(transaction.AccountId, transaction.Price, transaction.TargetChainId, transaction.PurchaseItemId, transaction.Timestamp);
                }

                foreach (var transaction in _blockInfo.AccountUpdates.Joins)
                {
                    _blockInfo.AccountUpdates.Operation.AddJoin(transaction.AccountId, transaction.TargetChainId, transaction.AccountKey != null ? transaction.AccountKey.KeyIndex : Protocol.CoreAccountSignKeyIndex, transaction.Timestamp);
                }

                foreach (var transaction in _blockInfo.AccountUpdates.Revenues)
                {
                    _blockInfo.AccountUpdates.Operation.AddRevenue(transaction.AccountId, transaction.TargetChainId, transaction.PayoutAmount, transaction.Timestamp);
                }

                ++nextCoreTransactionId;
                coreOperations.Add(_blockInfo.AccountUpdates.Operation);
            }

            var blockStateOperation = new BlockStateOperation(timestamp);

            coreOperations.Add(blockStateOperation);
            blockStateOperation.UpdateOperationId(nextCoreTransactionId);
            blockStateOperation.AddBlockState(Protocol.CoreChainId, blockId, issuer, revision, nextCoreTransactionId);
            ++nextCoreTransactionId;

            foreach (var serviceBlockTransaction in _blockInfo.ServiceBlocks.Values)
            {
                blockStateOperation.AddBlockState(serviceBlockTransaction.ServiceBlock);
                coreTransactions.Add(serviceBlockTransaction);
            }

            var lastOperationHash = _lastBlock.Items.Last().Validation.Hash;
            var block             = new CoreBlock(blockId, issuer, revision, timestamp, NextAccountId, NextChainId, _lastBlock.BlockHash, lastOperationHash, coreOperations, coreTransactions);

            // return a copy of the block and all its transactions
            // if we don't do this, transaction ids might change during another generator run and we get invalid ids
            return(Block.Restore <CoreBlock>(block.BlockData));
        }
Пример #2
0
        public static GenesisBlockResult Generate(Base.Storage storage)
        {
            (var networkKeyStore, var networkKey, _) = LoadCoreAccountKey(storage, "Network Account", "Heleus Core");

            if (networkKeyStore == null)
            {
                networkKey = Key.Generate(KeyTypes.Ed25519);
                var networkPassword = Hex.ToString(Rand.NextSeed(32));
                networkKeyStore = new CoreAccountKeyStore("Heleus Core Network Account", CoreAccount.NetworkAccountId, networkKey, networkPassword);

                SaveCoreAccountKey(storage, networkKeyStore, "Network Account", networkPassword, "Heleus Core");
            }

            Console.WriteLine($"Heleus Core Network Account Key: {networkKey.PublicKey.HexString}.");

            var networkKeys      = new List <NetworkKey>();
            var networkChainKeys = new List <PublicChainKey>();

            for (var i = 0; i < 3; i++)
            {
                var keyFlags = GetCoreKeyFlags(i);
                var keyName  = $"Network {GetKeyName(keyFlags, true)}";

                (var store, var key, var storePassword) = LoadKeyStore <ChainKeyStore>(storage, null, keyName, "Heleus Core");

                if (store == null)
                {
                    key           = Key.Generate(KeyTypes.Ed25519);
                    storePassword = Hex.ToString(Rand.NextSeed(32));

                    var publicChainKey = new PublicChainKey(keyFlags, CoreChain.CoreChainId, 0, 0, (short)i, key);
                    store = new ChainKeyStore($"Heleus Core {keyName}", publicChainKey, key, storePassword);

                    SaveKeyStore(storage, store, storePassword, null, keyName, "Heleus Core");
                }

                Console.WriteLine($"Heleus Core {keyName}: {key.PublicKey.HexString}.");

                networkKeys.Add(new NetworkKey {
                    Key = key, Password = storePassword, PublicKey = store.PublicChainKey, Store = store
                });
                networkChainKeys.Add(store.PublicChainKey);
            }

            var timestamp = Time.Timestamp;

            timestamp = Protocol.GenesisTime;

            var coreOperations = new List <CoreOperation>();

            var endPoints = new List <string>();

            Console.WriteLine("Type the core chain name (default: Heleus Core)");
            var name = Program.IsDebugging ? "Heleus Core" : Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                name = "Heleus Core";
            }
            Console.WriteLine($"Chain name: {name}");

            Console.WriteLine("Type the core chain website (default: https://heleuscore.com)");
            var website = Program.IsDebugging ? "https://heleuscore.com" : Console.ReadLine();

            if (string.IsNullOrWhiteSpace(website))
            {
                website = "https://heleuscore.com";
            }
            Console.WriteLine($"Chain Website: {website}");

            Console.WriteLine("Type the core chain endpoints. (none for none, default: https://heleusnode.heleuscore.com)");
            while (true)
            {
                if (Program.IsDebugging)
                {
                    var ips = GetLocalIPV4Addresss();
                    foreach (var ip in ips)
                    {
                        endPoints.Add($"http://{ip}:54321/");
                    }
                    break;
                }

                var ep = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(ep))
                {
                    if (endPoints.Count == 0)
                    {
                        endPoints.Add("https://heleusnode.heleuscore.com");
                    }
                    break;
                }

                if (ep.IsValdiUrl(false))
                {
                    Console.WriteLine($"Added endpoint {ep}");
                    endPoints.Add(ep);
                }

                if (ep == "none")
                {
                    break;
                }
            }

            Console.WriteLine($"Chain Endpoints: {string.Join(", ", endPoints)}");

            var useGenesisInfo       = false;
            var useCoreChainEndpoint = false;

            if (storage.FileExists("genesisservices.txt"))
            {
                Console.WriteLine("Process genesisservices.txt [yes/no] (default: yes)");

                var p = Program.IsDebugging ? "yes" : Console.ReadLine();
                if (string.IsNullOrWhiteSpace(p) || p.ToLower() == "yes")
                {
                    Console.WriteLine("Processing genesisservices.txt");

                    useGenesisInfo = true;

                    if (endPoints.Count > 0)
                    {
                        Console.WriteLine("Use first core chain endpoint, not endpoint from genesisservices.txt [yes/no] (default: no)");

                        var p2 = Program.IsDebugging ? "yes" : Console.ReadLine()?.ToLower();

                        if (p2 == "yes")
                        {
                            useCoreChainEndpoint = true;
                            Console.WriteLine($"Using endpoint {endPoints[0]} for all services from genesisservices.txt");
                        }
                    }
                }
            }

            coreOperations.Add((ChainInfoOperation) new ChainInfoOperation(CoreChain.CoreChainId, CoreAccount.NetworkAccountId, name, website, timestamp, networkChainKeys, endPoints, new List <PurchaseInfo>()).UpdateOperationId(Operation.FirstTransactionId));
            coreOperations.Add((AccountOperation) new AccountOperation(CoreAccount.NetworkAccountId, networkKey.PublicKey, timestamp).UpdateOperationId(coreOperations.Count + 1)); // network account
            coreOperations.Add(((AccountUpdateOperation) new AccountUpdateOperation().UpdateOperationId(Operation.FirstTransactionId + 2)).AddAccount(CoreAccount.NetworkAccountId, coreOperations.Count + 1, long.MaxValue / 2).AddTransfer(CoreAccount.NetworkAccountId, CoreAccount.NetworkAccountId, long.MaxValue / 2, null, timestamp));

            var blockStateOperation = new BlockStateOperation();

            var nextChainId       = CoreChain.CoreChainId + 1;
            var nextTransactionId = coreOperations.Count + 1;
            var nextAccountId     = CoreAccount.NetworkAccountId + 1;

            var accounts = new List <AccountOperation>();
            var serviceTransactionsList = new Dictionary <int, List <ServiceTransaction> >();

            if (useGenesisInfo)
            {
                try
                {
                    var json = storage.ReadFileText("genesisservices.txt");
                    if (json != null)
                    {
                        var services = Newtonsoft.Json.JsonConvert.DeserializeObject <List <GenesisService> >(json);
                        if (services != null)
                        {
                            foreach (var service in services)
                            {
                                if (!service.Valid)
                                {
                                    continue;
                                }

                                var serviceKeys = new List <PublicChainKey>();
                                var ep          = useCoreChainEndpoint ? endPoints[0] : service.Endpoint;
                                var accountId   = service.AccountId;

                                if (accountId != CoreAccount.NetworkAccountId)
                                {
                                    var found = accounts.Find((a) => a.AccountId == accountId) != null;
                                    if (!found)
                                    {
                                        (var store, var key, var accountPassword) = LoadCoreAccountKey(storage, "Core Account " + accountId, "Core Accounts");
                                        if (store == null)
                                        {
                                            key             = Key.Generate(KeyTypes.Ed25519);
                                            accountPassword = Hex.ToString(Rand.NextSeed(32));
                                            store           = new CoreAccountKeyStore(service.AccountName, accountId, key, accountPassword);

                                            SaveCoreAccountKey(storage, store, "Core Account " + accountId, accountPassword, "Core Accounts");
                                        }

                                        if (accountId == nextAccountId)
                                        {
                                            var ao = new AccountOperation(accountId, key.PublicKey, timestamp);
                                            ao.UpdateOperationId(nextTransactionId);

                                            coreOperations.Add(ao);
                                            accounts.Add(ao);

                                            nextTransactionId++;
                                            nextAccountId++;
                                        }
                                        else
                                        {
                                            Console.WriteLine($"Invalid account id for {service.Title}, should be {nextAccountId}, but is {accountId}.");
                                            continue;
                                        }
                                    }
                                }

                                Console.WriteLine($"Adding Service {service.Title} with endpoint {ep}.");

                                var count = 2 + service.DataChainCount;
                                for (var i = 0; i < count; i++)
                                {
                                    var keyFlags  = GetServiceKeyFlags(i);
                                    var keyName   = $"{service.Name} {GetKeyName(keyFlags, false)}";
                                    var dataChain = i >= 2;
                                    if (dataChain)
                                    {
                                        keyName += $" (ChainIndex {i - 2})";
                                    }

                                    (var store, var key, var servicePassword) = LoadKeyStore <ChainKeyStore>(storage, null, keyName, service.Name);

                                    if (store == null)
                                    {
                                        key             = Key.Generate(KeyTypes.Ed25519);
                                        servicePassword = Hex.ToString(Rand.NextSeed(32));

                                        var signedKey = new PublicChainKey(keyFlags, nextChainId, dataChain ? (uint)i - 2 : 0, 0, (short)i, key);
                                        store = new ChainKeyStore($"{service.Name} {keyName}", signedKey, key, servicePassword);

                                        SaveKeyStore(storage, store, servicePassword, null, keyName, service.Name);
                                    }

                                    Console.WriteLine($"{service.Name} {keyName}: {key.PublicKey.HexString}.");

                                    serviceKeys.Add(store.PublicChainKey);
                                }

                                var pc = new ChainInfoOperation(nextChainId, accountId, service.Title, service.Website, timestamp, serviceKeys, new List <string> {
                                    ep
                                }, new List <PurchaseInfo>());
                                pc.UpdateOperationId(nextTransactionId);
                                coreOperations.Add(pc);

                                nextTransactionId++;

                                if (service.Revenue > 0)
                                {
                                    var rev = new ChainRevenueInfoOperation(nextChainId, service.Revenue, service.RevenueAccountFactor, timestamp);
                                    rev.UpdateOperationId(nextTransactionId);
                                    coreOperations.Add(rev);
                                    nextTransactionId++;
                                }

                                if (service.Accounts > 0)
                                {
                                    var serviceTransactions = new List <ServiceTransaction>();
                                    for (var i = 0; i < service.Accounts; i++)
                                    {
                                        (var accountStore, var accountKey, var accountPassword) = LoadCoreAccountKey(storage, "Core Account " + nextAccountId, "Core Accounts");
                                        if (accountStore == null)
                                        {
                                            accountKey      = Key.Generate(KeyTypes.Ed25519);
                                            accountPassword = Hex.ToString(Rand.NextSeed(32));
                                            accountStore    = new CoreAccountKeyStore($"Core Account {nextAccountId}", nextAccountId, accountKey, accountPassword);

                                            SaveCoreAccountKey(storage, accountStore, "Core Account " + nextAccountId, accountPassword, "Core Accounts");
                                        }

                                        (var serviceAccountStore, var serviceAccountKey, var serviceAccountPassword) = LoadKeyStore <ServiceAccountKeyStore>(storage, null, $"{service.Name} Service Account {nextAccountId}", $"{service.Name}/Service Accounts");
                                        if (serviceAccountStore == null)
                                        {
                                            serviceAccountKey      = Key.Generate(KeyTypes.Ed25519);
                                            serviceAccountPassword = Hex.ToString(Rand.NextSeed(32));

                                            var signedPublicKey = PublicServiceAccountKey.GenerateSignedPublicKey(nextAccountId, nextChainId, 0, 0, serviceAccountKey.PublicKey, accountKey);
                                            serviceAccountStore = new ServiceAccountKeyStore($"{service.Name} Service Account {nextAccountId}", signedPublicKey, serviceAccountKey, serviceAccountPassword);

                                            SaveKeyStore(storage, serviceAccountStore, serviceAccountPassword, null, $"{service.Name} Service Account {nextAccountId}", $"{service.Name}/Service Accounts");
                                        }

                                        var join = new JoinServiceTransaction(serviceAccountStore.SignedPublicKey)
                                        {
                                            SignKey = accountKey
                                        };
                                        join.ToArray();
                                        serviceTransactions.Add(join);

                                        var ao = new AccountOperation(nextAccountId, accountKey.PublicKey, timestamp);
                                        ao.UpdateOperationId(nextTransactionId);

                                        coreOperations.Add(ao);
                                        accounts.Add(ao);

                                        nextTransactionId++;
                                        nextAccountId++;
                                    }

                                    blockStateOperation.AddBlockState(nextChainId, Protocol.GenesisBlockId, 0, 0, serviceTransactions.Count);
                                    serviceTransactionsList[nextChainId] = serviceTransactions;
                                }

                                nextChainId++;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.HandleException(ex, LogLevels.Error);
                }
            }

            coreOperations.Add(blockStateOperation);
            blockStateOperation.UpdateOperationId(nextTransactionId);
            blockStateOperation.AddBlockState(Protocol.CoreChainId, Protocol.GenesisBlockId, Protocol.GenesisBlockNetworkKeyIssuer, 0, nextTransactionId);

            var block = new CoreBlock(Protocol.GenesisBlockId, Protocol.GenesisBlockNetworkKeyIssuer, 0, timestamp, nextAccountId, nextChainId, Hash.Empty(Protocol.TransactionHashType), Hash.Empty(ValidationOperation.ValidationHashType), coreOperations, new List <CoreTransaction>());

            var signatures = new BlockSignatures(block);

            signatures.AddSignature(block.Issuer, block, networkKey);

            return(new GenesisBlockResult(block, signatures, networkKey.PublicKey, networkKeys[1].Store, networkKeys[1].Password, serviceTransactionsList));
        }