From 48c55a98f0f801c1fb8293122c7084f39efac2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 4 Feb 2021 14:42:58 -0800 Subject: [PATCH] debug log encoding prepare calls --- runtime/interface.go | 12 +++++++++--- runtime/runtime.go | 2 +- runtime/runtime_storage.go | 24 +++++++++++++++++++++++- runtime/runtime_test.go | 18 +++++++++++++----- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/runtime/interface.go b/runtime/interface.go index 87b618fee0..25bd075158 100644 --- a/runtime/interface.go +++ b/runtime/interface.go @@ -69,8 +69,8 @@ type Interface interface { RemoveAccountContractCode(address Address, name string) (err error) // GetSigningAccounts returns the signing accounts. GetSigningAccounts() ([]Address, error) - // Log logs a string. - Log(string) error + // ProgramLog logs program logs. + ProgramLog(string) error // EmitEvent is called when an event is emitted by the runtime. EmitEvent(cadence.Event) error // ValueExists returns true if the given key exists in the storage, owned by the given account. @@ -106,6 +106,8 @@ type Interface interface { GetStorageUsed(address Address) (value uint64, err error) // GetStorageCapacity gets storage capacity in bytes on the address. GetStorageCapacity(address Address) (value uint64, err error) + // ImplementationDebugLog logs implementation log statements on a debug-level + ImplementationDebugLog(message string) error } type HighLevelStorage interface { @@ -197,7 +199,7 @@ func (i *EmptyRuntimeInterface) GetSigningAccounts() ([]Address, error) { return nil, nil } -func (i *EmptyRuntimeInterface) Log(_ string) error { +func (i *EmptyRuntimeInterface) ProgramLog(_ string) error { return nil } @@ -233,6 +235,10 @@ func (i *EmptyRuntimeInterface) UnsafeRandom() (uint64, error) { return 0, nil } +func (i *EmptyRuntimeInterface) ImplementationDebugLog(_ string) error { + return nil +} + func (i *EmptyRuntimeInterface) VerifySignature( _ []byte, _ string, diff --git a/runtime/runtime.go b/runtime/runtime.go index bb75956ec4..8f99b5e62b 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -1376,7 +1376,7 @@ func (r *interpreterRuntime) newLogFunction(runtimeInterface Interface) interpre message := fmt.Sprint(invocation.Arguments[0]) var err error wrapPanic(func() { - err = runtimeInterface.Log(message) + err = runtimeInterface.ProgramLog(message) }) if err != nil { panic(err) diff --git a/runtime/runtime_storage.go b/runtime/runtime_storage.go index 2debcc0bd1..7b26490286 100644 --- a/runtime/runtime_storage.go +++ b/runtime/runtime_storage.go @@ -19,6 +19,7 @@ package runtime import ( + "fmt" "time" "github.com/onflow/cadence" @@ -331,7 +332,12 @@ func (s *runtimeStorage) encodeValue( ) { reportMetric( func() { - data, deferrals, err = interpreter.EncodeValue(value, []string{path}, true) + data, deferrals, err = interpreter.EncodeValue( + value, + []string{path}, + true, + s.prepareCallback, + ) }, s.runtimeInterface, func(metrics Metrics, duration time.Duration) { @@ -361,3 +367,19 @@ func (s *runtimeStorage) move( panic(err) } } + +func (s *runtimeStorage) prepareCallback(value interpreter.Value, path []string) { + logMessage := fmt.Sprintf( + "encoding value for key %s: %[1]T, %[1]v", + path, + value, + ) + var err error + wrapPanic(func() { + err = s.runtimeInterface.ImplementationDebugLog(logMessage) + }) + if err != nil { + panic(err) + } + +} diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index 672e053580..12dcb59b59 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -117,10 +117,11 @@ type testRuntimeInterface struct { signatureAlgorithm string, hashAlgorithm string, ) (bool, error) - hash func(data []byte, hashAlgorithm string) ([]byte, error) - setCadenceValue func(owner Address, key string, value cadence.Value) (err error) - getStorageUsed func(_ Address) (uint64, error) - getStorageCapacity func(_ Address) (uint64, error) + hash func(data []byte, hashAlgorithm string) ([]byte, error) + setCadenceValue func(owner Address, key string, value cadence.Value) (err error) + getStorageUsed func(address Address) (uint64, error) + getStorageCapacity func(address Address) (uint64, error) + implementationDebugLog func(message string) error } var _ Interface = &testRuntimeInterface{} @@ -201,7 +202,7 @@ func (i *testRuntimeInterface) GetSigningAccounts() ([]Address, error) { return i.getSigningAccounts() } -func (i *testRuntimeInterface) Log(message string) error { +func (i *testRuntimeInterface) ProgramLog(message string) error { i.log(message) return nil } @@ -340,6 +341,13 @@ func (i *testRuntimeInterface) GetStorageCapacity(address Address) (uint64, erro return i.getStorageCapacity(address) } +func (i *testRuntimeInterface) ImplementationDebugLog(message string) error { + if i.implementationDebugLog == nil { + return nil + } + return i.implementationDebugLog(message) +} + func TestRuntimeImport(t *testing.T) { t.Parallel()