From 1c0406dc60bffff65c71c1ab106c75d8b407ffb2 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Wed, 13 Oct 2021 21:22:17 +0200 Subject: [PATCH] Housekeeping! Removing unnecessary "blockchain" prefix of status codes within the blockchain package. --- blockchain/Blockchain File.go | 20 +++++------ blockchain/Blockchain Profile.go | 26 +++++++------- blockchain/Blockchain.go | 59 ++++++++++++++++---------------- blockchain/Test_test.go | 2 +- webapi/File.go | 8 ++--- webapi/Profile.go | 4 +-- webapi/readme.md | 26 +++++++------- 7 files changed, 71 insertions(+), 74 deletions(-) diff --git a/blockchain/Blockchain File.go b/blockchain/Blockchain File.go index 5d06eb9..108d4f0 100644 --- a/blockchain/Blockchain File.go +++ b/blockchain/Blockchain File.go @@ -14,28 +14,28 @@ import ( "github.com/google/uuid" ) -// AddFiles adds files to the blockchain. Status is BlockchainStatusX. +// AddFiles adds files to the blockchain. Status is StatusX. // It makes sense to group all files in the same directory into one call, since only one directory record will be created per unique directory per block. func (blockchain *Blockchain) AddFiles(files []BlockRecordFile) (newHeight, newVersion uint64, status int) { encoded, err := encodeBlockRecordFiles(files) if err != nil { - return 0, 0, BlockchainStatusCorruptBlockRecord + return 0, 0, StatusCorruptBlockRecord } return blockchain.Append(encoded) } -// ListFiles returns a list of all files. Status is BlockchainStatusX. +// ListFiles returns a list of all files. Status is StatusX. // If there is a corruption in the blockchain it will stop reading but return the files parsed so far. func (blockchain *Blockchain) ListFiles() (files []BlockRecordFile, status int) { status = blockchain.Iterate(func(block *Block) (statusI int) { filesMore, err := decodeBlockRecordFiles(block.RecordsRaw, block.NodeID) if err != nil { - return BlockchainStatusCorruptBlockRecord + return StatusCorruptBlockRecord } files = append(files, filesMore...) - return BlockchainStatusOK + return StatusOK }) return files, status @@ -47,7 +47,7 @@ func (blockchain *Blockchain) FileExists(hash []byte) (files []BlockRecordFile, status = blockchain.Iterate(func(block *Block) (statusI int) { filesD, err := decodeBlockRecordFiles(block.RecordsRaw, block.NodeID) if err != nil { - return BlockchainStatusCorruptBlockRecord + return StatusCorruptBlockRecord } for _, file := range filesD { if bytes.Equal(file.Hash, hash) { @@ -55,13 +55,13 @@ func (blockchain *Blockchain) FileExists(hash []byte) (files []BlockRecordFile, } } - return BlockchainStatusOK + return StatusOK }) return files, status } -// DeleteFiles deletes files from the blockchain. Status is BlockchainStatusX. +// DeleteFiles deletes files from the blockchain. Status is StatusX. func (blockchain *Blockchain) DeleteFiles(IDs []uuid.UUID) (newHeight, newVersion uint64, deletedFiles []BlockRecordFile, status int) { newHeight, newVersion, status = blockchain.IterateDeleteRecord(func(record *BlockRecordRaw) (deleteAction int) { if record.Type != RecordTypeFile { @@ -86,7 +86,7 @@ func (blockchain *Blockchain) DeleteFiles(IDs []uuid.UUID) (newHeight, newVersio return } -// ReplaceFiles is a convenience wrapper to replace files in the blockchain identified via their IDs. Status is BlockchainStatusX. +// ReplaceFiles is a convenience wrapper to replace files in the blockchain identified via their IDs. Status is StatusX. // If a file does not exist on the blockchain, it acts as add. func (blockchain *Blockchain) ReplaceFiles(files []BlockRecordFile) (newHeight, newVersion uint64, status int) { var deleteIDs []uuid.UUID @@ -94,7 +94,7 @@ func (blockchain *Blockchain) ReplaceFiles(files []BlockRecordFile) (newHeight, deleteIDs = append(deleteIDs, files[n].ID) } - if newHeight, newVersion, _, status = blockchain.DeleteFiles(deleteIDs); status != BlockchainStatusOK { + if newHeight, newVersion, _, status = blockchain.DeleteFiles(deleteIDs); status != StatusOK { return } diff --git a/blockchain/Blockchain Profile.go b/blockchain/Blockchain Profile.go index ffe83f3..8283cfa 100644 --- a/blockchain/Blockchain Profile.go +++ b/blockchain/Blockchain Profile.go @@ -6,16 +6,16 @@ Author: Peter Kleissner package blockchain -// ProfileReadField reads the specified profile field. See ProfileX for the list of recognized fields. The encoding depends on the field type. Status is BlockchainStatusX. +// ProfileReadField reads the specified profile field. See ProfileX for the list of recognized fields. The encoding depends on the field type. Status is StatusX. func (blockchain *Blockchain) ProfileReadField(index uint16) (data []byte, status int) { found := false status = blockchain.Iterate(func(block *Block) (statusI int) { fields, err := decodeBlockRecordProfile(block.RecordsRaw) if err != nil { - return BlockchainStatusCorruptBlockRecord + return StatusCorruptBlockRecord } else if len(fields) == 0 { - return BlockchainStatusOK + return StatusOK } // Check if the field is available in the profile record. If there are multiple records, only return the latest one. @@ -26,33 +26,33 @@ func (blockchain *Blockchain) ProfileReadField(index uint16) (data []byte, statu } } - return BlockchainStatusOK + return StatusOK }) - if status != BlockchainStatusOK { + if status != StatusOK { return nil, status } else if !found { - return nil, BlockchainStatusDataNotFound + return nil, StatusDataNotFound } - return data, BlockchainStatusOK + return data, StatusOK } -// ProfileList lists all profile fields. Status is BlockchainStatusX. +// ProfileList lists all profile fields. Status is StatusX. func (blockchain *Blockchain) ProfileList() (fields []BlockRecordProfile, status int) { uniqueFields := make(map[uint16][]byte) status = blockchain.Iterate(func(block *Block) (statusI int) { fields, err := decodeBlockRecordProfile(block.RecordsRaw) if err != nil { - return BlockchainStatusCorruptBlockRecord + return StatusCorruptBlockRecord } for n := range fields { uniqueFields[fields[n].Type] = fields[n].Data } - return BlockchainStatusOK + return StatusOK }) for key, value := range uniqueFields { @@ -62,17 +62,17 @@ func (blockchain *Blockchain) ProfileList() (fields []BlockRecordProfile, status return fields, status } -// ProfileWrite writes profile fields and blobs to the blockchain. Status is BlockchainStatusX. +// ProfileWrite writes profile fields and blobs to the blockchain. Status is StatusX. func (blockchain *Blockchain) ProfileWrite(fields []BlockRecordProfile) (newHeight, newVersion uint64, status int) { encoded, err := encodeBlockRecordProfile(fields) if err != nil { - return 0, 0, BlockchainStatusCorruptBlockRecord + return 0, 0, StatusCorruptBlockRecord } return blockchain.Append(encoded) } -// ProfileDelete deletes fields and blobs from the blockchain. Status is BlockchainStatusX. +// ProfileDelete deletes fields and blobs from the blockchain. Status is StatusX. func (blockchain *Blockchain) ProfileDelete(fields []uint16) (newHeight, newVersion uint64, status int) { return blockchain.IterateDeleteRecord(func(record *BlockRecordRaw) (deleteAction int) { if record.Type != RecordTypeProfile { diff --git a/blockchain/Blockchain.go b/blockchain/Blockchain.go index 275a888..d11f882 100644 --- a/blockchain/Blockchain.go +++ b/blockchain/Blockchain.go @@ -133,14 +133,14 @@ func (blockchain *Blockchain) headerWrite(height, version uint64) (err error) { return err } -// BlockchainStatusX provides information about the blockchain status. Some errors codes indicate a corruption. +// StatusX provides information about the blockchain status. Some errors codes indicate a corruption. const ( - BlockchainStatusOK = 0 // No problems in the blockchain detected. - BlockchainStatusBlockNotFound = 1 // Missing block in the blockchain. - BlockchainStatusCorruptBlock = 2 // Error block encoding - BlockchainStatusCorruptBlockRecord = 3 // Error block record encoding - BlockchainStatusDataNotFound = 4 // Requested data not available in the blockchain - BlockchainStatusNotInWarehouse = 5 // File to be added to blockchain does not exist in the Warehouse + StatusOK = 0 // No problems in the blockchain detected. + StatusBlockNotFound = 1 // Missing block in the blockchain. + StatusCorruptBlock = 2 // Error block encoding + StatusCorruptBlockRecord = 3 // Error block record encoding + StatusDataNotFound = 4 // Requested data not available in the blockchain + StatusNotInWarehouse = 5 // File to be added to blockchain does not exist in the Warehouse ) // blockNumberToKey returns the database key for the given block number @@ -151,7 +151,7 @@ func blockNumberToKey(number uint64) (key []byte) { return target[:] } -// Iterate iterates over the blockchain. Status is BlockchainStatusX. +// Iterate iterates over the blockchain. Status is StatusX. // If the callback returns non-zero, the function aborts and returns the inner status code. func (blockchain *Blockchain) Iterate(callback func(block *Block) int) (status int) { // read all blocks until height is reached @@ -160,23 +160,23 @@ func (blockchain *Blockchain) Iterate(callback func(block *Block) int) (status i for blockN := uint64(0); blockN < height; blockN++ { blockRaw, found := blockchain.database.Get(blockNumberToKey(blockN)) if !found || len(blockRaw) == 0 { - return BlockchainStatusBlockNotFound + return StatusBlockNotFound } block, err := decodeBlock(blockRaw) if err != nil { - return BlockchainStatusCorruptBlock + return StatusCorruptBlock } - if statusI := callback(block); statusI != BlockchainStatusOK { + if statusI := callback(block); statusI != StatusOK { return statusI } } - return BlockchainStatusOK + return StatusOK } -// IterateDeleteRecord iterates over the blockchain to find records to delete. Status is BlockchainStatusX. +// IterateDeleteRecord iterates over the blockchain to find records to delete. Status is StatusX. // deleteAction is 0 = no action on record, 1 = delete record, 2 = replace record, 3 = error blockchain corrupt // If the callback returns true, the record will be deleted. The blockchain will be automatically refactored and height and version updated. func (blockchain *Blockchain) IterateDeleteRecord(callback func(record *BlockRecordRaw) (deleteAction int)) (newHeight, newVersion uint64, status int) { @@ -194,12 +194,12 @@ func (blockchain *Blockchain) IterateDeleteRecord(callback func(record *BlockRec for blockN := uint64(0); blockN < height; blockN++ { blockRaw, found := blockchain.database.Get(blockNumberToKey(blockN)) if !found || len(blockRaw) == 0 { - return 0, 0, BlockchainStatusBlockNotFound + return 0, 0, StatusBlockNotFound } block, err := decodeBlock(blockRaw) if err != nil { - return 0, 0, BlockchainStatusCorruptBlock + return 0, 0, StatusCorruptBlock } // loop through all records in this block @@ -221,7 +221,7 @@ func (blockchain *Blockchain) IterateDeleteRecord(callback func(record *BlockRec refactorBlockchain = true case 3: // error blockchain corrupt - return 0, 0, BlockchainStatusCorruptBlockRecord + return 0, 0, StatusCorruptBlockRecord } } @@ -244,7 +244,7 @@ func (blockchain *Blockchain) IterateDeleteRecord(callback func(record *BlockRec raw, err := encodeBlock(&block, blockchain.privateKey) if err != nil { - return 0, 0, BlockchainStatusCorruptBlock + return 0, 0, StatusCorruptBlock } // store the block @@ -262,7 +262,7 @@ func (blockchain *Blockchain) IterateDeleteRecord(callback func(record *BlockRec } } - return blockchain.height, blockchain.version, BlockchainStatusOK + return blockchain.height, blockchain.version, StatusOK } // ---- blockchain manipulation functions ---- @@ -275,14 +275,13 @@ func (blockchain *Blockchain) Header() (publicKey *btcec.PublicKey, height uint6 return blockchain.publicKey, blockchain.height, blockchain.version } -// Append appends a new block to the blockchain based on the provided raw records. -// Status: BlockchainStatusX (0-2): 0 = Success, 1 = Error block not found, 2 = Error block encoding +// Append appends a new block to the blockchain based on the provided raw records. Status is StatusX. func (blockchain *Blockchain) Append(RecordsRaw []BlockRecordRaw) (newHeight, newVersion uint64, status int) { blockchain.Lock() defer blockchain.Unlock() if len(RecordsRaw) == 0 { - return blockchain.height, blockchain.version, BlockchainStatusOK + return blockchain.height, blockchain.version, StatusOK } block := &Block{OwnerPublicKey: blockchain.publicKey, RecordsRaw: RecordsRaw} @@ -291,7 +290,7 @@ func (blockchain *Blockchain) Append(RecordsRaw []BlockRecordRaw) (newHeight, ne if blockchain.height > 0 { previousBlockRaw, found := blockchain.database.Get(blockNumberToKey(blockchain.height - 1)) if !found || len(previousBlockRaw) == 0 { - return 0, 0, BlockchainStatusBlockNotFound + return 0, 0, StatusBlockNotFound } block.LastBlockHash = HashFunction(previousBlockRaw) @@ -302,7 +301,7 @@ func (blockchain *Blockchain) Append(RecordsRaw []BlockRecordRaw) (newHeight, ne raw, err := encodeBlock(block, blockchain.privateKey) if err != nil { - return 0, 0, BlockchainStatusCorruptBlock + return 0, 0, StatusCorruptBlock } // store the block @@ -311,29 +310,29 @@ func (blockchain *Blockchain) Append(RecordsRaw []BlockRecordRaw) (newHeight, ne // update the blockchain header in the database, increase blockchain height blockchain.headerWrite(blockchain.height+1, blockchain.version) - return blockchain.height, blockchain.version, BlockchainStatusOK + return blockchain.height, blockchain.version, StatusOK } -// Read reads the block number from the blockchain. Status is BlockchainStatusX. +// Read reads the block number from the blockchain. Status is StatusX. func (blockchain *Blockchain) Read(number uint64) (decoded *BlockDecoded, status int, err error) { if number >= blockchain.height { - return nil, BlockchainStatusBlockNotFound, errors.New("block number exceeds blockchain height") + return nil, StatusBlockNotFound, errors.New("block number exceeds blockchain height") } blockRaw, found := blockchain.database.Get(blockNumberToKey(number)) if !found || len(blockRaw) == 0 { - return nil, BlockchainStatusBlockNotFound, errors.New("block not found") + return nil, StatusBlockNotFound, errors.New("block not found") } block, err := decodeBlock(blockRaw) if err != nil { - return nil, BlockchainStatusCorruptBlock, err + return nil, StatusCorruptBlock, err } decoded, err = decodeBlockRecords(block) if err != nil { - return nil, BlockchainStatusCorruptBlock, err + return nil, StatusCorruptBlock, err } - return decoded, BlockchainStatusOK, nil + return decoded, StatusOK, nil } diff --git a/blockchain/Test_test.go b/blockchain/Test_test.go index f0435ff..1e1a17d 100644 --- a/blockchain/Test_test.go +++ b/blockchain/Test_test.go @@ -231,7 +231,7 @@ func TestBlockchainProfile(t *testing.T) { func printProfileData(blockchain *Blockchain) { fields, status := blockchain.ProfileList() - if status != BlockchainStatusOK { + if status != StatusOK { fmt.Printf("Reading profile data error status: %d\n", status) return } diff --git a/webapi/File.go b/webapi/File.go index 8e73406..44cdff8 100644 --- a/webapi/File.go +++ b/webapi/File.go @@ -149,7 +149,7 @@ func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) { http.Error(w, "", http.StatusBadRequest) return } else if _, _, valid := core.UserWarehouse.FileExists(hashA); !valid { - EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.BlockchainStatusNotInWarehouse}) + EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse}) return } } @@ -204,9 +204,9 @@ func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) { newHeight, newVersion, deletedFiles, status := core.UserBlockchain.DeleteFiles(deleteIDs) // If successfully deleted from the blockchain, delete from the Warehouse in case there are no other references. - if status == blockchain.BlockchainStatusOK { + if status == blockchain.StatusOK { for n := range deletedFiles { - if files, status := core.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.BlockchainStatusOK && len(files) == 0 { + if files, status := core.UserBlockchain.FileExists(deletedFiles[n].Hash); status == blockchain.StatusOK && len(files) == 0 { core.UserWarehouse.DeleteFile(deletedFiles[n].Hash) } } @@ -237,7 +237,7 @@ func apiBlockchainFileUpdate(w http.ResponseWriter, r *http.Request) { http.Error(w, "", http.StatusBadRequest) return } else if _, _, valid := core.UserWarehouse.FileExists(hashA); !valid { - EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.BlockchainStatusNotInWarehouse}) + EncodeJSON(w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse}) return } } diff --git a/webapi/Profile.go b/webapi/Profile.go index 0ed0586..e2d025b 100644 --- a/webapi/Profile.go +++ b/webapi/Profile.go @@ -17,7 +17,7 @@ import ( // apiProfileData contains profile metadata stored on the blockchain. Any data is treated as untrusted and unverified by default. type apiProfileData struct { Fields []apiBlockRecordProfile `json:"fields"` // All fields - Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. See core.BlockchainStatusX. + Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. See blockchain.StatusX. } // apiBlockRecordProfile provides information about the end user. Note that all profile data is arbitrary and shall be considered untrusted and unverified. @@ -64,7 +64,7 @@ func apiProfileRead(w http.ResponseWriter, r *http.Request) { var result apiProfileData var data []byte - if data, result.Status = core.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.BlockchainStatusOK { + if data, result.Status = core.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK { result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data})) } diff --git a/webapi/readme.md b/webapi/readme.md index 36737ab..722eb1c 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -107,18 +107,16 @@ type apiResponsePeerSelf struct { ## Blockchain Functions -Common status codes returned by various endpoints: +Common status codes returned by various endpoints in the `blockchain` package: -```go -const ( - BlockchainStatusOK = 0 // No problems in the blockchain detected. - BlockchainStatusBlockNotFound = 1 // Missing block in the blockchain. - BlockchainStatusCorruptBlock = 2 // Error block encoding - BlockchainStatusCorruptBlockRecord = 3 // Error block record encoding - BlockchainStatusDataNotFound = 4 // Requested data not available in the blockchain - BlockchainStatusNotInWarehouse = 5 // File to be added to blockchain does not exist in the Warehouse -) -``` +| Status | Constant | Info | +|------|----------------|-------------------------------| +| 0 | StatusOK | Successful operation. | +| 1 | StatusBlockNotFound | Missing block in the blockchain. | +| 2 | StatusCorruptBlock | Error block encoding. | +| 3 | StatusCorruptBlockRecord | Error block record encoding. | +| 4 | StatusDataNotFound | Requested data not available in the blockchain. | +| 5 | StatusNotInWarehouse | File to be added to blockchain does not exist in the Warehouse. | ### Blockchain Self Header @@ -158,7 +156,7 @@ type apiBlockchainBlockRaw struct { } type apiBlockchainBlockStatus struct { - Status int `json:"status"` // Status: 0 = Success, 1 = Error invalid data + 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. } @@ -175,7 +173,7 @@ Response: 200 with JSON structure apiBlockchainBlock ```go type apiBlockchainBlock struct { - Status int `json:"status"` // Status: 0 = Success, 1 = Error block not found, 2 = Error block encoding (indicates that the blockchain is corrupt) + 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 @@ -417,7 +415,7 @@ Response: 200 with JSON structure apiProfileData ```go type apiProfileData struct { Fields []apiBlockRecordProfile `json:"fields"` // All fields - Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. See core.BlockchainStatusX. + Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. See blockchain.StatusX. } type apiBlockRecordProfile struct {