From 78acd1d174e63d0f79407db84f12bc45b6f35f26 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Mon, 29 Jul 2024 14:04:20 -0700 Subject: [PATCH 1/3] expose logger to evm environment --- fvm/environment/env.go | 3 +-- fvm/environment/logger.go | 8 ++++++++ fvm/evm/backends/wrappedEnv.go | 5 +++++ fvm/evm/testutils/backend.go | 16 ++++++++++++++++ fvm/evm/types/backend.go | 1 + 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 fvm/environment/logger.go diff --git a/fvm/environment/env.go b/fvm/environment/env.go index 33d7068e4e2..5b01728111c 100644 --- a/fvm/environment/env.go +++ b/fvm/environment/env.go @@ -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" @@ -27,7 +26,7 @@ type Environment interface { TransactionInfo // ProgramLogger - Logger() zerolog.Logger + LoggerProvider Logs() []string // EventEmitter diff --git a/fvm/environment/logger.go b/fvm/environment/logger.go new file mode 100644 index 00000000000..85b70e1520f --- /dev/null +++ b/fvm/environment/logger.go @@ -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 +} diff --git a/fvm/evm/backends/wrappedEnv.go b/fvm/evm/backends/wrappedEnv.go index 6f122aecbbe..5eea87bd128 100644 --- a/fvm/evm/backends/wrappedEnv.go +++ b/fvm/evm/backends/wrappedEnv.go @@ -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" @@ -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 diff --git a/fvm/evm/testutils/backend.go b/fvm/evm/testutils/backend.go index 8e797388e50..b692812ef9d 100644 --- a/fvm/evm/testutils/backend.go +++ b/fvm/evm/testutils/backend.go @@ -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" @@ -195,6 +196,7 @@ type TestBackend struct { *testUUIDGenerator *TestTracer *TestMetricsReporter + *TestLoggerProvider } var _ types.Backend = &TestBackend{} @@ -549,15 +551,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() +} diff --git a/fvm/evm/types/backend.go b/fvm/evm/types/backend.go index 7f955179ad4..7f46cddecc8 100644 --- a/fvm/evm/types/backend.go +++ b/fvm/evm/types/backend.go @@ -17,4 +17,5 @@ type Backend interface { environment.UUIDGenerator environment.Tracer environment.EVMMetricsReporter + environment.LoggerProvider } From f7181af7e18cffadde5064bb385c3c29994f3e4b Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 15 Aug 2024 14:01:52 -0700 Subject: [PATCH 2/3] capture evm block committed log --- fvm/evm/handler/handler.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fvm/evm/handler/handler.go b/fvm/evm/handler/handler.go index c8e0338778c..dcc86ea56b7 100644 --- a/fvm/evm/handler/handler.go +++ b/fvm/evm/handler/handler.go @@ -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 } From b9b064dcb4934b0bca412b96fa20c50f519db1cc Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 15 Aug 2024 20:42:16 -0700 Subject: [PATCH 3/3] fix test --- fvm/evm/testutils/backend.go | 1 + fvm/evm/types/balance_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/fvm/evm/testutils/backend.go b/fvm/evm/testutils/backend.go index b692812ef9d..0be5599d093 100644 --- a/fvm/evm/testutils/backend.go +++ b/fvm/evm/testutils/backend.go @@ -46,6 +46,7 @@ func RunWithTestBackend(t testing.TB, f func(*TestBackend)) { TestContractFunctionInvoker: &TestContractFunctionInvoker{}, TestTracer: &TestTracer{}, TestMetricsReporter: &TestMetricsReporter{}, + TestLoggerProvider: &TestLoggerProvider{}, } f(tb) } diff --git a/fvm/evm/types/balance_test.go b/fvm/evm/types/balance_test.go index d01b086c3a1..f8381d1513f 100644 --- a/fvm/evm/types/balance_test.go +++ b/fvm/evm/types/balance_test.go @@ -45,4 +45,5 @@ func TestBalance(t *testing.T) { require.NoError(t, err) ret := types.UnsafeCastOfBalanceToFloat64(bal) require.Equal(t, 100.0002, ret) + }