Refactor ReadBlock and ReadFile functions. Close #84

This commit is contained in:
Kleissner
2022-01-03 01:21:05 +01:00
parent 5247d88345
commit a19bac1916
4 changed files with 53 additions and 50 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}