laconicd/x/evm/types/utils.go

67 lines
1.7 KiB
Go
Raw Normal View History

package types
import (
2021-04-18 15:54:18 +00:00
log "github.com/xlab/suplog"
2021-04-17 10:00:07 +00:00
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"golang.org/x/crypto/sha3"
2021-04-18 15:54:18 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
ethcmn "github.com/ethereum/go-ethereum/common"
2018-11-28 22:19:22 +00:00
"github.com/ethereum/go-ethereum/rlp"
)
2018-11-28 22:19:22 +00:00
func rlpHash(x interface{}) (hash ethcmn.Hash) {
hasher := sha3.NewLegacyKeccak256()
_ = rlp.Encode(hasher, x)
_ = hasher.Sum(hash[:0])
2018-11-28 22:19:22 +00:00
return hash
2018-08-24 15:29:10 +00:00
}
2021-04-17 10:00:07 +00:00
// EncodeTxResponse takes all of the necessary data from the EVM execution
// and returns the data as a byte slice encoded with protobuf.
func EncodeTxResponse(res *MsgEthereumTxResponse) ([]byte, error) {
return proto.Marshal(res)
}
2021-04-17 10:00:07 +00:00
// DecodeTxResponse decodes an protobuf-encoded byte slice into TxResponse
2021-04-18 15:54:18 +00:00
func DecodeTxResponse(in []byte) (*MsgEthereumTxResponse, error) {
var txMsgData sdk.TxMsgData
if err := proto.Unmarshal(in, &txMsgData); err != nil {
log.WithError(err).Errorln("failed to unmarshal TxMsgData")
return nil, err
}
dataList := txMsgData.GetData()
if len(dataList) == 0 {
return &MsgEthereumTxResponse{}, nil
}
var res MsgEthereumTxResponse
err := proto.Unmarshal(dataList[0].GetData(), &res)
if err != nil {
err = errors.Wrap(err, "proto.Unmarshal failed")
return nil, err
}
return &res, nil
}
// EncodeTransactionLogs encodes TransactionLogs slice into a protobuf-encoded byte slice.
func EncodeTransactionLogs(res *TransactionLogs) ([]byte, error) {
return proto.Marshal(res)
}
// DecodeTxResponse decodes an protobuf-encoded byte slice into TransactionLogs
func DecodeTransactionLogs(data []byte) (TransactionLogs, error) {
var logs TransactionLogs
err := proto.Unmarshal(data, &logs)
if err != nil {
2021-04-18 15:54:18 +00:00
return TransactionLogs{}, err
}
2021-04-18 15:54:18 +00:00
return logs, nil
}