private byte GetTokenID(IEnumerable <KeyValuePair <UInt256, AssetState> > r, UInt256 assetId, DataCache <UInt256, TransactionState> transactionsCache) { if (assetId.Equals(Blockchain.UtilityToken.Hash)) { return(0); } if (assetId.Equals(Blockchain.GoverningToken.Hash)) { return(1); } var asset = r.Where(x => x.Key.Equals(assetId)).Select(x => x.Value).FirstOrDefault(); if (asset == null) { return(255); } if (!EnsureCreatedTokenIdMap(r, asset, assetId, transactionsCache)) { return(255); } return(_dicTokenIds[assetId]); }
public void TestEquals2() { UInt256 temp1 = new UInt256(); object temp2 = null; object temp3 = new object(); Assert.AreEqual(false, temp1.Equals(temp2)); Assert.AreEqual(false, temp1.Equals(temp3)); }
public void TestEquals1() { UInt256 temp1 = new UInt256(); UInt256 temp2 = new UInt256(); UInt160 temp3 = new UInt160(); Assert.AreEqual(false, temp1.Equals(null)); Assert.AreEqual(true, temp1.Equals(temp1)); Assert.AreEqual(true, temp1.Equals(temp2)); Assert.AreEqual(false, temp1.Equals(temp3)); }
/// <summary> /// Check if is equal to other /// </summary> /// <param name="other">Other</param> /// <returns>Return true if equal</returns> public bool Equals(TransactionOutput other) { if (ReferenceEquals(this, other)) { return(true); } if (other is null) { return(false); } return(AssetId.Equals(other.AssetId) && Value.Equals(other.Value) && ScriptHash.Equals(other.ScriptHash)); }
/// <summary> /// Check if is equal to other /// </summary> /// <param name="other">Other</param> /// <returns>Return true if equal</returns> public bool Equals(CoinReference other) { if (ReferenceEquals(this, other)) { return(true); } if (other is null) { return(false); } return(PrevHash.Equals(other.PrevHash) && PrevIndex.Equals(other.PrevIndex)); }
public async Task UpdateBalance(UInt160 hash, UInt256 assetId, Fixed8 delta) { var account = await Get(hash) ?? new Account(hash); if (account.Balances.ContainsKey(assetId)) { account.Balances[assetId] += delta; } else { account.Balances[assetId] = delta; } await _repository.AddAccount(account); if (assetId.Equals(GenesisAssets.GoverningTokenRegisterTransaction.Hash) && account.Votes?.Length > 0) { foreach (var pubKey in account.Votes) { await UpdateValidatorVote(pubKey, delta); } } // TODO: Check if we need to store validatorCount because existing implementation has it // validators_count.GetAndChange().Votes[account.Votes.Length - 1] += output.Value; }
public void Can_be_equal_to_another_number_of_object_type() { var a = new UInt256(new byte[] { 157, 179, 60, 8, 66, 122, 255, 105, 126, 49, 180, 74, 212, 41, 126, 177, 14, 255, 59, 82, 218, 113, 248, 145, 98, 5, 128, 140, 42, 70, 32, 69 }); var b = (object)UInt256.Parse("0x4520462a8c80056291f871da523bff0eb17e29d44ab4317e69ff7a42083cb39d"); a.Equals(b).Should().BeTrue(); }
public void Can_be_not_equal_to_another_number_of_object_type() { var a = new UInt256( 0x0807060504030201, 0x100f0e0d0c0b0a09, 0x1817161514131211, 0x201f1e1d1c1b1a19); object o = (object)1; a.Equals(o).Should().BeFalse(); }
public FirstClassAssetItem GetFirstClassAsset(UInt256 assetId) { if (assetId == null) { return(null); } return(this.assets.FirstOrDefault(asset => asset is FirstClassAssetItem && assetId.Equals(((FirstClassAssetItem)asset).AssetId)) as FirstClassAssetItem); }
public void TestEquals() { byte[] temp = new byte[32]; temp[31] = 0x01; UInt256 result = new UInt256(temp); Assert.AreEqual(true, UInt256.Zero.Equals(UInt256.Zero)); Assert.AreEqual(false, UInt256.Zero.Equals(result)); Assert.AreEqual(false, result.Equals(null)); }
public void Can_TryParse() { var a = new UInt256( 0x0807060504030201, 0x100f0e0d0c0b0a09, 0x1817161514131211, 0x201f1e1d1c1b1a18); const string @string = "0x201f1e1d1c1b1a181817161514131211100f0e0d0c0b0a090807060504030201"; UInt256.TryParse(@string, out var b).Should().BeTrue(); a.Equals(b).Should().BeTrue(); }
public bool Equals(TransactionInput other) { if (ReferenceEquals(this, other)) { return(true); } if (ReferenceEquals(null, other)) { return(false); } return(PrevHash.Equals(other.PrevHash) && PrevIndex.Equals(other.PrevIndex)); }
private bool EnsureCreatedTokenIdMap( IEnumerable <KeyValuePair <UInt256, AssetState> > assets, AssetState r, UInt256 id, DataCache <UInt256, TransactionState> transactionsCache) { if (_dicTokenIds.ContainsKey(id)) { return(true); } Dictionary <string, UInt256> L = new Dictionary <string, UInt256>(); assets = assets.Where(x => (!x.Key.Equals(Blockchain.GoverningToken.Hash)) && (!x.Key.Equals(Blockchain.UtilityToken.Hash))); // Logger.Info($"== Preparing assets: {assets.Count()}"); foreach (var s in assets) { // Logger.Info($"==== {s.Value.GetName()} {s.Key.ToString()}"); var tx = transactionsCache.TryGet(s.Value.AssetId); if (tx != null) { var f = tx.BlockIndex.ToString() + s.Value.AssetId.ToString(); L[f] = s.Value.AssetId; } } var LKS = L.Keys.ToList(); LKS.Sort(); byte baseTokenID = 2; foreach (var k in LKS) { var aid = L[k]; if (baseTokenID > 254) { return(false); // still only 255 token IDs available } if (id.Equals(aid)) { // Logger.Info($"=== Adding {aid.ToString()} with id = {baseTokenID}"); _dicTokenIds[aid] = baseTokenID; return(true); } baseTokenID++; } return(false); }
/// <summary> /// 比较当前对象与指定对象是否相等 /// </summary> /// <param name="other">要比较的对象</param> /// <returns>返回对象是否相等</returns> public bool Equals(CoinReference other) { TR.Enter(); if (ReferenceEquals(this, other)) { return(TR.Exit(true)); } if (ReferenceEquals(null, other)) { return(TR.Exit(false)); } return(TR.Exit(PrevHash.Equals(other.PrevHash) && PrevIndex.Equals(other.PrevIndex))); }
public void TestUInt256Equality() { var part1 = 0UL; var part2 = 1UL; var part3 = 2UL; var part4 = 3UL; var expectedBigInt = (new BigInteger(part1) << 96) + (new BigInteger(part2) << 64) + (new BigInteger(part3) << 32) + new BigInteger(part4); var expected = new UInt256(expectedBigInt); var same = new UInt256(expectedBigInt); var differentPart1 = new UInt256((new BigInteger(~part1) << 96) + (new BigInteger(part2) << 64) + (new BigInteger(part3) << 32) + new BigInteger(part4)); var differentPart2 = new UInt256((new BigInteger(part1) << 96) + (new BigInteger(~part2) << 64) + (new BigInteger(part3) << 32) + new BigInteger(part4)); var differentPart3 = new UInt256((new BigInteger(part1) << 96) + (new BigInteger(part2) << 64) + (new BigInteger(~part3) << 32) + new BigInteger(part4)); var differentPart4 = new UInt256((new BigInteger(part1) << 96) + (new BigInteger(part2) << 64) + (new BigInteger(part3) << 32) + new BigInteger(~part4)); Assert.IsTrue(expected.Equals(same)); Assert.IsTrue(expected == same); Assert.IsFalse(expected != same); Assert.IsFalse(expected.Equals(differentPart1)); Assert.IsFalse(expected == differentPart1); Assert.IsTrue(expected != differentPart1); Assert.IsFalse(expected.Equals(differentPart2)); Assert.IsFalse(expected == differentPart2); Assert.IsTrue(expected != differentPart2); Assert.IsFalse(expected.Equals(differentPart3)); Assert.IsFalse(expected == differentPart3); Assert.IsTrue(expected != differentPart3); Assert.IsFalse(expected.Equals(differentPart4)); Assert.IsFalse(expected == differentPart4); Assert.IsTrue(expected != differentPart4); }
public Dictionary <string, List <UnspentEntry> > GetUnspent(string address, UInt256 th) { JObject unspent = th.Equals(UInt256.Zero) ? ProcessGetUnspents(new JArray(address)) : ProcessGetUnspents(new JArray(address, true, th.ToString())); var result = new Dictionary <string, List <UnspentEntry> >(); foreach (var node in (JArray)unspent["balance"]) { var child = (JArray)node["unspent"]; if (child != null) { List <UnspentEntry> list; string sym = node["asset_symbol"].AsString(); if (result.ContainsKey(sym)) { list = result[sym]; } else { list = new List <UnspentEntry>(); result[sym] = list; } foreach (var utxo in child) { var input = new UnspentEntry() { txid = utxo["txid"].AsString(), index = (uint)utxo["n"].AsNumber(), value = (decimal)utxo["value"].AsNumber() }; list.Add(input); } } } return(result); }
private JObject ProcessGetUnspents(JArray _params) { UInt160 scriptHash = GetScriptHashFromParam(_params[0].AsString()); byte startingToken = 0; // 0 = Utility Token (CRON), 1 = Governing Token (CRONIUM) int maxIterations = 2; UInt256 th = UInt256.Zero; if (_params.Count > 1) { string gh = _params[1].AsString(); bool isGoverningToken = (gh == "yes"); bool isUtilityToken = (gh == "util"); if (isGoverningToken) { startingToken = 1; maxIterations = 1; } else if (isUtilityToken) { startingToken = 0; maxIterations = 1; } if (_params.Count > 2) { th = ParseTokenHash(_params[2].AsString()); if (th.Equals(Blockchain.UtilityToken.Hash)) { th = UInt256.Zero; } } } var unspentsCache = new DbCache <UserSystemAssetCoinOutputsKey, UserSystemAssetCoinOutputs>( _db, null, null, SystemAssetUnspentCoinsPrefix); string[] nativeAssetNames = { "CRON", "CRONIUM" }; UInt256[] nativeAssetIds = { Blockchain.UtilityToken.Hash, Blockchain.GoverningToken.Hash }; byte tokenId = 255; var snapshot = Blockchain.Singleton.GetSnapshot(); var r = snapshot.Assets.Find(); var txs = snapshot.Transactions; if (!th.Equals(UInt256.Zero)) { tokenId = GetTokenID(r, th, txs); if (tokenId != 255) { nativeAssetNames[1] = snapshot.Assets.TryGet(th).GetName(); nativeAssetIds[1] = th; } else { throw new Cron.Network.RPC.RpcException(-7166, "Token not found"); } } JObject json = new JObject(); JArray balances = new JArray(); json["balance"] = balances; json["address"] = scriptHash.ToAddress(); for (byte tokenIndex = startingToken; maxIterations-- > 0; tokenIndex++) { byte[] prefix = new[] { (tokenId == 255 || tokenIndex == 0 ? tokenIndex : tokenId) }.Concat(scriptHash.ToArray()).ToArray(); var unspents = new JArray(); Fixed8 total = new Fixed8(0); foreach (var unspentInTx in unspentsCache.Find(prefix)) { if (!AddUnspents(unspents, ref total, unspentInTx)) { break; } } if (unspents.Count <= 0) { continue; } var balance = new JObject(); balance["unspent"] = unspents; balance["asset_hash"] = nativeAssetIds[tokenIndex].ToString().Substring(2); balance["asset_symbol"] = balance["asset"] = nativeAssetNames[tokenIndex]; balance["amount"] = new JNumber((double)(decimal)total);; balances.Add(balance); } return(json); }
public override bool Verify(IEnumerable <Transaction> mempool) { switch (GetTxType()) { case RingConfidentialTransactionType.S_S_Transaction: for (int i = 0; i < RingCTSig.Count; i++) { if (!RingCT.Impls.RingCTSignature.Verify(RingCTSig[i], Fixed8.Zero)) { return(false); } // Double Spending Check if (Blockchain.Default.IsDoubleRingCTCommitment(this)) { return(false); } // Check Mix Rings Link & Asset UInt256 assetID = RingCTSig[i].AssetID; for (int j = 0; j < RingCTSig[i].mixRing.Count; j++) { for (int k = 0; k < RingCTSig[i].mixRing[j].Count; k++) { Transaction tx = Blockchain.Default.GetTransaction(RingCTSig[i].mixRing[j][k].txHash); if (tx is RingConfidentialTransaction rtx) { if (rtx.RingCTSig[RingCTSig[i].mixRing[j][k].RingCTIndex].AssetID != assetID) { //AssetState asset_open = Blockchain.Default.GetAssetState(rtx.RingCTSig[RingCTSig[i].mixRing[j][k].RingCTIndex].AssetID); AssetState asset_send = Blockchain.Default.GetAssetState(assetID); if (!(asset_send.AssetType == AssetType.StealthToken && rtx.RingCTSig[RingCTSig[i].mixRing[j][k].RingCTIndex].AssetID == Blockchain.StealthNormalHash)) { return(false); } } } else { return(false); } } } } break; case RingConfidentialTransactionType.S_T_Transaction: // Check Amount try { Dictionary <UInt256, Fixed8> amount = new Dictionary <UInt256, Fixed8>(); UInt256 mainAssetId = null; for (int i = 0; i < Outputs.Length; i++) { if (amount.ContainsKey(Outputs[i].AssetId)) { amount[Outputs[i].AssetId] += Outputs[i].Value; } else { mainAssetId = Outputs[i].AssetId; amount[Outputs[i].AssetId] = Outputs[i].Value; } } for (int i = 0; i < RingCTSig.Count; i++) { if (!RingCT.Impls.RingCTSignature.Verify(RingCTSig[i], Fixed8.Zero)) { return(false); } // Double Spending Check if (Blockchain.Default.IsDoubleRingCTCommitment(this)) { return(false); } if (amount.ContainsKey(RingCTSig[i].AssetID)) { amount[RingCTSig[i].AssetID] -= RingCTSig[i].vPub; } else if (i == RingCTSig.Count - 1 && RingCTSig[i].AssetID == Blockchain.UtilityToken.Hash && RingCTSig[i].vPub != Fixed8.Zero) { amount[RingCTSig[i].AssetID] = Fixed8.Zero - RingCTSig[i].vPub; } else { return(false); } } if (SystemFee > Fixed8.Zero) { if (mainAssetId.Equals(Blockchain.UtilityToken.Hash) || mainAssetId.Equals(Blockchain.GoverningToken.Hash)) { amount[Blockchain.UtilityToken.Hash] += Blockchain.UtilityToken.A_Fee; } else { AssetState asset = Blockchain.Default.GetAssetState(mainAssetId); amount[Blockchain.UtilityToken.Hash] += asset.AFee; } } foreach (var key in amount.Keys) { if (amount[key] > Fixed8.Zero) { return(false); } } } catch (Exception ex) { return(false); } break; case RingConfidentialTransactionType.T_S_Transaction: for (int i = 0; i < RingCTSig.Count; i++) { Fixed8 vPubOld = Fixed8.Zero; for (int j = 0; j < Inputs.Length; j++) { Transaction tx = Blockchain.Default.GetTransaction(Inputs[j].PrevHash); if (tx.Outputs[Inputs[j].PrevIndex].AssetId.ToString() == RingCTSig[i].AssetID.ToString()) { vPubOld += tx.Outputs[Inputs[j].PrevIndex].Value; } } if (!RingCT.Impls.RingCTSignature.Verify(RingCTSig[i], vPubOld)) { return(false); } // Check the amount Fixed8 vPubOut = Fixed8.Zero; for (int j = 0; j < Outputs.Length; j++) { if (Outputs[j].AssetId == RingCTSig[i].AssetID) { vPubOut += Outputs[j].Value; } } Fixed8 vPrivOut = Fixed8.Zero; for (int j = 0; j < RingCTSig[i].ecdhInfo.Count; j++) { vPrivOut += RingCTSig[i].ecdhInfo[j].amount.ToFixed8(); } if (RingCTSig[i].AssetID == Blockchain.GoverningToken.Hash) { if (vPubOld < vPrivOut + vPubOut + QrsSystemFee) { return(false); } } else if (RingCTSig[i].AssetID == Blockchain.UtilityToken.Hash) { if (vPubOld < vPrivOut + vPubOut + SystemFee) { return(false); } } else { if (vPubOld < vPrivOut + vPubOut) { return(false); } } } break; default: break; } return(true); }
public bool Equals(CoinReference other) { if (ReferenceEquals(this, other)) return true; if (other is null) return false; return PrevHash.Equals(other.PrevHash) && PrevIndex.Equals(other.PrevIndex); }
public static bool IsZero(this UInt256 value) { return(Zero.Equals(value)); }