Beispiel #1
0
        public void EndiannessHash256()
        {
            // A sequence of bytes, all different, each line fills an entire ulong
            var bytes = new byte[]
            {
                0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
                0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
                0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
            };

            var hash = new Hash256(bytes);

            // Did the bytes move to the correct locations in the four ulongs ?
            Assert.Equal(0x1112131415161718UL, hash.Var1);
            Assert.Equal(0x2122232425262728UL, hash.Var2);
            Assert.Equal(0x3132333435363738UL, hash.Var3);
            Assert.Equal(0x4142434445464748UL, hash.Var4);

            // Does ToString() display the correct overall hash, in hex ?
            Assert.Equal(
                "1112131415161718 2122232425262728 3132333435363738 4142434445464748",
                hash.ToString());

            // Does writing out the hash back to a byte array restore the right order ?
            var write = new byte[Hash256.SizeInBytes];

            hash.WriteTo(write);

            Assert.Equal(bytes, write);
        }
Beispiel #2
0
        public AccountId CreatedAccount()
        {
            AccountId destination      = null;
            Hash256   destinationIndex = null;

            if (TransactionType == TransactionType.Payment && Meta.Has(Field.AffectedNodes))
            {
                StArray affected = Meta[StArray.AffectedNodes];

                foreach (var node in affected)
                {
                    if (node.Has(StObject.CreatedNode))
                    {
                        StObject created = node[StObject.CreatedNode];

                        if (StObject.LedgerEntryType(created) == LedgerEntryType.AccountRoot)
                        {
                            if (destination == null)
                            {
                                destination      = Txn[AccountId.Destination];
                                destinationIndex = Hash256.AccountIdLedgerIndex(destination);
                            }

                            if (destinationIndex.Equals(created[Hash256.LedgerHash]))
                            {
                                return(destination);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Beispiel #3
0
 private B000170()
 {
     base.ExpectedHash = Hash256.Parse("00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee");
     base.Header       = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Parse("000000002a22cfee1f2c846adbd12b3e183d4f97683f85dad08a79780a84bd55"),
         MerkleRoot        = Hash256.Parse("7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff"),
         Timestamp         = 1231731025,
         DifficultyBits    = 486604799, //0x1d00ffff
         Nonce             = 1889418792,
     };
     base.Header.Freeze();
     base.Block = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Parse("000000002a22cfee1f2c846adbd12b3e183d4f97683f85dad08a79780a84bd55"),
         Timestamp         = 1231731025,
         DifficultyBits    = 486604799, //0x1d00ffff
         Nonce             = 1889418792,
         Transactions      =
         {
             B000170.Coinbase,
             B000170.Tx1,
         },
     };
     base.Block.Freeze();
     base.ExpectedMerkleTree = new[]
     {
         Hash256.Parse("b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082"),
         Hash256.Parse("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"),
         Hash256.Parse("7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff"),
     };
 }
Beispiel #4
0
 private B103640()
 {
     base.ExpectedHash = Hash256.Parse("0000000000003702793ba6bf5a085eccee1ec9b249f6ff42063b34980e7cf028");
     base.Header       = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Parse("000000000000111a11fcee10f2b46210aacc2e2e4c7dd1d1924b2f6f03e8316f"),
         MerkleRoot        = Hash256.Parse("63a37fe141ab610d471067f75591f291c4d6d4333e3213363f06f187fce04e82"),
         Timestamp         = 1295526788,
         DifficultyBits    = 453217774, //0x1b038dee
         Nonce             = 3592101678,
     };
     base.Header.Freeze();
     base.Block = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Parse("000000000000111a11fcee10f2b46210aacc2e2e4c7dd1d1924b2f6f03e8316f"),
         Timestamp         = 1295526788,
         DifficultyBits    = 453217774, //0x1b038dee
         Nonce             = 3592101678,
         Transactions      =
         {
             B103640.Coinbase,
             B103640.Tx1,
         },
     };
     base.Block.Freeze();
     ExpectedMerkleTree = new[]
     {
         Hash256.Parse("cdd3cf10fe83c28d1ac80714a48440e47f1c6b6950fec54f6b8ea04241004cb5"),
         Hash256.Parse("945691940e0ccd9f526ee1edd57a77ce170804915749702f5564c49b1f70f330"),
         Hash256.Parse("63a37fe141ab610d471067f75591f291c4d6d4333e3213363f06f187fce04e82"),
     };
 }
Beispiel #5
0
        public void DefaultConstruction()
        {
            var hash = new Hash256();

            Assert.AreEqual(Hash256.Empty, hash);
            Assert.AreElementsEqual(Hash256.Empty.Bytes, hash.Bytes);
        }
Beispiel #6
0
        public void ExpectedHashSize()
        {
            var hash = new Hash256();

            Assert.AreEqual(32, hash.HashByteSize);
            Assert.AreEqual(hash.HashByteSize, hash.SerializedByteSize);
        }
Beispiel #7
0
        public Signature SignHash(Hash256 hash, Key key, DisposableContext?context = null)
        {
            var dsa = context?.Context as ECDsaCng;

            if (dsa == null)
            {
                dsa = key.ToECDsaCngKey();
            }

            try
            {
                byte[] signature = dsa.SignHash(hash.Bytes.ToArray());

                return(new Signature {
                    Bytes = signature
                });
            }
            finally
            {
                if (context == null)
                {
                    dsa?.Dispose();
                }
            }
        }
        public static Signature CreateSignature(this PrivateKeyNotWallet account, Hash256 hash, Network network)
        {
            var signature = account.PrivateKey.SignMessage(hash.Bytes, network);

            Debug.Assert(account.PublicKey.Verify(hash, signature, network));
            return(new Signature(account.PublicKey, signature));
        }
Beispiel #9
0
        public ActionResult Edit(string oldPassword, [Bind(Include = "Id,FirstLastName,Username,Password,Admin")] Users users)
        {
            var getAllUsers = db.Users.ToList();
            var checkUser   = getAllUsers.FirstOrDefault(x => x.Username == users.Username && x.Id != users.Id);

            if (checkUser != null)
            {
                ViewBag.Error = "Girmiş olduğunuz kullanıcı adı sisteme mevcuttur";
                return(View(users));
            }

            var getUser = db.Users.Find(users.Id);

            if (getUser.Password != Hash256.Hash(oldPassword))
            {
                ViewBag.ErrorOldPassword = "******";
                return(View(users));
            }

            if (ModelState.IsValid)
            {
                getUser.Password      = Hash256.Hash(users.Password);
                getUser.Username      = users.Username;
                getUser.Admin         = users.Admin;
                getUser.FirstLastName = users.FirstLastName;

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(users));
        }
Beispiel #10
0
 /// <summary>
 /// Facilitates the writing of <see cref="Terab.UTXO.Core.Hash.Hash256"/>.
 /// </summary>
 public static void WriteHash256(this BinaryWriter writer, Hash256 hash)
 {
     writer.Write(hash.Var1);
     writer.Write(hash.Var2);
     writer.Write(hash.Var3);
     writer.Write(hash.Var4);
 }
        public ActionResult Auth([Bind(Include = "Username,Password")] Users user)
        {
            if (string.IsNullOrEmpty(user.Username) || string.IsNullOrEmpty(user.Password))
            {
                ViewBag.Error = "Lütfen boşukları doldurunuz.";
                return(View("Index", user));
            }

            var getUser = db.Users.FirstOrDefault(x => x.Username == user.Username);

            if (getUser != null)
            {
                if (getUser.Password == Hash256.Hash(user.Password))
                {
                    Session["Auth"] = 1;
                    Session["Role"] = getUser.Admin;
                    Session["Id"]   = getUser.Id;
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ViewBag.Error = "Girmiş olduğunuz şifre yanlıştır.";
                }
            }
            else
            {
                ViewBag.Error = "Kullanıcı mevcut değildir.";
            }

            return(View("Index"));
        }
Beispiel #12
0
 private B000000()
 {
     base.ExpectedHash = Hash256.Parse("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
     base.Header       = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Empty,
         MerkleRoot        = Hash256.Parse("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"),
         Timestamp         = 1231006505,
         DifficultyBits    = 486604799, //0x1d00ffff
         Nonce             = 2083236893,
     };
     base.Header.Freeze();
     base.Block = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Empty,
         Timestamp         = 1231006505,
         DifficultyBits    = 486604799, //0x1d00ffff
         Nonce             = 2083236893,
         Transactions      =
         {
             B000000.Coinbase,
         },
     };
     base.Block.Freeze();
     base.ExpectedMerkleTree = new[]
     {
         Hash256.Parse("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"),
     };
 }
Beispiel #13
0
 private B072783()
 {
     base.ExpectedHash = Hash256.Parse("000000000074672e28f2049c94c5d7fe3b20753f0ed6a8aa168e1c103cc48388");
     base.Header       = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Parse("0000000000b94dd60e9a8fabb3d6b3a820486a566ed260855583a17f1ba18a29"),
         MerkleRoot        = Hash256.Parse("bdc105236f133fb9fd8ef55fc0247bf46bb400e9d7b8b2e925b16dea931bce4f"),
         Timestamp         = 1281155353,
         DifficultyBits    = 469809688, //0x1c00ba18
         Nonce             = 1000411397,
     };
     base.Header.Freeze();
     base.Block = new Block
     {
         Version           = 1,
         PreviousBlockHash = Hash256.Parse("0000000000b94dd60e9a8fabb3d6b3a820486a566ed260855583a17f1ba18a29"),
         Timestamp         = 1281155353,
         DifficultyBits    = 469809688, //0x1c00ba18
         Nonce             = 1000411397,
         Transactions      =
         {
             B072783.Coinbase,
             B072783.Tx1,
         },
     };
     base.Block.Freeze();
     base.ExpectedMerkleTree = new[]
     {
         Hash256.Parse("9f92327b5a71d5acf608974fd8bbf16abc948ae21f934a2a5a7c841c499b5f92"),
         Hash256.Parse("b77e0fc6d275c951342a33473015937e62b25a68538d78a260f1225b2835a283"),
         Hash256.Parse("bdc105236f133fb9fd8ef55fc0247bf46bb400e9d7b8b2e925b16dea931bce4f"),
     };
 }
Beispiel #14
0
        private Dictionary <string, List <UTXO> > GetUTXO(SqlDataReader reader)
        {
            //建一个以asset为key,utxo对象为value的字典
            Dictionary <string, List <UTXO> > dic = new Dictionary <string, List <UTXO> >();

            //读取reader并写入字典
            while (reader.Read())
            {
                Hash256 txid    = new Hash256(reader["txid"].ToString());
                int     n       = int.Parse(reader["n"].ToString());
                string  asset   = reader["asset"].ToString();
                string  address = reader["address"].ToString();
                decimal value   = decimal.Parse(reader["value"].ToString());

                UTXO uTXO = new UTXO(txid, n, asset, address, value);

                if (dic.ContainsKey(asset))
                {
                    dic[asset].Add(uTXO);
                }
                else
                {
                    List <UTXO> uTXOs = new List <UTXO>();
                    uTXOs.Add(uTXO);
                    dic[asset] = uTXOs;
                }
            }
            if (dic.Count == 0)
            {
                throw new Exception("你都没有这种钱");//须添加币种显示
            }
            return(dic);
        }
Beispiel #15
0
        private static async void TransGasForNep5(string address, byte[] prikey, byte[] pubkey, string targetAddr,
                                                  decimal sendCount)
        {
            var    assetnep5 = "0xa0b53d2efa8b1c4a62fcc1fcb54b7641510810c7";
            var    assetgas  = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7";
            string api       = "https://api.nel.group/api/testnet";

            Dictionary <string, List <Utxo> > dir = await GetBalanceByAddress(api, address);

            ThinNeo.Transaction tran = null;
            {
                byte[] script = null;
                using (var sb = new ThinNeo.ScriptBuilder())
                {
                    var array = new MyJson.JsonNode_Array();
                    sb.EmitParamJson(array);                                              //参数倒序入
                    sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)mintTokens")); //参数倒序入
                    ThinNeo.Hash160 shash = new ThinNeo.Hash160(assetnep5);
                    sb.EmitAppCall(shash);                                                //nep5脚本
                    script = sb.ToArray();
                }

                var nep5scripthash = new ThinNeo.Hash160(assetnep5);
                var targetaddr     = ThinNeo.Helper.GetAddressFromScriptHash(nep5scripthash);
                Console.WriteLine("contract address=" + targetaddr); //往合约地址转账

                //生成交易
                tran      = makeTran(dir[assetgas], targetaddr, new ThinNeo.Hash256(assetgas), 5);
                tran.type = ThinNeo.TransactionType.InvocationTransaction;
                var idata = new ThinNeo.InvokeTransData();
                tran.extdata = idata;
                idata.script = script;
            }

            //sign and broadcast
            var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey);

            tran.AddWitness(signdata, pubkey, address);
            var trandata    = tran.GetRawData();
            var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata);

            byte[] postdata;
            var    url    = HttpHelper.MakeRpcUrlPost("http://127.0.0.1:20332", "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata));
            var    result = await HttpHelper.HttpPost(url, postdata);

            Console.WriteLine("得到的结果是:" + result);
            var json = MyJson.Parse(result).AsDict();

            if (json.ContainsKey("result"))
            {
                var resultv = json["result"].AsList()[0].AsDict();
                var txid    = resultv["txid"].AsString();
                if (txid.Length > 0)
                {
                    Hash256 test = tran.GetHash();
                }

                Console.WriteLine("txid=" + txid);
            }
        }
Beispiel #16
0
        public void EndiannessWithTruncation2()
        {
            // A sequence of bytes, all different, each line fills an entire ulong
            var bytes = new byte[]
            {
                0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0x2F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                0x4F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
            };

            var hash          = new Hash256(bytes);
            var truncatedHash = hash.Truncate(238);

            // Does ToString() display the correct overall hash, in hex ?
            Assert.Equal(
                "1FFFFFFFFFFFFFFF 2FFFFFFFFFFFFFFF 3FFFFFFFFFFFFFFF 4FFFFFFFFFFC0000",
                truncatedHash.ToString());

            // Did the bytes move to the correct locations in the four ulongs ?
            Assert.Equal(0x1FFFFFFFFFFFFFFFUL, truncatedHash.Var1);
            Assert.Equal(0x2FFFFFFFFFFFFFFFUL, truncatedHash.Var2);
            Assert.Equal(0x3FFFFFFFFFFFFFFFUL, truncatedHash.Var3);
            Assert.Equal(0x4FFFFFFFFFFC0000UL, truncatedHash.Var4);
        }
Beispiel #17
0
        public bool TryReadVector256(out Hash256[] value)
        {
            var currentOffset = ConsumedBytes;

            if (!TryReadLengthPrefix(out var length))
            {
                value = default;
                return(false);
            }

            if (length % 32 != 0)
            {
                ConsumedBytes = currentOffset;
                value         = default;
                return(false);
            }

            if (data.Length < ConsumedBytes + length)
            {
                value = default;
                return(false);
            }

            value = new Hash256[length / 32];
            for (int i = 0; i < value.Length; ++i)
            {
                value[i]       = new Hash256(data.Slice(ConsumedBytes, 32));
                ConsumedBytes += 32;
            }
            return(true);
        }
Beispiel #18
0
        public void StringParseRoundTrip(string hashString)
        {
            var hash      = Hash256.Parse(hashString);
            var roundTrip = hash.ToString();

            Assert.AreEqual(hashString, roundTrip, StringComparison.OrdinalIgnoreCase);
        }
Beispiel #19
0
 public SozuConfig(bool implicitSectors, int sectorBitDepth, Hash256 start, Hash256 end)
 {
     ImplicitSectors = implicitSectors;
     SectorBitDepth  = sectorBitDepth;
     Start           = start;
     End             = end;
 }
Beispiel #20
0
        public void ByteArrayContruction(byte[] hashBytes)
        {
            var hash      = new Hash256(hashBytes);
            var roundTrip = hash.Bytes;

            Assert.AreElementsEqual(hashBytes, roundTrip);
        }
Beispiel #21
0
        private B124009()
        {
            base.ExpectedHash = Hash256.Parse("0000000000003a2bf9728419cd10d1c68493c99f275242db8e7df2ee9079e0ed");
            base.Header       = new Block
            {
                Version           = 1,
                PreviousBlockHash = Hash256.Parse("00000000000048c38a17019b15672158434e5dc7ae89bb808109843e4c7af45f"),
                MerkleRoot        = Hash256.Parse("a2198c4f6b03a76aaea0f8c174de28358eb6affd66471d91ad7d0e0115bb36c1"),
                Timestamp         = 1305421686,
                DifficultyBits    = 443192243, //0x1a6a93b3
                Nonce             = 3195439750,
            };
            base.Header.Freeze();
            base.ExpectedMerkleTree = new[]
            {
                Hash256.Parse("b3fb95ee04a3d3159ad7d1773d6fe6a1397f37fa99c138a23ef9f645b7a4a6b7"),
                Hash256.Parse("ad5512b67fee69c0e3293290c3d7ff88d6576fb8b2c55a037e0f003cf88a0151"),
                Hash256.Parse("a2198c4f6b03a76aaea0f8c174de28358eb6affd66471d91ad7d0e0115bb36c1"),
            };
            var serMsg  = SerializedBlockMessage;
            var serData = new byte[serMsg.Length - 24];

            Array.Copy(serMsg, 24, serData, 0, serData.Length);
            base.SerializedBlockData = serData;
        }
Beispiel #22
0
        private void AddLeaf(Hash256 index, ShaMapLeafNode leaf)
        {
            int ix       = index.Nibblet(this.depth);
            var existing = this.branches[ix];

            this.Invalidate();

            if (existing == null)
            {
                this.SetNode(ix, leaf);
            }
            else
            {
                var node = existing as ShaMapLeafNode;
                if (node != null)
                {
                    if (node.Index.Equals(index))
                    {
                        throw new InvalidOperationException("Tried to add node already in tree!");
                    }

                    var container = this.MakeInnerChild();
                    container.AddLeaf(node.Index, node);
                    container.AddLeaf(index, leaf);
                    this.SetNode(ix, container);
                }
                else
                {
                    var existingInner = (ShaMapInnerNode)existing;
                    existingInner.AddLeaf(index, leaf);
                }
            }
        }
Beispiel #23
0
        private void DiscontinueAuction(object sender, RoutedEventArgs e)
        {
            Hash256 auctionid = new Hash256(this.tb_auction_auctionid.Text);

            byte[] script = dex_ScriptPackage.GetScript_DiscontinueAuction(auctionid);
            MakeTran(script);
        }
Beispiel #24
0
        private void MakeStack(ShaMapInner root, Hash256 index)
        {
            _inners = new LinkedList \ \ ();
            var top = root;

            while (true)
            {
                _inners.AddLast(top);
                var existing = top.GetBranch(index);
                if (existing == null)
                {
                    break;
                }
                if (existing.IsLeaf)
                {
                    Leaf     = existing.AsLeaf();
                    _matched = Leaf.Index.Equals(index);
                    break;
                }
                if (existing.IsInner)
                {
                    top = existing.AsInner();
                }
            }
        }
Beispiel #25
0
        private void GetOfferToBuyerPrice(object sender, RoutedEventArgs e)
        {
            Hash256 offerid = new Hash256(this.tb_offerToBuyer_offerid.Text);

            byte[] script = dex_ScriptPackage.GetScript_GetOfferToBuyPrice(offerid);
            Invoke(script);
        }
Beispiel #26
0
        private void Sell(object sender, RoutedEventArgs e)
        {
            Hash256 fullhash = new Hash256(this.tb_sell_offerid.Text);

            byte[] script = dex_ScriptPackage.GetScript_Sell(fullhash);
            MakeTran(script);
        }
Beispiel #27
0
        private void GetAuctionPrice(object sender, RoutedEventArgs e)
        {
            Hash256 auctionid = new Hash256(this.tb_bet_auctionid.Text);

            byte[] script = dex_ScriptPackage.GetScript_GetAuctionPrice(auctionid);
            Invoke(script);
        }
Beispiel #28
0
        public void Read()
        {
            var path = @"Read-Test.store";

            File.Delete(path);
            File.Delete(Path.Combine(Path.GetDirectoryName(path), @"hot-" + Path.GetFileName(path)));
            using (var fw = File.OpenWrite(path))
            {
                fw.Write(new byte[]
                {
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                    0x01, 0x00, 0x00, 0x00, 0xf1, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00,
                    0x22, 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x00, 0x00, 0x00, 0x00,
                    0x44, 0x44, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0xb8, 0x02, 0xb4, 0xb6, 0x09, 0x57, 0x75,
                    0x10, 0x92, 0xd2, 0x04, 0x1b, 0xac, 0xae, 0x6e, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0xd6, 0x17, 0xe6, 0x61, 0x8f, 0xa0, 0xb9, 0x49, 0x99, 0xf9, 0xb2, 0x89, 0xe1, 0xb1, 0x72, 0xac,
                    0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5a, 0x92, 0x65, 0xbe, 0xf7, 0x46, 0xc3, 0x12,
                    0xd1, 0xa2, 0x27, 0xfa, 0x1b, 0x2f, 0x3e, 0x4c, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0xda, 0x26, 0x9a, 0x14, 0x9f, 0x07, 0xc9, 0xa8, 0x7d, 0xeb, 0xd7, 0xf7, 0x4f, 0x94, 0x9b, 0x7e
                });
            }
            var startHash = new Hash256(0xC000000000000000UL);
            var endHash   = new Hash256(0xF400000000000000UL);

            var sozuFactoryConfigs = new SozuFactoryConfig[1];

            sozuFactoryConfigs[0] = new SozuFactoryConfig(2, SozuFactory.TwoPhaseStoreType, 88);

            var whatToWrite = new TxoPack(new Outpoint[1], new byte[2048]);
            var firstHash   = new TxId(new Hash256(0xF111111111111122UL, 0x22222222UL, 0x33333333UL, 0x44444444UL));
            var wa          = whatToWrite.GetResetWriter();
            var outpoint    = new Outpoint(firstHash, 0, 8);
            var payload     = new byte[8];

            payload[0] = 42;
            wa.Write(in outpoint, payload);

            var txoRead = new TxoPack(new Outpoint[1], new byte[2048]);

            var sozuTable = new SozuFactory(null, startHash, endHash, sozuFactoryConfigs, path).Create();

            sozuTable.Read(whatToWrite, txoRead);

            Assert.Equal(txoRead.Count, whatToWrite.Count);
            Assert.True(txoRead.OutPoints[0].Equals(whatToWrite.OutPoints[0]));
            Assert.True(payload.AsSpan().SequenceEqual(txoRead.Payloads.Slice(0, 8)));
        }
Beispiel #29
0
        private static SozuTable PrepareTests(int layersCount, bool firstLayerVolatile = false)
        {
            var startHash = new Hash256(0xC000000000000000UL);
            var endHash   = new Hash256(0xF400000000000000UL);

            var sozuFactoryConfigs = new SozuFactoryConfig[layersCount];

            sozuFactoryConfigs[0] = new SozuFactoryConfig(
                2,
                firstLayerVolatile ? SozuFactory.VolatileStoreType : SozuFactory.TwoPhaseStoreType,
                4096);

            if (layersCount == 2)
            {
                sozuFactoryConfigs[1] = new SozuFactoryConfig(4, SozuFactory.TwoPhaseStoreType, 4096);
            }

            var path = String.Format(@".\Sozu{1}-{0}-{{0}}-Test.store", layersCount, firstLayerVolatile ? "Volatile" : "");
            var sf   = new SozuFactory(null, startHash, endHash, sozuFactoryConfigs, path);

            var path1 = String.Format(path, 0);

            File.Delete(path1);
            File.Delete(Path.Combine(Path.GetDirectoryName(path1), @"hot-" + Path.GetFileName(path1)));
            if (layersCount == 2)
            {
                path1 = String.Format(path, 1);
                File.Delete(path1);
                File.Delete(Path.Combine(Path.GetDirectoryName(path1), @"hot-" + Path.GetFileName(path1)));
            }
            return(sf.Create());
        }
Beispiel #30
0
        public void testTreeMapSuitability()
        {
            Hash256 a = new Hash256(new byte[32]);
            Hash256 b = new Hash256(new byte[32]);
            Hash256 c = new Hash256(new byte[32]);
            Hash256 d = new Hash256(new byte[32]);

            StObject objectA = new StObject();
            StObject objectB = new StObject();
            StObject objectC = new StObject();

            a.Bytes[0] = (byte)'a';
            b.Bytes[0] = (byte)'b';
            c.Bytes[0] = (byte)'c';
            d.Bytes[0] = (byte)'a';

            var tree = new Hash256.Hash256Map <StObject> {
                { a, objectA }
            };

            // There can be ONLY one
            Assert.True(tree.ContainsKey(d));

            tree.Add(b, objectB);
            tree.Add(c, objectC);

            Assert.True(tree[a] == objectA);
            Assert.True(tree[b] == objectB);
            Assert.True(tree[c] == objectC);
        }
Beispiel #31
0
        public void Prepare(IKeyPair keyPair, Amount fee, UInt32 sequence, UInt32 lastLedgerSequence)
        {
            // This won't always be specified
            if (lastLedgerSequence != null)
            {
                Txn.Add(UInt32.LastLedgerSequence, lastLedgerSequence);
            }

            Txn.Add(UInt32.Sequence, sequence);
            Txn.Add(Amount.Fee, fee);
            Txn.Add(VariableLength.SigningPubKey, keyPair.PubBytes());

            if (Transaction.CanonicalFlagDeployed)
            {
                Txn.SetCanonicalSignatureFlag();
            }

            SigningHash = Txn.SigningHash();
            if (PreviousSigningHash != null && SigningHash.Equals(PreviousSigningHash))
            {
                return;
            }

            try
            {
                byte[] signature = keyPair.Sign(SigningHash.Bytes);
                Txn.Add(VariableLength.TxnSignature, signature);

                var blob = new BytesList();
                Hash256.HalfSha512 id = Hash256.Prefixed256(HashPrefix.TransactionId);

                Txn.ToBytesSink(new MultiSink(blob, id));
                TxBlob = blob.BytesHex();
                Hash = id.Finish();
            }
            catch (Exception e)
            {
                // electric paranoia
                PreviousSigningHash = null;
                throw new ApplicationException("Something went wrong.", e);
            }

            PreviousSigningHash = SigningHash;
        }
Beispiel #32
0
        public TransactionResult(JObject json, Source resultMessageSource)
        {
            Message = json;

            try
            {
                if (resultMessageSource == Source.transaction_subscription_notification)
                {
                    EngineResult = TransactionEngineResult.FromString(json.GetValue("engine_result").ToObject<string>());
                    Validated = json.GetValue("validated").ToObject<bool>();
                    LedgerHash = Hash256.Translate.FromString(json.GetValue("ledger_hash").ToObject<string>());
                    LedgerIndex = new UInt32(json.GetValue("ledger_index").ToObject<long>());

                    JToken transaction;
                    if (json.TryGetValue("transaction", out transaction))
                    {
                        Txn = (Transaction) StObject.FromJObject(transaction.ToObject<JObject>());
                        Hash = Txn[Hash256.hash];
                    }

                    JToken meta;
                    if (json.TryGetValue("meta", out meta))
                    {
                        Meta = (TransactionMeta)StObject.FromJObject(meta.ToObject<JObject>());
                    }
                }
                else if (resultMessageSource == Source.request_tx_result)
                {
                    JToken validated;
                    Validated = json.TryGetValue("validated", out validated) && validated.ToObject<bool>();

                    JToken meta;
                    if (Validated && !json.TryGetValue("meta", out meta))
                    {
                        throw new InvalidOperationException("It's validated, why doesn't it have meta??");
                    }

                    if (Validated)
                    {
                        Meta = (TransactionMeta)StObject.FromJObject(json.GetValue("meta").ToObject<JObject>());
                        EngineResult = TransactionEngineResult.FromNumber(Meta[UInt8.TransactionResult]);
                        Txn = (Transaction)StObject.FromJObject(json);
                        Hash = Txn[Hash256.hash];
                        LedgerHash = null; // XXXXXX
                    }
                }
                else if (resultMessageSource == Source.request_account_tx)
                {
                    JToken validated;
                    Validated = json.TryGetValue("validated", out validated) && validated.ToObject<bool>();

                    JToken meta;
                    if (Validated && !json.TryGetValue("meta", out meta))
                    {
                        throw new InvalidOperationException("It's validated, why doesn't it have meta??");
                    }

                    if (Validated)
                    {
                        var tx = json.GetValue("tx").ToObject<JObject>();
                        Meta = (TransactionMeta)StObject.FromJObject(json.GetValue("meta").ToObject<JObject>());
                        EngineResult = TransactionEngineResult.FromNumber(Meta[UInt8.TransactionResult]);
                        Txn = (Transaction)StObject.FromJObject(tx);
                        Hash = Txn[Hash256.hash];
                        LedgerIndex = new UInt32(tx.GetValue("ledger_index").ToObject<long>());
                        LedgerHash = null;
                    }
                }
                else if (resultMessageSource == Source.request_account_tx_binary)
                {
                    JToken validated;
                    Validated = json.TryGetValue("validated", out validated) && validated.ToObject<bool>();

                    JToken meta;
                    if (Validated && !json.TryGetValue("meta", out meta))
                    {
                        throw new InvalidOperationException("It's validated, why doesn't it have meta??");
                    }

                    if (Validated)
                    {
                        /*
                        {
                            "ledger_index": 3378767,
                            "meta": "201 ...",
                            "tx_blob": "120 ...",
                            "validated": true
                        },
                        */

                        var tx = json.GetValue("tx_blob").ToObject<string>();
                        byte[] decodedTx = B16.Decode(tx);
                        Meta = (TransactionMeta)StObject.Translate.FromHex(json.GetValue("meta").ToObject<string>());
                        Txn = (Transaction)StObject.Translate.FromBytes(decodedTx);
                        Hash = Hash256.TransactionId(decodedTx);
                        Txn.Add(Field.hash, Hash);

                        EngineResult = Meta.TransactionResult();
                        LedgerIndex = new UInt32(json.GetValue("ledger_index").ToObject<long>());
                        LedgerHash = null;
                    }
                }
            }
            catch (JsonException e)
            {
                throw new ApplicationException("Json deserialization failed.", e);
            }
        }