a2f246c2a6
Uses the fastest slice making idiom of creating the well known size of a slice using make([]sdk.Attribute, len(response.Logs)) for i, log := range response.Logs { txLogAttrs[i] = ... } instead of make([]sdk.Attribute, 0) for _, log := range response.Logs { txLogAttrs = append(txLogAttrs, ...) } which had a few problems: 1. Using 0 for size then appending is quite slow yet we know the exact size 2. Using append instead of indexing is slower If we examine the advisory at https://bencher.orijtech.com/perfclinic/sliceupdate/ and the verdict at https://bencher.orijtech.com/perfclinic/sliceupdate/#verdict this new scheme shows a massive improvement in that call site. Fixes #825
84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package keeper
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
tmbytes "github.com/tendermint/tendermint/libs/bytes"
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
|
)
|
|
|
|
var _ types.MsgServer = &Keeper{}
|
|
|
|
// EthereumTx implements the gRPC MsgServer interface. It receives a transaction which is then
|
|
// executed (i.e applied) against the go-ethereum EVM. The provided SDK Context is set to the Keeper
|
|
// so that it can implements and call the StateDB methods without receiving it as a function
|
|
// parameter.
|
|
func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*types.MsgEthereumTxResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
k.WithContext(ctx)
|
|
|
|
sender := msg.From
|
|
tx := msg.AsTransaction()
|
|
|
|
response, err := k.ApplyTransaction(tx)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(err, "failed to apply transaction")
|
|
}
|
|
|
|
attrs := []sdk.Attribute{
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, tx.Value().String()),
|
|
// add event for ethereum transaction hash format
|
|
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, response.Hash),
|
|
}
|
|
|
|
if len(ctx.TxBytes()) > 0 {
|
|
// add event for tendermint transaction hash format
|
|
hash := tmbytes.HexBytes(tmtypes.Tx(ctx.TxBytes()).Hash())
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyTxHash, hash.String()))
|
|
}
|
|
|
|
if to := tx.To(); to != nil {
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyRecipient, to.Hex()))
|
|
}
|
|
|
|
if response.Failed() {
|
|
attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, response.VmError))
|
|
}
|
|
|
|
txLogAttrs := make([]sdk.Attribute, len(response.Logs))
|
|
for i, log := range response.Logs {
|
|
value, err := json.Marshal(log)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(err, "failed to encode log")
|
|
}
|
|
txLogAttrs[i] = sdk.NewAttribute(types.AttributeKeyTxLog, string(value))
|
|
}
|
|
|
|
// emit events
|
|
ctx.EventManager().EmitEvents(sdk.Events{
|
|
sdk.NewEvent(
|
|
types.EventTypeEthereumTx,
|
|
attrs...,
|
|
),
|
|
sdk.NewEvent(
|
|
types.EventTypeTxLog,
|
|
txLogAttrs...,
|
|
),
|
|
sdk.NewEvent(
|
|
sdk.EventTypeMessage,
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
sdk.NewAttribute(sdk.AttributeKeySender, sender),
|
|
sdk.NewAttribute(types.AttributeKeyTxType, fmt.Sprintf("%d", tx.Type())),
|
|
),
|
|
})
|
|
|
|
return response, nil
|
|
}
|