From a19bac19163f71546e36e139478a7ff710f8fd7e Mon Sep 17 00:00:00 2001 From: Kleissner Date: Mon, 3 Jan 2022 01:21:05 +0100 Subject: [PATCH] Refactor ReadBlock and ReadFile functions. Close #84 --- Blockchain Cache Global.go | 47 ----------------------------------- Blockchain User.go | 50 ++++++++++++++++++++++++++++++++++++++ webapi/Search Dispatch.go | 4 +-- webapi/Shared Recent.go | 2 +- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/Blockchain Cache Global.go b/Blockchain Cache Global.go index c7085cb..fa708d7 100644 --- a/Blockchain Cache Global.go +++ b/Blockchain Cache Global.go @@ -8,10 +8,8 @@ package core import ( "github.com/PeernetOfficial/core/blockchain" - "github.com/PeernetOfficial/core/btcec" "github.com/PeernetOfficial/core/protocol" "github.com/enfipy/locker" - "github.com/google/uuid" ) // The blockchain cache stores blockchains. @@ -144,48 +142,3 @@ func (peer *PeerInfo) remoteBlockchainUpdate() { // TODO: This entire function should be instead a non-blocking message via a buffer channel. go peer.Backend.GlobalBlockchainCache.SeenBlockchainVersion(peer) } - -func (cache *BlockchainCache) ReadFile(PublicKey *btcec.PublicKey, Version, BlockNumber uint64, FileID uuid.UUID) (file blockchain.BlockRecordFile, raw []byte, found bool, err error) { - blockDecoded, raw, found, err := cache.ReadBlock(PublicKey, Version, BlockNumber) - if !found { - return file, raw, found, err - } - - for _, decodedR := range blockDecoded.RecordsDecoded { - if file, ok := decodedR.(blockchain.BlockRecordFile); ok && file.ID == FileID { - return file, raw, true, nil - } - } - - return file, raw, false, nil -} - -// ReadBlock reads a block and decodes the records. -func (cache *BlockchainCache) ReadBlock(PublicKey *btcec.PublicKey, Version, BlockNumber uint64) (decoded *blockchain.BlockDecoded, raw []byte, found bool, err error) { - // requesting a block from the user's blockchain? - if PublicKey.IsEqual(cache.backend.peerPublicKey) { - _, _, version := cache.backend.UserBlockchain.Header() - if Version != version { - return nil, nil, false, nil - } - - var status int - raw, status, err = cache.backend.UserBlockchain.GetBlockRaw(BlockNumber) - if err != nil || status != blockchain.StatusOK { - return nil, raw, false, err - } - } else { - // read from the cache - if raw, found = cache.Store.ReadBlock(PublicKey, Version, BlockNumber); !found { - return nil, nil, false, nil - } - } - - // decode the entire block - blockDecoded, status, err := blockchain.DecodeBlockRaw(raw) - if err != nil || status != blockchain.StatusOK { - return nil, raw, false, err - } - - return blockDecoded, raw, true, nil -} diff --git a/Blockchain User.go b/Blockchain User.go index a04f5b4..f10559a 100644 --- a/Blockchain User.go +++ b/Blockchain User.go @@ -10,6 +10,8 @@ import ( "os" "github.com/PeernetOfficial/core/blockchain" + "github.com/PeernetOfficial/core/btcec" + "github.com/google/uuid" ) // initUserBlockchain initializes the users blockchain. It creates the blockchain file if it does not exist already. @@ -58,3 +60,51 @@ func (backend *Backend) userBlockchainUpdateSearchIndex() { } } } + +// ReadBlock reads a block and decodes the records. This may be a block of the user's blockchain, or any other that is cached in the global blockchain cache. +func (backend *Backend) ReadBlock(PublicKey *btcec.PublicKey, Version, BlockNumber uint64) (decoded *blockchain.BlockDecoded, raw []byte, found bool, err error) { + // requesting a block from the user's blockchain? + if PublicKey.IsEqual(backend.peerPublicKey) { + _, _, version := backend.UserBlockchain.Header() + if Version != version { + return nil, nil, false, nil + } + + var status int + raw, status, err = backend.UserBlockchain.GetBlockRaw(BlockNumber) + if err != nil || status != blockchain.StatusOK { + return nil, raw, false, err + } + } else if backend.GlobalBlockchainCache != nil { + // read from the cache + if raw, found = backend.GlobalBlockchainCache.Store.ReadBlock(PublicKey, Version, BlockNumber); !found { + return nil, nil, false, nil + } + } else { + return nil, nil, false, nil + } + + // decode the entire block + blockDecoded, status, err := blockchain.DecodeBlockRaw(raw) + if err != nil || status != blockchain.StatusOK { + return nil, raw, false, err + } + + return blockDecoded, raw, true, nil +} + +// ReadFile decodes a file from the given blockchain (the user or any other cached). The block number must be provided. +func (backend *Backend) ReadFile(PublicKey *btcec.PublicKey, Version, BlockNumber uint64, FileID uuid.UUID) (file blockchain.BlockRecordFile, raw []byte, found bool, err error) { + blockDecoded, raw, found, err := backend.ReadBlock(PublicKey, Version, BlockNumber) + if !found { + return file, raw, found, err + } + + for _, decodedR := range blockDecoded.RecordsDecoded { + if file, ok := decodedR.(blockchain.BlockRecordFile); ok && file.ID == FileID { + return file, raw, true, nil + } + } + + return file, raw, false, nil +} diff --git a/webapi/Search Dispatch.go b/webapi/Search Dispatch.go index d405e81..3107bfa 100644 --- a/webapi/Search Dispatch.go +++ b/webapi/Search Dispatch.go @@ -32,7 +32,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob) } func (job *SearchJob) localSearch(api *WebapiInstance, term string) { - if api.backend.SearchIndex == nil || api.backend.GlobalBlockchainCache == nil { + if api.backend.SearchIndex == nil { job.Status = SearchStatusNoIndex return } @@ -43,7 +43,7 @@ func (job *SearchJob) localSearch(api *WebapiInstance, term string) { resultLoop: for _, result := range results { - file, _, found, err := api.backend.GlobalBlockchainCache.ReadFile(result.PublicKey, result.BlockchainVersion, result.BlockNumber, result.FileID) + file, _, found, err := api.backend.ReadFile(result.PublicKey, result.BlockchainVersion, result.BlockNumber, result.FileID) if err != nil || !found { continue } diff --git a/webapi/Shared Recent.go b/webapi/Shared Recent.go index 296cf27..a4055db 100644 --- a/webapi/Shared Recent.go +++ b/webapi/Shared Recent.go @@ -34,7 +34,7 @@ func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int // decode blocks from top down blockLoop: for blockN := peer.BlockchainHeight - 1; blockN > 0; blockN-- { - blockDecoded, _, found, _ := backend.GlobalBlockchainCache.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN) + blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN) if !found { continue }