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

57 & 58 Request/Push Handling Logic (cont) #87

Merged
merged 6 commits into from
Jul 12, 2017

Conversation

chadlagore
Copy link
Contributor

@chadlagore chadlagore commented Jul 9, 2017

What Changed

  • Implemented Request handling logic.
  • Finished Push handling logic.
    • This required an update to the miner module, @david-julien please 👀 .

TODO 💡

  • Mine should take an interface with a callback as an argument.
  • We can use this interface as a way to update progress on mining, alert the app of progress etc etc.
type MinerUpdater interface {
    UpdateProgress(i int)
    UpdateFinished(result MiningResult)
}

go miner.Mine(bc, block, updater)

// As opposed to:
result := miner.Mine(bc, block)   // => Spins forever.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.1%) to 78.226% when pulling 9fd850b on 57-request-handling-logic into 0ce1330 on dev.

@chadlagore chadlagore changed the title 57 & 58 Request/Push Handling Logic. 57 & 58 Request/Push Handling Logic (cont) Jul 9, 2017
@@ -159,13 +165,17 @@ func initializeWorkers() {
}
}

// initializeChain creates the blockchain for the node.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is TODO.

Copy link
Contributor

@david-julien david-julien Jul 10, 2017

Choose a reason for hiding this comment

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

Don't know how we're all handling TODOs but should we mark this as TODO in the comment just to make sure we don't forget?


// Set min difficulty to be equal to the target so that the block validation
// passes
blockchain.MaxTarget = new(big.Int).Sub(
Copy link
Contributor

Choose a reason for hiding this comment

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

Reset the max target to its original value before the test returns so it doesn't cause any problems for any tests in the same file written after this test.

miner/miner.go Outdated
"time"

log "github.com/Sirupsen/logrus"
"github.com/ubclaunchpad/cumulus/blockchain"
"github.com/ubclaunchpad/cumulus/consensus"
)

// CurrentlyMining is a flag to control the miner.
var CurrentlyMining bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Should CurrentlyMining and the lock be private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Originally I wanted to have other packages check on them, but the exported function does that now.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.1%) to 78.226% when pulling 3b1804f on 57-request-handling-logic into 0ce1330 on dev.

Copy link
Member

@bfbachmann bfbachmann left a comment

Choose a reason for hiding this comment

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

Nice work

@@ -61,8 +61,10 @@ func Run(cfg conf.Config) {
go peer.MaintainConnections()

// Request the blockchain.
log.Info("Requesting blockchain from peers... ")
RequestBlockChain()
if chain == nil {
Copy link
Member

Choose a reason for hiding this comment

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

I'm doing some refactoring this week such that we don't have all these global variables, but instead we have instances of App for each Cumulus "node" we are running. The chain variable will also be part of that at some point just so you're aware.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good stuff 👍

@@ -91,19 +93,23 @@ func ConnectAndDiscover(target string) {
// on peers whos PushHandlers have been overridden.
func RequestHandler(req *msg.Request) msg.Response {
res := msg.Response{ID: req.ID}
typeErr := msg.NewProtocolError(msg.InvalidResourceType,
"Invalid resource type")
paramErr := msg.NewProtocolError(msg.InvalidResourceType,
Copy link
Member

Choose a reason for hiding this comment

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

We should probably add a different error code here to be more specific since this technically isn't an invalid resource type error.

app/app.go Outdated
// initializeChain creates the blockchain for the node.
func initializeChain() {
chain, _ = blockchain.NewValidChainAndBlock()
// Check if chain exists on disk.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you should check if the chain exists here because this function should only be called when we don't already have a blockchain right? Actually, either way, when a node boots it should always ask its peers for blocks anyway (regardless of whether it already has a blockchain stored locally) since there is no other way of knowing for sure that you have the most up-to-date blockchain.

@@ -13,6 +14,15 @@ func init() {
log.SetLevel(log.InfoLevel)
}

func createNewBlockRequest(blockNumber interface{}) *msg.Request {
Copy link
Member

Choose a reason for hiding this comment

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

Why not blockNumber of type int?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😆


// CopyLocalBlockByIndex returns a copy of a block in the local chain by index.
func (bc *BlockChain) CopyBlockByIndex(i uint32) *Block {
blk := bc.Blocks[i]
Copy link
Member

Choose a reason for hiding this comment

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

What if this is an out-of-bounds reference? I think you can also aggregate this line and the one below into one.

var currentlyMining bool

// currentlyMiningLock is a read/write lock to change the Mining flag.
var currentlyMiningLock sync.RWMutex
Copy link
Member

Choose a reason for hiding this comment

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

I guess this might be overkill but you could put currentlyMining and currentlyMiningLock into a struct of type Miner. Then you could make the functions below receivers of the Miner type. That way we can manage multiple instances of Miner.

Copy link
Member

Choose a reason for hiding this comment

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

Edit: I see you want to make Mine() take an interface and a callback as a means of updating progress. I think using a Miner type could help with that. The only thing is, I fell like in mining there is no notion of progress since you can never know for sure how close you are to the solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. As for Miner type, I agree. Not in scope here, but I think we should do it. It would be nice to build in the ability to scale your miners outwards instead of upwards.
  2. By progress I more meant, how many hashes it has attempted. Progress = number_attempted / number_of_hashes_to_needed_to_likely_produce_a_match.


const (
// MiningSuccessful is returned when the miner mines a block.
MiningSuccessful = iota
Copy link
Member

Choose a reason for hiding this comment

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

You only need to set the first constant to iota, the compiler will fill in the following ones on its own (see https://golang.org/ref/spec#Constant_declarations and the section below).

miner/miner.go Outdated
func Mine(bc *blockchain.BlockChain, b *blockchain.Block) bool {
// TODO: Make Mine take an interface with a callback as an arguement.
func Mine(bc *blockchain.BlockChain, b *blockchain.Block) *MiningResult {
setStart()
if valid, _ := bc.ValidBlock(b); !valid {
log.Error("Invalid block")
Copy link
Member

Choose a reason for hiding this comment

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

I know you added this earlier but we should probably add more detail here. Generally when we surface errors we want to know what package they're from and what action what being taken when the error occurred.
In this case something like "Mine failed, miner received an invalid block".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I don't even think the miner should be worried about validation. Good eye 👀

miner/miner.go Outdated
)

// MiningResult contains the result of the mining operation.
type MiningResult struct {
Copy link
Member

Choose a reason for hiding this comment

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

Why have this be a type when we can just return (complete bool, info int) from Mine()? Do we use MiningResult outside this file?

}

// StopMining stops the miner from mining.
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 love the idea of cancelling a mining job. Nice work, we'll definitely need to do this when we are notified of new blocks that were added to the chain that contain transactions we are trying to mine in this block.

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.

🚢

@jordanschalm
Copy link
Member

@chadlagore Feel free to merge this after review comments and merge conflicts are resolved 👍

@jordanschalm
Copy link
Member

closes #56
closes #57
closes #58

Let's close these and make more descriptive issues next meeting

@coveralls
Copy link

Coverage Status

Coverage increased (+9.6%) to 78.226% when pulling 181e2e2 on 57-request-handling-logic into 2fef206 on dev.

@coveralls
Copy link

Coverage Status

Coverage increased (+9.6%) to 78.226% when pulling 992236f on 57-request-handling-logic into 2fef206 on dev.

@coveralls
Copy link

Coverage Status

Coverage increased (+9.6%) to 78.226% when pulling 992236f on 57-request-handling-logic into 2fef206 on dev.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.3%) to 69.978% when pulling 3f5da68 on 57-request-handling-logic into 2fef206 on dev.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.4%) to 69.986% when pulling 17b6ced on 57-request-handling-logic into 2fef206 on dev.

@chadlagore chadlagore merged commit 6788fe6 into dev Jul 12, 2017
@jordanschalm jordanschalm deleted the 57-request-handling-logic branch July 15, 2017 23:00
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

5 participants