示例#1
0
        public async Task Test_GetLastBlock()
        {
            var getLastBlock = GetLastBlock.Create(Consts.ApiUrl.TestNet);
            var lastBlock    = await getLastBlock();

            var getBlockByHeight = GetBlockByHeight.Create(Consts.ApiUrl.TestNet);
            var blockByHeight    = await getBlockByHeight(lastBlock.Height.Value);

            Assert.AreEqual(lastBlock.Height, blockByHeight.Height);
            Assert.AreEqual(lastBlock.Hash, blockByHeight.Hash);
            Assert.AreEqual(lastBlock.Signature, blockByHeight.Signature);

            var getBlockByHash = GetBlockByHash.Create(Consts.ApiUrl.TestNet);
            var blockByHash    = await getBlockByHash(lastBlock.Hash);

            Assert.AreEqual(lastBlock.Height, blockByHash.Height);
            Assert.AreEqual(lastBlock.Hash, blockByHash.Hash);
            Assert.AreEqual(lastBlock.Signature, blockByHash.Signature);
        }
示例#2
0
        private async Task <GetLastBlock> GetLastBlock()
        {
            if (!memoryCache_.TryGetValue(Constants.Cache.LAST_BLOCK_HEIGHT_CACHE_KEY, out ulong currentHeight))
            {
                var getLastHeightResult = await chain_.FetchLastHeightAsync();

                Utils.CheckBitprimApiErrorCode(getLastHeightResult.ErrorCode, "FetchLastHeightAsync() failed");
                currentHeight = getLastHeightResult.Result;

                memoryCache_.Set(Constants.Cache.LAST_BLOCK_HEIGHT_CACHE_KEY, currentHeight,
                                 new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = Constants.Cache.LAST_BLOCK_HEIGHT_MAX_AGE,
                    Size = Constants.Cache.LAST_BLOCK_HEIGHT_ENTRY_SIZE
                });
            }

            if (!memoryCache_.TryGetValue(Constants.Cache.LAST_BLOCK_CACHE_KEY, out GetLastBlock ret))
            {
                using (var getBlockDataResult = await chain_.FetchBlockHeaderByHeightAsync(currentHeight))
                {
                    Utils.CheckBitprimApiErrorCode(getBlockDataResult.ErrorCode, "FetchBlockHeaderByHeightAsync(" + currentHeight + ") failed");

                    ret = new GetLastBlock
                    {
                        BlockHeight = getBlockDataResult.Result.BlockHeight
                        , Bits      = getBlockDataResult.Result.BlockData.Bits
                        , Hash      = getBlockDataResult.Result.BlockData.Hash
                    };

                    memoryCache_.Set(Constants.Cache.LAST_BLOCK_CACHE_KEY, ret,
                                     new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = Constants.Cache.LAST_BLOCK_MAX_AGE,
                        Size = Constants.Cache.LAST_BLOCK_ENTRY_SIZE
                    });
                }
            }

            return(ret);
        }