Skip to content
2 changes: 1 addition & 1 deletion include/class/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function add(&$error = null, $syncing=false)
Cache::set("current", $this->toArray());
Cache::set("height", $this->height);
Cache::set("current_export", Block::export($hash));
Cache::set("mineInfo", Blockchain::getMineInfo());
Cache::set("mineInfo", Blockchain::calculateMineInfo());

Masternode::resetVerified();

Expand Down
8 changes: 7 additions & 1 deletion include/class/Blockchain.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static function getAvgBlockTime($blocks) {
return $elapsed / $blocks;
}

static function getMineInfo() {
static function calculateMineInfo() {
global $_config;
$diff = Block::difficulty();
$current = Block::current();
Expand All @@ -55,6 +55,12 @@ static function getMineInfo() {
return $res;
}

static function getMineInfo() {
return Cache::get("mineInfo", function () {
return Blockchain::calculateMineInfo();
});
}

static function addBlock(Block $block) {

}
Expand Down
17 changes: 7 additions & 10 deletions include/class/NodeMiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,13 @@ function start($mine_blocks = null, $sleep = 3) {

_log("Mining attempt={$this->attempt} height=$height difficulty=$difficulty elapsed=$elapsed hit=$hit target=$target speed={$this->speed} blockFound=$blockFound", 3);
$this->miningStat['hashes']++;
$mod = 10+$this->cpu;
if($this->attempt % $mod == 0) {
$info = $this->getMiningInfo();
if($info!==false) {
_log("Checking new block from server ".$info['block']. " with our block $prev_block_id", 4);
if($info['block']!= $prev_block_id) {
_log("New block received", 3);
$this->miningStat['dropped']++;
break;
}
$info = $this->getMiningInfo();
if($info!==false) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad logic

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Checking on every hash is inefficient due to the overhead of calling getMiningInfo, even with caching. I have updated the logic to check for a new block approximately once per second, based on the miner's hash speed. This should provide a good balance between responsiveness and performance.

_log("Checking new block from server ".$info['block']. " with our block $prev_block_id", 4);
if($info['block']!= $prev_block_id) {
_log("New block received", 3);
$this->miningStat['dropped']++;
break;
}
}

Expand Down