2018-08-09 01:06:34 +00:00
|
|
|
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"
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/crypto/sha3"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-09 01:06:34 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2018-11-28 22:19:22 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-08-09 01:06:34 +00:00
|
|
|
)
|
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
func rlpHash(x interface{}) (hash ethcmn.Hash) {
|
2019-07-11 18:05:34 +00:00
|
|
|
hasher := sha3.NewLegacyKeccak256()
|
2020-10-06 18:57:55 +00:00
|
|
|
_ = rlp.Encode(hasher, x)
|
|
|
|
_ = hasher.Sum(hash[:0])
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2019-07-11 18:05:34 +00:00
|
|
|
return hash
|
2018-08-24 15:29:10 +00:00
|
|
|
}
|
2019-10-22 16:40:34 +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)
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-04-01 18:49:21 +00:00
|
|
|
if err != nil {
|
2021-04-18 15:54:18 +00:00
|
|
|
return TransactionLogs{}, err
|
2019-10-22 16:40:34 +00:00
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
return logs, nil
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|