The mining process from askcoin project (
www.askcoin.me) is:
void Blockchain::do_mine()
{
while(!m_stop.load(std::memory_order_relaxed))
{
if(!m_need_remine.load(std::memory_order_acquire))
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
std::list> mined_txs;
uint64 cur_block_id, cur_block_utc;
uint32 zero_bits;
std::string cur_block_hash, miner_key;
std::atomic mine_id_2 {0};
remine:
{
std::lock_guard guard(m_mine_mutex);
mined_txs = std::move(m_mined_txs);
mine_id_2.store(m_mine_id_1.load(std::memory_order_relaxed), std::memory_order_relaxed);
cur_block_id = m_mine_cur_block_id;
cur_block_utc = m_mine_cur_block_utc;
cur_block_hash = m_mine_cur_block_hash;
zero_bits = m_mine_zero_bits;
miner_key = m_miner_privkey;
m_need_remine.store(false, std::memory_order_relaxed);
}
char privk[32];
fly::base::base64_decode(miner_key.c_str(), miner_key.length(), privk, 32);
CKey miner_priv_key;
miner_priv_key.Set(privk, privk + 32, false);
CPubKey miner_pub_key = miner_priv_key.GetPubKey();
std::string miner_pub_key_b64 = fly::base::base64_encode(miner_pub_key.begin(), miner_pub_key.size());
auto doc_ptr = std::make_shared();
rapidjson::Document &doc = *doc_ptr;
doc.SetObject();
rapidjson::Document::AllocatorType &allocator = doc.GetAllocator();
doc.AddMember("hash", "", allocator);
doc.AddMember("sign", "", allocator);
rapidjson::Value data(rapidjson::kObjectType);
data.AddMember("id", cur_block_id + 1, allocator);
data.AddMember("utc", time(NULL), allocator);
data.AddMember("version", ASKCOIN_VERSION, allocator);
data.AddMember("zero_bits", zero_bits, allocator);
data.AddMember("pre_hash", rapidjson::Value(cur_block_hash.c_str(), allocator), allocator);
data.AddMember("miner", rapidjson::Value(miner_pub_key_b64.c_str(), allocator), allocator);
rapidjson::Value tx_ids(rapidjson::kArrayType);
for(auto tx : mined_txs)
{
tx_ids.PushBack(rapidjson::Value(tx->m_id.c_str(), allocator), allocator);
}
data.AddMember("tx_ids", tx_ids, allocator);
rapidjson::Value nonce(rapidjson::kArrayType);
nonce.PushBack(0, allocator);
nonce.PushBack(0, allocator);
nonce.PushBack(0, allocator);
nonce.PushBack(0, allocator);
data.AddMember("nonce", nonce, allocator);
char hash_raw[32];
for(uint64 i = 0; i < (uint64)-1; ++i)
{
for(uint64 j = 0; j < (uint64)-1; ++j)
{
for(uint64 k = 0; k < (uint64)-1; ++k)
{
for(uint64 m = 0; m < (uint64)-1; ++m)
{
if(m_stop.load(std::memory_order_relaxed))
{
return;
}
if(m_need_remine.load(std::memory_order_acquire))
{
goto remine;
}
uint64 utc = time(NULL);
if(utc < cur_block_utc)
{
utc = cur_block_utc;
}
data["utc"].SetUint64(utc);
data["nonce"][0] = m;
data["nonce"][1] = k;
data["nonce"][2] = j;
data["nonce"][3] = i;
rapidjson::StringBuffer buffer;
rapidjson::Writer writer(buffer);
data.Accept(writer);
uint32 buf[16] = {0};
char *p = (char*)buf;
coin_hash(buffer.GetString(), buffer.GetSize(), p);
char * ptr = buffer.Push(16);
memcpy(ptr, "another_32_bytes", 16);
coin_hash(buffer.GetString(), buffer.GetSize(), p + 32);
uint32 arr_16[16] = {0};
for(uint32 i = 0; i < 16; ++i)
{
arr_16[i] = ntohl(buf[i]);
}
for(uint32 i = 0; i < ASIC_RESISTANT_DATA_NUM;)
{
for(int j = 0; j < 16; ++j)
{
arr_16[j] = (arr_16[j] + __asic_resistant_data__[i + j]) * (arr_16[j] ^ __asic_resistant_data__[i + j]);
}
i += 16;
}
for(uint32 i = 0; i < 16; ++i)
{
buf[i] = htonl(arr_16[i]);
}
ptr = buffer.Push(88);
std::string p_b64 = fly::base::base64_encode(p, 64);
memcpy(ptr, p_b64.data(), 88);
coin_hash(buffer.GetString(), buffer.GetSize(), hash_raw);
uint32 zero_char_num = zero_bits / 8;
uint32 zero_remain_bit = 0;
for(uint32 i = 0; i < zero_char_num; ++i)
{
if(hash_raw[i] != 0)
{
goto try_next;
}
}
zero_remain_bit = zero_bits % 8;
if(zero_remain_bit == 0)
{
goto mine_success;
}
if((uint8)hash_raw[zero_char_num] < 1 << 8 - zero_remain_bit)
{
goto mine_success;
}
try_next:
;
}
}
}
}
mine_success:
std::string hex_hash = fly::base::byte2hexstr(hash_raw, 32);
std::string block_hash = fly::base::base64_encode(hash_raw, 32);
LOG_DEBUG_INFO("mine successfully, zero_bits: %u, block_id: %lu, block_hash: %s (hex: %s)", \
zero_bits, cur_block_id + 1, block_hash.c_str(), hex_hash.c_str());
doc["hash"].SetString(block_hash.c_str(), allocator);
std::vector sign_vec;
if(!miner_priv_key.Sign(uint256(std::vector(hash_raw, hash_raw + 32)), sign_vec))
{
ASKCOIN_EXIT(EXIT_FAILURE);
}
std::string block_sign = fly::base::base64_encode(&sign_vec[0], sign_vec.size());
doc["sign"].SetString(block_sign.c_str(), allocator);
doc.AddMember("data", data, allocator);
rapidjson::Value doc_tx(rapidjson::kArrayType);
for(auto tx : mined_txs)
{
rapidjson::Document &doc = *tx->m_doc;
rapidjson::Value tx_node(rapidjson::kObjectType);
tx_node.AddMember("sign", doc["sign"], allocator);
tx_node.AddMember("data", doc["data"], allocator);
doc_tx.PushBack(tx_node, allocator);
}
doc.AddMember("tx", doc_tx, allocator);
{
std::lock_guard guard(m_mine_mutex);
m_mine_doc = doc_ptr;
m_mine_id_2.store(mine_id_2.load(std::memory_order_relaxed), std::memory_order_relaxed);
}
m_mine_success.store(true, std::memory_order_release);
}
}
The mainnet of askcoin is coming soon, the source code will be open when this project has passed its infancy.
FYI:
https://bitcointalksearch.org/topic/annaskcoin-is-a-decentralized-real-time-qa-platform-3733771