Ejemplo n.º 1
0
        public void Ok()
        {
            using (IApplication app = PolkaApi.GetAppication())
            {
                app.Connect();
                // Get the most recent block number and hash
                var lastblock  = app.GetBlock(null);
                var lastNumber = (int)lastblock.Block.Header.Number;
                var lastHash   = app.GetBlockHash(new GetBlockHashParams {
                    BlockNumber = lastNumber
                });

                // Get the 10 blocks back number and hash
                var prm2 = new GetBlockHashParams
                {
                    BlockNumber = lastNumber - 10
                };
                var tenBackHash = app.GetBlockHash(prm2);
                output.WriteLine($"Most recent block  : {lastNumber}, hash: {lastHash.Hash} ");

                // Get timestamp history for last 50 blocks
                string        module   = "Timestamp";
                string        variable = "Now";
                string        key      = app.GetKeys("", module, variable);
                StorageItem[] items    = app.QueryStorage(key, tenBackHash.Hash, lastHash.Hash, 20);

                // Assert that item count is at least 10
                output.WriteLine($"Item count returned : {items.Length} ");
                Assert.True(items.Length >= 10);

                // Assert that all items are different (time changes every block)
                // set<string> values;
                foreach (var item in items)
                {
                    output.WriteLine($"Block: {item.BlockHash}, value: {item.Value} ");
                }

                Assert.True(items.ToList().Distinct(new StorageItemTimestampComparer()).Count() == items.Length);
                output.WriteLine("All timestamps are different");

                app.Disconnect();
            }
        }
Ejemplo n.º 2
0
        public BlockHash GetBlockHash(GetBlockHashParams param)
        {
            JArray prm = new JArray {
            };

            if (param != null)
            {
                prm = new JArray {
                    param.BlockNumber
                }
            }
            ;

            JObject query = new JObject {
                { "method", "chain_getBlockHash" }, { "params", prm }
            };

            JObject response = _jsonRpc.Request(query);

            return(Deserialize <BlockHash, ParseBlockHash>(response));
        }
Ejemplo n.º 3
0
        public int Connect(string node_url = "", string metadataBlockHash = null)
        {
            int result = Consts.PAPI_OK;

            // Connect to WS
            result = _jsonRpc.Connect(node_url);

            _protocolParams.GenesisBlockHash = new byte[Consts.BLOCK_HASH_SIZE];

            GetBlockHashParams par = new GetBlockHashParams();

            par.BlockNumber = 0;
            var genesisHashStr = GetBlockHash(par);

            for (int i = 0; i < Consts.BLOCK_HASH_SIZE; ++i)
            {
                _protocolParams.GenesisBlockHash[i] = Converters.FromHexByte(genesisHashStr.Hash.Substring(2 + i * 2));
            }

            // Read metadata for head block and initialize protocol parameters
            var meta = metadataBlockHash != null ? new GetMetadataParams {
                BlockHash = metadataBlockHash
            } : null;

            _protocolParams.Metadata            = new Metadata(GetMetadata(meta));
            _protocolParams.FreeBalanceHasher   = _protocolParams.Metadata.GetFuncHasher("Balances", "FreeBalance");
            _protocolParams.FreeBalancePrefix   = "Balances FreeBalance";
            _protocolParams.BalanceModuleIndex  = (byte)_protocolParams.Metadata.GetModuleIndex("Balances", true);
            _protocolParams.TransferMethodIndex = (byte)_protocolParams.Metadata.GetCallMethodIndex(
                _protocolParams.Metadata.GetModuleIndex("Balances", false), "transfer");

            if (_protocolParams.FreeBalanceHasher == Hasher.XXHASH)
            {
                _logger.Info("FreeBalance hash function is xxHash");
            }
            else
            {
                _logger.Info("FreeBalance hash function is Blake2-256");
            }

            _logger.Info($"Balances module index: {_protocolParams.BalanceModuleIndex}");
            _logger.Info($"Transfer call index: {_protocolParams.TransferMethodIndex}");

            // Calculate storage hashes
            _storageKeyCurrentEra = _protocolParams.Metadata.GetPlainStorageKey(_protocolParams.FreeBalanceHasher, "Staking CurrentEra");

            _storageKeySessionsPerEra =
                _protocolParams.Metadata.GetPlainStorageKey(_protocolParams.FreeBalanceHasher, "Staking SessionsPerEra");

            _storageKeyCurrentSessionIndex =
                _protocolParams.Metadata.GetPlainStorageKey(_protocolParams.FreeBalanceHasher, "Session CurrentIndex");

            try
            {
                _storageKeyBabeGenesisSlot = GetKeys("Babe", "GenesisSlot");
                //_storageKeyBabeCurrentSlot = GetKeys("Babe", "CurrentSlot");
                //_storageKeyBabeEpochIndex = GetKeys("Babe", "EpochIndex");
                _sessionsPerEra    = _protocolParams.Metadata.GetConst("Staking", "SessionsPerEra");
                _babeEpochDuration = _protocolParams.Metadata.GetConst("Babe", "EpochDuration");
            }
            catch (ApplicationException)
            {
                // Expected exception if Babe module is not present (e.g. Alexander network)
            }

            // Detect if epochs or sessions should be used
            _isEpoch = _storageKeyBabeGenesisSlot != null;
            if (_isEpoch)
            {
                _logger.Info("Using epochs");
            }
            else
            {
                _logger.Info("Using sessions");
            }

            return(result);
        }