public void Set() { var key = new KeyBytes(Random.NextBytes(PreStoredDataKeySize)); byte[] value = Random.NextBytes(PreStoredDataValueSize); KeyValueStore.Set(key, value); Assert.Equal(value, KeyValueStore.Get(key)); }
public KeyBytes NewRandomKey() { KeyBytes randomKey; do { randomKey = new KeyBytes(Random.NextBytes(PreStoredDataKeySize)); }while (KeyValueStore.Exists(randomKey)); return(randomKey); }
protected void InitializePreStoredData() { PreStoredDataKeys = new KeyBytes[PreStoredDataCount]; PreStoredDataValues = new byte[PreStoredDataCount][]; for (int i = 0; i < PreStoredDataCount; ++i) { PreStoredDataKeys[i] = new KeyBytes(Random.NextBytes(PreStoredDataKeySize)); PreStoredDataValues[i] = Random.NextBytes(PreStoredDataValueSize); KeyValueStore.Set(PreStoredDataKeys[i], PreStoredDataValues[i]); } }
public void ByteArray() { KeyBytes empty = default; AssertBytesEqual(ImmutableArray <byte> .Empty, empty.ByteArray); AssertBytesEqual(Array.Empty <byte>(), empty.ToByteArray()); var foo = new KeyBytes(0x66, 0x6f, 0x6f); AssertBytesEqual(ImmutableArray.Create <byte>(0x66, 0x6f, 0x6f), foo.ByteArray); AssertBytesEqual(new byte[] { 0x66, 0x6f, 0x6f }, foo.ToByteArray()); }
public void Delete() { foreach (KeyBytes key in PreStoredDataKeys) { KeyValueStore.Delete(key); Assert.False(KeyValueStore.Exists(key)); } KeyBytes nonExistent = NewRandomKey(); KeyValueStore.Delete(nonExistent); Assert.False(KeyValueStore.Exists(nonExistent)); }
public void Next() { var cursor = new PathCursor(KeyBytes.FromHex("cfed4460"), false); Assert.Equal(0, cursor.NibbleOffset); Assert.Equal(8, cursor.RemainingNibbleLength); Assert.Equal((byte)0xc, cursor.NextNibble); AssertBytesEqual(FromHex("0c0f0e0d04040600"), cursor.GetRemainingNibbles()); Assert.Throws <ArgumentOutOfRangeException>(() => { cursor = cursor.Next(-1); }); Assert.Equal(0, cursor.NibbleOffset); Assert.Equal(8, cursor.RemainingNibbleLength); Assert.Throws <ArgumentOutOfRangeException>(() => { cursor = cursor.Next(9); }); Assert.Equal(0, cursor.NibbleOffset); Assert.Equal(8, cursor.RemainingNibbleLength); var next = cursor.Next(1); Assert.Equal(0, cursor.NibbleOffset); Assert.Equal(8, cursor.RemainingNibbleLength); Assert.Equal((byte)0xc, cursor.NextNibble); AssertBytesEqual(FromHex("0c0f0e0d04040600"), cursor.GetRemainingNibbles()); Assert.Equal(1, next.NibbleOffset); Assert.Equal((byte)0xf, next.NextNibble); Assert.Equal(7, next.RemainingNibbleLength); AssertBytesEqual(FromHex("0f0e0d04040600"), next.GetRemainingNibbles()); Assert.Throws <ArgumentOutOfRangeException>(() => { next = next.Next(-1); }); Assert.Equal(1, next.NibbleOffset); Assert.Equal(7, next.RemainingNibbleLength); Assert.Throws <ArgumentOutOfRangeException>(() => { next = next.Next(8); }); Assert.Equal(1, next.NibbleOffset); Assert.Equal(7, next.RemainingNibbleLength); var next2 = next.Next(5); Assert.Equal(0, cursor.NibbleOffset); Assert.Equal(8, cursor.RemainingNibbleLength); Assert.Equal((byte)0xc, cursor.NextNibble); AssertBytesEqual(FromHex("0c0f0e0d04040600"), cursor.GetRemainingNibbles()); Assert.Equal(1, next.NibbleOffset); Assert.Equal(7, next.RemainingNibbleLength); Assert.Equal((byte)0xf, next.NextNibble); AssertBytesEqual(FromHex("0f0e0d04040600"), next.GetRemainingNibbles()); Assert.Equal(6, next2.NibbleOffset); Assert.Equal(2, next2.RemainingNibbleLength); Assert.Equal((byte)0x6, next2.NextNibble); AssertBytesEqual(FromHex("0600"), next2.GetRemainingNibbles()); }
public void RemainingNibblesStartWith() { var cursor = new PathCursor(KeyBytes.FromHex("cfed4460"), false); Assert.True(cursor.RemainingNibblesStartWith(FromHex("0c0f0e0d04"))); Assert.False(cursor.RemainingNibblesStartWith(FromHex("0c0f0e0d040406000a"))); Assert.False(cursor.RemainingNibblesStartWith(FromHex("0c0f0e0d0f0f0f"))); PathCursor next = cursor.Next(3); Assert.True(next.RemainingNibblesStartWith(FromHex("0d0404"))); Assert.False(next.RemainingNibblesStartWith(FromHex("0d040406000a0b0c0d"))); Assert.False(next.RemainingNibblesStartWith(FromHex("0d040a0b0c"))); }
public void NibbleAt() { var cursor = new PathCursor(KeyBytes.FromHex("cfed4460"), false); Assert.Equal(0xc, cursor.NibbleAt(0)); Assert.Equal(0xf, cursor.NibbleAt(1)); Assert.Equal(0xe, cursor.NibbleAt(2)); Assert.Throws <ArgumentOutOfRangeException>(() => { cursor.NibbleAt(-1); }); Assert.Throws <ArgumentOutOfRangeException>(() => { cursor.NibbleAt(8); }); cursor = PathCursor.FromNibbles(FromHex("0c0f0e0d040406"), 3); Assert.Equal(0xd, cursor.NibbleAt(0)); Assert.Equal(0x4, cursor.NibbleAt(1)); Assert.Throws <ArgumentOutOfRangeException>(() => { cursor.NibbleAt(-1); }); Assert.Throws <ArgumentOutOfRangeException>(() => { cursor.NibbleAt(5); }); }
public void Constructor() { KeyBytes keyBytes = KeyBytes.FromHex("cfed4460"); var cursor = new PathCursor(keyBytes, false); AssertBytesEqual(keyBytes.ByteArray, cursor.Bytes); Assert.Equal(8, cursor.NibbleLength); Assert.Equal(0, cursor.NibbleOffset); cursor = new PathCursor(keyBytes, true); ImmutableArray <byte> hash = FromHex("42b7a23ca82b1d195f73ac729f216074c7781af7bd808bea825e38556eca13a6"); AssertBytesEqual(hash, cursor.Bytes); Assert.Equal(64, cursor.NibbleLength); Assert.Equal(0, cursor.NibbleOffset); }
public void CountCommonStartingNibbles() { var cursor = new PathCursor(KeyBytes.FromHex("cfed4460"), false); Assert.Equal( 0, cursor.CountCommonStartingNibbles(ImmutableArray <byte> .Empty) ); Assert.Equal( 0, cursor.CountCommonStartingNibbles(FromHex("0a0b0c0d")) ); Assert.Equal( 3, cursor.CountCommonStartingNibbles(FromHex("0c0f0e0f0f0f0f")) ); Assert.Equal( 8, cursor.CountCommonStartingNibbles(FromHex("0c0f0e0d040406000a0b0c0d")) ); PathCursor next = cursor.Next(3); Assert.Equal( 0, next.CountCommonStartingNibbles(ImmutableArray <byte> .Empty) ); Assert.Equal( 0, next.CountCommonStartingNibbles(FromHex("0c0f0e0f0f0f0f")) ); Assert.Equal( 3, next.CountCommonStartingNibbles(FromHex("0d04040a0b0c")) ); Assert.Equal( 5, next.CountCommonStartingNibbles(FromHex("0d040406000a0b0c0d")) ); }
public void Commit(int addressCount) { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); var codec = new Codec(); ITrie trieA = new MerkleTrie(keyValueStore); var addresses = new Address[addressCount]; var states = new IValue[addressCount]; for (int i = 0; i < addressCount; ++i) { addresses[i] = new PrivateKey().ToAddress(); states[i] = (Binary)TestUtils.GetRandomBytes(128); trieA = trieA.Set(new KeyBytes(addresses[i].ByteArray), states[i]); } KeyBytes path = new KeyBytes(TestUtils.GetRandomBytes(32)); trieA = trieA.Set(path, (Text)"foo"); Assert.Equal((Text)"foo", trieA.Get(new[] { path })[0]); ITrie trieB = trieA.Commit(); Assert.Equal((Text)"foo", trieB.Get(new[] { path })[0]); trieB = trieB.Set(path, (Text)"bar"); Assert.Equal((Text)"foo", trieA.Get(new[] { path })[0]); Assert.Equal((Text)"bar", trieB.Get(new[] { path })[0]); ITrie trieC = trieB.Commit(); ITrie trieD = trieC.Commit(); Assert.NotEqual(trieA.Hash, trieB.Hash); Assert.NotEqual(trieA.Hash, trieC.Hash); Assert.NotEqual(trieB.Hash, trieC.Hash); Assert.Equal(trieC.Hash, trieD.Hash); }
public static void AssertBytesEqual(KeyBytes expected, KeyBytes actual) => AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());