Exemple #1
0
        internal static void LoadPlugins(TrustlinkSystem system)
        {
            System = system;
            if (!Directory.Exists(pluginsPath))
            {
                return;
            }
            foreach (string filename in Directory.EnumerateFiles(pluginsPath, "*.dll", SearchOption.TopDirectoryOnly))
            {
                var      file     = File.ReadAllBytes(filename);
                Assembly assembly = Assembly.Load(file);
                foreach (Type type in assembly.ExportedTypes)
                {
                    if (!type.IsSubclassOf(typeof(Plugin)))
                    {
                        continue;
                    }
                    if (type.IsAbstract)
                    {
                        continue;
                    }

                    ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
                    try
                    {
                        constructor?.Invoke(null);
                    }
                    catch (Exception ex)
                    {
                        Log(nameof(Plugin), LogLevel.Error, $"Failed to initialize plugin: {ex.Message}");
                    }
                }
            }
        }
Exemple #2
0
 public LocalNode(TrustlinkSystem system)
 {
     lock (lockObj)
     {
         if (singleton != null)
         {
             throw new InvalidOperationException();
         }
         this.system = system;
         singleton   = this;
     }
 }
Exemple #3
0
 public Blockchain(TrustlinkSystem system, Store store)
 {
     this.system  = system;
     this.MemPool = new MemoryPool(system, ProtocolSettings.Default.MemoryPoolMaxTransactions);
     this.Store   = store;
     lock (lockObj)
     {
         if (singleton != null)
         {
             throw new InvalidOperationException();
         }
         header_index.AddRange(store.GetHeaderHashList().Find().OrderBy(p => (uint)p.Key).SelectMany(p => p.Value.Hashes));
         stored_header_count += (uint)header_index.Count;
         if (stored_header_count == 0)
         {
             header_index.AddRange(store.GetBlocks().Find().OrderBy(p => p.Value.Index).Select(p => p.Key));
         }
         else
         {
             HashIndexState hashIndex = store.GetHeaderHashIndex().Get();
             if (hashIndex.Index >= stored_header_count)
             {
                 DataCache <UInt256, TrimmedBlock> cache = store.GetBlocks();
                 for (UInt256 hash = hashIndex.Hash; hash != header_index[(int)stored_header_count - 1];)
                 {
                     header_index.Insert((int)stored_header_count, hash);
                     hash = cache[hash].PrevHash;
                 }
             }
         }
         if (header_index.Count == 0)
         {
             Persist(GenesisBlock);
         }
         else
         {
             UpdateCurrentSnapshot();
             MemPool.LoadPolicy(currentSnapshot);
         }
         singleton = this;
     }
 }
Exemple #4
0
        public RemoteNode(TrustlinkSystem system, object connection, IPEndPoint remote, IPEndPoint local)
            : base(connection, remote, local)
        {
            this.system   = system;
            this.protocol = Context.ActorOf(ProtocolHandler.Props(system));
            LocalNode.Singleton.RemoteNodes.TryAdd(Self, this);

            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())));
        }
Exemple #5
0
 public TaskManager(TrustlinkSystem system)
 {
     this.system      = system;
     this.knownHashes = new FIFOSet <UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
 }
Exemple #6
0
 public static Props Props(TrustlinkSystem system)
 {
     return(Akka.Actor.Props.Create(() => new TaskManager(system)).WithMailbox("task-manager-mailbox"));
 }
Exemple #7
0
 public MemoryPool(TrustlinkSystem system, int capacity)
 {
     _system  = system;
     Capacity = capacity;
 }
Exemple #8
0
 public RpcServer(TrustlinkSystem system, Wallet wallet = null, long maxLinkInvoke = default)
 {
     this.system        = system;
     this.Wallet        = wallet;
     this.MaxLinkInvoke = maxLinkInvoke;
 }
Exemple #9
0
 public static Props Props(TrustlinkSystem system)
 {
     return(Akka.Actor.Props.Create(() => new ProtocolHandler(system)).WithMailbox("protocol-handler-mailbox"));
 }
Exemple #10
0
 public ProtocolHandler(TrustlinkSystem system)
 {
     this.system      = system;
     this.knownHashes = new FIFOSet <UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
     this.sentHashes  = new FIFOSet <UInt256>(Blockchain.Singleton.MemPool.Capacity * 2);
 }
Exemple #11
0
 public static Props Props(TrustlinkSystem system)
 {
     return(Akka.Actor.Props.Create(() => new LocalNode(system)));
 }
Exemple #12
0
 internal static Props Props(TrustlinkSystem system, object connection, IPEndPoint remote, IPEndPoint local)
 {
     return(Akka.Actor.Props.Create(() => new RemoteNode(system, connection, remote, local)).WithMailbox("remote-node-mailbox"));
 }
Exemple #13
0
 public static Props Props(TrustlinkSystem system, Store store)
 {
     return(Akka.Actor.Props.Create(() => new Blockchain(system, store)).WithMailbox("blockchain-mailbox"));
 }
Exemple #14
0
        protected internal override void OnStart(string[] args)
        {
            bool useRPC = false;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "/rpc":
                case "--rpc":
                case "-r":
                    useRPC = true;
                    break;

                case "/testnet":
                case "--testnet":
                case "-t":
                    ProtocolSettings.Initialize(new ConfigurationBuilder().AddJsonFile("protocol.testnet.json").Build());
                    Settings.Initialize(new ConfigurationBuilder().AddJsonFile("config.testnet.json").Build());
                    break;

                case "/mainnet":
                case "--mainnet":
                case "-m":
                    ProtocolSettings.Initialize(new ConfigurationBuilder().AddJsonFile("protocol.mainnet.json").Build());
                    Settings.Initialize(new ConfigurationBuilder().AddJsonFile("config.mainnet.json").Build());
                    break;
                }
            }
            store  = new LevelDBStore(Path.GetFullPath(Settings.Default.Paths.Chain));
            system = new TrustlinkSystem(store);
            system.StartNode(new ChannelsConfig
            {
                Tcp                      = new IPEndPoint(IPAddress.Any, Settings.Default.P2P.Port),
                WebSocket                = new IPEndPoint(IPAddress.Any, Settings.Default.P2P.WsPort),
                MinDesiredConnections    = Settings.Default.P2P.MinDesiredConnections,
                MaxConnections           = Settings.Default.P2P.MaxConnections,
                MaxConnectionsPerAddress = Settings.Default.P2P.MaxConnectionsPerAddress
            });
            if (Settings.Default.UnlockWallet.IsActive)
            {
                try
                {
                    Program.Wallet = OpenWallet(Settings.Default.UnlockWallet.Path, Settings.Default.UnlockWallet.Password);
                }
                catch (CryptographicException)
                {
                    Console.WriteLine($"failed to open file \"{Settings.Default.UnlockWallet.Path}\"");
                }
                if (Settings.Default.UnlockWallet.StartConsensus && Program.Wallet != null)
                {
                    OnStartConsensusCommand(null);
                }
            }
            if (useRPC)
            {
                system.StartRpc(Settings.Default.RPC.BindAddress,
                                Settings.Default.RPC.Port,
                                wallet: Program.Wallet,
                                sslCert: Settings.Default.RPC.SslCert,
                                password: Settings.Default.RPC.SslCertPassword,
                                maxLinkInvoke: Settings.Default.RPC.MaxLINKInvoke);
            }
        }