Proof of work is added

This commit is contained in:
Efe 2019-12-28 16:53:24 +03:00
parent 6744cc923f
commit 0cedd811fa
6 changed files with 91 additions and 50 deletions

View File

@ -1,36 +1,37 @@
'''
print(int('b',16), int('62ba1cd8a',16)) # Prints the hexadecimal representation of the value.
/
testnet : A small replica of bitcoin for developers to test their app without real money.
We use bitcoin-cli command to communicate with a node. It is a command line utility.
In this course we use >testnet rather than >bitcoin-cli.
/
>testnet help
>mainnet help
Prints all the commands to use in testnet or mainnet
/
>testnet help getblockchaininfo
Gets the help for the related command
/
>testnet getblockchaininfo
Makes the query with the method for the testnet. mainnet for the actual chain
/
>mainnet getmininginfo
Gives the mining info
/
Check how the nodes talk with each other from https://bitcoin.org/en/developer-reference#protocol-versions
This changes from version to version. ipv4 / ipv6 / onion
/
>testnet getwalletinfo
Gives the wallet info
/
>testnet listtransactions
The transactions related to wallet
/
Code to connect to node
If you don't have bitcoinrpc install it with pip:
pip install python-bitcoinrpc
/
# Prints the hexadecimal representation of the value.
print(int('b',16), int('62ba1cd8a',16))
#testnet : A small replica of bitcoin for developers to test their app without real money.
#We use bitcoin-cli command to communicate with a node. It is a command line utility.
#In this course we use >testnet rather than >bitcoin-cli.
#Prints all the commands to use in testnet or mainnet
testnet help
mainnet help
#Gets the help for the related command
testnet help getblockchaininfo
#Makes the query with the method for the testnet. mainnet for the actual chain
testnet getblockchaininfo
#Gives the mining info
mainnet getmininginfo
#Check how the nodes talk with each other from https://bitcoin.org/en/developer-reference#protocol-versions
#This changes from version to version. ipv4 / ipv6 / onion
#Gives the wallet info
testnet getwalletinfo
#The transactions related to wallet
testnet listtransactions
#Code to connect to node
#If you don't have bitcoinrpc install it with pip:
#pip install python-bitcoinrpc
"""
Code:
from bitcoinrpc.authproxy import AuthServiceProxy
@ -51,10 +52,11 @@ mainnet = RPC(rpc_template %
('bitcoin', 'python', '68.183.110.103', 8332))
testnet = RPC(rpc_template %
('bitcoin', 'python', 'localhost', 18332))
/
"""
"""
pprint : A package to print objects pretty.Especially JSON objects.
from helpers import testnet
from pprint import pprint
pprint(testnet.getblockchaininfo())
'''
"""

17
5_proof_of_work.py Normal file
View File

@ -0,0 +1,17 @@
import hashlib
#Try to find the hash (of a block) which starts with some amount of zeros. We are impersonating the miners acivities here.
def hashed(d, n):
d += n.to_bytes(4, 'little')
return hashlib.sha256(d).hexdigest()
data = b'hash of a block'
nonce = 0
zeros_required = 4
while not (hashed(data, nonce)[:zeros_required] == ('0' * zeros_required)):
print (nonce, hashed(data, nonce))
nonce +=1
print('Found:',nonce,hashed(data, nonce))

View File

@ -0,0 +1,17 @@
from helpers import mainnet, testnet
from decimal import Decimal
from pprint import pprint
def calculate_bitcoin_cycle_and_block_reward():
#This little program gives the bitcoin's cycle count and the bitcoin block reward amount
INITIAL_SUBSIDY = 50
BLOCKS_PER_HALVENING = 210000
last_block = mainnet.getblockchaininfo()["blocks"]
cycle_count = int(last_block/BLOCKS_PER_HALVENING)
block_reward = INITIAL_SUBSIDY / (cycle_count*2)
return cycle_count+1, block_reward
if __name__ == "__main__":
cycle, block_reward = calculate_bitcoin_cycle_and_block_reward()
pprint(cycle)
print(f"Bitcoin is in the {cycle}. halvening cycle and the block reward is {block_reward} ₿ right now.")

20
7_transactions.py Normal file
View File

@ -0,0 +1,20 @@
### TRANSACTIONS ###
from helpers import mainnet, testnet
from decimal import Decimal
from pprint import pprint
#We want to send bitcoin to an address
#help(testnet.sendtoaddress()) bitcoin-cli sendtoaddress "1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd" 0.1
#First send tbtc to your address from a tbtc faucet then eun the program
address = testnet.getnewaddress('','legacy')
address2 = testnet.getnewaddress('','legacy')
print(address)
print(address2)
print(testnet.getbalance())
#We have sent the tbtc to an adress Bize output olarak bir tx döndürdü. We have sent tbtc to ourselves actually. It returned the transaction id
tx = testnet.sendtoaddress(address2, 0.0000100)
print(tx)
#We can get tha transaction and check the input and the outputs
pprint(testnet.gettransaction(tx))

View File

@ -1,15 +0,0 @@
from helpers import mainnet
from decimal import Decimal
def calculate_bitcoin_cycle_and_block_reward():
#This little program gives the bitcoin's cycle count and the bitcoin block reward amount
INITIAL_SUBSIDY = Decimal(50)
BLOCKS_PER_HALVENING = 210000
last_block = mainnet.getblockchaininfo()["blocks"]
cycle_count = int(last_block/BLOCKS_PER_HALVENING)
block_reward = INITIAL_SUBSIDY / cycle_count*2
return {"Cycle":cycle_count+1, "Block_reward":block_reward}
if __name__ == "__main__":
cycle = calculate_bitcoin_cycle_and_block_reward()
print(f"Bitcoin is in the {cycle.get('Cycle')}. halvening cycle and the block reward is {cycle.get('Block_reward')} ₿ right now.")