Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve Blockchain logs #114

Merged
merged 1 commit into from
Jun 6, 2023
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
33 changes: 21 additions & 12 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type EmulatorBackend struct {
configuration *stdlib.Configuration

stdlibHandler stdlib.StandardLibraryHandler

// logCollection is a hook attached in the server logger, in order
// to aggregate and expose log messages from the blockchain.
logCollection *LogCollectionHook
}

type keyInfo struct {
Expand Down Expand Up @@ -112,14 +116,16 @@ func NewEmulatorBackend(
stdlibHandler stdlib.StandardLibraryHandler,
coverageReport *runtime.CoverageReport,
) *EmulatorBackend {
logCollectionHook := NewLogCollectionHook()
var blockchain *emulator.Blockchain
if coverageReport != nil {
excludeCommonLocations(coverageReport)
blockchain = newBlockchain(
logCollectionHook,
emulator.WithCoverageReport(coverageReport),
)
} else {
blockchain = newBlockchain()
blockchain = newBlockchain(logCollectionHook)
}

return &EmulatorBackend{
Expand All @@ -129,6 +135,7 @@ func NewEmulatorBackend(
fileResolver: fileResolver,
configuration: baseConfiguration(),
stdlibHandler: stdlibHandler,
logCollection: logCollectionHook,
}
}

Expand Down Expand Up @@ -430,17 +437,24 @@ func (e *EmulatorBackend) ReadFile(path string) (string, error) {
return e.fileResolver(path)
}

// Logs returns all the log messages from the blockchain.
func (e *EmulatorBackend) Logs() []string {
return e.logCollection.Logs
}

// newBlockchain returns an emulator blockchain for testing.
func newBlockchain(opts ...emulator.Option) *emulator.Blockchain {
func newBlockchain(
hook *LogCollectionHook,
opts ...emulator.Option,
) *emulator.Blockchain {
output := zerolog.ConsoleWriter{Out: os.Stdout}
logger := zerolog.New(output).With().Timestamp().Logger().Hook(hook)

b, err := emulator.New(
append(
[]emulator.Option{
emulator.WithStorageLimitEnabled(false),
emulator.WithServerLogger(
zerolog.New(
zerolog.ConsoleWriter{Out: os.Stdout},
).With().Timestamp().Logger(),
),
emulator.WithServerLogger(logger),
emulator.Contracts(emulator.CommonContracts),
},
opts...,
Expand Down Expand Up @@ -520,11 +534,6 @@ func (e *EmulatorBackend) Reset() {
e.blockOffset = 0
}

func (e *EmulatorBackend) Logs() []string {
// TODO
return nil
}

func (e *EmulatorBackend) ServiceAccount() (*stdlib.Account, error) {
// TODO
return nil, errors.New("TODO")
Expand Down
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/onflow/cadence-tools/test
go 1.18

require (
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/onflow/cadence v0.39.1
github.com/onflow/flow-emulator v0.50.1
github.com/onflow/flow-go v0.30.1-0.20230602200347-2a0e824db380
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.24.2 // indirect
github.com/libp2p/go-openssl v0.1.0 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/logrusorgru/aurora/v4 v4.0.0 // indirect
github.com/m4ksio/wal v1.0.1-0.20221209164835-154a17396e4c // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand Down
Loading