Esempio n. 1
0
 public ProtocolHandler(LyraSystem system)
 {
     this.system = system;
     _log        = new SimpleLogger("ProtocolHandler").Logger;
     //this.knownHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
     //this.sentHashes = new FIFOSet<UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
 }
        public RemoteNode(LyraSystem system, object connection, IPEndPoint remote, IPEndPoint local)
            : base(connection, remote, local)
        {
            _log = new SimpleLogger("RemoteNode").Logger;

            this.system   = system;
            this.protocol = Context.ActorOf(ProtocolHandler.Props(system));
            LocalNode.Singleton.RemoteNodes.TryAdd(Self, this);

            while (BlockChain.Singleton == null)
            {
                Task.Delay(100).Wait();
            }

            var capabilities = new List <NodeCapability>
            {
                new FullNodeCapability(BlockChain.Singleton.Height)
            };

            if (LocalNode.Singleton.ListenerTcpPort > 0)
            {
                capabilities.Add(new ServerCapability(NodeCapabilityType.TcpServer, (ushort)LocalNode.Singleton.ListenerTcpPort));
            }
            if (LocalNode.Singleton.ListenerWsPort > 0)
            {
                capabilities.Add(new ServerCapability(NodeCapabilityType.WsServer, (ushort)LocalNode.Singleton.ListenerWsPort));
            }

            SendMessage(Message.Create(MessageCommand.Version, VersionPayload.Create(LocalNode.Nonce, LocalNode.UserAgent, capabilities.ToArray())));
        }
Esempio n. 3
0
        public LocalNode(LyraSystem system)
        {
            _log = new SimpleLogger("LocalNode").Logger;

            lock (lockObj)
            {
                if (singleton != null)
                {
                    throw new InvalidOperationException();
                }
                this.system = system;
                singleton   = this;

                // Start dns resolution in parallel

                for (int i = 0; i < ProtocolSettings.Default.SeedList.Length; i++)
                {
                    int index = i;
                    Task.Run(() => SeedList[index] = GetIpEndPoint(ProtocolSettings.Default.SeedList[index]));
                }
            }
        }
Esempio n. 4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _waitOrder = new AutoResetEvent(false);
            try
            {
                _log.LogInformation($"NodeService: ExecuteAsync Called.");

                // something must be initialized first
                new AuthorizersFactory().Init();

                var    walletStore = new LiteAccountDatabase();
                var    tmpWallet   = new Wallet(walletStore, Neo.Settings.Default.LyraNode.Lyra.NetworkId);
                string lyra_folder = BaseAccount.GetFullFolderName("Lyra-CLI-" + Neo.Settings.Default.LyraNode.Lyra.NetworkId);
                string full_path   = BaseAccount.GetFullPath(lyra_folder);
                tmpWallet.OpenAccount(full_path, Neo.Settings.Default.LyraNode.Lyra.Wallet.Name);

                if (ProtocolSettings.Default.StandbyValidators.Any(a => a == tmpWallet.AccountId))
                {
                    // not update balance for seed nodes.
                    PosWallet = tmpWallet;
                }
                else
                {
                    // create wallet and update balance
                    var memStor    = new AccountInMemoryStorage();
                    var acctWallet = new ExchangeAccountWallet(memStor, Neo.Settings.Default.LyraNode.Lyra.NetworkId);
                    acctWallet.AccountName = "tmpAcct";
                    await acctWallet.RestoreAccountAsync("", tmpWallet.PrivateKey);

                    acctWallet.OpenAccount("", acctWallet.AccountName);

                    Console.WriteLine("Sync wallet for " + acctWallet.AccountId);
                    var rpcClient = await LyraRestClient.CreateAsync(Neo.Settings.Default.LyraNode.Lyra.NetworkId, Environment.OSVersion.Platform.ToString(), "Lyra Client Cli", "1.0a");

                    await acctWallet.Sync(rpcClient);

                    PosWallet = acctWallet;
                }

                var sys = new LyraSystem();
                sys.Start();

                if (_db == null)
                {
                    //BsonSerializer.RegisterSerializer(typeof(decimal), new DecimalSerializer(BsonType.Decimal128));
                    //BsonSerializer.RegisterSerializer(typeof(decimal?), new NullableSerializer<decimal>(new DecimalSerializer(BsonType.Decimal128)));

                    client = new MongoClient(Neo.Settings.Default.LyraNode.Lyra.Database.DexDBConnect);
                    _db    = client.GetDatabase("Dex");

                    var exchangeAccounts = _db.GetCollection <ExchangeAccount>("exchangeAccounts");
                    var queue            = _db.GetCollection <ExchangeOrder>("queuedDexOrders");
                    var finished         = _db.GetCollection <ExchangeOrder>("finishedDexOrders");

                    // TODO: make it DI
                    Dealer             = new DealEngine(exchangeAccounts, queue, finished);
                    Dealer.OnNewOrder += (s, a) => _waitOrder.Set();
                }

                //_watcher = new ZooKeeperWatcher(_log);
                //await UsingZookeeper(_zkClusterOptions.ConnectionString, async (zk) => {
                //    // get Lyra network configurations from /lyra
                //    // {"mode":"permissioned","seeds":["node1","node2"]}
                //    var cfg = await zk.getDataAsync("/lyra");
                //    var runtimeConfig = JsonConvert.DeserializeObject<ConsensusRuntimeConfig>(Encoding.ASCII.GetString(cfg.Data));
                //    // do copy because the object is global
                //    _consensus.Mode = runtimeConfig.Mode;
                //    _consensus.Seeds = runtimeConfig.Seeds;
                //    _consensus.CurrentSeed = runtimeConfig.CurrentSeed;
                //    _consensus.PrimaryAuthorizerNodes = runtimeConfig.PrimaryAuthorizerNodes;
                //    _consensus.BackupAuthorizerNodes = runtimeConfig.BackupAuthorizerNodes;
                //    _consensus.VotingNodes = runtimeConfig.VotingNodes;

                //    _log.LogInformation($"NodeService: Got runtimeconfig success.");
                //});

                // all seeds do node election
                //if (_consensus.Seeds.Contains(Neo.Settings.Default.LyraNode.Orleans.EndPoint.AdvertisedIPAddress))
                //{
                //while(true)     // we do nothing without zk
                //{
                //    try
                //    {
                //        var electRoot = "/lyra/seedelect";
                //        var zk = new ZooKeeper(_zkClusterOptions.ConnectionString, ZOOKEEPER_CONNECTION_TIMEOUT, _watcher);
                //        var stat = await zk.existsAsync(electRoot);
                //        if (stat == null)
                //            await zk.createAsync(electRoot, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                //        _leader = new LeaderElectionSupport(zk, electRoot, Neo.Settings.Default.LyraNode.Orleans.EndPoint.AdvertisedIPAddress);

                //        _leader.addListener(this);
                //        await _leader.start();

                //        break;
                //    }
                //    catch(Exception ex)
                //    {
                //        _log.Fail(Orleans.ErrorCode.MembershipShutDownFailure, ex.Message);
                //        await Task.Delay(1000);
                //    }
                //}
                //}
            }
            catch (Exception ex)
            {
                throw new Exception("Error Initialize Node Service", ex);
            }

            while (!stoppingToken.IsCancellationRequested)
            {
                // do work
                if (_waitOrder.WaitOne(1000))
                {
                    _waitOrder.Reset();

                    await Dealer.MakeDealAsync();
                }
                else
                {
                    // no new order. do house keeping.
                }
            }
        }
Esempio n. 5
0
 public static Props Props(LyraSystem system)
 {
     return(Akka.Actor.Props.Create(() => new LocalNode(system)));
 }
Esempio n. 6
0
 public static Props Props(LyraSystem system)
 {
     return(Akka.Actor.Props.Create(() => new ProtocolHandler(system)).WithMailbox("protocol-handler-mailbox"));
 }
 internal static Props Props(LyraSystem system, object connection, IPEndPoint remote, IPEndPoint local)
 {
     return(Akka.Actor.Props.Create(() => new RemoteNode(system, connection, remote, local)).WithMailbox("remote-node-mailbox"));
 }