github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/rpc/rpcinfo/methods.go (about)

     1  package rpcinfo
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/hyperledger/burrow/acm"
     8  	"github.com/hyperledger/burrow/execution/names"
     9  	"github.com/hyperledger/burrow/rpc"
    10  	"github.com/hyperledger/burrow/rpc/lib/server"
    11  )
    12  
    13  // Method names
    14  const (
    15  	// Status and healthcheck
    16  	Status          = "status"
    17  	Network         = "network"
    18  	NetworkRegistry = "network/registry"
    19  
    20  	// Accounts
    21  	Accounts        = "accounts"
    22  	Account         = "account"
    23  	Storage         = "storage"
    24  	DumpStorage     = "dump_storage"
    25  	GetAccountHuman = "account_human"
    26  	AccountStats    = "account_stats"
    27  
    28  	// Names
    29  	Name  = "name"
    30  	Names = "names"
    31  
    32  	// Blockchain
    33  	Genesis = "genesis"
    34  	ChainID = "chain_id"
    35  	Block   = "block"
    36  	Blocks  = "blocks"
    37  
    38  	// Consensus
    39  	UnconfirmedTxs = "unconfirmed_txs"
    40  	Validators     = "validators"
    41  	Consensus      = "consensus"
    42  )
    43  
    44  const maxRegexLength = 255
    45  
    46  // The methods below all get mounted at the info server address (specified in config at RPC/Info) in the following form:
    47  //
    48  // http://<info-host>:<info-port>/<name>?<param1>=<value1>&<param2>=<value2>[&...]
    49  //
    50  // For example:
    51  // http://0.0.0.0:26658/status?block_time_within=10m&block_seen_time_within=1h
    52  // http://0.0.0.0:26658/names?regex=<regular expression to match name>
    53  //
    54  // They keys in the route map below are the endpoint name, and the comma separated values are the url query params
    55  //
    56  // They info endpoint also all be called with a JSON-RPC payload like:
    57  //
    58  // curl -X POST -d '{"method": "names", "id": "foo", "params": ["loves"]}' http://0.0.0.0:26658
    59  //
    60  func GetRoutes(service *rpc.Service) map[string]*server.RPCFunc {
    61  	// TODO: overhaul this with gRPC-gateway / swagger
    62  	return map[string]*server.RPCFunc{
    63  		// Status
    64  		Status:          server.NewRPCFunc(service.StatusWithin, "block_time_within,block_seen_time_within"),
    65  		Network:         server.NewRPCFunc(service.Network, ""),
    66  		NetworkRegistry: server.NewRPCFunc(service.NetworkRegistry, ""),
    67  
    68  		// Accounts
    69  		Accounts: server.NewRPCFunc(func() (*rpc.ResultAccounts, error) {
    70  			return service.Accounts(func(*acm.Account) bool {
    71  				return true
    72  			})
    73  		}, ""),
    74  
    75  		Account:         server.NewRPCFunc(service.Account, "address"),
    76  		Storage:         server.NewRPCFunc(service.Storage, "address,key"),
    77  		DumpStorage:     server.NewRPCFunc(service.DumpStorage, "address"),
    78  		GetAccountHuman: server.NewRPCFunc(service.AccountHumanReadable, "address"),
    79  		AccountStats:    server.NewRPCFunc(service.AccountStats, ""),
    80  
    81  		// Blockchain
    82  		Genesis: server.NewRPCFunc(service.Genesis, ""),
    83  		ChainID: server.NewRPCFunc(service.ChainIdentifiers, ""),
    84  		Blocks:  server.NewRPCFunc(service.Blocks, "minHeight,maxHeight"),
    85  		Block:   server.NewRPCFunc(service.Block, "height"),
    86  
    87  		// Consensus
    88  		UnconfirmedTxs: server.NewRPCFunc(service.UnconfirmedTxs, "maxTxs"),
    89  		Validators:     server.NewRPCFunc(service.Validators, ""),
    90  		Consensus:      server.NewRPCFunc(service.ConsensusState, ""),
    91  
    92  		// Names
    93  		Name: server.NewRPCFunc(service.Name, "name"),
    94  		Names: server.NewRPCFunc(func(regex string) (*rpc.ResultNames, error) {
    95  			if regex == "" {
    96  				return service.Names(func(*names.Entry) bool { return true })
    97  			}
    98  			// Regex attacks...
    99  			if len(regex) > maxRegexLength {
   100  				return nil, fmt.Errorf("regular expression longer than maximum length %d", maxRegexLength)
   101  			}
   102  			re, err := regexp.Compile(regex)
   103  			if err != nil {
   104  				return nil, fmt.Errorf("could not compile '%s' as regular expression: %v", regex, err)
   105  			}
   106  			return service.Names(func(entry *names.Entry) bool {
   107  				return re.MatchString(entry.Name)
   108  			})
   109  		}, "regex"),
   110  	}
   111  }