Пример #1
0
        public void LinearDictionaryCoder()
        {
            var testPath = "linearDictionaryUnitTest";

            if (File.Exists(testPath))
            {
                File.Delete(testPath);
            }

            var linearDictionaryCoder = new LinearDictionaryEncoder(testPath, 32, 32);

            var expectedDictionary = new Dictionary <byte[], byte[]>();

            LargeInteger value = 0;

            for (int i = 0; i < 1500; i++)
            {
                var valueBytes = ByteManipulator.BigEndianTruncate(value.GetBytes(), 32);

                var key = CryptographyHelper.Sha3256(valueBytes);

                linearDictionaryCoder.Add(key, valueBytes);
                expectedDictionary.Add(key, valueBytes);

                value = value + 1;
            }

            foreach (var kvp in expectedDictionary)
            {
                var entryValue = linearDictionaryCoder.Get(kvp.Key);
                Assert.IsTrue(ArrayManipulator.Compare(kvp.Value, entryValue));
            }
        }
Пример #2
0
        public bool ApplyDiff(Dictionary <byte[], Tuple <long, LargeInteger> > diff)
        {
            var ret = true;

            foreach (var kvp in diff)
            {
                if (kvp.Value.Item2 < 0)
                {
                    ret = false;
                    break;
                }
            }

            if (ret)
            {
                foreach (var kvp in diff)
                {
                    //update balances
                    if (balanceCoder.ContainsKey(kvp.Key))
                    {
                        balanceCoder.Replace(kvp.Key, kvp.Value.Item2.GetBytes());
                    }
                    else
                    {
                        balanceCoder.Add(kvp.Key, kvp.Value.Item2.GetBytes());
                    }

                    //update nonces
                    if (nonceCoder.ContainsKey(kvp.Key))
                    {
                        if (kvp.Value.Item1 >= 0)
                        {
                            nonceCoder.Replace(kvp.Key, ByteManipulator.GetBytes((uint)kvp.Value.Item1));
                        }
                        else
                        {
                            nonceCoder.Remove(kvp.Key);
                        }
                    }
                    else
                    {
                        if (kvp.Value.Item1 >= 0)
                        {
                            nonceCoder.Add(kvp.Key, ByteManipulator.GetBytes((uint)kvp.Value.Item1));
                        }
                    }
                }
            }

            return(ret);
        }
Пример #3
0
        public async Task <bool> AddContact(string url)
        {
            var ret = false;

            if (url != null)
            {
                var hostnameBytes = ToBytes(url);

                if ((hostnameBytes != null) && (url != Program.Settings.url))
                {
                    if (!coder.ContainsKey(ByteManipulator.BigEndianTruncate(hostnameBytes, 128)))
                    {
                        if (await Handshake(url))
                        {
                            coder.Add(hostnameBytes, ByteManipulator.GetBytes((uint)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds));
                            ret = true;
                        }
                    }
                }
            }

            return(ret);
        }