Skip to content

Commit

Permalink
Merge pull request #6343 from onflow/ramtin/evm-inject-logger-into-evm
Browse files Browse the repository at this point in the history
[Flow EVM] Expose logger to the EVM environment
  • Loading branch information
ramtinms authored Aug 20, 2024
2 parents caa362a + 1b73cda commit 3bd915b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
3 changes: 1 addition & 2 deletions fvm/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package environment
import (
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/rs/zerolog"

reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
"github.com/onflow/flow-go/model/flow"
Expand All @@ -27,7 +26,7 @@ type Environment interface {
TransactionInfo

// ProgramLogger
Logger() zerolog.Logger
LoggerProvider
Logs() []string

// EventEmitter
Expand Down
8 changes: 8 additions & 0 deletions fvm/environment/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package environment

import "github.com/rs/zerolog"

// Logger provides access to the logger to collect logs
type LoggerProvider interface {
Logger() zerolog.Logger
}
5 changes: 5 additions & 0 deletions fvm/evm/backends/wrappedEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/rs/zerolog"
otelTrace "go.opentelemetry.io/otel/trace"

"github.com/onflow/flow-go/fvm/environment"
Expand Down Expand Up @@ -205,6 +206,10 @@ func (we *WrappedEnvironment) EVMBlockExecuted(
we.env.EVMBlockExecuted(txCount, totalGasUsed, totalSupplyInFlow)
}

func (we *WrappedEnvironment) Logger() zerolog.Logger {
return we.env.Logger()
}

func handleEnvironmentError(err error) error {
if err == nil {
return nil
Expand Down
9 changes: 9 additions & 0 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ func (h *ContractHandler) commitBlockProposal() error {
types.UnsafeCastOfBalanceToFloat64(bp.TotalSupply),
)

// log evm block commitment
logger := h.backend.Logger()
logger.Info().
Uint64("height", bp.Height).
Int("tx_count", len(bp.TxHashes)).
Uint64("total_gas_used", bp.TotalGasUsed).
Uint64("total_supply", bp.TotalSupply.Uint64()).
Msg("EVM Block Committed")

return nil
}

Expand Down
17 changes: 17 additions & 0 deletions fvm/evm/testutils/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/onflow/cadence/runtime/stdlib"
"github.com/rs/zerolog"
otelTrace "go.opentelemetry.io/otel/trace"

"github.com/onflow/atree"
Expand Down Expand Up @@ -45,6 +46,7 @@ func RunWithTestBackend(t testing.TB, f func(*TestBackend)) {
TestContractFunctionInvoker: &TestContractFunctionInvoker{},
TestTracer: &TestTracer{},
TestMetricsReporter: &TestMetricsReporter{},
TestLoggerProvider: &TestLoggerProvider{},
}
f(tb)
}
Expand Down Expand Up @@ -195,6 +197,7 @@ type TestBackend struct {
*testUUIDGenerator
*TestTracer
*TestMetricsReporter
*TestLoggerProvider
}

var _ types.Backend = &TestBackend{}
Expand Down Expand Up @@ -549,15 +552,29 @@ func (tmr *TestMetricsReporter) SetNumberOfDeployedCOAs(count uint64) {
tmr.SetNumberOfDeployedCOAsFunc(count)
}
}

func (tmr *TestMetricsReporter) EVMTransactionExecuted(gasUsed uint64, isDirectCall bool, failed bool) {
// call the method if available otherwise skip
if tmr.EVMTransactionExecutedFunc != nil {
tmr.EVMTransactionExecutedFunc(gasUsed, isDirectCall, failed)
}
}

func (tmr *TestMetricsReporter) EVMBlockExecuted(txCount int, totalGasUsed uint64, totalSupplyInFlow float64) {
// call the method if available otherwise skip
if tmr.EVMBlockExecutedFunc != nil {
tmr.EVMBlockExecutedFunc(txCount, totalGasUsed, totalSupplyInFlow)
}
}

type TestLoggerProvider struct {
LoggerFunc func() zerolog.Logger
}

func (tlp *TestLoggerProvider) Logger() zerolog.Logger {
// call the method if not available return noop logger
if tlp.LoggerFunc != nil {
return tlp.LoggerFunc()
}
return zerolog.Nop()
}
1 change: 1 addition & 0 deletions fvm/evm/types/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type Backend interface {
environment.UUIDGenerator
environment.Tracer
environment.EVMMetricsReporter
environment.LoggerProvider
}
1 change: 1 addition & 0 deletions fvm/evm/types/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ func TestBalance(t *testing.T) {
require.NoError(t, err)
ret := types.UnsafeCastOfBalanceToFloat64(bal)
require.Equal(t, 100.0002, ret)

}

0 comments on commit 3bd915b

Please sign in to comment.