public void Tip( [Argument("STORE-TYPE")] StoreType storeType, [Argument("STORE-PATH")] string storePath) { if (!Directory.Exists(storePath)) { throw new CommandExitedException($"The given STORE-PATH, {storePath} seems not existed.", -1); } IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <PolymorphicAction <ActionBase> >(); IBlockPolicy <NCAction> blockPolicy = new BlockPolicySource(Logger.None).GetPolicy(); IStore store = storeType.CreateStore(storePath); var stateStore = new TrieStateStore(new DefaultKeyValueStore(null)); Block <NCAction> genesisBlock = store.GetGenesisBlock <NCAction>(blockPolicy.GetHashAlgorithm); BlockChain <NCAction> chain = new BlockChain <NCAction>( blockPolicy, stagePolicy, store, stateStore, genesisBlock); _console.Out.WriteLine(Utils.SerializeHumanReadable(chain.Tip.Header)); (store as IDisposable)?.Dispose(); }
public void ToStoreConstructor(StoreType storeType, Type expectedType) { IStore store = storeType.CreateStore(_storePath); Assert.IsType(expectedType, store); (store as IDisposable)?.Dispose(); }
public void Tip( [Argument("STORE-TYPE")] StoreType storeType, [Argument("STORE-PATH")] string storePath) { if (!Directory.Exists(storePath)) { throw new CommandExitedException($"The given STORE-PATH, {storePath} seems not existed.", -1); } const int minimumDifficulty = 5000000, maximumTransactions = 100; IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <PolymorphicAction <ActionBase> >(); IBlockPolicy <NCAction> blockPolicy = new BlockPolicySource(Logger.None).GetPolicy(minimumDifficulty, maximumTransactions); IStore store = storeType.CreateStore(storePath); Block <NCAction> genesisBlock = store.GetGenesisBlock <NCAction>(); BlockChain <NCAction> chain = new BlockChain <NCAction>( blockPolicy, stagePolicy, store, new NoOpStateStore(), genesisBlock); _console.Out.WriteLine(JsonSerializer.Serialize(chain.Tip.Header)); }
public void GetGenesisBlock_ThrowsInvalidOperationException_IfChainIdNotExist(StoreType storeType) { IStore store = storeType.CreateStore(_storePath); Assert.Throws <InvalidOperationException>(() => store.GetGenesisBlock <NCAction>()); (store as IDisposable)?.Dispose(); }
public async Task Inspect(StoreType storeType) { Block <NCAction> genesisBlock = BlockChain <NCAction> .MakeGenesisBlock( HashAlgorithmType.Of <SHA256>() ); IStore store = storeType.CreateStore(_storePath2); Guid chainId = Guid.NewGuid(); store.SetCanonicalChainId(chainId); store.PutBlock(genesisBlock); store.AppendIndex(chainId, genesisBlock.Hash); var stateStore = new TrieStateStore(new DefaultKeyValueStore(null)); IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <PolymorphicAction <ActionBase> >(); IBlockPolicy <NCAction> blockPolicy = new BlockPolicySource(Logger.None).GetPolicy(); BlockChain <NCAction> chain = new BlockChain <NCAction>( blockPolicy, stagePolicy, store, stateStore, genesisBlock); var action = new HackAndSlash { costumes = new List <Guid>(), equipments = new List <Guid>(), foods = new List <Guid>(), worldId = 1, stageId = 1, avatarAddress = default }; var minerKey = new PrivateKey(); chain.MakeTransaction(minerKey, new PolymorphicAction <ActionBase>[] { action }); await chain.MineBlock(minerKey, DateTimeOffset.Now); store.Dispose(); _command.Inspect(storeType, _storePath2); List <double> output = _console.Out.ToString().Split("\n")[1] .Split(',').Select(double.Parse).ToList(); var totalTxCount = Convert.ToInt32(output[2]); var hackandslashCount = Convert.ToInt32(output[3]); Assert.Equal(1, totalTxCount); Assert.Equal(1, hackandslashCount); }
public void Tip(StoreType storeType) { HashAlgorithmType hashAlgo = HashAlgorithmType.Of <SHA256>(); Block <NCAction> genesisBlock = BlockChain <NCAction> .MakeGenesisBlock(hashAlgo); IStore store = storeType.CreateStore(_storePath); Guid chainId = Guid.NewGuid(); store.SetCanonicalChainId(chainId); store.PutBlock(genesisBlock); store.AppendIndex(chainId, genesisBlock.Hash); store.Dispose(); // FIXME For an unknown reason, BlockHeader.TimeStamp precision issue occurred and the store we should open it again. store = storeType.CreateStore(_storePath); genesisBlock = store.GetBlock <NCAction>(_ => hashAlgo, genesisBlock.Hash); store.Dispose(); _command.Tip(storeType, _storePath); Assert.Equal( Utils.SerializeHumanReadable(genesisBlock.Header), _console.Out.ToString().Trim() ); }
public void GetGenesisBlock(StoreType storeType) { IStore store = storeType.CreateStore(_storePath); Block <NCAction> genesisBlock = BlockChain <NCAction> .MakeGenesisBlock(); Guid chainId = Guid.NewGuid(); store.SetCanonicalChainId(chainId); store.PutBlock(genesisBlock); store.AppendIndex(chainId, genesisBlock.Hash); Assert.Equal(genesisBlock, store.GetGenesisBlock <NCAction>()); (store as IDisposable)?.Dispose(); }
public void Tip(StoreType storeType) { Block <NCAction> genesisBlock = BlockChain <NCAction> .MakeGenesisBlock(); IStore store = storeType.CreateStore(_storePath); Guid chainId = Guid.NewGuid(); store.SetCanonicalChainId(chainId); store.PutBlock(genesisBlock); store.AppendIndex(chainId, genesisBlock.Hash); (store as IDisposable)?.Dispose(); _command.Tip(storeType, _storePath); Assert.Equal(JsonSerializer.Serialize(genesisBlock.Header) + "\n", _console.Out.ToString()); }
public void Inspect( [Argument("STORE-TYPE", Description = "Store type of RocksDb (rocksdb or monorocksdb).")] StoreType storeType, [Argument("STORE-PATH", Description = "Store path to inspect.")] string storePath, [Argument("OFFSET", Description = "Offset of block index.")] int?offset = null, [Argument("LIMIT", Description = "Limit of block count.")] int?limit = null) { if (!Directory.Exists(storePath)) { throw new CommandExitedException($"The given STORE-PATH, {storePath} seems not existed.", -1); } IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <PolymorphicAction <ActionBase> >(); IBlockPolicy <NCAction> blockPolicy = new BlockPolicySource(Logger.None).GetPolicy(); IStore store = storeType.CreateStore(storePath); var stateStore = new TrieStateStore(new DefaultKeyValueStore(null)); Block <NCAction> genesisBlock = store.GetGenesisBlock <NCAction>(blockPolicy.GetHashAlgorithm); if (!(store.GetCanonicalChainId() is { } chainId)) { throw new CommandExitedException($"There is no canonical chain: {storePath}", -1); } if (!(store.IndexBlockHash(chainId, 0) is { } gHash)) { throw new CommandExitedException($"There is no genesis block: {storePath}", -1); } BlockChain <NCAction> chain = new BlockChain <NCAction>( blockPolicy, stagePolicy, store, stateStore, genesisBlock); long height = chain.Tip.Index; if (offset + limit > (int)height) { throw new CommandExitedException( $"The sum of the offset and limit is greater than the chain tip index: {height}", -1); } _console.Out.WriteLine("Block Index," + "Mining Time (sec)," + "Total Tx #," + "HAS #," + "RankingBattle #," + "Mimisbrunnr #"); var typeOfActionTypeAttribute = typeof(ActionTypeAttribute); foreach (var item in store.IterateIndexes(chain.Id, offset + 1 ?? 1, limit).Select((value, i) => new { i, value })) { var block = store.GetBlock <NCAction>(blockPolicy.GetHashAlgorithm, item.value); var previousBlock = store.GetBlock <NCAction>( blockPolicy.GetHashAlgorithm, block.PreviousHash ?? block.Hash ); var miningTime = block.Timestamp - previousBlock.Timestamp; var txCount = 0; var hackAndSlashCount = 0; var rankingBattleCount = 0; var mimisbrunnrBattleCount = 0; foreach (var tx in block.Transactions) { txCount++; foreach (var action in tx.Actions) { var actionTypeAttribute = Attribute.GetCustomAttribute(action.InnerAction.GetType(), typeOfActionTypeAttribute) as ActionTypeAttribute; if (actionTypeAttribute is null) { continue; } var typeIdentifier = actionTypeAttribute.TypeIdentifier; if (typeIdentifier.StartsWith("hack_and_slash")) { hackAndSlashCount++; } else if (typeIdentifier.StartsWith("ranking_battle")) { rankingBattleCount++; } else if (typeIdentifier.StartsWith("mimisbrunnr_battle")) { mimisbrunnrBattleCount++; } } } _console.Out.WriteLine($"{block.Index}," + $"{miningTime:s\\.ff}," + $"{txCount}," + $"{hackAndSlashCount}," + $"{rankingBattleCount}," + $"{mimisbrunnrBattleCount}"); } (store as IDisposable)?.Dispose(); }