public void LookupStateReference(StoreFixture fx) { Address address = fx.Address1; Transaction <DumbAction> tx4 = fx.MakeTransaction( new DumbAction[] { new DumbAction(address, "foo") } ); Block <DumbAction> block4 = TestUtils.MineNext(fx.Block3, new[] { tx4 }); Transaction <DumbAction> tx5 = fx.MakeTransaction( new DumbAction[] { new DumbAction(address, "bar") } ); Block <DumbAction> block5 = TestUtils.MineNext(block4, new[] { tx5 }); Block <DumbAction> block6 = TestUtils.MineNext(block5, new Transaction <DumbAction> [0]); Assert.Null(fx.Store.LookupStateReference(fx.StoreNamespace, address, fx.Block3)); Assert.Null(fx.Store.LookupStateReference(fx.StoreNamespace, address, block4)); Assert.Null(fx.Store.LookupStateReference(fx.StoreNamespace, address, block5)); Assert.Null(fx.Store.LookupStateReference(fx.StoreNamespace, address, block6)); fx.Store.StoreStateReference(fx.StoreNamespace, tx4.UpdatedAddresses, block4); Assert.Null(fx.Store.LookupStateReference(fx.StoreNamespace, address, fx.Block3)); Assert.Equal( Tuple.Create(block4.Hash, block4.Index), fx.Store.LookupStateReference(fx.StoreNamespace, address, block4) ); Assert.Equal( Tuple.Create(block4.Hash, block4.Index), fx.Store.LookupStateReference(fx.StoreNamespace, address, block5) ); Assert.Equal( Tuple.Create(block4.Hash, block4.Index), fx.Store.LookupStateReference(fx.StoreNamespace, address, block6) ); fx.Store.StoreStateReference(fx.StoreNamespace, tx5.UpdatedAddresses, block5); Assert.Null(fx.Store.LookupStateReference( fx.StoreNamespace, address, fx.Block3)); Assert.Equal( Tuple.Create(block4.Hash, block4.Index), fx.Store.LookupStateReference(fx.StoreNamespace, address, block4) ); Assert.Equal( Tuple.Create(block5.Hash, block5.Index), fx.Store.LookupStateReference(fx.StoreNamespace, address, block5) ); Assert.Equal( Tuple.Create(block5.Hash, block5.Index), fx.Store.LookupStateReference(fx.StoreNamespace, address, block6) ); }
public void ListAllStateReferences(StoreFixture fx) { Address address1 = fx.Address1; Address address2 = fx.Address2; Address address3 = new PrivateKey().PublicKey.ToAddress(); Transaction <DumbAction> tx4 = fx.MakeTransaction( new[] { new DumbAction(address1, "foo1"), new DumbAction(address2, "foo2"), } ); Block <DumbAction> block4 = TestUtils.MineNext(fx.Block3, new[] { tx4 }); Transaction <DumbAction> tx5 = fx.MakeTransaction( new[] { new DumbAction(address1, "bar1"), new DumbAction(address3, "bar3"), } ); Block <DumbAction> block5 = TestUtils.MineNext(block4, new[] { tx5 }); Block <DumbAction> block6 = TestUtils.MineNext(block5, new Transaction <DumbAction> [0]); var chain = new BlockChain <DumbAction>(new NullPolicy <DumbAction>(), fx.Store); chain.Append(fx.Block1); chain.Append(fx.Block2); chain.Append(fx.Block3); chain.Append(block4); chain.Append(block5); chain.Append(block6); IImmutableDictionary <Address, IImmutableList <HashDigest <SHA256> > > refs = fx.Store.ListAllStateReferences(chain.Id.ToString()); Assert.Equal( new HashSet <Address> { address1, address2, address3 }, refs.Keys.ToHashSet() ); Assert.Equal(new[] { block4.Hash, block5.Hash }, refs[address1]); Assert.Equal(new[] { block4.Hash }, refs[address2]); Assert.Equal(new[] { block5.Hash }, refs[address3]); }
public void GetBlock() { using (StoreFixture fx = FxConstructor()) { Block <DumbAction> genesisBlock = fx.GenesisBlock; // NOTE: it depends on that Block<T>.CurrentProtocolVersion is not 0. Block <DumbAction> block = TestUtils.MineNext( genesisBlock, protocolVersion: 0); fx.Store.PutBlock(block); Block <DumbAction> storedBlock = fx.Store.GetBlock <DumbAction>(block.Hash); Assert.Equal(block, storedBlock); } }
public void Copy() { using (StoreFixture fx = FxConstructor()) using (StoreFixture fx2 = FxConstructor()) { IStore s1 = fx.Store, s2 = fx2.Store; var blocks = new BlockChain <DumbAction>( new NullPolicy <DumbAction>(), s1, Fx.GenesisBlock ); // FIXME: Need to add more complex blocks/transactions. blocks.Append(Fx.Block1); blocks.Append(Fx.Block2); blocks.Append(Fx.Block3); s1.Copy(to: Fx.Store); Fx.Store.Copy(to: s2); Assert.Equal(s1.ListChainIds().ToHashSet(), s2.ListChainIds().ToHashSet()); Assert.Equal(s1.GetCanonicalChainId(), s2.GetCanonicalChainId()); foreach (Guid chainId in s1.ListChainIds()) { Assert.Equal(s1.IterateIndexes(chainId), s2.IterateIndexes(chainId)); foreach (HashDigest <SHA256> blockHash in s1.IterateIndexes(chainId)) { Assert.Equal( s1.GetBlock <DumbAction>(blockHash), s2.GetBlock <DumbAction>(blockHash) ); Assert.Equal( s1.GetBlockStates(blockHash), s2.GetBlockStates(blockHash) ); } Assert.Equal( s1.ListAllStateReferences(chainId), s2.ListAllStateReferences(chainId) ); } // ArgumentException is thrown if the destination store is not empty. Assert.Throws <ArgumentException>(() => Fx.Store.Copy(fx2.Store)); } }
public async Task Copy() { using (StoreFixture fx = FxConstructor()) using (StoreFixture fx2 = FxConstructor()) { IStore s1 = fx.Store, s2 = fx2.Store; var policy = new NullBlockPolicy <DumbAction>(); var blocks = new BlockChain <DumbAction>( policy, new VolatileStagePolicy <DumbAction>(), s1, fx.StateStore, MineGenesis <DumbAction>(policy.GetHashAlgorithm, GenesisMiner.PublicKey) .Evaluate(GenesisMiner, policy.BlockAction, fx.StateStore) ); // FIXME: Need to add more complex blocks/transactions. var key = new PrivateKey(); await blocks.MineBlock(key); await blocks.MineBlock(key); await blocks.MineBlock(key); s1.Copy(to: Fx.Store); Fx.Store.Copy(to: s2); Assert.Equal(s1.ListChainIds().ToHashSet(), s2.ListChainIds().ToHashSet()); Assert.Equal(s1.GetCanonicalChainId(), s2.GetCanonicalChainId()); foreach (Guid chainId in s1.ListChainIds()) { Assert.Equal(s1.IterateIndexes(chainId), s2.IterateIndexes(chainId)); foreach (BlockHash blockHash in s1.IterateIndexes(chainId)) { Assert.Equal( s1.GetBlock <DumbAction>(fx.GetHashAlgorithm, blockHash), s2.GetBlock <DumbAction>(fx2.GetHashAlgorithm, blockHash) ); } } // ArgumentException is thrown if the destination store is not empty. Assert.Throws <ArgumentException>(() => Fx.Store.Copy(fx2.Store)); } }
public StoreTrackerTest() { _fx = new DefaultStoreFixture(); _tracker = new StoreTracker(_fx.Store); }
public BlockSetTest() { _fx = new DefaultStoreFixture(); _set = new BlockSet <DumbAction>(_fx.Store); }
public async Task PruneBlockStates() { using (StoreFixture fx = FxConstructor()) { IStore store = fx.Store; var blocks = new BlockChain <DumbAction>( new NullPolicy <DumbAction>(), store, Fx.GenesisBlock ); var privKey = new PrivateKey(); Transaction <DumbAction> tx1 = Transaction <DumbAction> .Create( 0, privKey, blocks.Genesis.Hash, new[] { new DumbAction(fx.Address1, "item0") }); Transaction <DumbAction> tx2 = Transaction <DumbAction> .Create( 1, privKey, blocks.Genesis.Hash, new[] { new DumbAction(fx.Address1, "item1") }); Transaction <DumbAction> tx3 = Transaction <DumbAction> .Create( 2, privKey, blocks.Genesis.Hash, new[] { new DumbAction(fx.Address1, "item2") }); blocks.StageTransaction(tx1); var block1 = await blocks.MineBlock(fx.Address2); blocks.StageTransaction(tx2); var block2 = await blocks.MineBlock(fx.Address2); blocks.StageTransaction(tx3); var block3 = await blocks.MineBlock(fx.Address2); Assert.Equal( (Text)"item0", blocks.GetState(fx.Address1, block1.Hash)); Assert.Equal( (Text)"item0,item1", blocks.GetState(fx.Address1, block2.Hash)); Assert.Equal( (Text)"item0,item1,item2", blocks.GetState(fx.Address1, block3.Hash)); store.PruneBlockStates(blocks.Id, block2); Assert.Throws <IncompleteBlockStatesException>( () => blocks.GetState(fx.Address1, block1.Hash)); Assert.Equal( (Text)"item0,item1", blocks.GetState(fx.Address1, block2.Hash)); Assert.Equal( (Text)"item0,item1,item2", blocks.GetState(fx.Address1, block3.Hash)); store.PruneBlockStates(blocks.Id, block3); Assert.Throws <IncompleteBlockStatesException>( () => blocks.GetState(fx.Address1, block1.Hash)); Assert.Throws <IncompleteBlockStatesException>( () => blocks.GetState(fx.Address1, block2.Hash)); Assert.Equal( (Text)"item0,item1,item2", blocks.GetState(fx.Address1, block3.Hash)); } }
public TransactionSetTest() { _fx = new DefaultStoreFixture(); _set = new TransactionSet <DumbAction>(_fx.Store); }
public BlockSetTest() { _fx = new MemoryStoreFixture(); _set = new BlockSet <DumbAction>(_fx.GetHashAlgorithm, _fx.Store); }
public StoreTrackerTest() { _fx = new LiteDBStoreFixture(); _tracker = new StoreTracker(_fx.Store); }
public StoreTrackerTest() { _fx = new MemoryStoreFixture(); _tracker = new StoreTracker(_fx.Store); }