Skip to content

Commit 4a4da9a

Browse files
committed
Merge pull request bnb-chain#588 from ethersphere/frontier/SEC-29
Frontier/sec 29
2 parents 936ddf2 + f56fc9c commit 4a4da9a

3 files changed

Lines changed: 225 additions & 17 deletions

File tree

‎core/types/block.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ func NewBlockWithHeader(header *Header) *Block {
148148
return &Block{header: header}
149149
}
150150

151+
func (self *Block) ValidateFields() error {
152+
if self.header == nil {
153+
return fmt.Errorf("header is nil")
154+
}
155+
for i, transaction := range self.transactions {
156+
if transaction == nil {
157+
return fmt.Errorf("transaction %d is nil", i)
158+
}
159+
}
160+
for i, uncle := range self.uncles {
161+
if uncle == nil {
162+
return fmt.Errorf("uncle %d is nil", i)
163+
}
164+
}
165+
return nil
166+
}
167+
151168
func (self *Block) DecodeRLP(s *rlp.Stream) error {
152169
var eb extblock
153170
if err := s.Decode(&eb); err != nil {

‎eth/protocol.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ func (self *ethProtocol) handle() error {
185185
if err := msg.Decode(&txs); err != nil {
186186
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
187187
}
188-
for _, tx := range txs {
188+
for i, tx := range txs {
189+
if tx == nil {
190+
return self.protoError(ErrDecode, "transaction %d is nil", i)
191+
}
189192
jsonlogger.LogJson(&logger.EthTxReceived{
190193
TxHash: tx.Hash().Hex(),
191194
RemoteId: self.peer.ID().String(),
@@ -268,6 +271,9 @@ func (self *ethProtocol) handle() error {
268271
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
269272
}
270273
}
274+
if err := block.ValidateFields(); err != nil {
275+
return self.protoError(ErrDecode, "block validation %v: %v", msg, err)
276+
}
271277
self.blockPool.AddBlock(&block, self.id)
272278
}
273279

@@ -276,6 +282,9 @@ func (self *ethProtocol) handle() error {
276282
if err := msg.Decode(&request); err != nil {
277283
return self.protoError(ErrDecode, "%v: %v", msg, err)
278284
}
285+
if err := request.Block.ValidateFields(); err != nil {
286+
return self.protoError(ErrDecode, "block validation %v: %v", msg, err)
287+
}
279288
hash := request.Block.Hash()
280289
_, chainHead, _ := self.chainManager.Status()
281290

‎eth/protocol_test.go‎

Lines changed: 198 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func (self *testChainManager) GetBlockHashesFromHash(hash common.Hash, amount ui
6363
func (self *testChainManager) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
6464
if self.status != nil {
6565
td, currentBlock, genesisBlock = self.status()
66+
} else {
67+
td = common.Big1
68+
currentBlock = common.Hash{1}
69+
genesisBlock = common.Hash{2}
6670
}
6771
return
6872
}
@@ -163,14 +167,29 @@ func (self *ethProtocolTester) run() {
163167
self.quit <- err
164168
}
165169

170+
func (self *ethProtocolTester) handshake(t *testing.T, mock bool) {
171+
td, currentBlock, genesis := self.chainManager.Status()
172+
// first outgoing msg should be StatusMsg.
173+
err := p2p.ExpectMsg(self, StatusMsg, &statusMsgData{
174+
ProtocolVersion: ProtocolVersion,
175+
NetworkId: NetworkId,
176+
TD: td,
177+
CurrentBlock: currentBlock,
178+
GenesisBlock: genesis,
179+
})
180+
if err != nil {
181+
t.Fatalf("incorrect outgoing status: %v", err)
182+
}
183+
if mock {
184+
go p2p.Send(self, StatusMsg, &statusMsgData{ProtocolVersion, NetworkId, td, currentBlock, genesis})
185+
}
186+
}
187+
166188
func TestStatusMsgErrors(t *testing.T) {
167189
logInit()
168190
eth := newEth(t)
169-
td := common.Big1
170-
currentBlock := common.Hash{1}
171-
genesis := common.Hash{2}
172-
eth.chainManager.status = func() (*big.Int, common.Hash, common.Hash) { return td, currentBlock, genesis }
173191
go eth.run()
192+
td, currentBlock, genesis := eth.chainManager.Status()
174193

175194
tests := []struct {
176195
code uint64
@@ -195,18 +214,7 @@ func TestStatusMsgErrors(t *testing.T) {
195214
},
196215
}
197216
for _, test := range tests {
198-
// first outgoing msg should be StatusMsg.
199-
err := p2p.ExpectMsg(eth, StatusMsg, &statusMsgData{
200-
ProtocolVersion: ProtocolVersion,
201-
NetworkId: NetworkId,
202-
TD: td,
203-
CurrentBlock: currentBlock,
204-
GenesisBlock: genesis,
205-
})
206-
if err != nil {
207-
t.Fatalf("incorrect outgoing status: %v", err)
208-
}
209-
217+
eth.handshake(t, false)
210218
// the send call might hang until reset because
211219
// the protocol might not read the payload.
212220
go p2p.Send(eth, test.code, test.data)
@@ -216,3 +224,177 @@ func TestStatusMsgErrors(t *testing.T) {
216224
go eth.run()
217225
}
218226
}
227+
228+
func TestNewBlockMsg(t *testing.T) {
229+
// logInit()
230+
eth := newEth(t)
231+
232+
var disconnected bool
233+
eth.blockPool.removePeer = func(peerId string) {
234+
disconnected = true
235+
}
236+
237+
go eth.run()
238+
239+
eth.handshake(t, true)
240+
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
241+
if err != nil {
242+
t.Errorf("transactions expected, got %v", err)
243+
}
244+
245+
var tds = make(chan *big.Int)
246+
eth.blockPool.addPeer = func(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool) {
247+
tds <- td
248+
return
249+
}
250+
251+
var delay = 1 * time.Second
252+
// eth.reset()
253+
block := types.NewBlock(common.Hash{1}, common.Address{1}, common.Hash{1}, common.Big1, 1, "extra")
254+
255+
go p2p.Send(eth, NewBlockMsg, &newBlockMsgData{Block: block})
256+
timer := time.After(delay)
257+
258+
select {
259+
case td := <-tds:
260+
if td.Cmp(common.Big0) != 0 {
261+
t.Errorf("incorrect td %v, expected %v", td, common.Big0)
262+
}
263+
case <-timer:
264+
t.Errorf("no td recorded after %v", delay)
265+
return
266+
case err := <-eth.quit:
267+
t.Errorf("no error expected, got %v", err)
268+
return
269+
}
270+
271+
go p2p.Send(eth, NewBlockMsg, &newBlockMsgData{block, common.Big2})
272+
timer = time.After(delay)
273+
274+
select {
275+
case td := <-tds:
276+
if td.Cmp(common.Big2) != 0 {
277+
t.Errorf("incorrect td %v, expected %v", td, common.Big2)
278+
}
279+
case <-timer:
280+
t.Errorf("no td recorded after %v", delay)
281+
return
282+
case err := <-eth.quit:
283+
t.Errorf("no error expected, got %v", err)
284+
return
285+
}
286+
287+
go p2p.Send(eth, NewBlockMsg, []interface{}{})
288+
// Block.DecodeRLP: validation failed: header is nil
289+
eth.checkError(ErrDecode, delay)
290+
291+
}
292+
293+
func TestBlockMsg(t *testing.T) {
294+
// logInit()
295+
eth := newEth(t)
296+
blocks := make(chan *types.Block)
297+
eth.blockPool.addBlock = func(block *types.Block, peerId string) (err error) {
298+
blocks <- block
299+
return
300+
}
301+
302+
var disconnected bool
303+
eth.blockPool.removePeer = func(peerId string) {
304+
disconnected = true
305+
}
306+
307+
go eth.run()
308+
309+
eth.handshake(t, true)
310+
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
311+
if err != nil {
312+
t.Errorf("transactions expected, got %v", err)
313+
}
314+
315+
var delay = 3 * time.Second
316+
// eth.reset()
317+
newblock := func(i int64) *types.Block {
318+
return types.NewBlock(common.Hash{byte(i)}, common.Address{byte(i)}, common.Hash{byte(i)}, big.NewInt(i), uint64(i), string(i))
319+
}
320+
b := newblock(0)
321+
b.Header().Difficulty = nil // check if nil as *big.Int decodes as 0
322+
go p2p.Send(eth, BlocksMsg, types.Blocks{b, newblock(1), newblock(2)})
323+
timer := time.After(delay)
324+
for i := int64(0); i < 3; i++ {
325+
select {
326+
case block := <-blocks:
327+
if (block.ParentHash() != common.Hash{byte(i)}) {
328+
t.Errorf("incorrect block %v, expected %v", block.ParentHash(), common.Hash{byte(i)})
329+
}
330+
if block.Difficulty().Cmp(big.NewInt(i)) != 0 {
331+
t.Errorf("incorrect block %v, expected %v", block.Difficulty(), big.NewInt(i))
332+
}
333+
case <-timer:
334+
t.Errorf("no td recorded after %v", delay)
335+
return
336+
case err := <-eth.quit:
337+
t.Errorf("no error expected, got %v", err)
338+
return
339+
}
340+
}
341+
342+
go p2p.Send(eth, BlocksMsg, []interface{}{[]interface{}{}})
343+
eth.checkError(ErrDecode, delay)
344+
if !disconnected {
345+
t.Errorf("peer not disconnected after error")
346+
}
347+
348+
// test empty transaction
349+
eth.reset()
350+
go eth.run()
351+
eth.handshake(t, true)
352+
err = p2p.ExpectMsg(eth, TxMsg, []interface{}{})
353+
if err != nil {
354+
t.Errorf("transactions expected, got %v", err)
355+
}
356+
b = newblock(0)
357+
b.AddTransaction(nil)
358+
go p2p.Send(eth, BlocksMsg, types.Blocks{b})
359+
eth.checkError(ErrDecode, delay)
360+
361+
}
362+
363+
func TestTransactionsMsg(t *testing.T) {
364+
logInit()
365+
eth := newEth(t)
366+
txs := make(chan *types.Transaction)
367+
368+
eth.txPool.addTransactions = func(t []*types.Transaction) {
369+
for _, tx := range t {
370+
txs <- tx
371+
}
372+
}
373+
go eth.run()
374+
375+
eth.handshake(t, true)
376+
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
377+
if err != nil {
378+
t.Errorf("transactions expected, got %v", err)
379+
}
380+
381+
var delay = 3 * time.Second
382+
tx := &types.Transaction{}
383+
384+
go p2p.Send(eth, TxMsg, []interface{}{tx, tx})
385+
timer := time.After(delay)
386+
for i := int64(0); i < 2; i++ {
387+
select {
388+
case <-txs:
389+
case <-timer:
390+
return
391+
case err := <-eth.quit:
392+
t.Errorf("no error expected, got %v", err)
393+
return
394+
}
395+
}
396+
397+
go p2p.Send(eth, TxMsg, []interface{}{[]interface{}{}})
398+
eth.checkError(ErrDecode, delay)
399+
400+
}

0 commit comments

Comments
 (0)