Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

128 synchronized blockchain #129

Merged
merged 4 commits into from
Aug 26, 2017
Merged

128 synchronized blockchain #129

merged 4 commits into from
Aug 26, 2017

Conversation

bfbachmann
Copy link
Member

Related Issue

#128

Description

  • This commit adds a lock to to the blockchain that we use in App so ensure there are no race conditions in validation/manipulation of blocks on the blockchain. Notice, when you read the code, that none of the functions in blockchain.go make use of this lock. That's because with almost everything we do with the blockchain we check if certain conditions to do with the blockchain are true, do some validation, and then append a block, so locking each one of those steps individually wouldn't work - they all need to be done as one atomic operation.
  • I also updated miner so that it can be paused and resumed during normal operation.

WIKI Updates

TODO

Todos

General:

  • Tests
  • Documentation
  • Wiki

Other (links to TODOs in code):

  • Fix TestEncodeDecodeBlockchain()

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 76.465% when pulling af8c8c0 on 128-synchronized-blockchain into 836f6e7 on dev.

Copy link
Member

@jordanschalm jordanschalm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this change but would like to see this package follow the same design as app

t.Fail()
}
DecodeBlockChain(buf)
// TODO: fix this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

Copy link
Member Author

@bfbachmann bfbachmann Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, that started failing when I added the lock to the blockchain for some strange reason. I didn't want to spend time figuring out what the problem was so I just commented it out. I think we can just fix this when we implement saving the blockchain to disk.

miner/miner.go Outdated
defer currentlyMiningLock.Unlock()
currentlyMining = true
// StopMining causes the miner to abort the current mining job immediately.
func StopMining() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see these incorporated into a struct like we did with app. Package level state is an antipattern, even if you only expect that state to be instantiated once

miner/miner.go Outdated
return &MiningResult{
Complete: false,
Info: MiningHalted,
if stateChanged() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the stateChanged variable is probably unnecessary. Have you benchmarked this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will refactor.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.5%) to 76.233% when pulling 1f2f31c on 128-synchronized-blockchain into 836f6e7 on dev.

Copy link
Member

@jordanschalm jordanschalm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍
Two thumbs up

miner/miner.go Outdated
@@ -9,9 +9,6 @@ import (
"github.com/ubclaunchpad/cumulus/consensus"
)

// MinerState represents the state of the miner
type MinerState int

const (
// Paused represents the MinerState where the miner is not running but the
// previously running mining job can be resumed or stopped.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could define these values as type MinerState (I know it's not your code 😝 )

miner/miner.go Outdated
// MiningResult contains the result of the mining operation.
type MiningResult struct {
Complete bool
Info int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, could define type for this

app/app.go Outdated
@@ -295,32 +308,34 @@ func (a *App) HandleTransaction(txn *blockchain.Transaction) {
// HandleBlock handles new instance of BlockWork.
func (a *App) HandleBlock(blk *blockchain.Block) {
log.Info("Received new block")
wasMining := a.Miner.PauseIfRunning()

a.Chain.Lock.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just add a Lock method to the chain?

Copy link
Contributor

@chadlagore chadlagore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Maybe just change those test sleeps lengths if you can. 👍

@@ -116,30 +116,42 @@ func connect(ctx *ishell.Context, a *App) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice interface 🎉

defer currentlyMiningLock.Unlock()
currentlyMining = true
// setState synchronously sets the current state of the miner to the given state.
func (m *Miner) setState(state MinerState) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

m := New()

go m.Mine(b)
time.Sleep(time.Second / 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just do time.Millisecond * 50 - this type of stuff really slows our test suite down.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 76.384% when pulling be263f1 on 128-synchronized-blockchain into 836f6e7 on dev.

@bfbachmann bfbachmann merged commit 6dc8be0 into dev Aug 26, 2017
@jordanschalm jordanschalm deleted the 128-synchronized-blockchain branch August 30, 2017 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants