Esempio n. 1
0
 public RocksDBKeyValueStoreTest()
 {
     KeyValueStore = _rocksDbKeyValueStore = new RocksDBKeyValueStore(Path.Combine(
                                                                          Path.GetTempPath(),
                                                                          $"rocksdb_key_value_test_{Guid.NewGuid()}"));
     InitializePreStoredData();
 }
Esempio n. 2
0
        public IStateStore LoadTrieStateStore(string path)
        {
            IKeyValueStore stateKeyValueStore =
                new RocksDBKeyValueStore(System.IO.Path.Combine(path, "states"));

            return(new TrieStateStore(stateKeyValueStore));
        }
Esempio n. 3
0
        protected (IStore, IStateStore) LoadStore(string path, string type, int statesCacheSize)
        {
            IStore store = null;

            if (type == "rocksdb")
            {
                try
                {
                    store = new RocksDBStore.RocksDBStore(
                        path,
                        maxTotalWalSize: 16 * 1024 * 1024,
                        maxLogFileSize: 16 * 1024 * 1024,
                        keepLogFileNum: 1
                        );
                    Log.Debug("RocksDB is initialized.");
                }
                catch (TypeInitializationException e)
                {
                    Log.Error("RocksDB is not available. DefaultStore will be used. {0}", e);
                }
            }
            else if (type == "monorocksdb")
            {
                try
                {
                    store = new RocksDBStore.MonoRocksDBStore(
                        path,
                        maxTotalWalSize: 16 * 1024 * 1024,
                        maxLogFileSize: 16 * 1024 * 1024,
                        keepLogFileNum: 1
                        );
                    Log.Debug("MonoRocksDB is initialized.");
                }
                catch (TypeInitializationException e)
                {
                    Log.Error("MonoRocksDB is not available. DefaultStore will be used. {0}", e);
                }
            }
            else
            {
                var message = type is null
                    ? "Storage Type is not specified"
                    : $"Storage Type {type} is not supported";
                Log.Debug($"{message}. DefaultStore will be used.");
            }

            store ??= new DefaultStore(path, flush: false);
            store = new ReducedStore(store);

            IKeyValueStore stateKeyValueStore     = new RocksDBKeyValueStore(Path.Combine(path, "states")),
                           stateHashKeyValueStore = new RocksDBKeyValueStore(Path.Combine(path, "state_hashes"));
            IStateStore stateStore = new TrieStateStore(stateKeyValueStore, stateHashKeyValueStore);

            return(store, stateStore);
        }
Esempio n. 4
0
        public static (BlockChain <NCAction> Chain, IStore Store) GetBlockChain(
            ILogger logger,
            string storePath,
            bool monorocksdb = false,
            Guid?chainId     = null
            )
        {
            var policySource = new BlockPolicySource(logger);
            IBlockPolicy <NCAction> policy      = policySource.GetPolicy();
            IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <NCAction>();
            IStore store
                = monorocksdb
                ? (IStore) new MonoRocksDBStore(storePath)
                : new RocksDBStore(storePath);
            IKeyValueStore stateKeyValueStore =
                new RocksDBKeyValueStore(Path.Combine(storePath, "states"));
            IStateStore stateStore = new TrieStateStore(stateKeyValueStore);
            Guid        chainIdValue
                = chainId ??
                  store.GetCanonicalChainId() ??
                  throw new CommandExitedException(
                            "No canonical chain ID.  Available chain IDs:\n    " +
                            string.Join("\n    ", store.ListChainIds()),
                            1);

            BlockHash genesisBlockHash;

            try
            {
                genesisBlockHash = store.IterateIndexes(chainIdValue).First();
            }
            catch (InvalidOperationException)
            {
                throw new CommandExitedException(
                          $"The chain {chainIdValue} seems empty; try with another chain ID:\n    " +
                          string.Join("\n    ", store.ListChainIds()),
                          1
                          );
            }
            Block <NCAction> genesis = store.GetBlock <NCAction>(
                policy.GetHashAlgorithm,
                genesisBlockHash
                );
            BlockChain <NCAction> chain = new BlockChain <NCAction>(
                policy,
                stagePolicy,
                store,
                stateStore,
                genesis
                );

            return(chain, store);
        }
Esempio n. 5
0
 public RocksDBKeyValueStoreTest()
 {
     try
     {
         KeyValueStore = _rocksDbKeyValueStore = new RocksDBKeyValueStore(Path.Combine(
                                                                              Path.GetTempPath(),
                                                                              $"rocksdb_key_value_test_{Guid.NewGuid()}"));
         InitializePreStoredData();
     }
     catch (TypeInitializationException)
     {
         throw new SkipException("RocksDB is not available.");
     }
 }
Esempio n. 6
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.Error.WriteLine("Too few arguments.");
                Environment.Exit(1);
                return;
            }

            string storePath = args[0];
            int    limit     = int.Parse(args[1]);
            int    offset    = 0;

            if (args.Length >= 3)
            {
                offset = int.Parse(args[2]);
            }

            if (limit < 0)
            {
                Console.Error.WriteLine("Limit value must be greater than 0. Entered value: {0}", limit);
                Environment.Exit(1);
                return;
            }

            if (offset < 0)
            {
                Console.Error.WriteLine("Offset value must be greater than 0. Entered value: {0}", offset);
                Environment.Exit(1);
                return;
            }

            Log.Logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console().CreateLogger();
            Libplanet.Crypto.CryptoConfig.CryptoBackend = new Secp256K1CryptoBackend <SHA256>();
            var policySource = new BlockPolicySource(Log.Logger, LogEventLevel.Verbose);
            IBlockPolicy <NCAction> policy =
                policySource.GetPolicy(
                    // Explicitly set to lowest possible difficulty.
                    minimumDifficulty: BlockPolicySource.DifficultyStability,
                    maxBlockBytesPolicy: null,
                    minTransactionsPerBlockPolicy: null,
                    maxTransactionsPerBlockPolicy: null,
                    maxTransactionsPerSignerPerBlockPolicy: null,
                    authorizedMinersPolicy: null,
                    permissionedMinersPolicy: null);
            IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <NCAction>();
            var store = new RocksDBStore(storePath);

            if (!(store.GetCanonicalChainId() is Guid chainId))
            {
                Console.Error.WriteLine("There is no canonical chain: {0}", storePath);
                Environment.Exit(1);
                return;
            }

            if (!(store.IndexBlockHash(chainId, 0) is { } gHash))
            {
                Console.Error.WriteLine("There is no genesis block: {0}", storePath);
                Environment.Exit(1);
                return;
            }

            DateTimeOffset   started            = DateTimeOffset.UtcNow;
            Block <NCAction> genesis            = store.GetBlock <NCAction>(policy.GetHashAlgorithm, gHash);
            IKeyValueStore   stateKeyValueStore = new RocksDBKeyValueStore(Path.Combine(storePath, "states"));
            var  stateStore = new TrieStateStore(stateKeyValueStore);
            var  chain      = new BlockChain <NCAction>(policy, stagePolicy, store, stateStore, genesis);
            long height     = chain.Tip.Index;

            if (offset + limit > (int)height)
            {
                Console.Error.WriteLine(
                    "The sum of the offset and limit is greater than the chain tip index: {0}", height);
                Environment.Exit(1);
                return;
            }

            BlockHash[] blockHashes = store.IterateIndexes(chain.Id, offset, limit).Select((value, i) => value).ToArray();
            Console.Error.WriteLine(
                "Executing {0} blocks: {1}-{2} (inclusive).",
                blockHashes.Length,
                blockHashes[0],
                blockHashes.Last()
                );
            Block <NCAction>[] blocks       = blockHashes.Select(h => chain[h]).ToArray();
            DateTimeOffset     blocksLoaded = DateTimeOffset.UtcNow;
            long txs     = 0;
            long actions = 0;

            foreach (Block <NCAction> block in blocks)
            {
                Console.Error.WriteLine(
                    "Block #{0} {1}; {2} txs",
                    block.Index,
                    block.Hash,
                    block.Transactions.Count()
                    );

                IEnumerable <ActionEvaluation> blockEvals =
                    chain.ExecuteActions(block, StateCompleterSet <NCAction> .Reject);
                SetStates(
                    chain.Id,
                    store,
                    stateStore,
                    block,
                    blockEvals.ToArray(),
                    buildStateReferences: true
                    );
                txs     += block.Transactions.LongCount();
                actions += block.Transactions.Sum(tx => tx.Actions.LongCount()) + 1;
            }

            DateTimeOffset ended = DateTimeOffset.UtcNow;

            Console.WriteLine("Loading blocks\t{0}", blocksLoaded - started);
            long execActionsTotalMilliseconds = (long)(ended - blocksLoaded).TotalMilliseconds;

            Console.WriteLine("Executing actions\t{0}ms", execActionsTotalMilliseconds);
            Console.WriteLine("Average per block\t{0}ms", execActionsTotalMilliseconds / blockHashes.Length);
            Console.WriteLine("Average per tx\t{0}ms", execActionsTotalMilliseconds / txs);
            Console.WriteLine("Average per action\t{0}ms", execActionsTotalMilliseconds / actions);
            Console.WriteLine("Total elapsed\t{0}", ended - started);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.Error.WriteLine("Too few arguments.");
                Environment.Exit(1);
                return;
            }

            string  storePath = args[0];
            int     limit     = int.Parse(args[1]);
            ILogger logger    = new LoggerConfiguration().CreateLogger();

            Libplanet.Crypto.CryptoConfig.CryptoBackend = new Secp256K1CryptoBackend <SHA256>();
            var policySource = new BlockPolicySource(logger, LogEventLevel.Verbose);
            IBlockPolicy <NCAction> policy =
                policySource.GetPolicy(BlockPolicySource.DifficultyBoundDivisor + 1, 0);
            IStagePolicy <NCAction> stagePolicy = new VolatileStagePolicy <NCAction>();
            var store = new RocksDBStore(storePath);

            if (!(store.GetCanonicalChainId() is Guid chainId))
            {
                Console.Error.WriteLine("There is no canonical chain: {0}", storePath);
                Environment.Exit(1);
                return;
            }

            if (!(store.IndexBlockHash(chainId, 0) is HashDigest <SHA256> gHash))
            {
                Console.Error.WriteLine("There is no genesis block: {0}", storePath);
                Environment.Exit(1);
                return;
            }

            DateTimeOffset   started = DateTimeOffset.UtcNow;
            Block <NCAction> genesis = store.GetBlock <NCAction>(gHash);
            IKeyValueStore   stateRootKeyValueStore = new RocksDBKeyValueStore(Path.Combine(storePath, "state_hashes")),
                             stateKeyValueStore     = new RocksDBKeyValueStore(Path.Combine(storePath, "states"));
            IStateStore stateStore = new TrieStateStore(stateKeyValueStore, stateRootKeyValueStore);
            var         chain      = new BlockChain <NCAction>(policy, stagePolicy, store, stateStore, genesis);
            long        height     = chain.Tip.Index;

            HashDigest <SHA256>[] blockHashes = limit < 0
                ? chain.BlockHashes.SkipWhile((_, i) => i < height + limit).ToArray()
                : chain.BlockHashes.Take(limit).ToArray();
            Console.Error.WriteLine(
                "Executing {0} blocks: {1}-{2} (inclusive).",
                blockHashes.Length,
                blockHashes[0],
                blockHashes.Last()
                );
            Block <NCAction>[] blocks       = blockHashes.Select(h => chain[h]).ToArray();
            DateTimeOffset     blocksLoaded = DateTimeOffset.UtcNow;
            long txs     = 0;
            long actions = 0;

            foreach (Block <NCAction> block in blocks)
            {
                Console.Error.WriteLine(
                    "Block #{0} {1}; {2} txs",
                    block.Index,
                    block.Hash,
                    block.Transactions.Count()
                    );

                IEnumerable <ActionEvaluation> blockEvals;
                if (block.Index > 0)
                {
                    blockEvals = block.Evaluate(
                        DateTimeOffset.UtcNow,
                        address => chain.GetState(address, block.Hash),
                        (address, currency) => chain.GetBalance(address, currency, block.Hash)
                        );
                }
                else
                {
                    blockEvals = block.Evaluate(
                        DateTimeOffset.UtcNow,
                        _ => null,
                        ((_, currency) => new FungibleAssetValue(currency))
                        );
                }
                SetStates(chain.Id, stateStore, block, blockEvals.ToArray(), buildStateReferences: true);
                txs     += block.Transactions.LongCount();
                actions += block.Transactions.Sum(tx => tx.Actions.LongCount()) + 1;
            }

            DateTimeOffset ended = DateTimeOffset.UtcNow;

            Console.WriteLine("Loading blocks\t{0}", blocksLoaded - started);
            long execActionsTotalMilliseconds = (long)(ended - blocksLoaded).TotalMilliseconds;

            Console.WriteLine("Executing actions\t{0}ms", execActionsTotalMilliseconds);
            Console.WriteLine("Average per block\t{0}ms", execActionsTotalMilliseconds / blockHashes.Length);
            Console.WriteLine("Average per tx\t{0}ms", execActionsTotalMilliseconds / txs);
            Console.WriteLine("Average per action\t{0}ms", execActionsTotalMilliseconds / actions);
            Console.WriteLine("Total elapsed\t{0}", ended - started);
        }