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)); } }
public void BlockSerializationUnitTest() { var wallet = new Wallet(HexConverter.ToBytes("0x03b28e661bd1aeae3919fd83706f969c5cde6994b161089ce96306f34cb87322")); var sender = new Wallet(); var recipient = new Wallet(); var expected = new Block() { Difficulty = new LargeInteger("10000000000000"), Height = 256, MinerAddress = wallet.PublicKey, Nonce = new byte[] { 1, 0 }, Timestamp = 65464, PreviousBlockHash = new byte[32], Transactions = new List <Transaction>() { ChainManagerUnitTests.CreateTransaction(sender, recipient, 1337, 0), ChainManagerUnitTests.CreateTransaction(recipient, sender, 9000, 0) } }; var bytes = expected.Serialize(); var actual = new Block(bytes); Assert.IsTrue(ArrayManipulator.Compare(bytes, actual.Serialize())); }
public void SerialCoderUnitTest() { string path = "serialCoderTest"; if (File.Exists(path)) { File.Delete(path); } var serialCoder = new SerialEncoder(path, 2); var expected = new byte[][] { new byte[42], new byte[253], new byte[1250], new byte[8710], new byte[25] }; for (int i = 0; i < expected.Length; i++) { for (int j = 0; j < expected[i].Length; j++) { expected[i][j] = (byte)(i + 40); //test data } } serialCoder.Replace(expected); var actual = serialCoder.ReadDataByIndex(); for (int i = 0; i < expected.Length; i++) { Assert.IsTrue(ArrayManipulator.Compare(expected[i], actual[i])); } serialCoder.RemoveByIndex(2); serialCoder.RemoveByIndex(3); actual = serialCoder.ReadDataByIndex(); Assert.IsTrue(actual[2].Length == 8710); var value = serialCoder.ReadByIndex(1); for (int i = 0; i < value.Length; i++) { value[i] += 10; } serialCoder.ChangeValueByIndex(1, value); var actualValue = serialCoder.ReadByIndex(1); Assert.IsTrue(ArrayManipulator.Compare(actualValue, value)); }
public void Add(byte[] key, byte[] value) { value = ByteManipulator.BigEndianTruncate(value, DataLength); key = ByteManipulator.BigEndianTruncate(key, KeyLength); if (LinearCoder.Count == 0) { LinearCoder.Push(CreateEntry(key, value)); } else { int minimum = 0; int maximum = LinearCoder.Count - 1; while (minimum <= maximum) { int middle = (minimum + maximum) / 2; var middleValue = LinearCoder.Read(middle); if (ArrayManipulator.Compare(key, middleValue.Take(key.Length).ToArray())) { throw new Exception("Key already exists"); } else if (ArrayManipulator.IsGreater(key, middleValue, KeyLength)) { minimum = middle + 1; } else { maximum = middle - 1; } } var popCount = LinearCoder.Count - minimum; if ((maximum >= 0) && ArrayManipulator.IsGreater(LinearCoder.Read(maximum), key, KeyLength)) { popCount++; } List <byte[]> popedItems; if (popCount != 0) { popedItems = LinearCoder.BulkPop(popCount); } else { popedItems = new List <byte[]>(); } popedItems.Insert(0, CreateEntry(key, value)); LinearCoder.BulkPush(popedItems); } }
public bool Verify(Block previousBlock) { var ret = false; var single = SingleVerify(); var hash = ArrayManipulator.Compare(previousBlock.GetHash(), PreviousBlockHash); var height = (Height - previousBlock.Height) == 1; var timestamp = Timestamp >= previousBlock.Timestamp; ret = hash && height && single && timestamp; return(ret); }
private int GetIndex(byte[] key) { if (LinearCoder.Count == 1) { var value = LinearCoder.Read(0); if (!ArrayManipulator.Compare(key, value)) { return(-1); } return(0); } else if (key != null && key.Length == KeyLength) { int minimum = 0; int maximum = LinearCoder.Count - 1; while (minimum <= maximum) { int middle = (minimum + maximum) / 2; var middleValue = LinearCoder.Read(middle); if (ArrayManipulator.Compare(key, middleValue.Take(key.Length).ToArray())) { return(middle); } else if (ArrayManipulator.IsGreater(key, middleValue, KeyLength)) { minimum = middle + 1; } else { maximum = middle - 1; } } return(-1); } else { throw new Exception("Invalid key format"); } }
public void LinearOffsetCoderUnitTest() { string path = "linearCoderTest"; if (File.Exists(path)) { File.Delete(path); } var linearCoder = new LinearEncoder(path, 64); var expected = new byte[15][]; for (int i = 0; i < expected.Length; i++) { expected[i] = new byte[64]; for (int j = 0; j < 64; j++) { expected[i][j] = (byte)(33 + j + i); //test data } } linearCoder.Append(expected); var actual = linearCoder.ReadData(); for (int i = 0; i < expected.Length; i++) { Assert.IsTrue(ArrayManipulator.Compare(expected[i], actual[i])); } Assert.AreEqual(expected.Length, linearCoder.Count); linearCoder.Remove(1); Assert.AreEqual((byte)35, linearCoder.ReadData()[1][0]); }
public static bool operator !=(Transaction a, Transaction b) { return(!ArrayManipulator.Compare(a.Serialize(), b.Serialize())); }
public override bool Equals(object obj) { var transaction = obj as Transaction; return(ArrayManipulator.Compare(Serialize(), transaction.Serialize())); }
public bool Verify() { return(ArrayManipulator.Compare(MainNetwork, Network) && CryptographyHelper.Secp256k1Verify(Source, Signature, SigningData())); }