From 1dcca4778a19307dd092c3d0527219bb4c44bca4 Mon Sep 17 00:00:00 2001 From: avcdsld Date: Sun, 26 Sep 2021 23:55:06 +0900 Subject: [PATCH 1/7] add gasUsed report feature --- convert/vm.go | 9 +++++---- server/backend/backend.go | 2 ++ types/result.go | 9 +++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/convert/vm.go b/convert/vm.go index 6b23121b..d9127579 100644 --- a/convert/vm.go +++ b/convert/vm.go @@ -35,10 +35,11 @@ func VMTransactionResultToEmulator(tp *fvm.TransactionProcedure) (*types.Transac } return &types.TransactionResult{ - TransactionID: txID, - Error: VMErrorToEmulator(tp.Err), - Logs: tp.Logs, - Events: sdkEvents, + TransactionID: txID, + ComputationGasUsed: tp.ComputationUsed, + Error: VMErrorToEmulator(tp.Err), + Logs: tp.Logs, + Events: sdkEvents, }, nil } diff --git a/server/backend/backend.go b/server/backend/backend.go index 6bd7d4b2..69a8717b 100644 --- a/server/backend/backend.go +++ b/server/backend/backend.go @@ -583,10 +583,12 @@ func printTransactionResult(logger *logrus.Logger, result *types.TransactionResu if result.Succeeded() { logger. WithField("txID", result.TransactionID.String()). + WithField("gasUsed", result.ComputationGasUsed). Info("⭐ Transaction executed") } else { logger. WithField("txID", result.TransactionID.String()). + WithField("gasUsed", result.ComputationGasUsed). Warn("❗ Transaction reverted") } diff --git a/types/result.go b/types/result.go index 9fc7efe7..2d061707 100644 --- a/types/result.go +++ b/types/result.go @@ -33,10 +33,11 @@ type StorableTransactionResult struct { // A TransactionResult is the result of executing a transaction. type TransactionResult struct { - TransactionID flow.Identifier - Error error - Logs []string - Events []flow.Event + TransactionID flow.Identifier + ComputationGasUsed uint64 + Error error + Logs []string + Events []flow.Event } // Succeeded returns true if the transaction executed without errors. From 90fdfeee61c202f124a1a760f65ab0b794b68af1 Mon Sep 17 00:00:00 2001 From: avcdsld Date: Tue, 28 Sep 2021 21:51:46 +0900 Subject: [PATCH 2/7] add vm_test --- convert/vm_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 convert/vm_test.go diff --git a/convert/vm_test.go b/convert/vm_test.go new file mode 100644 index 00000000..f024c16c --- /dev/null +++ b/convert/vm_test.go @@ -0,0 +1,65 @@ +/* + * Flow Emulator + * + * Copyright 2019-2020 Dapper Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package convert_test + +import ( + "testing" + + "github.com/onflow/flow-go-sdk/test" + "github.com/onflow/flow-go/fvm" + "github.com/stretchr/testify/assert" + + "github.com/onflow/flow-emulator/convert" + sdkConvert "github.com/onflow/flow-emulator/convert/sdk" + flowgo "github.com/onflow/flow-go/model/flow" +) + +func TestVm(t *testing.T) { + t.Run("should be able to convert", func(t *testing.T) { + idGenerator := test.IdentifierGenerator() + + eventGenerator := test.EventGenerator() + event1, err := sdkConvert.SDKEventToFlow(eventGenerator.New()) + assert.NoError(t, err) + + event2, err := sdkConvert.SDKEventToFlow(eventGenerator.New()) + assert.NoError(t, err) + + tp := &fvm.TransactionProcedure{ + ID: flowgo.Identifier(idGenerator.New()), + Logs: []string{"TestLog1", "TestLog2"}, + Events: []flowgo.Event{event1, event2}, + ComputationUsed: 5, + Err: nil, + } + + tr, err := convert.VMTransactionResultToEmulator(tp) + assert.NoError(t, err) + + assert.Equal(t, tp.ID, flowgo.Identifier(tr.TransactionID)) + assert.Equal(t, tp.Logs, tr.Logs) + + flowEvents, err := sdkConvert.FlowEventsToSDK(tp.Events) + assert.NoError(t, err) + assert.Equal(t, flowEvents, tr.Events) + + assert.Equal(t, tp.ComputationUsed, tr.ComputationGasUsed) + assert.Equal(t, tp.Err, tr.Error) + }) +} From 0d90e5aa33591320a067220373426c122d832041 Mon Sep 17 00:00:00 2001 From: avcdsld Date: Tue, 28 Sep 2021 22:18:44 +0900 Subject: [PATCH 3/7] add types_test --- types/result_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 types/result_test.go diff --git a/types/result_test.go b/types/result_test.go new file mode 100644 index 00000000..118dc2b7 --- /dev/null +++ b/types/result_test.go @@ -0,0 +1,73 @@ +/* + * Flow Emulator + * + * Copyright 2019-2020 Dapper Labs, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package types_test + +import ( + "errors" + "testing" + + "github.com/onflow/cadence" + "github.com/onflow/flow-go-sdk" + "github.com/onflow/flow-go-sdk/test" + "github.com/stretchr/testify/assert" + + "github.com/onflow/flow-emulator/types" +) + +func TestResult(t *testing.T) { + t.Run("should return correct boolean", func(t *testing.T) { + idGenerator := test.IdentifierGenerator() + + trSucceed := &types.TransactionResult{ + TransactionID: idGenerator.New(), + ComputationGasUsed: 20, + Error: nil, + Logs: []string{}, + Events: []flow.Event{}, + } + assert.True(t, trSucceed.Succeeded()) + + trReverted := &types.TransactionResult{ + TransactionID: idGenerator.New(), + ComputationGasUsed: 20, + Error: errors.New("transaction execution error"), + Logs: []string{}, + Events: []flow.Event{}, + } + assert.True(t, trReverted.Reverted()) + + srSucceed := &types.ScriptResult{ + ScriptID: idGenerator.New(), + Value: cadence.Value(cadence.NewInt(1)), + Error: nil, + Logs: []string{}, + Events: []flow.Event{}, + } + assert.True(t, srSucceed.Succeeded()) + + srReverted := &types.ScriptResult{ + ScriptID: idGenerator.New(), + Value: cadence.Value(cadence.NewInt(1)), + Error: errors.New("transaction execution error"), + Logs: []string{}, + Events: []flow.Event{}, + } + assert.True(t, srReverted.Reverted()) + }) +} From c56b9eeb693e21b0407c12209122c4f26ae100c6 Mon Sep 17 00:00:00 2001 From: avcdsld Date: Tue, 28 Sep 2021 23:28:42 +0900 Subject: [PATCH 4/7] add test assertions --- types/result_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/types/result_test.go b/types/result_test.go index 118dc2b7..82dd6f29 100644 --- a/types/result_test.go +++ b/types/result_test.go @@ -42,6 +42,7 @@ func TestResult(t *testing.T) { Events: []flow.Event{}, } assert.True(t, trSucceed.Succeeded()) + assert.False(t, trSucceed.Reverted()) trReverted := &types.TransactionResult{ TransactionID: idGenerator.New(), @@ -51,6 +52,7 @@ func TestResult(t *testing.T) { Events: []flow.Event{}, } assert.True(t, trReverted.Reverted()) + assert.False(t, trReverted.Succeeded()) srSucceed := &types.ScriptResult{ ScriptID: idGenerator.New(), @@ -60,6 +62,7 @@ func TestResult(t *testing.T) { Events: []flow.Event{}, } assert.True(t, srSucceed.Succeeded()) + assert.False(t, srSucceed.Reverted()) srReverted := &types.ScriptResult{ ScriptID: idGenerator.New(), @@ -69,5 +72,6 @@ func TestResult(t *testing.T) { Events: []flow.Event{}, } assert.True(t, srReverted.Reverted()) + assert.False(t, srReverted.Succeeded()) }) } From 6305033e5dafe6b0f1450c2e51f726192998df8b Mon Sep 17 00:00:00 2001 From: Takamasa Arakawa Date: Wed, 29 Sep 2021 08:59:51 +0900 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Janez Podhostnik <67895329+janezpodhostnik@users.noreply.github.com> --- convert/vm.go | 2 +- convert/vm_test.go | 2 +- server/backend/backend.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/convert/vm.go b/convert/vm.go index d9127579..76f15355 100644 --- a/convert/vm.go +++ b/convert/vm.go @@ -36,7 +36,7 @@ func VMTransactionResultToEmulator(tp *fvm.TransactionProcedure) (*types.Transac return &types.TransactionResult{ TransactionID: txID, - ComputationGasUsed: tp.ComputationUsed, + ComputationUsed: tp.ComputationUsed, Error: VMErrorToEmulator(tp.Err), Logs: tp.Logs, Events: sdkEvents, diff --git a/convert/vm_test.go b/convert/vm_test.go index f024c16c..84e5db5c 100644 --- a/convert/vm_test.go +++ b/convert/vm_test.go @@ -1,7 +1,7 @@ /* * Flow Emulator * - * Copyright 2019-2020 Dapper Labs, Inc. + * Copyright 2019-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/server/backend/backend.go b/server/backend/backend.go index 69a8717b..e7eff218 100644 --- a/server/backend/backend.go +++ b/server/backend/backend.go @@ -583,12 +583,12 @@ func printTransactionResult(logger *logrus.Logger, result *types.TransactionResu if result.Succeeded() { logger. WithField("txID", result.TransactionID.String()). - WithField("gasUsed", result.ComputationGasUsed). + WithField("computationUsed", result.ComputationUsed). Info("⭐ Transaction executed") } else { logger. WithField("txID", result.TransactionID.String()). - WithField("gasUsed", result.ComputationGasUsed). + WithField("computationUsed", result.ComputationUsed). Warn("❗ Transaction reverted") } From f1137161cea780f2e2870f0fb16925b2b8afdb39 Mon Sep 17 00:00:00 2001 From: avcdsld Date: Wed, 29 Sep 2021 09:02:14 +0900 Subject: [PATCH 6/7] fix variable name --- convert/vm_test.go | 2 +- types/result.go | 12 ++++++------ types/result_test.go | 20 ++++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/convert/vm_test.go b/convert/vm_test.go index 84e5db5c..f858b384 100644 --- a/convert/vm_test.go +++ b/convert/vm_test.go @@ -59,7 +59,7 @@ func TestVm(t *testing.T) { assert.NoError(t, err) assert.Equal(t, flowEvents, tr.Events) - assert.Equal(t, tp.ComputationUsed, tr.ComputationGasUsed) + assert.Equal(t, tp.ComputationUsed, tr.ComputationUsed) assert.Equal(t, tp.Err, tr.Error) }) } diff --git a/types/result.go b/types/result.go index 2d061707..1fdba788 100644 --- a/types/result.go +++ b/types/result.go @@ -19,9 +19,9 @@ package types import ( - flowgo "github.com/onflow/flow-go/model/flow" "github.com/onflow/cadence" "github.com/onflow/flow-go-sdk" + flowgo "github.com/onflow/flow-go/model/flow" ) type StorableTransactionResult struct { @@ -33,11 +33,11 @@ type StorableTransactionResult struct { // A TransactionResult is the result of executing a transaction. type TransactionResult struct { - TransactionID flow.Identifier - ComputationGasUsed uint64 - Error error - Logs []string - Events []flow.Event + TransactionID flow.Identifier + ComputationUsed uint64 + Error error + Logs []string + Events []flow.Event } // Succeeded returns true if the transaction executed without errors. diff --git a/types/result_test.go b/types/result_test.go index 82dd6f29..b6e94422 100644 --- a/types/result_test.go +++ b/types/result_test.go @@ -35,21 +35,21 @@ func TestResult(t *testing.T) { idGenerator := test.IdentifierGenerator() trSucceed := &types.TransactionResult{ - TransactionID: idGenerator.New(), - ComputationGasUsed: 20, - Error: nil, - Logs: []string{}, - Events: []flow.Event{}, + TransactionID: idGenerator.New(), + ComputationUsed: 20, + Error: nil, + Logs: []string{}, + Events: []flow.Event{}, } assert.True(t, trSucceed.Succeeded()) assert.False(t, trSucceed.Reverted()) trReverted := &types.TransactionResult{ - TransactionID: idGenerator.New(), - ComputationGasUsed: 20, - Error: errors.New("transaction execution error"), - Logs: []string{}, - Events: []flow.Event{}, + TransactionID: idGenerator.New(), + ComputationUsed: 20, + Error: errors.New("transaction execution error"), + Logs: []string{}, + Events: []flow.Event{}, } assert.True(t, trReverted.Reverted()) assert.False(t, trReverted.Succeeded()) From 2b9498aa8b664c6649dca85f05c80484329cf92b Mon Sep 17 00:00:00 2001 From: avcdsld Date: Wed, 29 Sep 2021 12:28:53 +0900 Subject: [PATCH 7/7] fix indent --- convert/vm.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/convert/vm.go b/convert/vm.go index 76f15355..f3c98edc 100644 --- a/convert/vm.go +++ b/convert/vm.go @@ -35,11 +35,11 @@ func VMTransactionResultToEmulator(tp *fvm.TransactionProcedure) (*types.Transac } return &types.TransactionResult{ - TransactionID: txID, + TransactionID: txID, ComputationUsed: tp.ComputationUsed, - Error: VMErrorToEmulator(tp.Err), - Logs: tp.Logs, - Events: sdkEvents, + Error: VMErrorToEmulator(tp.Err), + Logs: tp.Logs, + Events: sdkEvents, }, nil }