cosme12 / SimpleCoin

Just a really simple, insecure and incomplete implementation of a blockchain for a cryptocurrency made in Python as educational material. In other words, a simple Bitcoin clone.
http://copitosystem.com
MIT License
1.78k stars 400 forks source link

miner.py doesn't work :( #54

Closed iamRuda closed 4 years ago

iamRuda commented 4 years ago

Help me! I run in cmd. 55556

cosme12 commented 4 years ago

Hey. It has been a while since I updated this code. If Im not wrong, there is a bug that happens with certain wallet addresses. Try making another one and test again.

iamRuda commented 4 years ago

I don't know what i am doing wrong. Check me out: Step 1. I install "requirements.txt"; Step 2. I open "wallet.py"; Step 3. I generate new wallet with new title "wallet056594"; Step 4. I open "miner.py". Then the program throws this error; I have Windows 7 x64 and Python 3.7. Тhank you in advance and Sorry to trouble you.

cosme12 commented 4 years ago

Try doing a fast test. Run the code again but without changing the miner_config.py file. Check if that way it still crashes. I don't have the laptop with me so I can't test it.

iamRuda commented 4 years ago

I always get the exactly the same error at the beginning of the program. Maybe I'm doing it wrong, since I'm usualy programming in C++. And There's no rush. ^_^

iamRuda commented 4 years ago

Sorry, question is still going. :-/

cosme12 commented 4 years ago

Just tested. Mine is working. Let me see if I can guess what's your error. asd

cosme12 commented 4 years ago

@RudFireMan go to miner_config.py and change your miner address to "asd". Run again.

cosme12 commented 4 years ago
  1. Do you see the welcome message?
  2. Go to miner.py and comment 2 lines that start with p1 = and p1.start(). Line 265 and 266. Run again and see if it crash.
iamRuda commented 4 years ago

Wow! When I write in "miner_config.py" asd and run "miner.py", I see it and I do not know what is this. 4444

cosme12 commented 4 years ago

It was probably due to your address having non utf characters. Each of those thing you see is a block, which may contain several transactions. So you are mining and adding a new block to the blockchain. Everytime a new block is added, the proof of work gets harder and slower to solve.

I think that solves your problem. I should fix the bug though.

iamRuda commented 4 years ago

What you need me to do?

elijose55 commented 4 years ago

Can I work on this issue?

cosme12 commented 4 years ago

Of course. Just make sure the changes are simple. We must keep it as much newbie friendly as we can, so avoid using external libraries.

elijose55 commented 4 years ago

Sure, thanks! Do you have any ideia about the bug?

cosme12 commented 4 years ago

Yes, apparently, when you generate a new address, it has some non utf-8 characters that make the code crash. So you should try to reproduce the error, learn what kind of characters are making the code crash and then we can think a solution.

Good luck!

elijose55 commented 4 years ago

I am trying to replicate the error but still no luck. With "generate a new address" you mean run wallet.py and select the "generate new wallet" option? I have already tried the following:

cosme12 commented 4 years ago

If I paste an address like this KovGGg/F8jSP+V4YiQT+qgEx4nDK95e/Kl29BIo63+epTfQp9wEgyrDOfAp83deO8EPkE0vwh0mQoLR0mboKvQ== in the config file, the code crash. Meanwhile an adress like this: q3nf394hjg-random-miner-address-34nf3i4nflkn3oi works as expected.

elijose55 commented 4 years ago

Found the problem. In the code snippet bellow the GET request is made by concatenating the URL with the update parameter. https://github.com/cosme12/SimpleCoin/blob/8da677ae42041ad1bf66b0756424b782283bbbc0/simpleCoin/miner.py#L110-L112

So when you used an URL reserved character in the MINER_ADDRESS variable, like "+", "&" or "=", the request was badly processed by the transaction function GET part bellow: https://github.com/cosme12/SimpleCoin/blob/8da677ae42041ad1bf66b0756424b782283bbbc0/simpleCoin/miner.py#L230-L234

So request.args.get("update") == MINER_ADDRESS would not be true and the request would return a 500 code and then the json.loads would fail as the response was empty, raising an error.

So MINER_ADDRESS like: q3nf394hjg-ra#ndom-miner-address-34nf3i4nflkn3oi q3nf394hjg-ra&ndom-miner-address-34nf3i4nflkn3oi q3nf394hjg-ra+ndom-miner-address-34nf3i4nflkn3oi q3nf394hjg-ra=ndom-miner-address-34nf3i4nflkn3oi

Would make the code crash.

I am submiting a PR with the fix at this moment, tell what you think. Thank you!

cosme12 commented 4 years ago

Awesome. Tested and working! Thanks @elijose55