Implemented get chain msg

This commit is contained in:
obscuren 2014-01-28 15:35:44 +01:00
parent 4a82230de5
commit 3e400739a7

44
peer.go
View File

@ -178,9 +178,14 @@ out:
// Respond back with pong // Respond back with pong
p.QueueMessage(ethwire.NewMessage(ethwire.MsgPongTy, "")) p.QueueMessage(ethwire.NewMessage(ethwire.MsgPongTy, ""))
case ethwire.MsgPongTy: case ethwire.MsgPongTy:
// If we received a pong back from a peer we set the
// last pong so the peer handler knows this peer is still
// active.
p.lastPong = time.Now().Unix() p.lastPong = time.Now().Unix()
case ethwire.MsgBlockTy: case ethwire.MsgBlockTy:
for i := 0; i < msg.Data.Length(); i++ { // Get all blocks and process them (TODO reverse order?)
msg.Data = msg.Data.Get(0)
for i := msg.Data.Length() - 1; i >= 0; i-- {
block := ethchain.NewBlockFromRlpValue(msg.Data.Get(i)) block := ethchain.NewBlockFromRlpValue(msg.Data.Get(i))
err := p.ethereum.BlockManager.ProcessBlock(block) err := p.ethereum.BlockManager.ProcessBlock(block)
@ -189,10 +194,15 @@ out:
} }
} }
case ethwire.MsgTxTy: case ethwire.MsgTxTy:
// If the message was a transaction queue the transaction
// in the TxPool where it will undergo validation and
// processing when a new block is found
for i := 0; i < msg.Data.Length(); i++ { for i := 0; i < msg.Data.Length(); i++ {
p.ethereum.TxPool.QueueTransaction(ethchain.NewTransactionFromRlpValue(msg.Data.Get(i))) p.ethereum.TxPool.QueueTransaction(ethchain.NewTransactionFromRlpValue(msg.Data.Get(i)))
} }
case ethwire.MsgGetPeersTy: case ethwire.MsgGetPeersTy:
// Flag this peer as a 'requested of new peers' this to
// prevent malicious peers being forced.
p.requestedPeerList = true p.requestedPeerList = true
// Peer asked for list of connected peers // Peer asked for list of connected peers
p.pushPeers() p.pushPeers()
@ -214,22 +224,31 @@ out:
p.requestedPeerList = false p.requestedPeerList = false
} }
case ethwire.MsgGetChainTy: case ethwire.MsgGetChainTy:
blocksFound := 0 var parent *ethchain.Block
l := msg.Data.Length() // FIXME
msg.Data = msg.Data.Get(0)
// Length minus one since the very last element in the array is a count
l := msg.Data.Length() - 1
// Amount of parents in the canonical chain
amountOfBlocks := msg.Data.Get(l).AsUint()
// Check each SHA block hash from the message and determine whether // Check each SHA block hash from the message and determine whether
// the SHA is in the database // the SHA is in the database
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
if p.ethereum.BlockManager.BlockChain().HasBlock(msg.Data.Get(i).AsString()) { if data := msg.Data.Get(i).AsBytes(); p.ethereum.BlockManager.BlockChain().HasBlock(data) {
blocksFound++ parent = p.ethereum.BlockManager.BlockChain().GetBlock(data)
// TODO send reply break
} }
} }
// If no blocks are found we send back a reply with msg not in chain // If a parent is found send back a reply
// and the last hash from get chain if parent != nil {
if blocksFound == 0 { chain := p.ethereum.BlockManager.BlockChain().GetChainFromHash(parent.Hash(), amountOfBlocks)
lastHash := msg.Data.Get(l - 1) p.QueueMessage(ethwire.NewMessage(ethwire.MsgBlockTy, chain))
p.QueueMessage(ethwire.NewMessage(ethwire.MsgNotInChainTy, lastHash)) } else {
// If no blocks are found we send back a reply with msg not in chain
// and the last hash from get chain
lastHash := msg.Data.Get(l)
p.QueueMessage(ethwire.NewMessage(ethwire.MsgNotInChainTy, lastHash.AsRaw()))
} }
case ethwire.MsgNotInChainTy: case ethwire.MsgNotInChainTy:
log.Println("Not in chain, not yet implemented") log.Println("Not in chain, not yet implemented")
@ -320,6 +339,9 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
p.Stop() p.Stop()
} }
} else {
msg := ethwire.NewMessage(ethwire.MsgGetChainTy, []interface{}{p.ethereum.BlockManager.BlockChain().CurrentBlock.Hash(), uint64(100)})
p.QueueMessage(msg)
} }
} }