public void Repository_CacheDoesntCarryOver()
        {
            const string             testContractType = "A String";
            ISource <byte[], byte[]> stateDB          = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource());
            StateRepositoryRoot      repository       = new StateRepositoryRoot(stateDB);

            byte[] initialRoot = repository.Root;

            IStateRepository txTrack = repository.StartTracking();

            txTrack.CreateAccount(testAddress);
            txTrack.SetStorageValue(testAddress, dog, cat);
            txTrack.SetContractType(testAddress, testContractType);
            txTrack.Commit();
            repository.Commit();

            byte[] postChangesRoot = repository.Root;

            IStateRepositoryRoot repository2 = repository.GetSnapshotTo(initialRoot);

            Assert.Null(repository2.GetAccountState(testAddress));
            repository2.SetContractType(testAddress, "Something Else");
            repository2.SyncToRoot(postChangesRoot);
            Assert.Equal(testContractType, repository2.GetContractType(testAddress));
        }
        public void Test20()
        {
            ISource <byte[], byte[]> stateDB    = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource());
            StateRepositoryRoot      repository = new StateRepositoryRoot(stateDB);

            byte[] root = repository.Root;

            uint160 cowAddress   = new uint160(cow);
            uint160 horseAddress = new uint160(horse);

            IStateRepository track2 = repository.StartTracking(); //repository

            track2.SetStorageValue(cowAddress, cowKey1, cowVal1);
            track2.SetStorageValue(horseAddress, horseKey1, horseVal1);
            track2.Commit();
            repository.Commit();

            byte[] root2 = repository.Root;

            track2 = repository.StartTracking(); //repository
            track2.SetStorageValue(cowAddress, cowKey2, cowVal0);
            track2.SetStorageValue(horseAddress, horseKey2, horseVal0);
            track2.Commit();
            repository.Commit();

            byte[] root3 = repository.Root;

            IStateRepository snapshot = new StateRepositoryRoot(stateDB, root);

            Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey1));
            Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2));
            Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey1));
            Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2));

            snapshot = new StateRepositoryRoot(stateDB, root2);
            Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1));
            Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2));
            Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1));
            Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2));

            snapshot = new StateRepositoryRoot(stateDB, root3);
            Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1));
            Assert.Equal(cowVal0, snapshot.GetStorageValue(cowAddress, cowKey2));
            Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1));
            Assert.Equal(horseVal0, snapshot.GetStorageValue(horseAddress, horseKey2));
        }
        public void Repository_Null_Is_Stored_As_Null()
        {
            // Demonstrates that our repository stores null and returns null

            ISource <byte[], byte[]> stateDB    = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource());
            StateRepositoryRoot      repository = new StateRepositoryRoot(stateDB);

            repository.CreateAccount(testAddress);
            repository.SetStorageValue(testAddress, dog, null);
            Assert.Null(repository.GetStorageValue(testAddress, dog));
            repository.Commit();

            // We have pushed null to the kv store. Should come back as null
            StateRepositoryRoot repository2 = new StateRepositoryRoot(stateDB, repository.Root);

            Assert.Null(repository2.GetStorageValue(testAddress, dog));
        }
        public void Repository_Bytes0VsNull()
        {
            // Demonstrates that our repository treats byte[0] and null as the same.

            ISource <byte[], byte[]> stateDB    = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource());
            StateRepositoryRoot      repository = new StateRepositoryRoot(stateDB);

            repository.CreateAccount(testAddress);
            repository.SetStorageValue(testAddress, dog, new byte[0]);
            Assert.Equal(new byte[0], repository.GetStorageValue(testAddress, dog));
            repository.Commit();

            // We have pushed byte[0] to the kv store. Should come back as byte[0] right?
            StateRepositoryRoot repository2 = new StateRepositoryRoot(stateDB, repository.Root);

            // Nope, comes back null...
            Assert.Null(repository2.GetStorageValue(testAddress, dog));
        }
        public void Repository_Empty_String_Is_Stored_As_Null()
        {
            // Demonstrates that our repository stores null and returns null

            ISource <byte[], byte[]> stateDB    = new NoDeleteSource <byte[], byte[]>(new MemoryDictionarySource());
            StateRepositoryRoot      repository = new StateRepositoryRoot(stateDB);

            repository.CreateAccount(testAddress);
            var emptyString = Encoding.UTF8.GetBytes(string.Empty);

            Assert.Empty(empty); // Empty string is empty byte array
            repository.SetStorageValue(testAddress, dog, emptyString);
            Assert.Equal(emptyString, repository.GetStorageValue(testAddress, dog));
            repository.Commit();

            // We have pushed null to the kv store. Should come back as null
            StateRepositoryRoot repository2 = new StateRepositoryRoot(stateDB, repository.Root);

            Assert.Null(repository2.GetStorageValue(testAddress, dog));
        }