Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 15 additions & 8 deletions helios-chain/rpc/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"

"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -48,6 +49,7 @@ type APICreator = func(
tendermintWebsocketClient *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer types.EVMTxIndexer,
appCodec codec.Codec,
) []rpc.API

// apiCreators defines the JSON-RPC API namespaces.
Expand All @@ -60,8 +62,9 @@ func init() {
tmWSClient *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer types.EVMTxIndexer,
appCodec codec.Codec,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, appCodec)

return []rpc.API{
{
Expand All @@ -78,7 +81,7 @@ func init() {
},
}
},
Web3Namespace: func(*server.Context, client.Context, *rpcclient.WSClient, bool, types.EVMTxIndexer) []rpc.API {
Web3Namespace: func(*server.Context, client.Context, *rpcclient.WSClient, bool, types.EVMTxIndexer, codec.Codec) []rpc.API {
return []rpc.API{
{
Namespace: Web3Namespace,
Expand All @@ -88,7 +91,7 @@ func init() {
},
}
},
NetNamespace: func(_ *server.Context, clientCtx client.Context, _ *rpcclient.WSClient, _ bool, _ types.EVMTxIndexer) []rpc.API {
NetNamespace: func(_ *server.Context, clientCtx client.Context, _ *rpcclient.WSClient, _ bool, _ types.EVMTxIndexer, _ codec.Codec) []rpc.API {
return []rpc.API{
{
Namespace: NetNamespace,
Expand All @@ -103,8 +106,9 @@ func init() {
_ *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer types.EVMTxIndexer,
appCodec codec.Codec,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, appCodec)
return []rpc.API{
{
Namespace: PersonalNamespace,
Expand All @@ -114,7 +118,7 @@ func init() {
},
}
},
TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *rpcclient.WSClient, _ bool, _ types.EVMTxIndexer) []rpc.API {
TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *rpcclient.WSClient, _ bool, _ types.EVMTxIndexer, _ codec.Codec) []rpc.API {
return []rpc.API{
{
Namespace: TxPoolNamespace,
Expand All @@ -129,8 +133,9 @@ func init() {
_ *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer types.EVMTxIndexer,
appCodec codec.Codec,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, appCodec)
return []rpc.API{
{
Namespace: DebugNamespace,
Expand All @@ -145,8 +150,9 @@ func init() {
_ *rpcclient.WSClient,
allowUnprotectedTxs bool,
indexer types.EVMTxIndexer,
appCodec codec.Codec,
) []rpc.API {
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, appCodec)
return []rpc.API{
{
Namespace: MinerNamespace,
Expand All @@ -166,12 +172,13 @@ func GetRPCAPIs(ctx *server.Context,
allowUnprotectedTxs bool,
indexer types.EVMTxIndexer,
selectedAPIs []string,
appCodec codec.Codec,
) []rpc.API {
var apis []rpc.API

for _, ns := range selectedAPIs {
if creator, ok := apiCreators[ns]; ok {
apis = append(apis, creator(ctx, clientCtx, tmWSClient, allowUnprotectedTxs, indexer)...)
apis = append(apis, creator(ctx, clientCtx, tmWSClient, allowUnprotectedTxs, indexer, appCodec)...)
} else {
ctx.Logger.Error("invalid namespace value", "namespace", ns)
}
Expand Down
4 changes: 4 additions & 0 deletions helios-chain/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -205,6 +206,7 @@ type Backend struct {
cfg config.Config
allowUnprotectedTxs bool
indexer evmostypes.EVMTxIndexer
AppCodec codec.Codec
}

// NewBackend creates a new Backend instance for cosmos and ethereum namespaces
Expand All @@ -214,6 +216,7 @@ func NewBackend(
clientCtx client.Context,
allowUnprotectedTxs bool,
indexer evmostypes.EVMTxIndexer,
appCodec codec.Codec,
) *Backend {

chainID, err := evmostypes.ParseChainID(clientCtx.ChainID)
Expand Down Expand Up @@ -246,5 +249,6 @@ func NewBackend(
cfg: appConf,
allowUnprotectedTxs: allowUnprotectedTxs,
indexer: indexer,
AppCodec: appCodec,
}
}
93 changes: 45 additions & 48 deletions helios-chain/rpc/backend/proposals_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package backend

import (
"encoding/json"
"strconv"
"strings"

Expand All @@ -20,6 +21,14 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
)

// func anyToJSON(cdc codec.Codec, exec *govtypes.MsgExecLegacyContent) (interface{}, error) {
// var msg sdk.Msg
// if err := cdc.UnpackAny(exec.Content, &msg); err != nil {
// return nil, err
// }
// return cdc.MarshalInterfaceJSON(msg)
// }

func ParseProposal(proposal *govtypes.Proposal, govParams *govtypes.Params, codec codec.Codec) (map[string]interface{}, error) {
statusTypes := map[govtypes.ProposalStatus]interface{}{
govtypes.ProposalStatus_PROPOSAL_STATUS_UNSPECIFIED: "UNSPECIFIED",
Expand All @@ -35,66 +44,54 @@ func ParseProposal(proposal *govtypes.Proposal, govParams *govtypes.Params, code
return nil, err
}
details := make([]map[string]interface{}, 0)

for _, anyJSON := range proposal.Messages {
msg := &govtypes.MsgExecLegacyContent{}

err := proto.Unmarshal(anyJSON.Value, msg)
if err != nil {
details = append(details, map[string]interface{}{
"type": "UnknownProposalType",
"error": err.Error(),
})
continue
}

var msgDecodec []map[string]interface{}

if err := codec.UnmarshalInterfaceJSON(msg.Content.Value, &msgDecodec); err != nil {
contentJson, err := codec.MarshalInterfaceJSON(msg)
if err != nil {
details = append(details, map[string]interface{}{
"type": "UnknownProposalType",
"type": msg.Content.TypeUrl,
"error": err.Error(),
})
continue
}
details = append(details, map[string]interface{}{
"type": msg.Content.TypeUrl,
"value": msgDecodec,
})
// json to interface
var content map[string]interface{}
err = json.Unmarshal(contentJson, &content)
if err != nil {
details = append(details, map[string]interface{}{
"type": msg.Content.TypeUrl,
"error": err.Error(),
})
continue
}
decodedContent := content["content"].(map[string]interface{})

// check if msg field exists and is string
if decodedContent["msg"] != nil && decodedContent["msg"].(string) != "" {
var interfaceMsgMap map[string]interface{}
err = json.Unmarshal([]byte(decodedContent["msg"].(string)), &interfaceMsgMap)
if err != nil {
details = append(details, map[string]interface{}{
"type": msg.Content.TypeUrl,
"error": err.Error(),
})
continue
}
decodedContent["msg"] = interfaceMsgMap
}

// newAssetConsensusProposal := &govprecompilestypes.AddNewAssetConsensusProposal{}
// err = proto.Unmarshal(msg.Content.Value, newAssetConsensusProposal)
// if err == nil {
// details = append(details, map[string]interface{}{
// "type": "AddNewAssetConsensusProposal",
// "assets": newAssetConsensusProposal.Assets,
// })
// continue
// }
// updateAssetConsensusProposal := &govprecompilestypes.UpdateAssetConsensusProposal{}
// err = proto.Unmarshal(msg.Content.Value, updateAssetConsensusProposal)
// if err == nil {
// details = append(details, map[string]interface{}{
// "type": "UpdateAssetConsensusProposal",
// "updates": updateAssetConsensusProposal.Updates,
// })
// continue
// }
// removeAssetConsensusProposal := &govprecompilestypes.RemoveAssetConsensusProposal{}
// err = proto.Unmarshal(msg.Content.Value, removeAssetConsensusProposal)
// if err == nil {
// details = append(details, map[string]interface{}{
// "type": "RemoveAssetConsensusProposal",
// "denoms": removeAssetConsensusProposal.Denoms,
// })
// continue
// }
// hyperionProposal := &hyperiontypes.HyperionProposal{}
// err = proto.Unmarshal(msg.Content.Value, hyperionProposal)
// if err == nil {
// details = append(details, map[string]interface{}{
// "type": "HyperionProposal",
// "msg": hyperionProposal.Msg,
// })
// continue
// }
// TODO: manage unknow proposals
// details = append(details, map[string]interface{}{
// "type": "UnknownProposalType",
// })
details = append(details, content["content"].(map[string]interface{}))
}

return map[string]interface{}{
Expand Down
7 changes: 5 additions & 2 deletions helios-chain/server/json_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"helios-core/helios-chain/rpc/backend"
"helios-core/helios-chain/server/middleware"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
ethlog "github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -178,6 +180,7 @@ func StartJSONRPC(ctx *server.Context,
tmEndpoint string,
config *svrconfig.Config,
indexer evmostypes.EVMTxIndexer,
appCodec codec.Codec,
) (*http.Server, chan struct{}, error) {

tmWsClient := ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger)
Expand All @@ -200,7 +203,7 @@ func StartJSONRPC(ctx *server.Context,
allowUnprotectedTxs := config.JSONRPC.AllowUnprotectedTxs
rpcAPIArr := config.JSONRPC.API

apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient, allowUnprotectedTxs, indexer, rpcAPIArr)
apis := rpc.GetRPCAPIs(ctx, clientCtx, tmWsClient, allowUnprotectedTxs, indexer, rpcAPIArr, appCodec)

r := mux.NewRouter()

Expand Down Expand Up @@ -494,7 +497,7 @@ func StartJSONRPC(ctx *server.Context,

// allocate separate WS connection to Tendermint
tmWsClient = ConnectTmWS(tmRPCAddr, tmEndpoint, ctx.Logger)
backend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
backend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, appCodec)
wsSrv := rpc.NewWebsocketsServer(clientCtx, ctx.Logger, tmWsClient, config, backend)
wsSrv.Start()
return httpSrv, httpSrvDone, nil
Expand Down
16 changes: 14 additions & 2 deletions helios-chain/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
}

app := opts.AppCreator(svrCtx.Logger, db, traceWriter, svrCtx.Viper)
// helApp, ok := app.(AppWithAppCodec)
// if !ok {
// return fmt.Errorf("expected app to be of type *heliosapp.HeliosApp, got %T", app)
// }

nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
Expand Down Expand Up @@ -427,6 +431,8 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
if (config.API.Enable || config.GRPC.Enable || config.JSONRPC.Enable || config.JSONRPC.EnableIndexer) && tmNode != nil {
clientCtx = clientCtx.WithClient(local.New(tmNode))

// clientCtx = clientCtx.WithCodec(svrCtx.Backend.GetCodec())

app.RegisterTxService(clientCtx)
app.RegisterTendermintService(clientCtx)
app.RegisterNodeService(clientCtx, config.Config)
Expand Down Expand Up @@ -484,7 +490,11 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
if apiSrv != nil {
defer apiSrv.Close()
}
clientCtx, httpSrv, httpSrvDone, err := startJSONRPCServer(svrCtx, clientCtx, g, config, genDocProvider, cfg.RPC.ListenAddress, idxer)

// prepare codec with all interfaces registered
// cdc := codec.NewProtoCodec(helApp.GetInterfaceRegistry())

clientCtx, httpSrv, httpSrvDone, err := startJSONRPCServer(svrCtx, clientCtx, g, config, genDocProvider, cfg.RPC.ListenAddress, idxer, nil)
if httpSrv != nil {
defer func() {
shutdownCtx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -679,6 +689,7 @@ func startAPIServer(
// - genDocProvider: A function that provides the Genesis document, used to retrieve the chain ID.
// - cmtRPCAddr: The address of the CometBFT RPC server for WebSocket connections.
// - idxer: The EVM transaction indexer for indexing transactions.
// - appCodec: The application's codec, used for marshaling and unmarshaling.
func startJSONRPCServer(
svrCtx *server.Context,
clientCtx client.Context,
Expand All @@ -687,6 +698,7 @@ func startJSONRPCServer(
genDocProvider node.GenesisDocProvider,
cmtRPCAddr string,
idxer evmostypes.EVMTxIndexer,
appCodec codec.Codec,
) (ctx client.Context, httpSrv *http.Server, httpSrvDone chan struct{}, err error) {
ctx = clientCtx
if !config.JSONRPC.Enable {
Expand All @@ -702,7 +714,7 @@ func startJSONRPCServer(
cmtEndpoint := "/websocket"
g.Go(func() error {

httpSrv, httpSrvDone, err = StartJSONRPC(svrCtx, clientCtx, cmtRPCAddr, cmtEndpoint, &config, idxer)
httpSrv, httpSrvDone, err = StartJSONRPC(svrCtx, clientCtx, cmtRPCAddr, cmtEndpoint, &config, idxer, appCodec)
return err
})
return
Expand Down
2 changes: 1 addition & 1 deletion helios-chain/testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func startInProcess(cfg Config, val *Validator) error {
tmEndpoint := "/websocket"
tmRPCAddr := fmt.Sprintf("tcp://%s", val.AppConfig.GRPC.Address)

val.jsonrpc, val.jsonrpcDone, err = server.StartJSONRPC(val.Ctx, val.ClientCtx, tmRPCAddr, tmEndpoint, val.AppConfig, nil)
val.jsonrpc, val.jsonrpcDone, err = server.StartJSONRPC(val.Ctx, val.ClientCtx, tmRPCAddr, tmEndpoint, val.AppConfig, nil, nil)
if err != nil {
return fmt.Errorf("failed to start JSON-RPC: %w", err)
}
Expand Down