check TxMsg

- add validation on TxMsg checking for nil
- add test for nil transaction
- add test for zero value transaction (no extra validation needed)
release/1.0.1
zelig 10 years ago
parent 82da6bf4d2
commit 6ffea34d8b
  1. 6
      core/types/block.go
  2. 5
      eth/protocol.go
  3. 39
      eth/protocol_test.go

@ -250,10 +250,10 @@ func (self *Block) AddReceipt(receipt *Receipt) {
} }
func (self *Block) RlpData() interface{} { func (self *Block) RlpData() interface{} {
// return []interface{}{self.header, self.transactions, self.uncles} return []interface{}{self.header, self.transactions, self.uncles}
// } }
// func (self *Block) RlpDataForStorage() interface{} { func (self *Block) RlpDataForStorage() interface{} {
return []interface{}{self.header, self.transactions, self.uncles, self.Td /* TODO receipts */} return []interface{}{self.header, self.transactions, self.uncles, self.Td /* TODO receipts */}
} }

@ -185,7 +185,10 @@ func (self *ethProtocol) handle() error {
if err := msg.Decode(&txs); err != nil { if err := msg.Decode(&txs); err != nil {
return self.protoError(ErrDecode, "msg %v: %v", msg, err) return self.protoError(ErrDecode, "msg %v: %v", msg, err)
} }
for _, tx := range txs { for i, tx := range txs {
if tx == nil {
return self.protoError(ErrDecode, "transaction %d is nil", i)
}
jsonlogger.LogJson(&logger.EthTxReceived{ jsonlogger.LogJson(&logger.EthTxReceived{
TxHash: tx.Hash().Hex(), TxHash: tx.Hash().Hex(),
RemoteId: self.peer.ID().String(), RemoteId: self.peer.ID().String(),

@ -359,3 +359,42 @@ func TestBlockMsg(t *testing.T) {
eth.checkError(ErrDecode, delay) eth.checkError(ErrDecode, delay)
} }
func TestTransactionsMsg(t *testing.T) {
logInit()
eth := newEth(t)
txs := make(chan *types.Transaction)
eth.txPool.addTransactions = func(t []*types.Transaction) {
for _, tx := range t {
txs <- tx
}
}
go eth.run()
eth.handshake(t, true)
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
if err != nil {
t.Errorf("transactions expected, got %v", err)
}
var delay = 3 * time.Second
tx := &types.Transaction{}
go p2p.Send(eth, TxMsg, []interface{}{tx, tx})
timer := time.After(delay)
for i := int64(0); i < 2; i++ {
select {
case <-txs:
case <-timer:
return
case err := <-eth.quit:
t.Errorf("no error expected, got %v", err)
return
}
}
go p2p.Send(eth, TxMsg, []interface{}{[]interface{}{}})
eth.checkError(ErrDecode, delay)
}

Loading…
Cancel
Save