From 2b2f20ca18917a1377d1a8a6a06de6671413da26 Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar <31743758+Akilan1999@users.noreply.github.com> Date: Mon, 27 Feb 2023 17:24:27 +0000 Subject: [PATCH] changed route to view files added by a certain node (#105) * changed route to view files added by a certain node * gofmt my changes --- Networks.go | 2 +- Peer ID.go | 16 ++--- Peernet.go | 6 +- webapi/API.go | 2 +- webapi/Blockchain.go | 137 ++++++++++++++++++++++++++----------------- webapi/Search.go | 27 --------- webapi/readme.md | 13 ++++ 7 files changed, 108 insertions(+), 95 deletions(-) diff --git a/Networks.go b/Networks.go index 991aeee..97bff74 100644 --- a/Networks.go +++ b/Networks.go @@ -49,7 +49,7 @@ type Networks struct { backend *Backend } -// ReplyTimeout is the round-trip timeout for message sequences. +// ReplyTimeout is the round-trip timeout for message sequences. const ReplyTimeout = 20 func (backend *Backend) initMessageSequence() { diff --git a/Peer ID.go b/Peer ID.go index b736dd2..680d8c2 100644 --- a/Peer ID.go +++ b/Peer ID.go @@ -21,7 +21,7 @@ import ( ) func (backend *Backend) initPeerID() { - backend.peerList = make(map[[btcec.PubKeyBytesLenCompressed]byte]*PeerInfo) + backend.PeerList = make(map[[btcec.PubKeyBytesLenCompressed]byte]*PeerInfo) backend.nodeList = make(map[[protocol.HashSize]byte]*PeerInfo) // load existing key from config, if available @@ -123,7 +123,7 @@ func (backend *Backend) PeerlistAdd(PublicKey *btcec.PublicKey, connections ...* backend.peerlistMutex.Lock() defer backend.peerlistMutex.Unlock() - peer, ok := backend.peerList[publicKeyCompressed] + peer, ok := backend.PeerList[publicKeyCompressed] if ok { return peer, false } @@ -131,7 +131,7 @@ func (backend *Backend) PeerlistAdd(PublicKey *btcec.PublicKey, connections ...* peer = &PeerInfo{Backend: backend, PublicKey: PublicKey, connectionActive: connections, connectionLatest: connections[0], NodeID: protocol.PublicKey2NodeID(PublicKey), messageSequence: rand.Uint32()} _, peer.IsRootPeer = rootPeers[publicKeyCompressed] - backend.peerList[publicKeyCompressed] = peer + backend.PeerList[publicKeyCompressed] = peer // also add to mirrored nodeList var nodeID [protocol.HashSize]byte @@ -141,7 +141,7 @@ func (backend *Backend) PeerlistAdd(PublicKey *btcec.PublicKey, connections ...* // add to Kademlia backend.nodesDHT.AddNode(&dht.Node{ID: peer.NodeID, Info: peer}) - // TODO: If the node isn't added to Kademlia, it should be either added temporarily to the peerList with an expiration, or to a temp list, or not at all. + // TODO: If the node isn't added to Kademlia, it should be either added temporarily to the PeerList with an expiration, or to a temp list, or not at all. // send to all channels non-blocking for _, monitor := range backend.peerMonitor { @@ -165,7 +165,7 @@ func (backend *Backend) PeerlistRemove(peer *PeerInfo) { // remove from Kademlia backend.nodesDHT.RemoveNode(peer.NodeID) - delete(backend.peerList, publicKey2Compressed(peer.PublicKey)) + delete(backend.PeerList, publicKey2Compressed(peer.PublicKey)) var nodeID [protocol.HashSize]byte copy(nodeID[:], peer.NodeID) @@ -178,7 +178,7 @@ func (backend *Backend) PeerlistGet() (peers []*PeerInfo) { backend.peerlistMutex.RLock() defer backend.peerlistMutex.RUnlock() - for _, peer := range backend.peerList { + for _, peer := range backend.PeerList { peers = append(peers, peer) } @@ -190,7 +190,7 @@ func (backend *Backend) PeerlistLookup(publicKey *btcec.PublicKey) (peer *PeerIn backend.peerlistMutex.RLock() defer backend.peerlistMutex.RUnlock() - return backend.peerList[publicKey2Compressed(publicKey)] + return backend.PeerList[publicKey2Compressed(publicKey)] } // NodelistLookup returns the peer from the list with the node ID @@ -209,7 +209,7 @@ func (backend *Backend) PeerlistCount() (count int) { backend.peerlistMutex.RLock() defer backend.peerlistMutex.RUnlock() - return len(backend.peerList) + return len(backend.PeerList) } func publicKey2Compressed(publicKey *btcec.PublicKey) [btcec.PubKeyBytesLenCompressed]byte { diff --git a/Peernet.go b/Peernet.go index d2902e6..796887c 100644 --- a/Peernet.go +++ b/Peernet.go @@ -107,11 +107,11 @@ type Backend struct { // The node ID is the blake3 hash of the public key compressed form. nodeID []byte - // peerList keeps track of all peers - peerList map[[btcec.PubKeyBytesLenCompressed]byte]*PeerInfo + // PeerList keeps track of all peers + PeerList map[[btcec.PubKeyBytesLenCompressed]byte]*PeerInfo peerlistMutex sync.RWMutex - // nodeList is a mirror of peerList but using the node ID + // nodeList is a mirror of PeerList but using the node ID nodeList map[[protocol.HashSize]byte]*PeerInfo // peerMonitor is a list of channels receiving information about new peers diff --git a/webapi/API.go b/webapi/API.go index 61e183f..9b4f9f2 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -80,6 +80,7 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi api.Router.HandleFunc("/blockchain/file/list", api.apiBlockchainFileList).Methods("GET") api.Router.HandleFunc("/blockchain/file/delete", api.apiBlockchainFileDelete).Methods("POST") api.Router.HandleFunc("/blockchain/file/update", api.apiBlockchainFileUpdate).Methods("POST") + api.Router.HandleFunc("/blockchain/view", api.apiExploreNodeID).Methods("GET") api.Router.HandleFunc("/profile/list", api.apiProfileList).Methods("GET") api.Router.HandleFunc("/profile/read", api.apiProfileRead).Methods("GET") api.Router.HandleFunc("/profile/write", api.apiProfileWrite).Methods("POST") @@ -90,7 +91,6 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi api.Router.HandleFunc("/search/statistic", api.apiSearchStatistic).Methods("GET") api.Router.HandleFunc("/search/terminate", api.apiSearchTerminate).Methods("GET") api.Router.HandleFunc("/explore", api.apiExplore).Methods("GET") - api.Router.HandleFunc("/explore/node", api.apiExploreNodeID).Methods("GET") api.Router.HandleFunc("/file/format", api.apiFileFormat).Methods("GET") api.Router.HandleFunc("/download/start", api.apiDownloadStart).Methods("GET") api.Router.HandleFunc("/download/status", api.apiDownloadStatus).Methods("GET") diff --git a/webapi/Blockchain.go b/webapi/Blockchain.go index 88e7960..90e741d 100644 --- a/webapi/Blockchain.go +++ b/webapi/Blockchain.go @@ -7,17 +7,17 @@ Author: Peter Kleissner package webapi import ( - "encoding/hex" - "net/http" - "strconv" + "encoding/hex" + "net/http" + "strconv" - "github.com/PeernetOfficial/core/blockchain" + "github.com/PeernetOfficial/core/blockchain" ) type apiBlockchainHeader struct { - PeerID string `json:"peerid"` // Peer ID hex encoded. - Version uint64 `json:"version"` // Current version number of the blockchain. - Height uint64 `json:"height"` // Height of the blockchain (number of blocks). If 0, no data exists. + PeerID string `json:"peerid"` // Peer ID hex encoded. + Version uint64 `json:"version"` // Current version number of the blockchain. + Height uint64 `json:"height"` // Height of the blockchain (number of blocks). If 0, no data exists. } /* @@ -27,25 +27,25 @@ Request: GET /blockchain/header Result: 200 with JSON structure apiResponsePeerSelf */ func (api *WebapiInstance) apiBlockchainHeaderFunc(w http.ResponseWriter, r *http.Request) { - publicKey, height, version := api.Backend.UserBlockchain.Header() + publicKey, height, version := api.Backend.UserBlockchain.Header() - EncodeJSON(api.Backend, w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())}) + EncodeJSON(api.Backend, w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())}) } type apiBlockRecordRaw struct { - Type uint8 `json:"type"` // Record Type. See core.RecordTypeX. - Data []byte `json:"data"` // Data according to the type. + Type uint8 `json:"type"` // Record Type. See core.RecordTypeX. + Data []byte `json:"data"` // Data according to the type. } // apiBlockchainBlockRaw contains a raw block of the blockchain via API type apiBlockchainBlockRaw struct { - Records []apiBlockRecordRaw `json:"records"` // Block records in encoded raw format. + Records []apiBlockRecordRaw `json:"records"` // Block records in encoded raw format. } type apiBlockchainBlockStatus struct { - Status int `json:"status"` // See blockchain.StatusX. - Height uint64 `json:"height"` // Height of the blockchain (number of blocks). - Version uint64 `json:"version"` // Version of the blockchain. + Status int `json:"status"` // See blockchain.StatusX. + Height uint64 `json:"height"` // Height of the blockchain (number of blocks). + Version uint64 `json:"version"` // Version of the blockchain. } /* @@ -56,30 +56,30 @@ Request: POST /blockchain/append with JSON structure apiBlockchainBlockRaw Response: 200 with JSON structure apiBlockchainBlockStatus */ func (api *WebapiInstance) apiBlockchainAppend(w http.ResponseWriter, r *http.Request) { - var input apiBlockchainBlockRaw - if err := DecodeJSON(w, r, &input); err != nil { - return - } + var input apiBlockchainBlockRaw + if err := DecodeJSON(w, r, &input); err != nil { + return + } - var records []blockchain.BlockRecordRaw + var records []blockchain.BlockRecordRaw - for _, record := range input.Records { - records = append(records, blockchain.BlockRecordRaw{Type: record.Type, Data: record.Data}) - } + for _, record := range input.Records { + records = append(records, blockchain.BlockRecordRaw{Type: record.Type, Data: record.Data}) + } - newHeight, newVersion, status := api.Backend.UserBlockchain.Append(records) + newHeight, newVersion, status := api.Backend.UserBlockchain.Append(records) - EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) + EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) } type apiBlockchainBlock struct { - Status int `json:"status"` // See blockchain.StatusX. - PeerID string `json:"peerid"` // Peer ID hex encoded. - LastBlockHash []byte `json:"lastblockhash"` // Hash of the last block. Blake3. - BlockchainVersion uint64 `json:"blockchainversion"` // Blockchain version - Number uint64 `json:"blocknumber"` // Block number - RecordsRaw []apiBlockRecordRaw `json:"recordsraw"` // Records raw. Successfully decoded records are parsed into the below fields. - RecordsDecoded []interface{} `json:"recordsdecoded"` // Records decoded. The encoding for each record depends on its type. + Status int `json:"status"` // See blockchain.StatusX. + PeerID string `json:"peerid"` // Peer ID hex encoded. + LastBlockHash []byte `json:"lastblockhash"` // Hash of the last block. Blake3. + BlockchainVersion uint64 `json:"blockchainversion"` // Blockchain version + Number uint64 `json:"blocknumber"` // Block number + RecordsRaw []apiBlockRecordRaw `json:"recordsraw"` // Records raw. Successfully decoded records are parsed into the below fields. + RecordsDecoded []interface{} `json:"recordsdecoded"` // Records decoded. The encoding for each record depends on its type. } /* @@ -89,34 +89,61 @@ Request: GET /blockchain/read?block=[number] Result: 200 with JSON structure apiBlockchainBlock */ func (api *WebapiInstance) apiBlockchainRead(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - blockN, err := strconv.Atoi(r.Form.Get("block")) - if err != nil || blockN < 0 { - http.Error(w, "", http.StatusBadRequest) - return - } + r.ParseForm() + blockN, err := strconv.Atoi(r.Form.Get("block")) + if err != nil || blockN < 0 { + http.Error(w, "", http.StatusBadRequest) + return + } - block, status, _ := api.Backend.UserBlockchain.Read(uint64(blockN)) - result := apiBlockchainBlock{Status: status} + block, status, _ := api.Backend.UserBlockchain.Read(uint64(blockN)) + result := apiBlockchainBlock{Status: status} - if status == 0 { - for _, record := range block.RecordsRaw { - result.RecordsRaw = append(result.RecordsRaw, apiBlockRecordRaw{Type: record.Type, Data: record.Data}) - } + if status == 0 { + for _, record := range block.RecordsRaw { + result.RecordsRaw = append(result.RecordsRaw, apiBlockRecordRaw{Type: record.Type, Data: record.Data}) + } - result.PeerID = hex.EncodeToString(block.OwnerPublicKey.SerializeCompressed()) + result.PeerID = hex.EncodeToString(block.OwnerPublicKey.SerializeCompressed()) - for _, record := range block.RecordsDecoded { - switch v := record.(type) { - case blockchain.BlockRecordFile: - result.RecordsDecoded = append(result.RecordsDecoded, blockRecordFileToAPI(v)) + for _, record := range block.RecordsDecoded { + switch v := record.(type) { + case blockchain.BlockRecordFile: + result.RecordsDecoded = append(result.RecordsDecoded, blockRecordFileToAPI(v)) - case blockchain.BlockRecordProfile: - result.RecordsDecoded = append(result.RecordsDecoded, blockRecordProfileToAPI(v)) + case blockchain.BlockRecordProfile: + result.RecordsDecoded = append(result.RecordsDecoded, blockRecordProfileToAPI(v)) - } - } - } + } + } + } - EncodeJSON(api.Backend, w, r, result) + EncodeJSON(api.Backend, w, r, result) +} + +/* +apiExploreNodeID returns the shared files of a particular node in Peernet. Results are returned in real-time. The file type is an optional filter. See TypeX. +Special type -2 = Binary, Compressed, Container, Executable. This special type includes everything except Documents, Video, Audio, Ebooks, Picture, Text. + +Request: GET /blockchain/view?limit=[max records]&type=[file type]&offset=[offset]&node=[node id] +Result: 200 with JSON structure SearchResult. Check the field status. +*/ +func (api *WebapiInstance) apiExploreNodeID(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + offset, _ := strconv.Atoi(r.Form.Get("offset")) + limit, err := strconv.Atoi(r.Form.Get("limit")) + if err != nil { + limit = 100 + } + // ID fields for results for a specific node ID. + NodeId, _ := DecodeBlake3Hash(r.Form.Get("node")) + + fileType, err := strconv.Atoi(r.Form.Get("type")) + if err != nil { + fileType = -1 + } + + result := api.ExploreHelper(fileType, limit, offset, NodeId, true) + + EncodeJSON(api.Backend, w, r, result) } diff --git a/webapi/Search.go b/webapi/Search.go index c486fa9..d25f2b2 100644 --- a/webapi/Search.go +++ b/webapi/Search.go @@ -374,33 +374,6 @@ func (api *WebapiInstance) apiExplore(w http.ResponseWriter, r *http.Request) { EncodeJSON(api.Backend, w, r, result) } -/* -apiExploreNodeID returns the shared files of a particular node in Peernet. Results are returned in real-time. The file type is an optional filter. See TypeX. -Special type -2 = Binary, Compressed, Container, Executable. This special type includes everything except Documents, Video, Audio, Ebooks, Picture, Text. - -Request: GET /explore/node?limit=[max records]&type=[file type]&offset=[offset]&NodeID=[node id] -Result: 200 with JSON structure SearchResult. Check the field status. -*/ -func (api *WebapiInstance) apiExploreNodeID(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - offset, _ := strconv.Atoi(r.Form.Get("offset")) - limit, err := strconv.Atoi(r.Form.Get("limit")) - if err != nil { - limit = 100 - } - // ID fields for results for a specific node ID. - NodeId, _ := DecodeBlake3Hash(r.Form.Get("NodeID")) - - fileType, err := strconv.Atoi(r.Form.Get("type")) - if err != nil { - fileType = -1 - } - - result := api.ExploreHelper(fileType, limit, offset, NodeId, true) - - EncodeJSON(api.Backend, w, r, result) -} - // ExploreHelper Helper function for the explore route with the possibility search based on a node ID func (api *WebapiInstance) ExploreHelper(fileType int, limit, offset int, nodeID []byte, nodeIDState bool) *SearchResult { resultFiles := api.queryRecentShared(api.Backend, fileType, uint64(limit*20/100), uint64(offset), uint64(limit), nodeID, nodeIDState) diff --git a/webapi/readme.md b/webapi/readme.md index 68c1427..9a0e6a5 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -457,6 +457,19 @@ Request: POST /blockchain/file/update with JSON structure apiBlockAddFiles Response: 200 with JSON structure apiBlockchainBlockStatus ``` +### List Recent files based on the Node ID + +This returns recently shared files in Peernet. Results are returned in real-time. The file type is an optional filter. + +``` +Request: GET /blockchain/view?node=[node ID]&limit=[max records]&type=[file type]&offset=[offset] +Result: 200 with JSON structure SearchResult. Check the field status. +``` + +Example request to list 20 recently shared files (all file types): `http://127.0.0.1:112/blockchain/view?node=[node ID]&limit=20` + +Example request to list 10 recent documents: `http://127.0.0.1:112/blockchain/view?node=[node ID]&type=5&limit=10` + ## Profile Functions User profile data such as the username, email address, and picture are stored on the blockchain. Profile fields are text (UTF-8) or binary encoded, depending on the type.