public static void OnGetMcBlock(HttpMessage httpMessage) { Dictionary <string, string> map = new Dictionary <string, string>(); // 版本检查 string version = httpMessage.map["version"]; if (version != Miner.version) { map.Remove("report"); map.Add("report", "error"); map.Add("tips", $"Miner.version: {Miner.version}"); return; } // 矿池登记检查 if (Pool.registerPool && poolIpAddress.IndexOf(httpMessage.request.RemoteEndPoint.Address.ToString()) == -1) { map.Add("tips", "need register Pool"); return; } long ___height = long.Parse(httpMessage.map["height"]); var mcblk = BlockChainHelper.GetMcBlock(___height); if (mcblk != null) { var poolBlock = new PoolBlock(); poolBlock.hash = mcblk.hash; poolBlock.Address = mcblk.Address; poolBlock.random = mcblk.random; httpMessage.result = JsonHelper.ToJson(poolBlock); } }
public Block GetMcBlock(long ___height) { if (___height <= 1) { return(null); } try { PoolBlock poolBlock = null; using (DbSnapshot snapshot = pool.PoolDBStore.GetSnapshot()) { var str = snapshot.Get($"PoolBlock_{___height}"); if (!string.IsNullOrEmpty(str)) { poolBlock = JsonHelper.FromJson <PoolBlock>(str); if (poolBlock != null && Wallet.CheckAddress(poolBlock.Address)) { return(new Block() { hash = poolBlock.hash, Address = poolBlock.Address, random = poolBlock.random }); } } } HttpMessage quest = new HttpMessage(); quest.map = new Dictionary <string, string>(); quest.map.Add("cmd", "GetMcBlock"); quest.map.Add("version", version); quest.map.Add("height", ___height.ToString()); var result = ComponentNetworkHttp.QueryStringSync($"http://{poolUrl}", quest, 5); if (string.IsNullOrEmpty(result)) { return(null); } poolBlock = JsonHelper.FromJson <PoolBlock>(result); if (poolBlock != null && Wallet.CheckAddress(poolBlock.Address)) { using (DbSnapshot snapshot = pool.PoolDBStore.GetSnapshot()) { snapshot.Add($"PoolBlock_{___height}", JsonHelper.ToJson(poolBlock)); snapshot.Commit(); } return(new Block() { hash = poolBlock.hash, Address = poolBlock.Address, random = poolBlock.random }); } } catch (Exception) { } return(null); }
void Awake() { loaded = true; // required to allow creation or modification of pools at runtime. (Timing of script creation and initialization can get wonkey) if (poolBlock == null) { poolBlock = new PoolBlock(0, AP_enum.EmptyBehavior.Grow, 0, AP_enum.MaxEmptyBehavior.Fail, null, false); } else { poolBlock = new PoolBlock(poolBlock.size, poolBlock.emptyBehavior, poolBlock.maxSize, poolBlock.maxEmptyBehavior, poolBlock.prefab, poolBlock.printLogOnQuit); } pool = new Stack <PoolItem>(); masterPool = new List <PoolItem>(); origSize = Mathf.Max(0, poolBlock.size); poolBlock.size = 0; for (int i = 0; i < origSize; i++) { CreateObject(true); } }
private void GrowPool() { HLSTrace.WriteLine("Try to grow pool"); int absoluteAllocOffset = _allocIndex._blockIndex * _blockSize + _allocIndex._blockOffset; int absoluteReclaimOffset = _reclaimIndex._blockIndex * _blockSize + _reclaimIndex._blockOffset; if (absoluteAllocOffset >= absoluteReclaimOffset && _freeSize != 0) { // Allocation offset is bigger or equal to reclaim offset, and free size is not zero, // inserting a new elelment at the end is the cheapest way to grwo the pool _blockCount++; PoolBlock poolBlock = new PoolBlock(); poolBlock._bytes = new byte[_blockSize]; _listOfBlocks.Add(poolBlock); _totalSize += _blockSize; _freeSize += _blockSize; } else if (absoluteAllocOffset > absoluteReclaimOffset && _freeSize == 0) { Debug.Assert(false, "Against design assumption!"); } else { // Insert a new elelment after allocation block PoolBlock poolBlock = new PoolBlock(); poolBlock._bytes = new byte[_blockSize]; if (_allocIndex._blockIndex == _reclaimIndex._blockIndex) { // Move Array.Copy(_listOfBlocks[_reclaimIndex._blockIndex]._bytes, _reclaimIndex._blockOffset, poolBlock._bytes, _reclaimIndex._blockOffset, _blockSize - _reclaimIndex._blockOffset); } for (int i = 0; i < _listOfAllocItems.Count; i++) { int absoluteItemStartIndex = _listOfAllocItems[i].StartIndex._blockIndex * _blockSize + _listOfAllocItems[i].StartIndex._blockOffset; if (absoluteItemStartIndex >= absoluteReclaimOffset) { _listOfAllocItems[i].IncrementIndex(); } } _listOfBlocks.Insert(_allocIndex._blockIndex + 1, poolBlock); _totalSize += _blockSize; _freeSize += _blockSize; _reclaimIndex._blockIndex++; _blockCount++; } _growCount++; if (_listOfAllocItems.Count > 0) { Debug.Assert(_listOfAllocItems[0].StartIndex._blockIndex == _reclaimIndex._blockIndex && _listOfAllocItems[0].StartIndex._blockOffset == _reclaimIndex._blockOffset); } }
/// <summary> /// Common construct a growable and shrinkable fifo memory pool with block size /// </summary> /// <param name="initialPoolSize">Initial pool size</param> /// <param name="shrinkThresholdPercentage">When free size is over shrinkThresholdPercentage of total size, /// pool tries to shrink. But it will never shrink down to smaller than initial size /// </param> /// <param name="blockSize">Block size of pool, pool grows by block</param> private void CommonConstruct(int initialPoolSize, double shrinkThresholdPercentage, int blockSize) { if (shrinkThresholdPercentage <= 0.0 || shrinkThresholdPercentage > 1.0) { throw new System.NotSupportedException("shrinkThresholdPercentage must be bigger than 0.0f, smaller or equal to 1.0"); } _blockSize = blockSize; _blockCount = (initialPoolSize + _blockSize - 1) / _blockSize; if (0 == _blockCount) { _blockCount = 1; } _listOfBlocks = new List<PoolBlock>(); for (int i = 0; i < _blockCount; i++) { PoolBlock poolBlock = new PoolBlock(); poolBlock._bytes = new byte[_blockSize]; _listOfBlocks.Add(poolBlock); } _totalSize = _blockCount * _blockSize; _allocIndex = new PoolBlockIndex(0, 0); _reclaimIndex = new PoolBlockIndex(0, 0); _freeSize = _totalSize; _listOfAllocItems = new List<PoolAllocItem>(); _itemPool = new Queue<PoolAllocItem>(); _allocCount = 0; _freeCount = 0; _accumulatedAllocSize = 0; _shrinkThresholdPercentage = shrinkThresholdPercentage; _initialSize = _totalSize; }