public OfflineNode(ProtocolSettings settings, RocksDbStorageProvider rocksDbStorageProvider, Wallet nodeWallet, ExpressChain chain, bool enableTrace) { this.nodeWallet = nodeWallet; this.chain = chain; this.rocksDbStorageProvider = rocksDbStorageProvider; applicationEngineProvider = enableTrace ? new ExpressApplicationEngineProvider() : null; consensusNodesKeys = new Lazy <KeyPair[]>(() => chain.ConsensusNodes .Select(n => n.Wallet.DefaultAccount ?? throw new Exception()) .Select(a => new KeyPair(a.PrivateKey.HexToBytes())) .ToArray()); var storageProviderPlugin = new StorageProviderPlugin(rocksDbStorageProvider); _ = new ExpressAppLogsPlugin(rocksDbStorageProvider); neoSystem = new NeoSystem(settings, storageProviderPlugin.Name); ApplicationEngine.Log += OnLog !; }
public void TestSetup() { // protect against external changes on TimeProvider TimeProvider.ResetToDefault(); NeoSystem TheNeoSystem = TestBlockchain.InitializeMockNeoSystem(); // Create a MemoryPool with capacity of 100 _unit = new MemoryPool(TheNeoSystem, 100); _unit.LoadPolicy(TestBlockchain.GetStore().GetSnapshot()); // Verify capacity equals the amount specified _unit.Capacity.ShouldBeEquivalentTo(100); _unit.VerifiedCount.ShouldBeEquivalentTo(0); _unit.UnVerifiedCount.ShouldBeEquivalentTo(0); _unit.Count.ShouldBeEquivalentTo(0); }
public static NeoSystem InitializeMockNeoSystem() { if (TheNeoSystem == null) { var mockSnapshot = new Mock <Snapshot>(); mockSnapshot.SetupGet(p => p.Blocks).Returns(new TestDataCache <UInt256, BlockState>()); mockSnapshot.SetupGet(p => p.Transactions).Returns(new TestDataCache <UInt256, TransactionState>()); mockSnapshot.SetupGet(p => p.UnspentCoins).Returns(new TestDataCache <UInt256, UnspentCoinState>()); mockSnapshot.SetupGet(p => p.Assets).Returns(new TestDataCache <UInt256, AssetState>()); mockSnapshot.SetupGet(p => p.Contracts).Returns(new TestDataCache <UInt160, ContractState>()); mockSnapshot.SetupGet(p => p.Storages).Returns(new TestDataCache <StorageKey, StorageItem>()); mockSnapshot.SetupGet(p => p.HeaderHashList).Returns(new TestDataCache <UInt32Wrapper, HeaderHashList>()); mockSnapshot.SetupGet(p => p.NextValidators).Returns(new TestMetaDataCache <NextValidatorsState>()); mockSnapshot.SetupGet(p => p.BlockHashIndex).Returns(new TestMetaDataCache <HashIndexState>()); mockSnapshot.SetupGet(p => p.HeaderHashIndex).Returns(new TestMetaDataCache <HashIndexState>()); _Store = new Mock <Store>(); var defaultTx = TestUtils.CreateRandomHashInvocationMockTransaction().Object; _Store.Setup(p => p.GetBlocks()).Returns(new TestDataCache <UInt256, BlockState>()); _Store.Setup(p => p.GetTransactions()).Returns(new TestDataCache <UInt256, TransactionState>( new TransactionState { BlockIndex = 1, Transaction = defaultTx })); _Store.Setup(p => p.GetUnspentCoins()).Returns(new TestDataCache <UInt256, UnspentCoinState>()); _Store.Setup(p => p.GetAssets()).Returns(new TestDataCache <UInt256, AssetState>()); _Store.Setup(p => p.GetContracts()).Returns(new TestDataCache <UInt160, ContractState>()); _Store.Setup(p => p.GetStorages()).Returns(new TestDataCache <StorageKey, StorageItem>()); _Store.Setup(p => p.GetHeaderHashList()).Returns(new TestDataCache <UInt32Wrapper, HeaderHashList>()); _Store.Setup(p => p.GetNextValidators()).Returns(new TestMetaDataCache <NextValidatorsState>()); _Store.Setup(p => p.GetBlockHashIndex()).Returns(new TestMetaDataCache <HashIndexState>()); _Store.Setup(p => p.GetHeaderHashIndex()).Returns(new TestMetaDataCache <HashIndexState>()); _Store.Setup(p => p.GetSnapshot()).Returns(mockSnapshot.Object); Console.WriteLine("initialize NeoSystem"); TheNeoSystem = new NeoSystem(_Store.Object); // new Mock<NeoSystem>(mockStore.Object); } return(TheNeoSystem); }
public Blockchain(NeoSystem system, IStore store) { this.system = system; this.MemPool = new MemoryPool(system, ProtocolSettings.Default.MemoryPoolMaxTransactions); this.Store = store; this.View = new ReadOnlyView(store); lock (lockObj) { if (singleton != null) { throw new InvalidOperationException(); } header_index.AddRange(View.HeaderHashList.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(View.Blocks.Find().OrderBy(p => p.Value.Index).Select(p => p.Key)); } else { HashIndexState hashIndex = View.HeaderHashIndex.Get(); if (hashIndex.Index >= stored_header_count) { DataCache <UInt256, TrimmedBlock> cache = View.Blocks; 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; } }
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; } } store = new LevelDBStore(Path.GetFullPath(Settings.Default.Paths.Chain)); system = new NeoSystem(store); system.StartNode(Settings.Default.P2P.Port, Settings.Default.P2P.WsPort, Settings.Default.P2P.MinDesiredConnections, Settings.Default.P2P.MaxConnections); if (Settings.Default.UnlockWallet.IsActive) { try { Program.Wallet = OpenWallet(GetIndexer(), 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, maxGasInvoke: Settings.Default.RPC.MaxGasInvoke); } }
public LocalNode(NeoSystem system) { 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])); } } }
public void Initialize() { system = TestBlockchain.TheNeoSystem; senderProbe = CreateTestProbe(); senderProbe.Send(system.Blockchain, new object()); senderProbe.ExpectNoMsg(); // Ensure blockchain it's created txSample = new Transaction() { Attributes = Array.Empty <TransactionAttribute>(), Script = Array.Empty <byte>(), Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }, Witnesses = Array.Empty <Witness>() }; system.MemPool.TryAdd(txSample, TestBlockchain.GetTestSnapshot()); }
public Blockchain(NeoSystem system, Store store) { this.system = system; this.MemPool = new MemoryPool(system, 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.TrimmedBlock.Index).Select(p => p.Key)); } else { HashIndexState hashIndex = store.GetHeaderHashIndex().Get(); if (hashIndex.Index >= stored_header_count) { DataCache <UInt256, BlockState> 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].TrimmedBlock.PrevHash; } } } if (header_index.Count == 0) { Persist(GenesisBlock); } else { UpdateCurrentSnapshot(); } singleton = this; } }
public static NeoSystem InitializeMockNeoSystem() { if (TheNeoSystem == null) { var mockSnapshot = new Mock <Snapshot>(); mockSnapshot.SetupGet(p => p.Blocks).Returns(new TestDataCache <UInt256, TrimmedBlock>()); mockSnapshot.SetupGet(p => p.Transactions).Returns(new TestDataCache <UInt256, TransactionState>()); mockSnapshot.SetupGet(p => p.Contracts).Returns(new TestDataCache <UInt160, ContractState>()); mockSnapshot.SetupGet(p => p.Storages).Returns(new TestDataCache <StorageKey, StorageItem>()); mockSnapshot.SetupGet(p => p.HeaderHashList).Returns(new TestDataCache <UInt32Wrapper, HeaderHashList>()); mockSnapshot.SetupGet(p => p.BlockHashIndex).Returns(new TestMetaDataCache <HashIndexState>()); mockSnapshot.SetupGet(p => p.HeaderHashIndex).Returns(new TestMetaDataCache <HashIndexState>()); _Store = new Mock <Store>(); var defaultTx = TestUtils.CreateRandomHashTransaction(); _Store.Setup(p => p.GetBlocks()).Returns(new TestDataCache <UInt256, TrimmedBlock>()); _Store.Setup(p => p.GetTransactions()).Returns(new TestDataCache <UInt256, TransactionState>( new TransactionState { BlockIndex = 1, Transaction = defaultTx })); _Store.Setup(p => p.GetContracts()).Returns(new TestDataCache <UInt160, ContractState>()); _Store.Setup(p => p.GetStorages()).Returns(new TestDataCache <StorageKey, StorageItem>()); _Store.Setup(p => p.GetHeaderHashList()).Returns(new TestDataCache <UInt32Wrapper, HeaderHashList>()); _Store.Setup(p => p.GetBlockHashIndex()).Returns(new TestMetaDataCache <HashIndexState>()); _Store.Setup(p => p.GetHeaderHashIndex()).Returns(new TestMetaDataCache <HashIndexState>()); _Store.Setup(p => p.GetSnapshot()).Returns(mockSnapshot.Object); Console.WriteLine("initialize NeoSystem"); TheNeoSystem = new NeoSystem(_Store.Object); // new Mock<NeoSystem>(mockStore.Object); // Ensure that blockchain is loaded var blockchain = Blockchain.Singleton; } return(TheNeoSystem); }
protected override void OnSystemLoaded(NeoSystem system) { if (system.Settings.Network != Settings.Default.Network) { return; } store = system.LoadStore(string.Format(Settings.Default.DBPath, Settings.Default.Network.ToString("X8"))); controller = new RosettaController(system, store); var dflt = Settings.Default; host = new WebHostBuilder().UseKestrel(options => options.Listen(dflt.BindAddress, dflt.Port, listenOptions => { // default is unlimited options.Limits.MaxConcurrentConnections = 50; // default is 2 minutes options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(1); // default is 30 seconds options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(15); if (string.IsNullOrEmpty(dflt.SslCert)) { return; } listenOptions.UseHttps(dflt.SslCert, dflt.SslCertPassword, httpsConnectionAdapterOptions => { if (dflt.TrustedAuthorities is null || dflt.TrustedAuthorities.Length == 0) { return; } httpsConnectionAdapterOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate; httpsConnectionAdapterOptions.ClientCertificateValidation = (cert, chain, err) => { if (err != SslPolicyErrors.None) { return(false); } X509Certificate2 authority = chain.ChainElements[chain.ChainElements.Count - 1].Certificate; return(dflt.TrustedAuthorities.Contains(authority.Thumbprint)); }; }); }))
public static IEnumerable <Nep17Contract> GetNep17Contracts(NeoSystem neoSystem, IStorageProvider storageProvider) { var scriptHashes = new HashSet <UInt160>(); foreach (var(_, _, notification) in GetNep17Transfers(storageProvider)) { scriptHashes.Add(notification.ScriptHash); } scriptHashes.Add(NativeContract.NEO.Hash); scriptHashes.Add(NativeContract.GAS.Hash); using var snapshot = neoSystem.GetSnapshot(); foreach (var scriptHash in scriptHashes) { if (Nep17Contract.TryLoad(neoSystem.Settings, snapshot, scriptHash, out var contract)) { yield return(contract); } } }
internal static void LoadPlugins(NeoSystem system) { System = system; if (!Directory.Exists(pluginsPath)) { return; } foreach (string filename in Directory.EnumerateFiles(pluginsPath, "*.dll", SearchOption.TopDirectoryOnly)) { Assembly assembly = Assembly.LoadFile(filename); foreach (Type type in assembly.ExportedTypes) { if (!type.IsSubclassOf(typeof(Plugin))) { continue; } if (type.IsAbstract) { continue; } ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes); if (constructor == null) { continue; } Plugin plugin; try { plugin = (Plugin)constructor.Invoke(null); } catch { continue; } plugin.Configure(); } } }
void IPersistencePlugin.OnPersist(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList <Blockchain.ApplicationExecuted> applicationExecutedList) { Header header = snapshot.GetCurrentHeader(); var analyzer = new BlockAnalyzer(snapshot, header, applicationExecutedList); analyzer.Analysis(); foreach (var analyzerResultInfo in analyzer.Result.ExecuteResultInfos) { _levelDb.SaveTxExecuteLog(analyzerResultInfo); } foreach (var analyzerAssetInfo in analyzer.Result.AssetInfos) { if (!_cachedAssets.Contains(analyzerAssetInfo.Key)) { _levelDb.SaveAssetInfo(analyzerAssetInfo.Value); _cachedAssets.Add(analyzerAssetInfo.Key); } } if (analyzer.Result.Transfers.NotEmpty()) { _levelDb.SaveTransfers(snapshot.GetHeight(), analyzer.Result.Transfers); } if (analyzer.Result.BalanceChangeAccounts.NotEmpty()) { _levelDb.UpdateBalancingAccounts(snapshot.GetHeight(), analyzer.Result.BalanceChangeAccounts); foreach (var item in analyzer.Result.BalanceChangeAccounts) { var balance = item.Account.GetBalanceOf(item.Asset, snapshot); _levelDb.UpdateBalance(item.Account, item.Asset, balance.Value, snapshot.GetHeight()); } } if (analyzer.Result.ContractChangeEvents.NotEmpty()) { _levelDb.SaveContractEvent(snapshot.GetHeight(), analyzer.Result.ContractChangeEvents); } }
protected override void OnSystemLoaded(NeoSystem system) { RpcServerSettings s = settings.Servers.FirstOrDefault(p => p.Network == system.Settings.Magic); if (s is null) { return; } RpcServer server = new RpcServer(system, s); if (handlers.Remove(s.Network, out var list)) { foreach (var handler in list) { server.RegisterMethods(handler); } } server.StartRpcServer(); servers.TryAdd(s.Network, server); }
internal static void LoadPlugins(NeoSystem system) { System = system; if (!Directory.Exists(PluginsDirectory)) { return; } List <Assembly> assemblies = new List <Assembly>(); foreach (string filename in Directory.EnumerateFiles(PluginsDirectory, "*.dll", SearchOption.TopDirectoryOnly)) { try { assemblies.Add(Assembly.Load(File.ReadAllBytes(filename))); } catch { } } foreach (Assembly assembly in assemblies) { LoadPlugin(assembly); } }
public void TestReVerifyTopUnverifiedTransactionsIfNeeded() { NeoSystem TheNeoSystem = TestBlockchain.InitializeMockNeoSystem(); var s = Blockchain.Singleton.Height; _unit = new MemoryPool(TheNeoSystem, 600); _unit.LoadPolicy(TestBlockchain.GetStore().GetSnapshot()); AddTransaction(CreateTransaction(100000001)); AddTransaction(CreateTransaction(100000001)); AddTransaction(CreateTransaction(100000001)); AddTransaction(CreateTransaction(1)); _unit.VerifiedCount.Should().Be(4); _unit.UnVerifiedCount.Should().Be(0); _unit.InvalidateVerifiedTransactions(); _unit.VerifiedCount.Should().Be(0); _unit.UnVerifiedCount.Should().Be(4); AddTransactions(511); // Max per block currently is 512 _unit.VerifiedCount.Should().Be(511); _unit.UnVerifiedCount.Should().Be(4); var result = _unit.ReVerifyTopUnverifiedTransactionsIfNeeded(1, Blockchain.Singleton.GetSnapshot()); result.Should().BeTrue(); _unit.VerifiedCount.Should().Be(512); _unit.UnVerifiedCount.Should().Be(3); result = _unit.ReVerifyTopUnverifiedTransactionsIfNeeded(2, Blockchain.Singleton.GetSnapshot()); result.Should().BeTrue(); _unit.VerifiedCount.Should().Be(514); _unit.UnVerifiedCount.Should().Be(1); result = _unit.ReVerifyTopUnverifiedTransactionsIfNeeded(3, Blockchain.Singleton.GetSnapshot()); result.Should().BeFalse(); _unit.VerifiedCount.Should().Be(515); _unit.UnVerifiedCount.Should().Be(0); }
public RemoteNode(NeoSystem 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()))); }
void IPersistencePlugin.OnPersist(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList <Blockchain.ApplicationExecuted> applicationExecutedList) { if (system.Settings.Network != Settings.Default.Network) { return; } ResetBatch(); //processing log for transactions foreach (var appExec in applicationExecutedList.Where(p => p.Transaction != null)) { var txJson = TxLogToJson(appExec); Put(appExec.Transaction.Hash.ToArray(), Neo.Utility.StrictUTF8.GetBytes(txJson.ToString())); } //processing log for block var blockJson = BlockLogToJson(block, applicationExecutedList); if (blockJson != null) { Put(block.Hash.ToArray(), Neo.Utility.StrictUTF8.GetBytes(blockJson.ToString())); } }
public TaskManager(NeoSystem system) { this.system = system; this.knownHashes = new HashSetCache <UInt256>(Blockchain.Singleton.MemPool.Capacity * 2 / 5); }
public static void TestSetup(TestContext ctx) { testBlockchain = TestBlockchain.TheNeoSystem; }
internal static Props Props(NeoSystem system) { return(Akka.Actor.Props.Create(() => new TransactionRouter(system)).WithRouter(new SmallestMailboxPool(Environment.ProcessorCount))); }
public TransactionRouter(NeoSystem system) { this.system = system; }
public TaskManager(NeoSystem system) { this.system = system; }
public static Props Props(NeoSystem system) { return(Akka.Actor.Props.Create(() => new TaskManager(system)).WithMailbox("task-manager-mailbox")); }
public static Props Props(NeoSystem system, IStore store) { return(Akka.Actor.Props.Create(() => new Blockchain(system, store)).WithMailbox("blockchain-mailbox")); }
public static Props Props(NeoSystem system) { return(Akka.Actor.Props.Create(() => new LocalNode(system))); }
public void Initialize() { system = TestBlockchain.TheNeoSystem; Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample); }
internal static Props Props(NeoSystem system, object connection, IPEndPoint remote, IPEndPoint local) { return(Akka.Actor.Props.Create(() => new RemoteNode(system, connection, remote, local)).WithMailbox("remote-node-mailbox")); }
public RpcServer(NeoSystem system, Wallet wallet = null, Fixed8 maxGasInvoke = default(Fixed8)) { this.system = system; this.wallet = wallet; this.maxGasInvoke = maxGasInvoke; }
public MemoryPool(NeoSystem system, int capacity) { _system = system; Capacity = capacity; LoadMaxTxLimitsFromPolicyPlugins(); }