Repository: NethermindEth/juno
Pull request #214 (2022-06-12 23:15:58)

Feature/rest api
Fixes | Closes | Resolves #

## Changes:
-Added docs for rest api
-Added "feeder_gateway/" prefix to all rest api calls for Voyager integration

## Types of changes

What types of changes does your code introduce?
_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [x] Documentation Update
- [x] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Other (please describe): 

## Testing
**Requires testing**

- [ ] Yes
- [x] No

**In case you checked yes, did you write tests??**

- [ ] Yes
- [x] No

## Further comments (optional)

Non-breaking changes


codecov[bot]: # [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/214?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) Report
> Merging [#214](https://codecov.io/gh/NethermindEth/juno/pull/214?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (fd52996) into [main](https://codecov.io/gh/NethermindEth/juno/commit/e58959d518b2402e6140293865bdbade30d849d2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (e58959d) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##              main      #214   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           52        52           
  Lines         4487      4478    -9     
=========================================
- Hits          4487      4478    -9     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/NethermindEth/juno/pull/214?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | Coverage Δ | |
|---|---|---|
| [pkg/rest/handler.go](https://codecov.io/gh/NethermindEth/juno/pull/214/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL3Jlc3QvaGFuZGxlci5nbw==) | `100.00% <100.00%> (ø)` | |
| [pkg/rpc/server.go](https://codecov.io/gh/NethermindEth/juno/pull/214/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL3JwYy9zZXJ2ZXIuZ28=) | `100.00% <0.00%> (ø)` | |
| [pkg/crypto/weierstrass/weierstrass.go](https://codecov.io/gh/NethermindEth/juno/pull/214/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL2NyeXB0by93ZWllcnN0cmFzcy93ZWllcnN0cmFzcy5nbw==) | `100.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/NethermindEth/juno/pull/214?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/214?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Last update [e58959d...fd52996](https://codecov.io/gh/NethermindEth/juno/pull/214?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).


Patch:
diff --git a/cmd/juno/cli/root.go b/cmd/juno/cli/root.go
index 3b6e51ff..ac29799b 100644
--- a/cmd/juno/cli/root.go
+++ b/cmd/juno/cli/root.go
@@ -60,7 +60,7 @@ var (
 			// Subscribe the REST API client to the main loop if it is enabled in
 			// the config.
 			if config.Runtime.REST.Enabled {
-				s := rest.NewServer(":"+strconv.Itoa(config.Runtime.REST.Port), config.Runtime.Starknet.FeederGateway)
+				s := rest.NewServer(":"+strconv.Itoa(config.Runtime.REST.Port), config.Runtime.Starknet.FeederGateway, config.Runtime.REST.Prefix)
 				handler.Add("REST", s.ListenAndServe, s.Close)
 			}
 
@@ -130,6 +130,7 @@ func initConfig() {
 		"Rpc Enabled", config.Runtime.RPC.Enabled,
 		"Rest Port", config.Runtime.REST.Port,
 		"Rest Enabled", config.Runtime.REST.Enabled,
+		"Rest Prefix", config.Runtime.REST.Prefix,
 	).Info("Config values.")
 }
 
diff --git a/docs/docs/features/rest.mdx b/docs/docs/features/rest.mdx
new file mode 100644
index 00000000..16bc8218
--- /dev/null
+++ b/docs/docs/features/rest.mdx
@@ -0,0 +1,26 @@
+---
+title: REST API
+description: Supported REST calls
+---
+
+At the time of writing Rest API makes calls to the feeder_gateway using feeder package
+
+Rest Api Getaway can be queried against these endpoints:
+- Goerli: http://alpha4.starknet.io
+- MainNet: https://alpha-mainnet.starknet.io
+
+At the time of writing, we support the next REST functions:
+
+- get_contract_addresses
+- get_block
+- get_state_update
+- get_code
+- get_full_contract
+- get_storage_at
+- get_transaction_status
+- get_transaction
+- get_transaction_receipt
+- get_block_hash_by_id
+- get_block_id_by_hash
+- get_transaction_hash_by_id
+- get_transaction_id_by_hash
\ No newline at end of file
diff --git a/internal/config/config.go b/internal/config/config.go
index 4114212d..1e382042 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -21,8 +21,9 @@ type rpcConfig struct {
 
 // restConfig represents the juno REST configuration.
 type restConfig struct {
-	Enabled bool `yaml:"enabled" mapstructure:"enabled"`
-	Port    int  `yaml:"port" mapstructure:"port"`
+	Enabled bool   `yaml:"enabled" mapstructure:"enabled"`
+	Port    int    `yaml:"port" mapstructure:"port"`
+	Prefix  string `yaml:"prefix" mapstructure:"prefix"`
 }
 
 type starknetConfig struct {
@@ -97,7 +98,7 @@ func New() {
 	}
 	data, err := yaml.Marshal(&Config{
 		RPC:      rpcConfig{Enabled: false, Port: 8080},
-		REST:     restConfig{Enabled: false, Port: 8100},
+		REST:     restConfig{Enabled: false, Port: 8100, Prefix: "/feeder_gateway"},
 		DbPath:   Dir,
 		Network:  goerli,
 		Starknet: starknetConfig{Enabled: true, ApiSync: true, FeederGateway: "https://alpha-mainnet.starknet.io"},
diff --git a/pkg/rest/handler.go b/pkg/rest/handler.go
index 5d028d3a..f4dad4e4 100644
--- a/pkg/rest/handler.go
+++ b/pkg/rest/handler.go
@@ -6,25 +6,27 @@ import (
 	"github.com/NethermindEth/juno/pkg/feeder"
 )
 
+// rest_handler object is used to route calls from the Rest Server
 var rest_handler RestHandler
 
-func NewServer(rest_port string, feeder_gateway string) *Server {
+// NewServer creates a REST API server with the listed endpoints
+func NewServer(rest_port string, feeder_gateway string, prefix string) *Server {
 	rest_handler.RestFeeder = feeder.NewClient(feeder_gateway, "/feeder_gateway", nil)
 	m := http.NewServeMux()
 
-	m.HandleFunc("/get_block", rest_handler.GetBlock)
-	m.HandleFunc("/get_code", rest_handler.GetCode)
-	m.HandleFunc("/get_storage_at", rest_handler.GetStorageAt)
-	m.HandleFunc("/get_transaction_status", rest_handler.GetTransactionStatus)
-	m.HandleFunc("/get_transaction_receipt", rest_handler.GetTransactionReceipt)
-	m.HandleFunc("/get_transaction", rest_handler.GetTransaction)
-	m.HandleFunc("/get_full_contract", rest_handler.GetFullContract)
-	m.HandleFunc("/get_state_update", rest_handler.GetStateUpdate)
-	m.HandleFunc("/get_contract_addresses", rest_handler.GetContractAddresses)
-	m.HandleFunc("/get_block_hash_by_id", rest_handler.GetBlockHashById)
-	m.HandleFunc("/get_block_id_by_hash", rest_handler.GetBlockIDByHash)
-	m.HandleFunc("/get_transaction_hash_by_id", rest_handler.GetTransactionHashByID)
-	m.HandleFunc("/get_transaction_id_by_hash", rest_handler.GetTransactionIDByHash)
+	m.HandleFunc(prefix+"/get_block", rest_handler.GetBlock)
+	m.HandleFunc(prefix+"/get_code", rest_handler.GetCode)
+	m.HandleFunc(prefix+"/get_storage_at", rest_handler.GetStorageAt)
+	m.HandleFunc(prefix+"/get_transaction_status", rest_handler.GetTransactionStatus)
+	m.HandleFunc(prefix+"/get_transaction_receipt", rest_handler.GetTransactionReceipt)
+	m.HandleFunc(prefix+"/get_transaction", rest_handler.GetTransaction)
+	m.HandleFunc(prefix+"/get_full_contract", rest_handler.GetFullContract)
+	m.HandleFunc(prefix+"/get_state_update", rest_handler.GetStateUpdate)
+	m.HandleFunc(prefix+"/get_contract_addresses", rest_handler.GetContractAddresses)
+	m.HandleFunc(prefix+"/get_block_hash_by_id", rest_handler.GetBlockHashById)
+	m.HandleFunc(prefix+"/get_block_id_by_hash", rest_handler.GetBlockIDByHash)
+	m.HandleFunc(prefix+"/get_transaction_hash_by_id", rest_handler.GetTransactionHashByID)
+	m.HandleFunc(prefix+"/get_transaction_id_by_hash", rest_handler.GetTransactionIDByHash)
 
 	return &Server{server: http.Server{Addr: rest_port, Handler: m}}
 }


Test patch:
diff --git a/pkg/rest/tests/rest_test.go b/pkg/rest/tests/rest_test.go
index 16ec3857..8c5db05c 100644
--- a/pkg/rest/tests/rest_test.go
+++ b/pkg/rest/tests/rest_test.go
@@ -67,7 +67,7 @@ func StructFaker(a interface{}) (string, error) {
 
 // TestRestClient
 func TestRestClient(t *testing.T) {
-	r := rest.NewServer(":8100", "http://localhost/")
+	r := rest.NewServer(":8100", "http://localhost/", "feeder_gateway")
 	go func() {
 		_ = r.ListenAndServe()
 	}()