/// <summary> /// Initializes a new instance of the <see cref="PutBlockListItem"/> class. /// </summary> /// <param name="id">The block ID.</param> /// <param name="searchMode">One of the enumeration values that specifies in which block lists to search for the block.</param> public PutBlockListItem(string id, BlockSearchMode searchMode) { this.Id = id; this.SearchMode = searchMode; }
// Static function used by the getMiningBlock API call public static Block getMiningBlock(BlockSearchMode searchMode) { Block candidate_block = null; lock (activePoolBlockLock) { if (activePoolBlock != null) { return(activePoolBlock); } } List <Block> blockList = null; int block_offset = 1; if (Node.blockChain.Count >= (long)ConsensusConfig.getRedactedWindowSize()) { block_offset = 1000; } if (searchMode == BlockSearchMode.lowestDifficulty) { blockList = Node.blockChain.getBlocks(block_offset, (int)Node.blockChain.Count - block_offset).Where(x => x.powField == null).OrderBy(x => x.difficulty).ToList(); } else if (searchMode == BlockSearchMode.randomLowestDifficulty) { Random rnd = new Random(); blockList = Node.blockChain.getBlocks(block_offset, (int)Node.blockChain.Count - block_offset).Where(x => x.powField == null).OrderBy(x => x.difficulty).Skip(rnd.Next(200)).ToList(); } else if (searchMode == BlockSearchMode.latestBlock) { blockList = Node.blockChain.getBlocks(block_offset, (int)Node.blockChain.Count - block_offset).Where(x => x.powField == null).OrderByDescending(x => x.blockNum).ToList(); } else if (searchMode == BlockSearchMode.random) { Random rnd = new Random(); blockList = Node.blockChain.getBlocks(block_offset, (int)Node.blockChain.Count - block_offset).Where(x => x.powField == null).OrderBy(x => rnd.Next()).ToList(); } // Check if the block list exists if (blockList == null) { Logging.error("No block list found while searching."); return(null); } // Go through each block in the list foreach (Block block in blockList) { if (block.powField == null) { ulong solved = 0; lock (solvedBlocks) { solved = solvedBlocks.Find(x => x == block.blockNum); } // Check if this block is in the solved list if (solved > 0) { // Do nothing at this point } else { // Block is not solved, select it candidate_block = block; if (searchMode == BlockSearchMode.random || searchMode == BlockSearchMode.randomLowestDifficulty) { lock (activePoolBlockLock) { activePoolBlock = candidate_block; } } break; } } } return(candidate_block); }
// Returns an empty PoW block based on the search algorithm provided as a parameter private JsonResponse onGetMiningBlock(Dictionary <string, object> parameters) { if (!parameters.ContainsKey("algo")) { JsonError error = new JsonError { code = (int)RPCErrorCode.RPC_INVALID_PARAMETER, message = "Parameter 'algo' is missing" }; return(new JsonResponse { result = null, error = error }); } int algo = int.Parse((string)parameters["algo"]); BlockSearchMode searchMode = BlockSearchMode.randomLowestDifficulty; if (algo == (int)BlockSearchMode.lowestDifficulty) { searchMode = BlockSearchMode.lowestDifficulty; } else if (algo == (int)BlockSearchMode.randomLowestDifficulty) { searchMode = BlockSearchMode.randomLowestDifficulty; } else if (algo == (int)BlockSearchMode.latestBlock) { searchMode = BlockSearchMode.latestBlock; } else if (algo == (int)BlockSearchMode.random) { searchMode = BlockSearchMode.random; } else { return(new JsonResponse { result = null, error = new JsonError() { code = (int)RPCErrorCode.RPC_INVALID_PARAMS, message = "Invalid algorithm was specified" } }); } Block block = Miner.getMiningBlock(searchMode); if (block == null) { return(new JsonResponse { result = null, error = new JsonError() { code = (int)RPCErrorCode.RPC_INTERNAL_ERROR, message = "Cannot retrieve mining block" } }); } byte[] solver_address = Node.walletStorage.getPrimaryAddress(); Dictionary <string, Object> resultArray = new Dictionary <string, Object> { { "num", block.blockNum }, // Block number { "ver", block.version }, // Block version { "dif", block.difficulty }, // Block difficulty { "chk", block.blockChecksum }, // Block checksum { "adr", solver_address } // Solver address }; return(new JsonResponse { result = resultArray, error = null }); }
public PutBlockListItem(string id, BlockSearchMode searchMode) { throw new System.NotImplementedException(); }