Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulator Feature: Report gas used #87

Merged
merged 7 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions convert/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
avcdsld marked this conversation as resolved.
Show resolved Hide resolved
Error: VMErrorToEmulator(tp.Err),
Logs: tp.Logs,
Events: sdkEvents,
}, nil
}

Expand Down
65 changes: 65 additions & 0 deletions convert/vm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Flow Emulator
*
* Copyright 2019-2020 Dapper Labs, Inc.
avcdsld marked this conversation as resolved.
Show resolved Hide resolved
*
* 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)
})
}
2 changes: 2 additions & 0 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
avcdsld marked this conversation as resolved.
Show resolved Hide resolved
Info("⭐ Transaction executed")
} else {
logger.
WithField("txID", result.TransactionID.String()).
WithField("gasUsed", result.ComputationGasUsed).
avcdsld marked this conversation as resolved.
Show resolved Hide resolved
Warn("❗ Transaction reverted")
}

Expand Down
9 changes: 5 additions & 4 deletions types/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
73 changes: 73 additions & 0 deletions types/result_test.go
Original file line number Diff line number Diff line change
@@ -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())

sideninja marked this conversation as resolved.
Show resolved Hide resolved
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())
})
}