Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

Expand All @@ -19,11 +20,15 @@ type TransactionResult struct {
func (tb FlowTransactionBuilder) Test(t *testing.T) TransactionResult {
locale, _ := time.LoadLocation("UTC")
time.Local = locale
events, err := tb.RunE(context.Background())
formattedEvents := make([]*FormatedEvent, len(events))
for i, event := range events {
ev := ParseEvent(event, uint64(0), time.Unix(0, 0), []string{})
formattedEvents[i] = ev
txResult, err := tb.RunE(context.Background())
var formattedEvents []*FormatedEvent
if err == nil {
events := txResult.Events
formattedEvents = make([]*FormatedEvent, len(events))
for i, event := range events {
ev := ParseEvent(event, uint64(0), time.Unix(0, 0), []string{})
formattedEvents[i] = ev
}
}
return TransactionResult{
Err: err,
Expand All @@ -45,6 +50,11 @@ func (t TransactionResult) AssertSuccess() TransactionResult {
return t
}

func (t TransactionResult) RequireSuccess() TransactionResult {
require.NoError(t.Testing, t.Err)
return t
}

func (t TransactionResult) AssertEventCount(number int) TransactionResult {
assert.Equal(t.Testing, number, len(t.Events))
return t
Expand Down
45 changes: 42 additions & 3 deletions connector.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package splash

import (
"errors"
"fmt"
"log"
"testing"

"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flow-go-sdk/access"
grpcAccess "github.com/onflow/flow-go-sdk/access/grpc"
flowgo "github.com/onflow/flow-go/model/flow"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/accounts"
"github.com/onflow/flowkit/v2/config"
"github.com/onflow/flowkit/v2/gateway"
"github.com/onflow/flowkit/v2/output"
"github.com/rs/zerolog"
zeroLog "github.com/rs/zerolog/log"
"github.com/spf13/afero"
"google.golang.org/grpc"
)
Expand All @@ -24,6 +29,8 @@ type Connector struct {
Network string
Logger output.Logger
PrependNetworkToAccountNames bool

emulatorGateway *EmulatorGateway
}

// maxGRPCMessageSize 60mb
Expand Down Expand Up @@ -82,11 +89,16 @@ func NewInMemoryConnector(paths []string, baseLoader flowkit.ReaderWriter, enabl
SigAlgo: acc.Key.SigAlgo(),
HashAlgo: acc.Key.HashAlgo(),
}
var gw *gateway.EmulatorGateway

loggerOpt := emulator.WithServerLogger(
zeroLog.Logger.Level(zerolog.InfoLevel).With().Str("module", "emulator").Logger(),
)

var gw *EmulatorGateway
if enableTxFees {
gw = gateway.NewEmulatorGatewayWithOpts(key, gateway.WithEmulatorOptions(emulator.WithTransactionFeesEnabled(true)))
gw = NewEmulatorGatewayWithOpts(key, WithEmulatorOptions(loggerOpt, emulator.WithTransactionFeesEnabled(true)))
} else {
gw = gateway.NewEmulatorGateway(key)
gw = NewEmulatorGatewayWithOpts(key, WithEmulatorOptions(loggerOpt))
}
service := flowkit.NewFlowkit(state, config.EmulatorNetwork, gw, logger)

Expand All @@ -96,6 +108,7 @@ func NewInMemoryConnector(paths []string, baseLoader flowkit.ReaderWriter, enabl
Logger: logger,
PrependNetworkToAccountNames: true,
Network: "emulator",
emulatorGateway: gw,
}, nil
}

Expand Down Expand Up @@ -128,3 +141,29 @@ func (c *Connector) Account(key string) *accounts.Account {

return account
}

func (c *Connector) EnableAutoMine() *Connector {
if c.emulatorGateway != nil {
c.emulatorGateway.EnableAutoMine()
}
return c
}

func (c *Connector) DisableAutoMine() *Connector {
if c.emulatorGateway != nil {
c.emulatorGateway.DisableAutoMine()
}
return c
}

func (c *Connector) ExecuteAndCommitBlock(t *testing.T) (*flowgo.Block, []TransactionResult, error) { //nolint:thelper
if c.emulatorGateway != nil {
return c.emulatorGateway.ExecuteAndCommitBlock(t)
} else {
return nil, nil, errors.New("emulator gateway not initialized")
}
}

func (c *Connector) IsInMemoryEmulator() bool {
return c.emulatorGateway != nil
}
Loading
Loading