public TransactionOutputsService( ISpentOutputRepository spentOutputRepository, IBlockchainProvider blockchainProvider) { _spentOutputRepository = spentOutputRepository; _blockchainProvider = blockchainProvider; }
private static bool Transaction_GetReferences(IBlockchainProvider provider, ExecutionEngine engine) { var tx = GetInteropFromStack <Transaction>(engine); if (tx == null) { return(false); } var items = new List <StackItem>(); var references = new List <Transaction.Output>(); foreach (var input in tx.inputs) { var other_tx = provider.GetTransaction(input.prevHash); references.Add(other_tx.outputs[input.prevIndex]); } foreach (var reference in references) { items.Add(new VM.Types.InteropInterface(reference)); } var result = new VM.Types.Array(items.ToArray <StackItem>()); engine.CurrentContext.EvaluationStack.Push(result); return(true); }
public TransactionBuilder(ITransactionOutputsService transactionOutputsService, IBlockchainProvider blockchainProvider, FeeSettings feeSettings) { _transactionOutputsService = transactionOutputsService; _blockchainProvider = blockchainProvider; _feeSettings = feeSettings; }
public static void RegisterAPI(IBlockchainProvider provider, InteropService target) { target.Register("Neo.Output.GetScriptHash", engine => { var output = GetInteropFromStack <Transaction.Output>(engine); if (output == null) { return(false); } engine.CurrentContext.EvaluationStack.Push(output.scriptHash.ToArray()); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Output.GetValue", engine => { var output = GetInteropFromStack <Transaction.Output>(engine); if (output == null) { return(false); } engine.CurrentContext.EvaluationStack.Push(output.value.ToBigInteger()); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Output.GetAssetId", engine => { var output = GetInteropFromStack <Transaction.Output>(engine); if (output == null) { return(false); } engine.CurrentContext.EvaluationStack.Push(output.assetID); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Transaction.GetReferences", engine => Transaction_GetReferences(provider, engine), 0.2m); target.Register("Neo.Transaction.GetOutputs", Transaction_GetOutputs, InteropService.defaultGasCost); target.Register("Neo.Transaction.GetInputs", Transaction_GetInputs, InteropService.defaultGasCost); target.Register("Neo.Transaction.GetHash", engine => { var tx = GetInteropFromStack <Transaction>(engine); if (tx == null) { return(false); } engine.CurrentContext.EvaluationStack.Push(tx.Hash.ToArray()); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Blockchain.GetHeight", engine => { engine.CurrentContext.EvaluationStack.Push((new VM.Types.Integer(provider.GetBlockHeight()))); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Blockchain.GetHeader", engine => { Block block; var bytes = engine.CurrentContext.EvaluationStack.Pop().GetByteArray(); if (bytes.Length == 32) { var hash = new UInt256(bytes); block = provider.GetBlock(hash); } else { var height = new BigInteger(bytes); block = provider.GetBlock((uint)height); } engine.CurrentContext.EvaluationStack.Push((new VM.Types.InteropInterface(block))); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Header.GetConsensusData", engine => { var output = GetInteropFromStack <Block>(engine); if (output == null) { return(false); } engine.CurrentContext.EvaluationStack.Push(output.ConsensusData); return(true); }, InteropService.defaultGasCost); target.Register("Neo.Runtime.Serialize", engine => Runtime_Serialize(provider, engine), InteropService.defaultGasCost); target.Register("Neo.Runtime.Deserialize", engine => Runtime_Deserialize(provider, engine), InteropService.defaultGasCost); }
public WalletBalanceService(IObservableWalletRepository observableWalletRepository, IWalletBalanceRepository walletBalanceRepository, ITransactionOutputsService transactionOutputsService, IBlockchainProvider blockchainProvider, ILogFactory logFactory) { _observableWalletRepository = observableWalletRepository; _walletBalanceRepository = walletBalanceRepository; _transactionOutputsService = transactionOutputsService; _blockchainProvider = blockchainProvider; _log = logFactory.CreateLog(this); }
public TransactionBroadcaster(NeoSendRawTransaction neoRawTransactionSender, IUnconfirmedTransactionRepository unconfirmedTransactionRepository, IObservableOperationRepository observableOperationRepository, ITransactionOutputsService transactionOutputsService, IBlockchainProvider blockchainProvider) { _neoRawTransactionSender = neoRawTransactionSender; _unconfirmedTransactionRepository = unconfirmedTransactionRepository; _observableOperationRepository = observableOperationRepository; _transactionOutputsService = transactionOutputsService; _blockchainProvider = blockchainProvider; }
public ListenerVM(IBlockchainProvider provider) { this.provider = provider; VMAPI.RegisterAPI(provider, this); Register("Neo.Storage.GetContext", engine => { var hash = engine.CurrentContext.ScriptHash; engine.CurrentContext.EvaluationStack.Push((new VM.Types.InteropInterface(storage[hash]))); return(true); }, defaultGasCost); Register("Neo.Storage.Get", Storage_Get, 0.1m); Register("Neo.Storage.Put", Storage_Put, 0.1m); Register("Neo.Storage.Delete", Storage_Delete, 0.1m); Register("Neo.Runtime.GetTime", engine => { engine.CurrentContext.EvaluationStack.Push(currentBlock.Date.ToTimestamp()); return(true); }, defaultGasCost); Register("Neo.Runtime.GetTrigger", engine => { engine.CurrentContext.EvaluationStack.Push((int)TriggerType.Application); return(true); }, defaultGasCost); //Register("Neo.Runtime.Log", Runtime_Log, defaultGasCost); Register("Neo.Runtime.Notify", Runtime_Notify, defaultGasCost); }
private static bool Runtime_Serialize(IBlockchainProvider provider, ExecutionEngine engine) { using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { try { Serialization.SerializeStackItem(engine.CurrentContext.EvaluationStack.Pop(), writer); } catch (NotSupportedException) { return(false); } writer.Flush(); engine.CurrentContext.EvaluationStack.Push(ms.ToArray()); } return(true); }
public DetectTransactionsPeriodicalHandler(ILogFactory logFactory, TimeSpan timerPeriod, IUnconfirmedTransactionRepository unconfirmedTransactionRepository, IOperationRepository operationRepository, IWalletBalanceService walletBalanceService, IObservableOperationRepository observableOperationRepository, IBlockchainProvider blockchainProvider) { _unconfirmedTransactionRepository = unconfirmedTransactionRepository; _operationRepository = operationRepository; _walletBalanceService = walletBalanceService; _observableOperationRepository = observableOperationRepository; _blockchainProvider = blockchainProvider; _log = logFactory.CreateLog(this); _timerTrigger = new TimerTrigger(nameof(UpdateBalancesPeriodicalHandler), timerPeriod, logFactory); _timerTrigger.Triggered += (trigger, args, token) => Execute(); }
public SnapshotVM(IBlockchainProvider provider) { this.provider = provider; Register("Neo.Transaction.GetReferences", Transaction_GetReferences, 0.2m); Register("Neo.Transaction.GetOutputs", Transaction_GetOutputs, defaultGasCost); Register("Neo.Transaction.GetInputs", Transaction_GetInputs, defaultGasCost); Register("Neo.Transaction.GetHash", engine => { var tx = GetInteropFromStack <Transaction>(engine); if (tx == null) { return(false); } engine.EvaluationStack.Push(tx.Hash.ToArray()); return(true); }, defaultGasCost); Register("Neo.Output.GetScriptHash", engine => { var output = GetInteropFromStack <Transaction.Output>(engine); if (output == null) { return(false); } engine.EvaluationStack.Push(output.scriptHash.ToArray()); return(true); }, defaultGasCost); Register("Neo.Output.GetValue", engine => { var output = GetInteropFromStack <Transaction.Output>(engine); if (output == null) { return(false); } engine.EvaluationStack.Push(output.value.ToBigInteger()); return(true); }, defaultGasCost); Register("Neo.Output.GetAssetId", engine => { var output = GetInteropFromStack <Transaction.Output>(engine); if (output == null) { return(false); } engine.EvaluationStack.Push(output.assetID); return(true); }, defaultGasCost); Register("Neo.Storage.GetContext", engine => { var hash = engine.CurrentContext.ScriptHash; engine.EvaluationStack.Push((new VM.Types.InteropInterface(storage[hash]))); return(true); }, defaultGasCost); Register("Neo.Storage.Get", Storage_Get, 0.1m); Register("Neo.Storage.Put", Storage_Put, 0.1m); Register("Neo.Storage.Delete", Storage_Delete, 0.1m); Register("Neo.Runtime.GetTime", engine => { engine.EvaluationStack.Push(currentBlock.Date.ToTimestamp()); return(true); }, defaultGasCost); Register("Neo.Runtime.GetTrigger", engine => { engine.EvaluationStack.Push((int)TriggerType.Application); return(true); }, defaultGasCost); Register("Neo.Runtime.CheckWitness", Runtime_CheckWitness, 0.2m); Register("Neo.Runtime.Log", Runtime_Log, defaultGasCost); Register("Neo.Runtime.Notify", Runtime_Notify, defaultGasCost); }
private static bool Runtime_Deserialize(IBlockchainProvider provider, ExecutionEngine engine) { byte[] data = engine.CurrentContext.EvaluationStack.Pop().GetByteArray(); using (MemoryStream ms = new MemoryStream(data, false)) using (BinaryReader reader = new BinaryReader(ms)) { StackItem item; try { item = Serialization.DeserializeStackItem(reader); } catch (FormatException) { return(false); } catch (IOException) { return(false); } engine.CurrentContext.EvaluationStack.Push(item); } return(true); }
public static async Task Main(string[] args) { if (args.Length == 0) { Console.WriteLine("User Mode"); CreateHostBuilder(args).Build().Run(); } else { if (args.Length >= 1) { Port = int.Parse(args[0]); } if (Port > 0) { // inject DbContext and HashProvider to Program.cs var services = new ServiceCollection(); services.AddTransient <IHashProvider, HashProvider>(); services.AddTransient <IBlockchainProvider, BlockchainProvider>(); services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer("Server=localhost; Database=BlockchainCharity; Trusted_Connection=True; MultipleActiveResultSets=True;") , ServiceLifetime.Transient); var serviceProvider = services.BuildServiceProvider(); _dbContext = serviceProvider.GetService <ApplicationDbContext>(); _hash = serviceProvider.GetService <IHashProvider>(); _blockchain = serviceProvider.GetService <IBlockchainProvider>(); Client = new Client(_blockchain, _hash); Server = new Server(); Server.Start(); // construct signalr connection SignalRConnection = new HubConnectionBuilder() .WithUrl(new Uri("https://127.0.0.1:5001/donateHub"), options => { //bypass SSL on SignalR Client var handler = new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual, ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true }; options.HttpMessageHandlerFactory = _ => handler; options.WebSocketConfiguration = sockets => { sockets.RemoteCertificateValidationCallback = (sender, certificate, chain, policyErrors) => true; }; }) .Build(); // subscribe on method SendTransaction SignalRConnection.On <Transaction>("SendTransaction", async transaction => { await Validate(transaction); // await ChangeLeader(); Client.Broadcast($"Decrease Cooldown"); Client.InvokeVote(); await SignalRConnection.InvokeAsync("RemoveFromGroup", "Donate"); }); await SignalRConnection.StartAsync(); } await LoadChain(); Console.WriteLine("========================="); Console.WriteLine("1. Connect to a node"); Console.WriteLine("2. Be A Leader"); Console.WriteLine("3. Sync Number of Node"); Console.WriteLine("4. Vote for New Leader"); Console.WriteLine("5. Get All nodes that this node connects to"); Console.WriteLine("6. Get Current Leader"); Console.WriteLine("7. Get Cooldown"); Console.WriteLine("8. Search Transaction"); Console.WriteLine("9. Edit Transaction (For malicious node)"); Console.WriteLine("10. Get Chains"); Console.WriteLine("999. Exit"); Console.WriteLine("========================="); int selection = 0; while (selection != 999) { switch (selection) { case 1: Console.WriteLine("Please enter the port"); string port = Console.ReadLine(); Client.Connect($"{SocketUrl}{port}/Blockchain"); break; case 2: if (IsChainValid) { await Coup(); } else { Console.WriteLine("The chain is invalid...."); } break; case 3: NumNode = Client.GetServers().Count + 1; Console.WriteLine("Current Nodes are : " + NumNode); Client.Broadcast($"NumNode : {NumNode}"); DoneConnecting = true; break; case 4: Client.Broadcast($"Decrease Cooldown"); Client.InvokeVote(); await SignalRConnection.InvokeAsync("RemoveFromGroup", "Donate"); break; case 5: Console.WriteLine(JsonConvert.SerializeObject(Client.GetServers(), Formatting.Indented)); break; case 6: Console.WriteLine($"CurrentLeader : {CurrentLeader}"); break; case 7: Console.WriteLine($"CoolDown : {CoolDown}"); Console.WriteLine($"Penalty : {Penalty()}"); Console.WriteLine($"NumNode : {NumNode}"); break; case 8: Console.WriteLine("Please enter the transactionId"); string transactionId = Console.ReadLine(); var searchedTransaction = _dbContext.Transactions .FirstOrDefault(x => x.HashedTransactionId == transactionId); if (searchedTransaction == null) { Console.WriteLine("Transaction not found!"); } else { Console.WriteLine(JsonConvert.SerializeObject(searchedTransaction)); } break; case 9: Console.WriteLine("Please enter the transactionId"); string editedTransactionId = Console.ReadLine(); var transaction = _dbContext.Transactions .AsNoTracking() .FirstOrDefault(x => x.HashedTransactionId == editedTransactionId); if (transaction == null) { Console.WriteLine("Transaction not found!"); } else { var allTransactions = _dbContext.Transactions .Where(x => x.BlockHeight == transaction.BlockHeight) .AsNoTracking() .ToList(); transaction.Amount = 5000; transaction.HashedTransactionId = _hash.HashTransaction(transaction); foreach (var item in allTransactions) { if (item.id == transaction.id) { item.HashedTransactionId = transaction.HashedTransactionId; item.Amount = transaction.Amount; } } if (Program.Port == 2222) { var block = _dbContext.Blockchains1.FirstOrDefault(x => x.Height == transaction.BlockHeight); var hashedTransactions = String.Join('-', allTransactions.Select(x => x.HashedTransactionId)); block.HashedTransactionIds = hashedTransactions; block.Hash = _hash.HashBlock(block); } else if (Program.Port == 2223) { var block = _dbContext.Blockchains1.FirstOrDefault(x => x.Height == transaction.BlockHeight); var hashedTransactions = String.Join('-', allTransactions.Select(x => x.HashedTransactionId)); block.HashedTransactionIds = hashedTransactions; block.Hash = _hash.HashBlock(block); } else { var block = _dbContext.Blockchains1.FirstOrDefault(x => x.Height == transaction.BlockHeight); var hashedTransactions = String.Join('-', allTransactions.Select(x => x.HashedTransactionId)); block.HashedTransactionIds = hashedTransactions; block.Hash = _hash.HashBlock(block); } await _dbContext.SaveChangesAsync(); Console.WriteLine("Done Changing Transaction....."); } break; case 10: Console.WriteLine(JsonConvert.SerializeObject(Chain, Formatting.Indented)); break; } Console.WriteLine("Please select an action"); string action = Console.ReadLine(); await LoadChain(); selection = int.Parse(action); // if (DoneConnecting) // { // await Task.Delay(5000); if (IsDonatable) { await DonateToFoundationIfAble(); } // Console.WriteLine("FCK ASYNC"); // } } Client.Close(); await SignalRConnection.InvokeAsync("RemoveFromGroup", "Donate"); } // CreateHostBuilder(args).Build().Run(); }
// public Client() { } public Client(IBlockchainProvider blockchainProvider, IHashProvider hashProvider) { _blockchainProvider = blockchainProvider; _hashProvider = hashProvider; }