i have start to convert by hand a python script to php its an example how to mine in python but i want implement a kind of same in my php project
That its what i did until now

$MAX_NONCE = 100000000000;

$prefix_zeros = need convertion;
//execute sql statement and return a single field value
$params        = array('active');
$value         = $db->rawQueryValue("SELECT id FROM mdlabsnetwork WHERE id = ? LIMIT 1", $params);

$block_number = $value;

//execute sql statement and return a single field value
$params        = array('active');
$value         = $db->rawQueryValue("SELECT hash FROM mdlabsnetwork WHERE hash = ? LIMIT 1", $params);

$previous_hash= $value;

$transactions=password_hash("New Job From Network->mdlabsnetwork->1", PASSWORD_DEFAULT);

$newhashtext = password_hash($previous_hash . ' ' .'".USER_NAME."'.' ' .'".datetime_now()."', PASSWORD_DEFAULT);

function getnewhash($newhashtext){
    return $newhashtext;
}

function mine($block_number, $transactions, $previous_hash, $prefix_zeros){
    $prefix_str = '0' * $prefix_zeros;

}

And this is the main python script in question

from hashlib import sha256
MAX_NONCE = 100000000000

def SHA256(text):
    return sha256(text.encode("ascii")).hexdigest()

def mine(block_number, transactions, previous_hash, prefix_zeros):
    prefix_str = '0'*prefix_zeros
    for nonce in range(MAX_NONCE):
        text = str(block_number) + transactions + previous_hash + str(nonce)
        new_hash = SHA256(text)
        if new_hash.startswith(prefix_str):
            print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
            return new_hash

    raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")

if __name__=='__main__':
    transactions='''
    Dhaval->Bhavin->20,
    Mando->Cara->45
    '''
    difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
    import time
    start = time.time()
    print("start mining")
    new_hash = mine(5,transactions,'0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
    total_time = str((time.time() - start))
    print(f"end mining. Mining took: {total_time} seconds")
    print(new_hash)

Can some one help me finish the convertion , Tank you in advance.

Recommended Answers

If I were to look into this I would not convert such as this would be a lot of work duplicating what looks to be BITCOIN MINING.

Instead I'd see what BITCOIN LIBRARIES might be out there and then craft around said libraries found with

Jump to Post

All 3 Replies

If I were to look into this I would not convert such as this would be a lot of work duplicating what looks to be BITCOIN MINING.

Instead I'd see what BITCOIN LIBRARIES might be out there and then craft around said libraries found with
https://www.google.com/search?&q=php+bitcoin+mining

Creating the functions that we see in your Python code (remember I always think this is your code you wrote and not found code) would be quite the exercise for you.

In fact tank you all any way but i think i did the full convertion on it alone and i think its very close to what i want
I let here the full code maybe some one can make some tune up.

$MAX_NONCE = 100000000000;
$newhashtext = "";
$prefix_zeros = ?;
//execute sql statement and return a single field value
$params        = array('active');
$value         = $db->rawQueryValue("SELECT id FROM mdlabsnetwork WHERE id = ? LIMIT 1", $params);

$block_number = $value;

//execute sql statement and return a single field value
$params        = array('active');
$value         = $db->rawQueryValue("SELECT hash FROM mdlabsnetwork WHERE hash = ? LIMIT 1", $params);

$previous_hash= $value;

$transactions=password_hash("New Job From Network->mdlabsnetwork->1", PASSWORD_DEFAULT);

$newhashtext = password_hash($previous_hash . ' ' .'".USER_NAME."'.' ' .'".datetime_now()."', PASSWORD_DEFAULT);

function getnewhash($newhashtext){
    return $newhashtext;
}

function mine($block_number, $transactions, $previous_hash, $prefix_zeros){
    $prefix_str = '0' * $prefix_zeros;
    foreach (nonce) in range(MAX_NONCE)) {
        $newhashtext = $block_number + $transactions + $previous_hash + nonce
        getnewhash($newhashtext);
        if (strpos($newhashtext, '0000') === 0) {
            $difficulty=4;
            // missing the start a timer
            $starttime = microtime(true);
            print("Yay! Successfully mined bitcoins with nonce value", nonce);
            return $newhashtext;
            print("start mining your post hash...");
            $newhashtext = password_hash($previous_hash . ' ' .'".USER_NAME."'.' ' .'".datetime_now()."', PASSWORD_DEFAULT);
            mine($block_number, $transactions, $previous_hash, $difficulty);
            // microtime(true) returns the unix timestamp plus milliseconds as a float
            $endtime = microtime(true);
            $timediff = $endtime - $starttime;
            print("end mining. Mining took: ", $timediff, " seconds");
            print($newhashtext);

        }


}

}
commented: Looks great. How's the speed though? +15

I would use a maths function not an SQL query. The PHP is messy.

When you refactor think of better ways the language can be used to write the software rather than painstakingly copying the old code to the new language.

PHP is good for database and also has many builtin functions. Use those functions. Google for them.

Python is better with lists and data manipulation in fancy ways. It has a much richer syntax than PHP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.