github.com/koko1123/flow-go-1@v0.29.6/state/protocol/util/testing.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  	"github.com/stretchr/testify/mock"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/module"
    12  	"github.com/koko1123/flow-go-1/module/metrics"
    13  	modulemock "github.com/koko1123/flow-go-1/module/mock"
    14  	"github.com/koko1123/flow-go-1/module/trace"
    15  	"github.com/koko1123/flow-go-1/state/protocol"
    16  	pbadger "github.com/koko1123/flow-go-1/state/protocol/badger"
    17  	"github.com/koko1123/flow-go-1/state/protocol/events"
    18  	mockprotocol "github.com/koko1123/flow-go-1/state/protocol/mock"
    19  	"github.com/koko1123/flow-go-1/storage"
    20  	"github.com/koko1123/flow-go-1/storage/util"
    21  	"github.com/koko1123/flow-go-1/utils/unittest"
    22  )
    23  
    24  // MockReceiptValidator returns a ReceiptValidator that accepts
    25  // all receipts without performing any
    26  // integrity checks.
    27  func MockReceiptValidator() module.ReceiptValidator {
    28  	validator := &modulemock.ReceiptValidator{}
    29  	validator.On("Validate", mock.Anything).Return(nil)
    30  	validator.On("ValidatePayload", mock.Anything).Return(nil)
    31  	return validator
    32  }
    33  
    34  // MockBlockTimer returns BlockTimer that accepts all timestamps
    35  // without performing any checks.
    36  func MockBlockTimer() protocol.BlockTimer {
    37  	blockTimer := &mockprotocol.BlockTimer{}
    38  	blockTimer.On("Validate", mock.Anything, mock.Anything).Return(nil)
    39  	return blockTimer
    40  }
    41  
    42  // MockSealValidator returns a SealValidator that accepts
    43  // all seals without performing any
    44  // integrity checks, returns first seal in block as valid one
    45  func MockSealValidator(sealsDB storage.Seals) module.SealValidator {
    46  	validator := &modulemock.SealValidator{}
    47  	validator.On("Validate", mock.Anything).Return(
    48  		func(candidate *flow.Block) *flow.Seal {
    49  			if len(candidate.Payload.Seals) > 0 {
    50  				return candidate.Payload.Seals[0]
    51  			}
    52  			last, _ := sealsDB.HighestInFork(candidate.Header.ParentID)
    53  			return last
    54  		},
    55  		func(candidate *flow.Block) error {
    56  			if len(candidate.Payload.Seals) > 0 {
    57  				return nil
    58  			}
    59  			_, err := sealsDB.HighestInFork(candidate.Header.ParentID)
    60  			return err
    61  		}).Maybe()
    62  	return validator
    63  }
    64  
    65  func RunWithBootstrapState(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.State)) {
    66  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    67  		metrics := metrics.NewNoopCollector()
    68  		headers, _, seals, _, _, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
    69  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
    70  		require.NoError(t, err)
    71  		f(db, state)
    72  	})
    73  }
    74  
    75  func RunWithFullProtocolState(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.MutableState)) {
    76  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    77  		metrics := metrics.NewNoopCollector()
    78  		tracer := trace.NewNoopTracer()
    79  		consumer := events.NewNoop()
    80  		headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
    81  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
    82  		require.NoError(t, err)
    83  		receiptValidator := MockReceiptValidator()
    84  		sealValidator := MockSealValidator(seals)
    85  		mockTimer := MockBlockTimer()
    86  		fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
    87  		require.NoError(t, err)
    88  		f(db, fullState)
    89  	})
    90  }
    91  
    92  func RunWithFullProtocolStateAndMetrics(t testing.TB, rootSnapshot protocol.Snapshot, metrics module.ComplianceMetrics, f func(*badger.DB, *pbadger.MutableState)) {
    93  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    94  		tracer := trace.NewNoopTracer()
    95  		consumer := events.NewNoop()
    96  		headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
    97  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
    98  		require.NoError(t, err)
    99  		receiptValidator := MockReceiptValidator()
   100  		sealValidator := MockSealValidator(seals)
   101  		mockTimer := MockBlockTimer()
   102  		fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
   103  		require.NoError(t, err)
   104  		f(db, fullState)
   105  	})
   106  }
   107  
   108  func RunWithFullProtocolStateAndValidator(t testing.TB, rootSnapshot protocol.Snapshot, validator module.ReceiptValidator, f func(*badger.DB, *pbadger.MutableState)) {
   109  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   110  		metrics := metrics.NewNoopCollector()
   111  		tracer := trace.NewNoopTracer()
   112  		consumer := events.NewNoop()
   113  		headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
   114  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
   115  		require.NoError(t, err)
   116  		sealValidator := MockSealValidator(seals)
   117  		mockTimer := MockBlockTimer()
   118  		fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, validator, sealValidator)
   119  		require.NoError(t, err)
   120  		f(db, fullState)
   121  	})
   122  }
   123  
   124  func RunWithFollowerProtocolState(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.FollowerState)) {
   125  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   126  		metrics := metrics.NewNoopCollector()
   127  		tracer := trace.NewNoopTracer()
   128  		consumer := events.NewNoop()
   129  		headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
   130  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
   131  		require.NoError(t, err)
   132  		mockTimer := MockBlockTimer()
   133  		followerState, err := pbadger.NewFollowerState(state, index, payloads, tracer, consumer, mockTimer)
   134  		require.NoError(t, err)
   135  		f(db, followerState)
   136  	})
   137  }
   138  
   139  func RunWithFullProtocolStateAndConsumer(t testing.TB, rootSnapshot protocol.Snapshot, consumer protocol.Consumer, f func(*badger.DB, *pbadger.MutableState)) {
   140  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   141  		metrics := metrics.NewNoopCollector()
   142  		tracer := trace.NewNoopTracer()
   143  		headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
   144  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
   145  		require.NoError(t, err)
   146  		receiptValidator := MockReceiptValidator()
   147  		sealValidator := MockSealValidator(seals)
   148  		mockTimer := MockBlockTimer()
   149  		fullState, err := pbadger.NewFullConsensusState(state, index, payloads, tracer, consumer, mockTimer, receiptValidator, sealValidator)
   150  		require.NoError(t, err)
   151  		f(db, fullState)
   152  	})
   153  }
   154  
   155  func RunWithFollowerProtocolStateAndHeaders(t testing.TB, rootSnapshot protocol.Snapshot, f func(*badger.DB, *pbadger.FollowerState, storage.Headers, storage.Index)) {
   156  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   157  		metrics := metrics.NewNoopCollector()
   158  		tracer := trace.NewNoopTracer()
   159  		consumer := events.NewNoop()
   160  		headers, _, seals, index, payloads, blocks, setups, commits, statuses, results := util.StorageLayer(t, db)
   161  		state, err := pbadger.Bootstrap(metrics, db, headers, seals, results, blocks, setups, commits, statuses, rootSnapshot)
   162  		require.NoError(t, err)
   163  		mockTimer := MockBlockTimer()
   164  		followerState, err := pbadger.NewFollowerState(state, index, payloads, tracer, consumer, mockTimer)
   165  		require.NoError(t, err)
   166  		f(db, followerState, headers, index)
   167  	})
   168  }