-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
@@ -159,13 +165,17 @@ func initializeWorkers() { | |||
} | |||
} | |||
|
|||
// initializeChain creates the blockchain for the node. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is TODO.
There was a problem hiding this comment.
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?
miner/miner_test.go
Outdated
|
||
// Set min difficulty to be equal to the target so that the block validation | ||
// passes | ||
blockchain.MaxTarget = new(big.Int).Sub( |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
blockchain/blockchain.go
Outdated
|
||
// CopyLocalBlockByIndex returns a copy of a block in the local chain by index. | ||
func (bc *BlockChain) CopyBlockByIndex(i uint32) *Block { | ||
blk := bc.Blocks[i] |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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. - 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 |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚢
@chadlagore Feel free to merge this after review comments and merge conflicts are resolved 👍 |
992236f
to
3b1804f
Compare
What Changed
Request
handling logic.Push
handling logic.miner
module, @david-julien please 👀 .TODO 💡
Mine
should take an interface with a callback as an argument.