Ejemplo n.º 1
0
        public List <byte[]> GetValuesNext(ISnapshot snapshot, byte[] key, long limit)
        {
            List <byte[]> result = new List <byte[]>();

            if (limit <= 0)
            {
                return(result);
            }

            Dictionary <WrappedByteArray, WrappedByteArray> collection = new Dictionary <WrappedByteArray, WrappedByteArray>(new WrapperdByteArrayEqualComparer());

            if (snapshot.GetPrevious() != null)
            {
                ((Snapshot)(snapshot)).Collect(collection);
            }

            Dictionary <byte[], byte[]> db_dictonary =
                new Dictionary <byte[], byte[]>((((Common.LevelDB)((SnapshotRoot)snapshot.GetRoot()).DB).DB.GetNext(key, limit)));

            foreach (KeyValuePair <WrappedByteArray, WrappedByteArray> pair in collection)
            {
                db_dictonary.Add(pair.Key.Data, pair.Value.Data);
            }


            return(db_dictonary
                   .OrderBy(x => x.Key, new UnsignedByteArrayCompare())
                   .Where(y => ByteUtil.Compare(y.Key, key) >= 0)
                   .Take((int)limit)
                   .Select(z => z.Value)
                   .ToList());
        }
Ejemplo n.º 2
0
        public int CompareTo(DataWord obj)
        {
            if (obj == null || obj.Data == null)
            {
                return(-1);
            }

            int result = ByteUtil.Compare(this.data, 0, this.data.Length,
                                          obj.Data, 0, obj.Data.Length);

            return(Math.Sign(result));
        }
Ejemplo n.º 3
0
        public Dictionary <byte[], byte[]> GetValuesPrevious(byte[] key, long limit, int precision)
        {
            Helper.IsNotNull(key, "Key must be not null.");
            Dictionary <byte[], byte[]> result = new Dictionary <byte[], byte[]>(new ByteArrayEqualComparer());

            if (limit <= 0 || key.Length < precision)
            {
                return(result);
            }

            Monitor.Enter(this.lock_read);

            try
            {
                long i = 0;
                using (Iterator it = this.db.CreateIterator(new ReadOptions()))
                {
                    for (it.SeekToFirst(); it.IsValid() && i++ < limit; it.Next())
                    {
                        if (it.Key().Length >= precision)
                        {
                            if (ByteUtil.Compare(
                                    ArrayUtil.GetRange(key, 0, precision),
                                    ArrayUtil.GetRange(it.Key(), 0, precision))
                                < 0)
                            {
                                break;
                            }
                            result.Add(it.Key(), it.Value());
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                Monitor.Exit(this.lock_read);
            }

            return(result);
        }
Ejemplo n.º 4
0
        /**
         * Address validation method.
         *
         * @static
         * @param {String/Hash} addr - Account address.
         * @param {Number} type - NormalType / ContractType
         *
         * @return {Boolean} Is address has correct format.
         *
         * @example
         * if ( Account.isValidAddress("n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5") ) {
         *     // some code
         * };
         */
        public static bool IsValidAddress(string address, AddressType?type = null)
        {
            byte[] addBytes;
            try {
                addBytes = Base58.Decode(address);

                if (addBytes.Length != ADDRESS_LENGTH)
                {
                    return(false);
                }
                if (Convert.ToUInt32(addBytes[0]) != ADDRESS_PREFIX)
                {
                    return(false);
                }
                uint typeUint;
                if (type.HasValue)
                {
                    typeUint = Convert.ToUInt32(type.Value);
                    if (typeUint != Convert.ToUInt32(addBytes[1]))
                    {
                        return(false);
                    }
                }
                else
                {
                    typeUint = Convert.ToUInt32(addBytes[1]);
                }

                if (typeUint != (uint)AddressType.ContractType && typeUint != (uint)AddressType.NormalType)
                {
                    return(false);
                }
                var content  = addBytes.Slice(0, 22);
                var checksum = addBytes.Slice(addBytes.Length - 4);
                return(ByteUtil.Compare(Sha3Util.Get256Hash(content).Slice(0, 4), checksum));
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        public List <byte[]> GetValuesPrevious(byte[] key, long limit)
        {
            Dictionary <WrappedByteArray, WrappedByteArray> collection = new Dictionary <WrappedByteArray, WrappedByteArray>(new WrapperdByteArrayEqualComparer());

            if (this.head.GetPrevious() != null)
            {
                ((Snapshot)head).Collect(collection);
            }

            int           precision = sizeof(long) / sizeof(byte);
            List <byte[]> result    = new List <byte[]>();

            foreach (WrappedByteArray array in collection.Keys)
            {
                if (ByteUtil.Compare(ArrayUtil.GetRange(array.Data, 0, precision), key) <= 0)
                {
                    result.Add(array.Data);
                    limit--;
                }
            }

            if (limit <= 0)
            {
                return(result);
            }

            List <byte[]> list =
                ((Common.LevelDB)((SnapshotRoot)this.head.GetRoot()).DB).DB.GetValuesPrevious(key, limit, precision).Values.ToList();;

            foreach (byte[] array in list)
            {
                result.Add(array);
            }

            return(result.Take((int)limit).ToList());
        }