github.com/onflow/flow-go@v0.33.17/utils/unittest/generator/events.go (about)

     1  package generator
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/cadence"
     7  	"github.com/onflow/cadence/encoding/ccf"
     8  	jsoncdc "github.com/onflow/cadence/encoding/json"
     9  	"github.com/onflow/cadence/runtime/common"
    10  	"github.com/onflow/flow/protobuf/go/flow/entities"
    11  
    12  	"github.com/onflow/flow-go/model/flow"
    13  	"github.com/onflow/flow-go/utils/unittest"
    14  )
    15  
    16  type EventGeneratorOption func(*Events)
    17  
    18  func WithEncoding(encoding entities.EventEncodingVersion) EventGeneratorOption {
    19  	return func(g *Events) {
    20  		g.encoding = encoding
    21  	}
    22  }
    23  
    24  type Events struct {
    25  	count    uint32
    26  	ids      *Identifiers
    27  	encoding entities.EventEncodingVersion
    28  }
    29  
    30  func EventGenerator(opts ...EventGeneratorOption) *Events {
    31  	g := &Events{
    32  		count:    1,
    33  		ids:      IdentifierGenerator(),
    34  		encoding: entities.EventEncodingVersion_CCF_V0,
    35  	}
    36  
    37  	for _, opt := range opts {
    38  		opt(g)
    39  	}
    40  
    41  	return g
    42  }
    43  
    44  func (g *Events) New() flow.Event {
    45  	address, err := common.BytesToAddress(unittest.RandomAddressFixture().Bytes())
    46  	if err != nil {
    47  		panic(fmt.Sprintf("unexpected error while creating random address: %s", err))
    48  	}
    49  
    50  	location := common.NewAddressLocation(nil, address, "TestContract")
    51  	identifier := fmt.Sprintf("TestContract.FooEvent%d", g.count)
    52  	typeID := location.TypeID(nil, identifier)
    53  
    54  	testEventType := &cadence.EventType{
    55  		Location:            location,
    56  		QualifiedIdentifier: identifier,
    57  		Fields: []cadence.Field{
    58  			{
    59  				Identifier: "a",
    60  				Type:       cadence.IntType{},
    61  			},
    62  			{
    63  				Identifier: "b",
    64  				Type:       cadence.StringType{},
    65  			},
    66  		},
    67  	}
    68  
    69  	fooString, err := cadence.NewString("foo")
    70  	if err != nil {
    71  		panic(fmt.Sprintf("unexpected error while creating cadence string: %s", err))
    72  	}
    73  
    74  	testEvent := cadence.NewEvent(
    75  		[]cadence.Value{
    76  			cadence.NewInt(int(g.count)),
    77  			fooString,
    78  		}).WithType(testEventType)
    79  
    80  	var payload []byte
    81  	switch g.encoding {
    82  	case entities.EventEncodingVersion_CCF_V0:
    83  		payload, err = ccf.Encode(testEvent)
    84  		if err != nil {
    85  			panic(fmt.Sprintf("unexpected error while ccf encoding events: %s", err))
    86  		}
    87  	case entities.EventEncodingVersion_JSON_CDC_V0:
    88  		payload, err = jsoncdc.Encode(testEvent)
    89  		if err != nil {
    90  			panic(fmt.Sprintf("unexpected error while json encoding events: %s", err))
    91  		}
    92  	}
    93  
    94  	event := flow.Event{
    95  		Type:             flow.EventType(typeID),
    96  		TransactionID:    g.ids.New(),
    97  		TransactionIndex: g.count,
    98  		EventIndex:       g.count,
    99  		Payload:          payload,
   100  	}
   101  
   102  	g.count++
   103  
   104  	return event
   105  }
   106  
   107  // GetEventsWithEncoding generates a specified number of events with a given encoding version.
   108  func GetEventsWithEncoding(n int, version entities.EventEncodingVersion) []flow.Event {
   109  	eventGenerator := EventGenerator(WithEncoding(version))
   110  	events := make([]flow.Event, 0, n)
   111  	for i := 0; i < n; i++ {
   112  		events = append(events, eventGenerator.New())
   113  	}
   114  	return events
   115  }