github.com/koko1123/flow-go-1@v0.29.6/consensus/integration/blockordelay_test.go (about)

     1  //go:build timesensitivetest
     2  // +build timesensitivetest
     3  
     4  package integration_test
     5  
     6  import (
     7  	"math/rand"
     8  	"time"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/model/messages"
    12  )
    13  
    14  // This file includes functions to simulate network conditions.
    15  // The network conditions are simulated by defining whether a message sent to a receiver should be
    16  // blocked or delayed.
    17  
    18  // block all messages sent by or received by a list of denied nodes for the first N messages
    19  func blockNodesForFirstNMessages(n int, denyList ...*Node) BlockOrDelayFunc {
    20  	denyDict := make(map[flow.Identifier]*Node, len(denyList))
    21  	for _, n := range denyList {
    22  		denyDict[n.id.ID()] = n
    23  	}
    24  
    25  	sent, received := 0, 0
    26  
    27  	return func(channelID string, event interface{}, sender, receiver *Node) (bool, time.Duration) {
    28  		block, notBlock := true, false
    29  
    30  		switch m := event.(type) {
    31  		case *messages.BlockProposal:
    32  		case *messages.BlockVote:
    33  		case *messages.BlockResponse:
    34  			log := receiver.log.With().Int("blocks", len(m.Blocks)).Uint64("first", m.Blocks[0].Header.View).
    35  				Uint64("last", m.Blocks[len(m.Blocks)-1].Header.View).Logger()
    36  			log.Info().Msg("receives BlockResponse")
    37  		case *messages.SyncRequest:
    38  		case *messages.SyncResponse:
    39  		case *messages.RangeRequest:
    40  		case *messages.BatchRequest:
    41  		default:
    42  			return notBlock, 0
    43  		}
    44  
    45  		if _, ok := denyDict[sender.id.ID()]; ok {
    46  			if sent >= n {
    47  				return notBlock, 0
    48  			}
    49  			sent++
    50  			return block, 0
    51  		}
    52  		if _, ok := denyDict[receiver.id.ID()]; ok {
    53  			if received >= n {
    54  				return notBlock, 0
    55  			}
    56  			received++
    57  			return block, 0
    58  		}
    59  		return false, 0
    60  	}
    61  }
    62  
    63  func blockReceiverMessagesByPercentage(percent int) BlockOrDelayFunc {
    64  	rand.Seed(time.Now().UnixNano())
    65  	return func(channelID string, event interface{}, sender, receiver *Node) (bool, time.Duration) {
    66  		block := rand.Intn(100) <= percent
    67  		return block, 0
    68  	}
    69  }
    70  
    71  func delayReceiverMessagesByRange(low time.Duration, high time.Duration) BlockOrDelayFunc {
    72  	rand.Seed(time.Now().UnixNano())
    73  	return func(channelID string, event interface{}, sender, receiver *Node) (bool, time.Duration) {
    74  		rng := high - low
    75  		delay := int64(low) + rand.Int63n(int64(rng))
    76  		return false, time.Duration(delay)
    77  	}
    78  }