web3coach / the-blockchain-bar

The source-code for: "Build a Blockchain from Scratch in Go" eBook.
https://web3.coach
GNU Lesser General Public License v3.0
499 stars 184 forks source link

Chapter 11 Branch's TestNode_Run fails with memory leak #27

Closed romuloctba closed 2 years ago

romuloctba commented 2 years ago

Hi there, I am found a issue when following the book, and want to bring it to your attention here:

How it happened

As I got to the book's chapter 11, at page 301~304, there is this TestNode_Run test that is added, but locally, I could not make it pass.

I struggled with it for quite some time, because i was stubborn and tried to type the code all the way to here..... So I went trough each of the files again and again trying to figure out what I was doing wrong that the test was failing..... And didn't get it

Finally I gave up and cloned the repo, went to branch c11_consensus and.... the test failed exactly as my local hand-typed version!

Checking out branch c12_crypto, and on, it Passes fine.

The error

Here is the error I get when run the go test -timeout=0 -count=1 ./node -test.v -test.run ^TestNode_Run$ command:

--- FAIL: TestNode_Run (5.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x68f7ef]

Now What

Well, now I plan to check the diff between branches c11 and c12, hoping to figure out the problem before moving on to the next pages of the book.

When I checked out the c12_crypto branch, and ran the test, I saw that many dependencies were added, so I figure there are lots of logic and new things going on.

Because on the book the test passes at this point, I really want to understand what is wrong before going on the new content.

web3coach commented 2 years ago

Hi @romuloctba !

Thanks for reporting this. I am glad you are progressing in such a detail through the book.

I fixed the test. When you look in c12_crypto u will see I did a change where the HTTP server no longer returns error when closed gracefully but forgot to update the test...

Screenshot from 2022-02-26 17-50-30

This is fixed now and the test is passing again.

romuloctba commented 2 years ago

Thanks, @web3coach ! I bought the book this Feb 15th, and, since then, I am managing to study/read/code it almost every night until 3am (with breaks ofc) \o/

It takes longer than just reading it, but I feel like I learn better this way

Sometimes if get a little lost in the book examples, I just go to diffs between chapter branches, or check the "study code" commits you present at the end of each chapter.

I am learning a lot. Thank you

web3coach commented 2 years ago

That's exactly the way! Well done.