public async Task <List <Transaction> > RollbackTransactions(Hash chainId, ulong currentHeight, ulong specificHeight) { var txs = new List <Transaction>(); for (var i = currentHeight - 1; i >= specificHeight; i--) { var rollBackBlockHash = await _dataStore.GetAsync <Hash>( DataPath.CalculatePointerForGettingBlockHashByHeight(chainId, i)); var header = await _dataStore.GetAsync <BlockHeader>(rollBackBlockHash); var body = await _dataStore.GetAsync <BlockBody>( Hash.Xor( header.GetHash(), header.MerkleTreeRootOfTransactions)); foreach (var txId in body.Transactions) { var tx = await _dataStore.GetAsync <Transaction>(txId); if (tx == null) { _logger?.Trace($"tx {txId} is null."); } txs.Add(tx); await _dataStore.RemoveAsync <Transaction>(txId); } _logger?.Trace($"Rollback block hash: {rollBackBlockHash.Value.ToByteArray().ToHex()}"); } return(txs); }
public async Task <Block> GetNextBlockOf(Hash chainId, Hash blockHash) { var nextBlockHeight = (await GetBlockHeaderAsync(blockHash)).Index + 1; var nextBlockHash = await _dataStore.GetAsync <Hash>( DataPath.CalculatePointerForGettingBlockHashByHeight(chainId, nextBlockHeight)); return(await GetBlockAsync(nextBlockHash)); }
public async Task <Block> GetBlockByHeight(Hash chainId, ulong height) { _logger?.Trace($"Trying to get block by height {height}."); var key = DataPath.CalculatePointerForGettingBlockHashByHeight(chainId, height); if (key == null) { _logger?.Error($"Invalid block height - {height}."); return(null); } var blockHash = await _dataStore.GetAsync <Hash>(key); var blockHeader = await _dataStore.GetAsync <BlockHeader>(blockHash.OfType(HashType.BlockHeaderHash)); var blockBody = await _dataStore.GetAsync <BlockBody>(blockHash.OfType(HashType.BlockBodyHash)); return(new Block { Header = blockHeader, Body = blockBody }); }