private void OnInvMessageReceived(InvPayload payload) { UInt256[] hashes = payload.Hashes.Where(p => knownHashes.Add(p)).ToArray(); if (hashes.Length == 0) { return; } switch (payload.Type) { case InventoryType.Block: using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) hashes = hashes.Where(p => !snapshot.ContainsBlock(p)).ToArray(); break; case InventoryType.Tx: using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) hashes = hashes.Where(p => !snapshot.ContainsTransaction(p)).ToArray(); break; } if (hashes.Length == 0) { return; } system.TaskManager.Tell(new TaskManager.NewTasks { Payload = InvPayload.Create(payload.Type, hashes) }, Context.Parent); }
public Fixed8 AvailableBonus() { using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { return(snapshot.CalculateBonus(currentWallet.GetUnclaimedCoins().Select(p => p.Reference))); } }
public ClaimTransaction Claim() { if (this.AvailableBonus() == Fixed8.Zero) { Console.WriteLine($"no gas to claim"); return(null); } CoinReference[] claims = currentWallet.GetUnclaimedCoins().Select(p => p.Reference).ToArray(); if (claims.Length == 0) { return(null); } using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { ClaimTransaction tx = new ClaimTransaction { Claims = claims, Attributes = new TransactionAttribute[0], Inputs = new CoinReference[0], Outputs = new[] { new TransactionOutput { AssetId = Blockchain.UtilityToken.Hash, Value = snapshot.CalculateBonus(claims), ScriptHash = currentWallet.GetChangeAddress() } } }; return((ClaimTransaction)SignTransaction(tx)); } }
public void Reset(Wallet wallet) { Snapshot?.Dispose(); Snapshot = Blockchain.Singleton.GetSnapshot(); State = ConsensusState.Initial; PrevHash = Snapshot.CurrentBlockHash; BlockIndex = Snapshot.Height + 1; ViewNumber = 0; Validators = Snapshot.GetValidators(); MyIndex = -1; PrimaryIndex = BlockIndex % (uint)Validators.Length; TransactionHashes = null; Signatures = new byte[Validators.Length][]; ExpectedView = new byte[Validators.Length]; KeyPair = null; for (int i = 0; i < Validators.Length; i++) { WalletAccount account = wallet.GetAccount(Validators[i]); if (account?.HasKey == true) { MyIndex = i; KeyPair = account.GetKey(); break; } } _header = null; }
public static ApplicationEngine Run(byte[] script, IScriptContainer container = null, Block persisting_block = null, bool testMode = false) { using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { snapshot.PersistingBlock = persisting_block ?? new Block { Version = 0, PrevHash = snapshot.CurrentBlockHash, MerkleRoot = new UInt256(), Timestamp = snapshot.Blocks[snapshot.CurrentBlockHash].TrimmedBlock.Timestamp + Blockchain.SecondsPerBlock, Index = snapshot.Height + 1, ConsensusData = 0, NextConsensus = snapshot.Blocks[snapshot.CurrentBlockHash].TrimmedBlock.NextConsensus, Witness = new Witness { InvocationScript = new byte[0], VerificationScript = new byte[0] }, Transactions = new Transaction[0] }; ApplicationEngine engine = new ApplicationEngine(TriggerType.Application, container, snapshot, Fixed8.Zero, testMode); engine.LoadScript(script); engine.Execute(); return(engine); } }
public ApplicationEngine(TriggerType trigger, IScriptContainer container, Snapshot snapshot, Fixed8 gas, bool testMode = false) : base(container, Cryptography.Crypto.Default, snapshot, new TrustEDUService(trigger, snapshot)) { this.gas_amount = gas_free + gas.GetData(); this.testMode = testMode; this.snapshot = snapshot; }
public override UInt160[] GetScriptHashesForVerifying(Snapshot snapshot) { HashSet <UInt160> hashes = new HashSet <UInt160>(base.GetScriptHashesForVerifying(snapshot)); foreach (TransactionResult result in GetTransactionResults().Where(p => p.Amount < Fixed8.Zero)) { AssetState asset = snapshot.Assets.TryGet(result.AssetId); if (asset == null) { throw new InvalidOperationException(); } hashes.Add(asset.Issuer); } return(hashes.OrderBy(p => p).ToArray()); }
public Fixed8 UnavailableBonus() { using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { uint height = snapshot.Height + 1; Fixed8 unavailable; try { unavailable = snapshot.CalculateBonus(currentWallet.FindUnspentCoins().Where(p => p.Output.AssetId.Equals(Blockchain.GoverningToken.Hash)).Select(p => p.Reference), height); } catch (Exception) { unavailable = Fixed8.Zero; } return(unavailable); } }
private bool Transaction_GetUnspentCoins(ExecutionEngine engine) { if (engine.CurrentContext.EvaluationStack.Pop() is InteropContract _interface) { Transaction tx = _interface.GetInterface <Transaction>(); if (tx == null) { return(false); } TransactionOutput[] outputs = Snapshot.GetUnspent(tx.Hash).ToArray(); if (outputs.Length > ApplicationEngine.MaxArraySize) { return(false); } engine.CurrentContext.EvaluationStack.Push(outputs.Select(p => StackItem.FromInterface(p)).ToArray()); return(true); } return(false); }
public override bool Verify(Snapshot snapshot, IEnumerable <Transaction> mempool) { if (!base.Verify(snapshot, mempool)) { return(false); } TransactionResult[] results = GetTransactionResults()?.Where(p => p.Amount < Fixed8.Zero).ToArray(); if (results == null) { return(false); } foreach (TransactionResult r in results) { AssetState asset = snapshot.Assets.TryGet(r.AssetId); if (asset == null) { return(false); } if (asset.Amount < Fixed8.Zero) { continue; } Fixed8 quantityIssued = asset.Available + mempool.OfType <IssueTransaction>() .Where(p => p != this) .SelectMany(p => p.Outputs) .Where(p => p.AssetId == r.AssetId) .Sum(p => p.Value); if (asset.Amount - quantityIssued < -r.Amount) { return(false); } } return(true); }
public TrustEDUService(TriggerType trigger, Snapshot snapshot) : base(trigger, snapshot) { Register("TrustEDU.Runtime.GetTrigger", Runtime_GetTrigger); Register("TrustEDU.Runtime.CheckWitness", Runtime_CheckWitness); Register("TrustEDU.Runtime.Notify", Runtime_Notify); Register("TrustEDU.Runtime.Log", Runtime_Log); Register("TrustEDU.Runtime.GetTime", Runtime_GetTime); Register("TrustEDU.Runtime.Serialize", Runtime_Serialize); Register("TrustEDU.Runtime.Deserialize", Runtime_Deserialize); Register("TrustEDU.Blockchain.GetHeight", Blockchain_GetHeight); Register("TrustEDU.Blockchain.GetHeader", Blockchain_GetHeader); Register("TrustEDU.Blockchain.GetBlock", Blockchain_GetBlock); Register("TrustEDU.Blockchain.GetTransaction", Blockchain_GetTransaction); Register("TrustEDU.Blockchain.GetTransactionHeight", Blockchain_GetTransactionHeight); Register("TrustEDU.Blockchain.GetAccount", Blockchain_GetAccount); Register("TrustEDU.Blockchain.GetValidators", Blockchain_GetValidators); Register("TrustEDU.Blockchain.GetAsset", Blockchain_GetAsset); Register("TrustEDU.Blockchain.GetContract", Blockchain_GetContract); Register("TrustEDU.Header.GetHash", Header_GetHash); Register("TrustEDU.Header.GetVersion", Header_GetVersion); Register("TrustEDU.Header.GetPrevHash", Header_GetPrevHash); Register("TrustEDU.Header.GetMerkleRoot", Header_GetMerkleRoot); Register("TrustEDU.Header.GetTimestamp", Header_GetTimestamp); Register("TrustEDU.Header.GetIndex", Header_GetIndex); Register("TrustEDU.Header.GetConsensusData", Header_GetConsensusData); Register("TrustEDU.Header.GetNextConsensus", Header_GetNextConsensus); Register("TrustEDU.Block.GetTransactionCount", Block_GetTransactionCount); Register("TrustEDU.Block.GetTransactions", Block_GetTransactions); Register("TrustEDU.Block.GetTransaction", Block_GetTransaction); Register("TrustEDU.Transaction.GetHash", Transaction_GetHash); Register("TrustEDU.Transaction.GetType", Transaction_GetType); Register("TrustEDU.Transaction.GetAttributes", Transaction_GetAttributes); Register("TrustEDU.Transaction.GetInputs", Transaction_GetInputs); Register("TrustEDU.Transaction.GetOutputs", Transaction_GetOutputs); Register("TrustEDU.Transaction.GetReferences", Transaction_GetReferences); Register("TrustEDU.Transaction.GetUnspentCoins", Transaction_GetUnspentCoins); Register("TrustEDU.InvocationTransaction.GetScript", InvocationTransaction_GetScript); Register("TrustEDU.Attribute.GetUsage", Attribute_GetUsage); Register("TrustEDU.Attribute.GetData", Attribute_GetData); Register("TrustEDU.Input.GetHash", Input_GetHash); Register("TrustEDU.Input.GetIndex", Input_GetIndex); Register("TrustEDU.Output.GetAssetId", Output_GetAssetId); Register("TrustEDU.Output.GetValue", Output_GetValue); Register("TrustEDU.Output.GetScriptHash", Output_GetScriptHash); Register("TrustEDU.Account.GetScriptHash", Account_GetScriptHash); Register("TrustEDU.Account.GetVotes", Account_GetVotes); Register("TrustEDU.Account.GetBalance", Account_GetBalance); Register("TrustEDU.Asset.Create", Asset_Create); Register("TrustEDU.Asset.Renew", Asset_Renew); Register("TrustEDU.Asset.GetAssetId", Asset_GetAssetId); Register("TrustEDU.Asset.GetAssetType", Asset_GetAssetType); Register("TrustEDU.Asset.GetAmount", Asset_GetAmount); Register("TrustEDU.Asset.GetAvailable", Asset_GetAvailable); Register("TrustEDU.Asset.GetPrecision", Asset_GetPrecision); Register("TrustEDU.Asset.GetOwner", Asset_GetOwner); Register("TrustEDU.Asset.GetAdmin", Asset_GetAdmin); Register("TrustEDU.Asset.GetIssuer", Asset_GetIssuer); Register("TrustEDU.Contract.Create", Contract_Create); Register("TrustEDU.Contract.Migrate", Contract_Migrate); Register("TrustEDU.Contract.Destroy", Contract_Destroy); Register("TrustEDU.Contract.GetScript", Contract_GetScript); Register("TrustEDU.Contract.IsPayable", Contract_IsPayable); Register("TrustEDU.Contract.GetStorageContext", Contract_GetStorageContext); Register("TrustEDU.Storage.GetContext", Storage_GetContext); Register("TrustEDU.Storage.GetReadOnlyContext", Storage_GetReadOnlyContext); Register("TrustEDU.Storage.Get", Storage_Get); Register("TrustEDU.Storage.Put", Storage_Put); Register("TrustEDU.Storage.Delete", Storage_Delete); Register("TrustEDU.Storage.Find", Storage_Find); Register("TrustEDU.StorageContext.AsReadOnly", StorageContext_AsReadOnly); Register("TrustEDU.Enumerator.Create", Enumerator_Create); Register("TrustEDU.Enumerator.Next", Enumerator_Next); Register("TrustEDU.Enumerator.Value", Enumerator_Value); Register("TrustEDU.Enumerator.Concat", Enumerator_Concat); Register("TrustEDU.Iterator.Create", Iterator_Create); Register("TrustEDU.Iterator.Key", Iterator_Key); Register("TrustEDU.Iterator.Keys", Iterator_Keys); Register("TrustEDU.Iterator.Values", Iterator_Values); Register("TrustEDU.Iterator.Next", Enumerator_Next); Register("TrustEDU.Iterator.Value", Enumerator_Value); Register("AntShares.Runtime.CheckWitness", Runtime_CheckWitness); Register("AntShares.Runtime.Notify", Runtime_Notify); Register("AntShares.Runtime.Log", Runtime_Log); Register("AntShares.Blockchain.GetHeight", Blockchain_GetHeight); Register("AntShares.Blockchain.GetHeader", Blockchain_GetHeader); Register("AntShares.Blockchain.GetBlock", Blockchain_GetBlock); Register("AntShares.Blockchain.GetTransaction", Blockchain_GetTransaction); Register("AntShares.Blockchain.GetAccount", Blockchain_GetAccount); Register("AntShares.Blockchain.GetValidators", Blockchain_GetValidators); Register("AntShares.Blockchain.GetAsset", Blockchain_GetAsset); Register("AntShares.Blockchain.GetContract", Blockchain_GetContract); Register("AntShares.Header.GetHash", Header_GetHash); Register("AntShares.Header.GetVersion", Header_GetVersion); Register("AntShares.Header.GetPrevHash", Header_GetPrevHash); Register("AntShares.Header.GetMerkleRoot", Header_GetMerkleRoot); Register("AntShares.Header.GetTimestamp", Header_GetTimestamp); Register("AntShares.Header.GetConsensusData", Header_GetConsensusData); Register("AntShares.Header.GetNextConsensus", Header_GetNextConsensus); Register("AntShares.Block.GetTransactionCount", Block_GetTransactionCount); Register("AntShares.Block.GetTransactions", Block_GetTransactions); Register("AntShares.Block.GetTransaction", Block_GetTransaction); Register("AntShares.Transaction.GetHash", Transaction_GetHash); Register("AntShares.Transaction.GetType", Transaction_GetType); Register("AntShares.Transaction.GetAttributes", Transaction_GetAttributes); Register("AntShares.Transaction.GetInputs", Transaction_GetInputs); Register("AntShares.Transaction.GetOutputs", Transaction_GetOutputs); Register("AntShares.Transaction.GetReferences", Transaction_GetReferences); Register("AntShares.Attribute.GetUsage", Attribute_GetUsage); Register("AntShares.Attribute.GetData", Attribute_GetData); Register("AntShares.Input.GetHash", Input_GetHash); Register("AntShares.Input.GetIndex", Input_GetIndex); Register("AntShares.Output.GetAssetId", Output_GetAssetId); Register("AntShares.Output.GetValue", Output_GetValue); Register("AntShares.Output.GetScriptHash", Output_GetScriptHash); Register("AntShares.Account.GetScriptHash", Account_GetScriptHash); Register("AntShares.Account.GetVotes", Account_GetVotes); Register("AntShares.Account.GetBalance", Account_GetBalance); Register("AntShares.Asset.Create", Asset_Create); Register("AntShares.Asset.Renew", Asset_Renew); Register("AntShares.Asset.GetAssetId", Asset_GetAssetId); Register("AntShares.Asset.GetAssetType", Asset_GetAssetType); Register("AntShares.Asset.GetAmount", Asset_GetAmount); Register("AntShares.Asset.GetAvailable", Asset_GetAvailable); Register("AntShares.Asset.GetPrecision", Asset_GetPrecision); Register("AntShares.Asset.GetOwner", Asset_GetOwner); Register("AntShares.Asset.GetAdmin", Asset_GetAdmin); Register("AntShares.Asset.GetIssuer", Asset_GetIssuer); Register("AntShares.Contract.Create", Contract_Create); Register("AntShares.Contract.Migrate", Contract_Migrate); Register("AntShares.Contract.Destroy", Contract_Destroy); Register("AntShares.Contract.GetScript", Contract_GetScript); Register("AntShares.Contract.GetStorageContext", Contract_GetStorageContext); Register("AntShares.Storage.GetContext", Storage_GetContext); Register("AntShares.Storage.Get", Storage_Get); Register("AntShares.Storage.Put", Storage_Put); Register("AntShares.Storage.Delete", Storage_Delete); }
private bool Blockchain_GetValidators(ExecutionEngine engine) { ECPoint[] validators = Snapshot.GetValidators(); engine.CurrentContext.EvaluationStack.Push(validators.Select(p => (StackItem)p.EncodePoint(true)).ToArray()); return(true); }