Major refactoring of blockchain code into new sub-package.

This commit is contained in:
Kleissner
2021-10-11 01:21:38 +02:00
parent 7495b823e4
commit fbeb01e7ff
16 changed files with 818 additions and 744 deletions

View File

@@ -12,6 +12,7 @@ import (
"strconv"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain"
)
type apiBlockchainHeader struct {
@@ -27,7 +28,7 @@ Request: GET /blockchain/self/header
Result: 200 with JSON structure apiResponsePeerSelf
*/
func apiBlockchainSelfHeader(w http.ResponseWriter, r *http.Request) {
publicKey, height, version := core.UserBlockchainHeader()
publicKey, height, version := core.UserBlockchain.Header()
EncodeJSON(w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
}
@@ -43,7 +44,7 @@ type apiBlockchainBlockRaw struct {
}
type apiBlockchainBlockStatus struct {
Status int `json:"status"` // Status: 0 = Success, 1 = Error invalid data
Status int `json:"status"` // See BlockchainStatusX.
Height uint64 `json:"height"` // Height of the blockchain (number of blocks).
Version uint64 `json:"version"` // Version of the blockchain.
}
@@ -61,13 +62,13 @@ func apiBlockchainSelfAppend(w http.ResponseWriter, r *http.Request) {
return
}
var records []core.BlockRecordRaw
var records []blockchain.BlockRecordRaw
for _, record := range input.Records {
records = append(records, core.BlockRecordRaw{Type: record.Type, Data: record.Data})
records = append(records, blockchain.BlockRecordRaw{Type: record.Type, Data: record.Data})
}
newHeight, newVersion, status := core.UserBlockchainAppend(records)
newHeight, newVersion, status := core.UserBlockchain.Append(records)
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
@@ -96,7 +97,7 @@ func apiBlockchainSelfRead(w http.ResponseWriter, r *http.Request) {
return
}
block, status, _ := core.UserBlockchainRead(uint64(blockN))
block, status, _ := core.UserBlockchain.Read(uint64(blockN))
result := apiBlockchainBlock{Status: status}
if status == 0 {
@@ -108,10 +109,10 @@ func apiBlockchainSelfRead(w http.ResponseWriter, r *http.Request) {
for _, record := range block.RecordsDecoded {
switch v := record.(type) {
case core.BlockRecordFile:
case blockchain.BlockRecordFile:
result.RecordsDecoded = append(result.RecordsDecoded, blockRecordFileToAPI(v))
case core.BlockRecordProfile:
case blockchain.BlockRecordProfile:
result.RecordsDecoded = append(result.RecordsDecoded, blockRecordProfileToAPI(v))
}

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain"
"github.com/google/uuid"
)
@@ -42,31 +43,31 @@ type apiFile struct {
// --- conversion from core to API data ---
func blockRecordFileToAPI(input core.BlockRecordFile) (output apiFile) {
func blockRecordFileToAPI(input blockchain.BlockRecordFile) (output apiFile) {
output = apiFile{ID: input.ID, Hash: input.Hash, NodeID: input.NodeID, Type: input.Type, Format: input.Format, Size: input.Size, Metadata: []apiFileMetadata{}}
for _, tag := range input.Tags {
switch tag.Type {
case core.TagName:
case blockchain.TagName:
output.Name = tag.Text()
case core.TagFolder:
case blockchain.TagFolder:
output.Folder = tag.Text()
case core.TagDescription:
case blockchain.TagDescription:
output.Description = tag.Text()
case core.TagDateShared:
case blockchain.TagDateShared:
output.Date, _ = tag.Date()
case core.TagDateCreated:
case blockchain.TagDateCreated:
date, _ := tag.Date()
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Name: "Date Created", Date: date})
case core.TagSharedByCount:
case blockchain.TagSharedByCount:
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Name: "Shared By Count", Number: tag.Number()})
case core.TagSharedByGeoIP:
case blockchain.TagSharedByGeoIP:
output.Metadata = append(output.Metadata, apiFileMetadata{Type: tag.Type, Name: "Shared By GeoIP", Text: tag.Text()})
default:
@@ -77,32 +78,32 @@ func blockRecordFileToAPI(input core.BlockRecordFile) (output apiFile) {
return output
}
func blockRecordFileFromAPI(input apiFile) (output core.BlockRecordFile) {
output = core.BlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size}
func blockRecordFileFromAPI(input apiFile) (output blockchain.BlockRecordFile) {
output = blockchain.BlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size}
if input.Name != "" {
output.Tags = append(output.Tags, core.TagFromText(core.TagName, input.Name))
output.Tags = append(output.Tags, blockchain.TagFromText(blockchain.TagName, input.Name))
}
if input.Folder != "" {
output.Tags = append(output.Tags, core.TagFromText(core.TagFolder, input.Folder))
output.Tags = append(output.Tags, blockchain.TagFromText(blockchain.TagFolder, input.Folder))
}
if input.Description != "" {
output.Tags = append(output.Tags, core.TagFromText(core.TagDescription, input.Description))
output.Tags = append(output.Tags, blockchain.TagFromText(blockchain.TagDescription, input.Description))
}
for _, meta := range input.Metadata {
if core.IsTagVirtual(meta.Type) { // Virtual tags are not mapped back. They are read-only.
if blockchain.IsTagVirtual(meta.Type) { // Virtual tags are not mapped back. They are read-only.
continue
}
switch meta.Type {
case core.TagName, core.TagFolder, core.TagDescription: // auto mapped tags
case blockchain.TagName, blockchain.TagFolder, blockchain.TagDescription: // auto mapped tags
case core.TagDateCreated:
output.Tags = append(output.Tags, core.TagFromDate(meta.Type, meta.Date))
case blockchain.TagDateCreated:
output.Tags = append(output.Tags, blockchain.TagFromDate(meta.Type, meta.Date))
default:
output.Tags = append(output.Tags, core.BlockRecordFileTag{Type: meta.Type, Data: meta.Blob})
output.Tags = append(output.Tags, blockchain.BlockRecordFileTag{Type: meta.Type, Data: meta.Blob})
}
}
@@ -129,7 +130,7 @@ func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) {
return
}
var filesAdd []core.BlockRecordFile
var filesAdd []blockchain.BlockRecordFile
for _, file := range input.Files {
if file.ID == uuid.Nil { // if the ID is not provided by the caller, set it
@@ -139,7 +140,7 @@ func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) {
filesAdd = append(filesAdd, blockRecordFileFromAPI(file))
}
newHeight, newVersion, status := core.UserBlockchainAddFiles(filesAdd)
newHeight, newVersion, status := core.UserBlockchain.AddFiles(filesAdd)
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
@@ -151,7 +152,7 @@ Request: GET /blockchain/self/list/file
Response: 200 with JSON structure apiBlockAddFiles
*/
func apiBlockchainSelfListFile(w http.ResponseWriter, r *http.Request) {
files, status := core.UserBlockchainListFiles()
files, status := core.UserBlockchain.ListFiles()
var result apiBlockAddFiles
@@ -182,7 +183,7 @@ func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) {
deleteIDs = append(deleteIDs, input.Files[n].ID)
}
newHeight, newVersion, status := core.UserBlockchainDeleteFiles(deleteIDs)
newHeight, newVersion, status := core.UserBlockchain.DeleteFiles(deleteIDs)
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}

View File

@@ -11,6 +11,7 @@ import (
"strconv"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain"
)
// apiProfileData contains profile metadata stored on the blockchain. Any data is treated as untrusted and unverified by default.
@@ -35,7 +36,7 @@ Request: GET /profile/list
Response: 200 with JSON structure apiProfileData
*/
func apiProfileList(w http.ResponseWriter, r *http.Request) {
fields, status := core.UserProfileList()
fields, status := core.UserBlockchain.ProfileList()
result := apiProfileData{Status: status}
for n := range fields {
@@ -63,8 +64,8 @@ func apiProfileRead(w http.ResponseWriter, r *http.Request) {
var result apiProfileData
var data []byte
if data, result.Status = core.UserProfileReadField(uint16(fieldN)); result.Status == core.BlockchainStatusOK {
result.Fields = append(result.Fields, blockRecordProfileToAPI(core.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
if data, result.Status = core.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.BlockchainStatusOK {
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
}
EncodeJSON(w, r, result)
@@ -82,13 +83,13 @@ func apiProfileWrite(w http.ResponseWriter, r *http.Request) {
return
}
var fields []core.BlockRecordProfile
var fields []blockchain.BlockRecordProfile
for n := range input.Fields {
fields = append(fields, blockRecordProfileFromAPI(input.Fields[n]))
}
newHeight, newVersion, status := core.UserProfileWrite(fields)
newHeight, newVersion, status := core.UserBlockchain.ProfileWrite(fields)
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
@@ -111,21 +112,21 @@ func apiProfileDelete(w http.ResponseWriter, r *http.Request) {
fields = append(fields, input.Fields[n].Type)
}
newHeight, newVersion, status := core.UserProfileDelete(fields)
newHeight, newVersion, status := core.UserBlockchain.ProfileDelete(fields)
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
}
// --- conversion from core to API data ---
func blockRecordProfileToAPI(input core.BlockRecordProfile) (output apiBlockRecordProfile) {
func blockRecordProfileToAPI(input blockchain.BlockRecordProfile) (output apiBlockRecordProfile) {
output.Type = input.Type
switch input.Type {
case core.ProfileName, core.ProfileEmail, core.ProfileWebsite, core.ProfileTwitter, core.ProfileYouTube, core.ProfileAddress:
case blockchain.ProfileName, blockchain.ProfileEmail, blockchain.ProfileWebsite, blockchain.ProfileTwitter, blockchain.ProfileYouTube, blockchain.ProfileAddress:
output.Text = input.Text()
case core.ProfilePicture:
case blockchain.ProfilePicture:
output.Blob = input.Data
default:
@@ -135,14 +136,14 @@ func blockRecordProfileToAPI(input core.BlockRecordProfile) (output apiBlockReco
return output
}
func blockRecordProfileFromAPI(input apiBlockRecordProfile) (output core.BlockRecordProfile) {
func blockRecordProfileFromAPI(input apiBlockRecordProfile) (output blockchain.BlockRecordProfile) {
output.Type = input.Type
switch input.Type {
case core.ProfileName, core.ProfileEmail, core.ProfileWebsite, core.ProfileTwitter, core.ProfileYouTube, core.ProfileAddress:
case blockchain.ProfileName, blockchain.ProfileEmail, blockchain.ProfileWebsite, blockchain.ProfileTwitter, blockchain.ProfileYouTube, blockchain.ProfileAddress:
output.Data = []byte(input.Text)
case core.ProfilePicture:
case blockchain.ProfilePicture:
output.Data = input.Blob
default:

View File

@@ -11,7 +11,7 @@ import (
"sync"
"time"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain"
"github.com/google/uuid"
)
@@ -273,11 +273,11 @@ func SortFiles(files []*apiFile, Sort int) (sorted []*apiFile) {
case SortSharedByCountAsc:
sort.SliceStable(files, func(i, j int) bool {
return files[i].GetMetadata(core.TagSharedByCount).Number < files[j].GetMetadata(core.TagSharedByCount).Number
return files[i].GetMetadata(blockchain.TagSharedByCount).Number < files[j].GetMetadata(blockchain.TagSharedByCount).Number
})
case SortSharedByCountDesc:
sort.SliceStable(files, func(i, j int) bool {
return files[i].GetMetadata(core.TagSharedByCount).Number > files[j].GetMetadata(core.TagSharedByCount).Number
return files[i].GetMetadata(blockchain.TagSharedByCount).Number > files[j].GetMetadata(blockchain.TagSharedByCount).Number
})
}

View File

@@ -15,6 +15,7 @@ import (
"time"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain"
"github.com/google/uuid"
)
@@ -58,7 +59,7 @@ func dispatchSearch(input SearchRequest) (job *SearchJob) {
}
// createTestResult creates a test file. fileType = -1 for any.
func createTestResult(fileType int) (file core.BlockRecordFile) {
func createTestResult(fileType int) (file blockchain.BlockRecordFile) {
randomData := make([]byte, 10*1024)
rand.Read(randomData)
@@ -192,12 +193,12 @@ func createTestResult(fileType int) (file core.BlockRecordFile) {
extension = ".bin"
}
file.Tags = append(file.Tags, core.TagFromText(core.TagName, tempFileName("", extension)))
//file.Tags = append(file.Tags, core.TagFromText(core.TagFolder, "not set"))
file.Tags = append(file.Tags, core.TagFromDate(core.TagDateShared, time.Now().UTC()))
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagName, tempFileName("", extension)))
//file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagFolder, "not set"))
file.Tags = append(file.Tags, blockchain.TagFromDate(blockchain.TagDateShared, time.Now().UTC()))
sharedByCount := uint64(rand.Intn(10))
file.Tags = append(file.Tags, core.TagFromNumber(core.TagSharedByCount, sharedByCount))
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, sharedByCount))
if sharedByCount > 0 {
var sharedByGeoIP string
@@ -210,7 +211,7 @@ func createTestResult(fileType int) (file core.BlockRecordFile) {
sharedByGeoIP += fmt.Sprintf("%.4f", latitude) + "," + fmt.Sprintf("%.4f", longitude)
}
file.Tags = append(file.Tags, core.TagFromText(core.TagSharedByGeoIP, sharedByGeoIP))
file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP))
}
return
@@ -235,7 +236,7 @@ func randomGeoIP() (latitude, longitude float32) {
}
// queryRecentShared returns recently shared files on the network. fileType = -1 for any.
func queryRecentShared(fileType, limit int) (files []*core.BlockRecordFile) {
func queryRecentShared(fileType, limit int) (files []*blockchain.BlockRecordFile) {
for n := 0; n < limit; n++ {
newFile := createTestResult(fileType)
files = append(files, &newFile)