Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 18 additions & 6 deletions routes/global_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"strings"
"time"

"github.com/deso-protocol/core/lib"

Expand Down Expand Up @@ -948,23 +949,34 @@ func (gs *GlobalState) CreateGetRequest(key []byte) (
return url, json_data, nil
}

var sharedClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
},
}

func (gs *GlobalState) Get(key []byte) (value []byte, _err error) {
// If we have a remote node then use that node to fulfill this request.
if gs.GlobalStateRemoteNode != "" {
// TODO: This codepath is currently annoying to test.

url, json_data, err := gs.CreateGetRequest(key)
url, jsonData, err := gs.CreateGetRequest(key)
if err != nil {
return nil, fmt.Errorf(
"Get: Error constructing request: %v", err)
}

resReturned, err := http.Post(
url,
"application/json", /*contentType*/
bytes.NewBuffer(json_data))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("Get: Error creating new request: ")
}
req.Header.Set("Content-Type", "application/json")

resReturned, err := sharedClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Get: Error processing remote request")
return nil, fmt.Errorf("Get: Error processing remote request: ")
}

res := GetRemoteResponse{}
Expand Down
29 changes: 26 additions & 3 deletions routes/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,9 @@ type GetNFTsForUserRequest struct {
ReaderPublicKeyBase58Check string `safeForLogging:"true"`
IsForSale *bool `safeForLogging:"true"`
// Ignored if IsForSale is provided
IsPending *bool `safeForLogging:"true"`
IsPending *bool `safeForLogging:"true"`
LastKeyHex string `safeForLogging:"true"`
Limit int `safeForLogging:"true"`
}

type NFTEntryAndPostEntryResponse struct {
Expand All @@ -939,7 +941,8 @@ type NFTEntryAndPostEntryResponse struct {
}

type GetNFTsForUserResponse struct {
NFTsMap map[string]*NFTEntryAndPostEntryResponse
NFTsMap map[string]*NFTEntryAndPostEntryResponse
LastKeyHex string
}

func (fes *APIServer) GetNFTsForUser(ww http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -969,6 +972,20 @@ func (fes *APIServer) GetNFTsForUser(ww http.ResponseWriter, req *http.Request)
}
}

limit := requestData.Limit
if limit <= 0 || limit > 100 {
limit = 100
}

lastKeyBytes := []byte{}
if requestData.LastKeyHex != "" {
lastKeyBytes, err = hex.DecodeString(requestData.LastKeyHex)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetNFTsForUser: Problem decoding LastKeyHex: %v", err))
return
}
}

// Get the NFT bid so we can do a more hardcore validation of the request data.
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
Expand All @@ -987,7 +1004,9 @@ func (fes *APIServer) GetNFTsForUser(ww http.ResponseWriter, req *http.Request)
readerPKID = readerPKIDEntry.PKID
}

nftEntries := utxoView.GetNFTEntriesForPKID(pkid.PKID)
// TODO: move filtering of IsForSale and IsPending to GetNFTEntriesForPKID
nftEntries, lastSeenKey := utxoView.GetNFTEntriesForPKID(
pkid.PKID, limit, lastKeyBytes, requestData.IsForSale, requestData.IsPending)

filteredNFTEntries := []*lib.NFTEntry{}
if requestData.IsForSale != nil {
Expand Down Expand Up @@ -1041,6 +1060,10 @@ func (fes *APIServer) GetNFTsForUser(ww http.ResponseWriter, req *http.Request)
fes._nftEntryToResponse(nftEntry, nil, utxoView, true, readerPKID))
}

if len(lastSeenKey) > 0 {
res.LastKeyHex = hex.EncodeToString(lastSeenKey)
}

if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, fmt.Sprintf("GetNFTsForUser: Problem serializing object to JSON: %v", err))
return
Expand Down
1 change: 0 additions & 1 deletion routes/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,6 @@ func (fes *APIServer) GetNotifications(ww http.ResponseWriter, req *http.Request
LastSeenIndex: lastSeenIndex,
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
fmt.Printf("%#v\n", res)
_AddBadRequestError(ww, fmt.Sprintf(
"GetNotifications: Problem encoding response as JSON: %v", err))
return
Expand Down