-
Notifications
You must be signed in to change notification settings - Fork 477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tools: new endpoints for block generator #5257
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
49da1fc
block generator builds
shiqizng 48711f1
add license, reviewdog fix
shiqizng 5615cc3
fix failing test, lint
shiqizng 690eb7c
lint fix
shiqizng 5de1ff7
update test
shiqizng e3a9516
algofix
shiqizng 69b79a4
address reviewdog errors
shiqizng 1ce7969
address reviewdog errors
shiqizng 45795d7
Merge branch 'master' into shiqi/block-generator
shiqizng 147ed1b
adding new endpoints
shiqizng 12959ee
fixing deltas endpoint
shiqizng c6edc6f
msgp encode delta
shiqizng e5f6987
update test
shiqizng 4c1830f
update
shiqizng f6e09ad
lint
shiqizng 978f446
simplify url path parser
shiqizng 5681dab
update tests
shiqizng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -67,6 +68,8 @@ func MakeServerWithMiddleware(configFile string, addr string, blocksMiddleware B | |
mux.HandleFunc("/genesis", getGenesisHandler(gen)) | ||
mux.HandleFunc("/report", getReportHandler(gen)) | ||
mux.HandleFunc("/v2/status/wait-for-block-after/", getStatusWaitHandler(gen)) | ||
mux.HandleFunc("/v2/ledger/sync/", func(w http.ResponseWriter, r *http.Request) {}) | ||
mux.HandleFunc("/v2/deltas/", getDeltasHandler(gen)) | ||
|
||
return &http.Server{ | ||
Addr: addr, | ||
|
@@ -107,68 +110,55 @@ func getGenesisHandler(gen Generator) func(w http.ResponseWriter, r *http.Reques | |
func getBlockHandler(gen Generator) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
// The generator doesn't actually care about the block... | ||
round, err := parseRound(r.URL.Path) | ||
s, err := parseURL(r.URL.Path) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
round, err := strconv.ParseUint(s, 0, 64) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
maybeWriteError(w, gen.WriteBlock(w, round)) | ||
} | ||
} | ||
|
||
func getAccountHandler(gen Generator) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
// The generator doesn't actually care about the block... | ||
account, err := parseAccount(r.URL.Path) | ||
account, err := parseURL(r.URL.Path) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
maybeWriteError(w, gen.WriteAccount(w, account)) | ||
} | ||
} | ||
|
||
const blockQueryPrefix = "/v2/blocks/" | ||
const blockQueryBlockIdx = len(blockQueryPrefix) | ||
const accountsQueryPrefix = "/v2/accounts/" | ||
const accountsQueryAccountIdx = len(accountsQueryPrefix) | ||
|
||
func parseRound(path string) (uint64, error) { | ||
if !strings.HasPrefix(path, blockQueryPrefix) { | ||
return 0, fmt.Errorf("not a blocks query: %s", path) | ||
} | ||
|
||
result := uint64(0) | ||
pathlen := len(path) | ||
|
||
if pathlen == blockQueryBlockIdx { | ||
return 0, fmt.Errorf("no block in path") | ||
} | ||
|
||
for i := blockQueryBlockIdx; i < pathlen; i++ { | ||
if path[i] < '0' || path[i] > '9' { | ||
if i == blockQueryBlockIdx { | ||
return 0, fmt.Errorf("no block in path") | ||
} | ||
break | ||
func getDeltasHandler(gen Generator) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
s, err := parseURL(r.URL.Path) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
result = (uint64(10) * result) + uint64(int(path[i])-'0') | ||
round, err := strconv.ParseUint(s, 0, 64) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
maybeWriteError(w, gen.WriteDeltas(w, round)) | ||
} | ||
return result, nil | ||
} | ||
|
||
func parseAccount(path string) (string, error) { | ||
if !strings.HasPrefix(path, accountsQueryPrefix) { | ||
return "", fmt.Errorf("not a accounts query: %s", path) | ||
func parseURL(path string) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generalizing the parser for all endpoints. I didn't think it was necessary to check the path prefix since each handler function is associated with only one endpoint. |
||
i := strings.LastIndex(path, "/") | ||
if i == len(path)-1 { | ||
return "", fmt.Errorf("invalid request path, %s", path) | ||
} | ||
|
||
pathlen := len(path) | ||
|
||
if pathlen == accountsQueryAccountIdx { | ||
return "", fmt.Errorf("no address in path") | ||
if strings.Contains(path[i+1:], "?") { | ||
return strings.Split(path[i+1:], "?")[0], nil | ||
} | ||
|
||
return path[accountsQueryAccountIdx:], nil | ||
return path[i+1:], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to utils.