Beispiel #1
0
        private void SaveBlock(BlockChainEntity blockChainEntity)
        {
            this.Sender.Tell($"CREATING BLOCK CHAIN : Received message  from {this.Sender} saying ... '{blockChainEntity?.BlockChain?.LastOrDefault()?.Transaction}'");

            ValidateAndComputeCurrentHash(blockChainEntity.Key, blockChainEntity.BlockChain);
            Blocks.GetOrAdd(blockChainEntity.Key, blockChainEntity.BlockChain);
            this.UpdateAllMembers(Blocks);

            Console.WriteLine("[{0}]: {1}", this.Sender, blockChainEntity);
        }
Beispiel #2
0
        public static void StartMain(string Id)
        {
            Id += Guid.NewGuid().ToString().Replace("-", "");
            try
            {
                Console.WriteLine("Starting " + Id + " ...");
                using (var system = ActorSystem.Create("FamilyCluster", ConfigurationFactory.ParseString(AppConfiguration.GetAkkaConfiguration(Id))))
                {
                    var actor = system.ActorOf(Props.Create(() => new BlockProcessorActor(new BlockChainHandler())).WithRouter(FromConfig.Instance), Id);

                    while (true)
                    {
                        var message = Console.ReadLine();

                        var Blocks = BlockProcessorActor.Blocks;

                        BlockChainEntity mainMessage;
                        string           key;
                        List <DataBlock> blockChain;
                        if (Blocks.Count == 0)
                        {
                            var newChain = new KeyValuePair <string, List <DataBlock> >(Guid.NewGuid().ToString(), new List <DataBlock>());
                            Blocks.GetOrAdd(newChain.Key, newChain.Value);

                            key        = Guid.NewGuid().ToString();
                            blockChain = new List <DataBlock>();
                        }
                        else
                        {
                            var blockChainData = Blocks.First();
                            key        = blockChainData.Key;
                            blockChain = blockChainData.Value;
                        }

                        var newBlock = BlockProcessorActor.CreateNewBlock(blockChain, new BlockTransaction(100, BlockProcessorActor.DefaultCreditScore));

                        blockChain.Add(newBlock);

                        mainMessage = new BlockChainEntity(blockChain, key);
                        var result = actor.Ask <string>(mainMessage).Result;
                        Console.WriteLine($"RESULT FIRST : {result}");
                        Console.WriteLine($"CURRENT BLOCK : {Blocks.Count}");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadKey();

                throw;
            }
        }