Skip to content

Commit

Permalink
debug log encoding prepare calls
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Feb 4, 2021
1 parent 035130e commit 48c55a9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
12 changes: 9 additions & 3 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion runtime/runtime_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package runtime

import (
"fmt"
"time"

"github.com/onflow/cadence"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

}
18 changes: 13 additions & 5 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 48c55a9

Please sign in to comment.