github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/light/proxy/routes.go (about)

     1  package proxy
     2  
     3  import (
     4  	"github.com/badrootd/celestia-core/libs/bytes"
     5  	lrpc "github.com/badrootd/celestia-core/light/rpc"
     6  	rpcclient "github.com/badrootd/celestia-core/rpc/client"
     7  	ctypes "github.com/badrootd/celestia-core/rpc/core/types"
     8  	rpcserver "github.com/badrootd/celestia-core/rpc/jsonrpc/server"
     9  	rpctypes "github.com/badrootd/celestia-core/rpc/jsonrpc/types"
    10  	"github.com/badrootd/celestia-core/types"
    11  )
    12  
    13  func RPCRoutes(c *lrpc.Client) map[string]*rpcserver.RPCFunc {
    14  	return map[string]*rpcserver.RPCFunc{
    15  		// Subscribe/unsubscribe are reserved for websocket events.
    16  		"subscribe":       rpcserver.NewWSRPCFunc(c.SubscribeWS, "query"),
    17  		"unsubscribe":     rpcserver.NewWSRPCFunc(c.UnsubscribeWS, "query"),
    18  		"unsubscribe_all": rpcserver.NewWSRPCFunc(c.UnsubscribeAllWS, ""),
    19  
    20  		// info API
    21  		"health":               rpcserver.NewRPCFunc(makeHealthFunc(c), ""),
    22  		"status":               rpcserver.NewRPCFunc(makeStatusFunc(c), ""),
    23  		"net_info":             rpcserver.NewRPCFunc(makeNetInfoFunc(c), ""),
    24  		"blockchain":           rpcserver.NewRPCFunc(makeBlockchainInfoFunc(c), "minHeight,maxHeight", rpcserver.Cacheable()),
    25  		"genesis":              rpcserver.NewRPCFunc(makeGenesisFunc(c), "", rpcserver.Cacheable()),
    26  		"genesis_chunked":      rpcserver.NewRPCFunc(makeGenesisChunkedFunc(c), "", rpcserver.Cacheable()),
    27  		"block":                rpcserver.NewRPCFunc(makeBlockFunc(c), "height", rpcserver.Cacheable("height")),
    28  		"block_by_hash":        rpcserver.NewRPCFunc(makeBlockByHashFunc(c), "hash", rpcserver.Cacheable()),
    29  		"block_results":        rpcserver.NewRPCFunc(makeBlockResultsFunc(c), "height", rpcserver.Cacheable("height")),
    30  		"commit":               rpcserver.NewRPCFunc(makeCommitFunc(c), "height", rpcserver.Cacheable("height")),
    31  		"header":               rpcserver.NewRPCFunc(makeHeaderFunc(c), "height", rpcserver.Cacheable("height")),
    32  		"header_by_hash":       rpcserver.NewRPCFunc(makeHeaderByHashFunc(c), "hash"),
    33  		"tx":                   rpcserver.NewRPCFunc(makeTxFunc(c), "hash,prove", rpcserver.Cacheable()),
    34  		"tx_search":            rpcserver.NewRPCFunc(makeTxSearchFuncMatchEvents(c), "query,prove,page,per_page,order_by,match_events"),
    35  		"block_search":         rpcserver.NewRPCFunc(makeBlockSearchFuncMatchEvents(c), "query,page,per_page,order_by,match_events"),
    36  		"validators":           rpcserver.NewRPCFunc(makeValidatorsFunc(c), "height,page,per_page", rpcserver.Cacheable("height")),
    37  		"dump_consensus_state": rpcserver.NewRPCFunc(makeDumpConsensusStateFunc(c), ""),
    38  		"consensus_state":      rpcserver.NewRPCFunc(makeConsensusStateFunc(c), ""),
    39  		"consensus_params":     rpcserver.NewRPCFunc(makeConsensusParamsFunc(c), "height", rpcserver.Cacheable("height")),
    40  		"unconfirmed_txs":      rpcserver.NewRPCFunc(makeUnconfirmedTxsFunc(c), "limit"),
    41  		"num_unconfirmed_txs":  rpcserver.NewRPCFunc(makeNumUnconfirmedTxsFunc(c), ""),
    42  
    43  		// tx broadcast API
    44  		"broadcast_tx_commit": rpcserver.NewRPCFunc(makeBroadcastTxCommitFunc(c), "tx"),
    45  		"broadcast_tx_sync":   rpcserver.NewRPCFunc(makeBroadcastTxSyncFunc(c), "tx"),
    46  		"broadcast_tx_async":  rpcserver.NewRPCFunc(makeBroadcastTxAsyncFunc(c), "tx"),
    47  
    48  		// abci API
    49  		"abci_query": rpcserver.NewRPCFunc(makeABCIQueryFunc(c), "path,data,height,prove"),
    50  		"abci_info":  rpcserver.NewRPCFunc(makeABCIInfoFunc(c), "", rpcserver.Cacheable()),
    51  
    52  		// evidence API
    53  		"broadcast_evidence": rpcserver.NewRPCFunc(makeBroadcastEvidenceFunc(c), "evidence"),
    54  	}
    55  }
    56  
    57  type rpcHealthFunc func(ctx *rpctypes.Context) (*ctypes.ResultHealth, error)
    58  
    59  func makeHealthFunc(c *lrpc.Client) rpcHealthFunc {
    60  	return func(ctx *rpctypes.Context) (*ctypes.ResultHealth, error) {
    61  		return c.Health(ctx.Context())
    62  	}
    63  }
    64  
    65  type rpcStatusFunc func(ctx *rpctypes.Context) (*ctypes.ResultStatus, error)
    66  
    67  //nolint:interfacer
    68  func makeStatusFunc(c *lrpc.Client) rpcStatusFunc {
    69  	return func(ctx *rpctypes.Context) (*ctypes.ResultStatus, error) {
    70  		return c.Status(ctx.Context())
    71  	}
    72  }
    73  
    74  type rpcNetInfoFunc func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultNetInfo, error)
    75  
    76  func makeNetInfoFunc(c *lrpc.Client) rpcNetInfoFunc {
    77  	return func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultNetInfo, error) {
    78  		return c.NetInfo(ctx.Context())
    79  	}
    80  }
    81  
    82  type rpcBlockchainInfoFunc func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error)
    83  
    84  func makeBlockchainInfoFunc(c *lrpc.Client) rpcBlockchainInfoFunc {
    85  	return func(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
    86  		return c.BlockchainInfo(ctx.Context(), minHeight, maxHeight)
    87  	}
    88  }
    89  
    90  type rpcGenesisFunc func(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error)
    91  
    92  func makeGenesisFunc(c *lrpc.Client) rpcGenesisFunc {
    93  	return func(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
    94  		return c.Genesis(ctx.Context())
    95  	}
    96  }
    97  
    98  type rpcGenesisChunkedFunc func(ctx *rpctypes.Context, chunk uint) (*ctypes.ResultGenesisChunk, error)
    99  
   100  func makeGenesisChunkedFunc(c *lrpc.Client) rpcGenesisChunkedFunc {
   101  	return func(ctx *rpctypes.Context, chunk uint) (*ctypes.ResultGenesisChunk, error) {
   102  		return c.GenesisChunked(ctx.Context(), chunk)
   103  	}
   104  }
   105  
   106  type rpcBlockFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlock, error)
   107  
   108  func makeBlockFunc(c *lrpc.Client) rpcBlockFunc {
   109  	return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlock, error) {
   110  		return c.Block(ctx.Context(), height)
   111  	}
   112  }
   113  
   114  type rpcHeaderFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultHeader, error)
   115  
   116  func makeHeaderFunc(c *lrpc.Client) rpcHeaderFunc {
   117  	return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultHeader, error) {
   118  		return c.Header(ctx.Context(), height)
   119  	}
   120  }
   121  
   122  type rpcHeaderByHashFunc func(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultHeader, error)
   123  
   124  func makeHeaderByHashFunc(c *lrpc.Client) rpcHeaderByHashFunc {
   125  	return func(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultHeader, error) {
   126  		return c.HeaderByHash(ctx.Context(), hash)
   127  	}
   128  }
   129  
   130  type rpcBlockByHashFunc func(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultBlock, error)
   131  
   132  func makeBlockByHashFunc(c *lrpc.Client) rpcBlockByHashFunc {
   133  	return func(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultBlock, error) {
   134  		return c.BlockByHash(ctx.Context(), hash)
   135  	}
   136  }
   137  
   138  type rpcBlockResultsFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlockResults, error)
   139  
   140  func makeBlockResultsFunc(c *lrpc.Client) rpcBlockResultsFunc {
   141  	return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultBlockResults, error) {
   142  		return c.BlockResults(ctx.Context(), height)
   143  	}
   144  }
   145  
   146  type rpcCommitFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultCommit, error)
   147  
   148  func makeCommitFunc(c *lrpc.Client) rpcCommitFunc {
   149  	return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultCommit, error) {
   150  		return c.Commit(ctx.Context(), height)
   151  	}
   152  }
   153  
   154  type rpcTxFunc func(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error)
   155  
   156  func makeTxFunc(c *lrpc.Client) rpcTxFunc {
   157  	return func(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) {
   158  		return c.Tx(ctx.Context(), hash, prove)
   159  	}
   160  }
   161  
   162  type rpcTxSearchFuncMatchEvents func(
   163  	ctx *rpctypes.Context,
   164  	query string,
   165  	prove bool,
   166  	page, perPage *int,
   167  	orderBy string,
   168  	matchEvents bool,
   169  ) (*ctypes.ResultTxSearch, error)
   170  
   171  func makeTxSearchFuncMatchEvents(c *lrpc.Client) rpcTxSearchFuncMatchEvents {
   172  	return func(
   173  		ctx *rpctypes.Context,
   174  		query string,
   175  		prove bool,
   176  		page, perPage *int,
   177  		orderBy string,
   178  		matchEvents bool,
   179  	) (*ctypes.ResultTxSearch, error) {
   180  		if matchEvents {
   181  			query = "match.events = 1 AND " + query
   182  		} else {
   183  			query = "match.events = 0 AND " + query
   184  		}
   185  		return c.TxSearch(ctx.Context(), query, prove, page, perPage, orderBy)
   186  	}
   187  }
   188  
   189  type rpcBlockSearchFuncMatchEvents func(
   190  	ctx *rpctypes.Context,
   191  	query string,
   192  	prove bool,
   193  	page, perPage *int,
   194  	orderBy string,
   195  	matchEvents bool,
   196  ) (*ctypes.ResultBlockSearch, error)
   197  
   198  func makeBlockSearchFuncMatchEvents(c *lrpc.Client) rpcBlockSearchFuncMatchEvents {
   199  	return func(
   200  		ctx *rpctypes.Context,
   201  		query string,
   202  		prove bool,
   203  		page, perPage *int,
   204  		orderBy string,
   205  		matchEvents bool,
   206  	) (*ctypes.ResultBlockSearch, error) {
   207  		if matchEvents {
   208  			query = "match.events = 1 AND " + query
   209  		} else {
   210  			query = "match.events = 0 AND " + query
   211  		}
   212  		return c.BlockSearch(ctx.Context(), query, page, perPage, orderBy)
   213  	}
   214  }
   215  
   216  type rpcValidatorsFunc func(ctx *rpctypes.Context, height *int64,
   217  	page, perPage *int) (*ctypes.ResultValidators, error)
   218  
   219  func makeValidatorsFunc(c *lrpc.Client) rpcValidatorsFunc {
   220  	return func(ctx *rpctypes.Context, height *int64, page, perPage *int) (*ctypes.ResultValidators, error) {
   221  		return c.Validators(ctx.Context(), height, page, perPage)
   222  	}
   223  }
   224  
   225  type rpcDumpConsensusStateFunc func(ctx *rpctypes.Context) (*ctypes.ResultDumpConsensusState, error)
   226  
   227  func makeDumpConsensusStateFunc(c *lrpc.Client) rpcDumpConsensusStateFunc {
   228  	return func(ctx *rpctypes.Context) (*ctypes.ResultDumpConsensusState, error) {
   229  		return c.DumpConsensusState(ctx.Context())
   230  	}
   231  }
   232  
   233  type rpcConsensusStateFunc func(ctx *rpctypes.Context) (*ctypes.ResultConsensusState, error)
   234  
   235  func makeConsensusStateFunc(c *lrpc.Client) rpcConsensusStateFunc {
   236  	return func(ctx *rpctypes.Context) (*ctypes.ResultConsensusState, error) {
   237  		return c.ConsensusState(ctx.Context())
   238  	}
   239  }
   240  
   241  type rpcConsensusParamsFunc func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultConsensusParams, error)
   242  
   243  func makeConsensusParamsFunc(c *lrpc.Client) rpcConsensusParamsFunc {
   244  	return func(ctx *rpctypes.Context, height *int64) (*ctypes.ResultConsensusParams, error) {
   245  		return c.ConsensusParams(ctx.Context(), height)
   246  	}
   247  }
   248  
   249  type rpcUnconfirmedTxsFunc func(ctx *rpctypes.Context, limit *int) (*ctypes.ResultUnconfirmedTxs, error)
   250  
   251  func makeUnconfirmedTxsFunc(c *lrpc.Client) rpcUnconfirmedTxsFunc {
   252  	return func(ctx *rpctypes.Context, limit *int) (*ctypes.ResultUnconfirmedTxs, error) {
   253  		return c.UnconfirmedTxs(ctx.Context(), limit)
   254  	}
   255  }
   256  
   257  type rpcNumUnconfirmedTxsFunc func(ctx *rpctypes.Context) (*ctypes.ResultUnconfirmedTxs, error)
   258  
   259  func makeNumUnconfirmedTxsFunc(c *lrpc.Client) rpcNumUnconfirmedTxsFunc {
   260  	return func(ctx *rpctypes.Context) (*ctypes.ResultUnconfirmedTxs, error) {
   261  		return c.NumUnconfirmedTxs(ctx.Context())
   262  	}
   263  }
   264  
   265  type rpcBroadcastTxCommitFunc func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error)
   266  
   267  func makeBroadcastTxCommitFunc(c *lrpc.Client) rpcBroadcastTxCommitFunc {
   268  	return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
   269  		return c.BroadcastTxCommit(ctx.Context(), tx)
   270  	}
   271  }
   272  
   273  type rpcBroadcastTxSyncFunc func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error)
   274  
   275  func makeBroadcastTxSyncFunc(c *lrpc.Client) rpcBroadcastTxSyncFunc {
   276  	return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   277  		return c.BroadcastTxSync(ctx.Context(), tx)
   278  	}
   279  }
   280  
   281  type rpcBroadcastTxAsyncFunc func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error)
   282  
   283  func makeBroadcastTxAsyncFunc(c *lrpc.Client) rpcBroadcastTxAsyncFunc {
   284  	return func(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   285  		return c.BroadcastTxAsync(ctx.Context(), tx)
   286  	}
   287  }
   288  
   289  type rpcABCIQueryFunc func(ctx *rpctypes.Context, path string,
   290  	data bytes.HexBytes, height int64, prove bool) (*ctypes.ResultABCIQuery, error)
   291  
   292  func makeABCIQueryFunc(c *lrpc.Client) rpcABCIQueryFunc {
   293  	return func(ctx *rpctypes.Context, path string, data bytes.HexBytes,
   294  		height int64, prove bool,
   295  	) (*ctypes.ResultABCIQuery, error) {
   296  		return c.ABCIQueryWithOptions(ctx.Context(), path, data, rpcclient.ABCIQueryOptions{
   297  			Height: height,
   298  			Prove:  prove,
   299  		})
   300  	}
   301  }
   302  
   303  type rpcABCIInfoFunc func(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error)
   304  
   305  func makeABCIInfoFunc(c *lrpc.Client) rpcABCIInfoFunc {
   306  	return func(ctx *rpctypes.Context) (*ctypes.ResultABCIInfo, error) {
   307  		return c.ABCIInfo(ctx.Context())
   308  	}
   309  }
   310  
   311  type rpcBroadcastEvidenceFunc func(ctx *rpctypes.Context, ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error)
   312  
   313  //nolint:interfacer
   314  func makeBroadcastEvidenceFunc(c *lrpc.Client) rpcBroadcastEvidenceFunc {
   315  	return func(ctx *rpctypes.Context, ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
   316  		return c.BroadcastEvidence(ctx.Context(), ev)
   317  	}
   318  }