From 60fd9588340f47fae53e72349c747cded9334f0f Mon Sep 17 00:00:00 2001 From: Akilan Date: Tue, 27 Jun 2023 17:56:56 +0100 Subject: [PATCH] changes for tracing the blockchain profile image not shown --- blockchain/Block Record Profile.go | 5 ---- blockchain/Multi.go | 17 ++++++++++- blockchain/Profile.go | 4 --- webapi/Blockchain.go | 3 +- webapi/Merge directory.go | 20 ------------- webapi/Profile.go | 46 ++++++++++++------------------ 6 files changed, 36 insertions(+), 59 deletions(-) diff --git a/blockchain/Block Record Profile.go b/blockchain/Block Record Profile.go index c40bab6..8b93720 100644 --- a/blockchain/Block Record Profile.go +++ b/blockchain/Block Record Profile.go @@ -15,7 +15,6 @@ package blockchain import ( "encoding/binary" "errors" - "fmt" "math" ) @@ -39,14 +38,10 @@ func DecodeBlockRecordProfile(recordsRaw []BlockRecordRaw) (fields []BlockRecord } fieldType := binary.LittleEndian.Uint16(record.Data[0:2]) - fmt.Println(record.Data[0:2]) - fmt.Println(fieldType) fieldMap[fieldType] = record.Data[2:] } for fieldType, fieldData := range fieldMap { - fmt.Println("type value added") - fmt.Println(fieldType) fields = append(fields, BlockRecordProfile{Type: fieldType, Data: fieldData}) } diff --git a/blockchain/Multi.go b/blockchain/Multi.go index c9012ba..75f3ec0 100644 --- a/blockchain/Multi.go +++ b/blockchain/Multi.go @@ -16,6 +16,7 @@ package blockchain import ( "encoding/binary" "errors" + "fmt" "time" "github.com/PeernetOfficial/core/btcec" @@ -273,11 +274,18 @@ func (multi *MultiStore) IngestBlock(header *MultiBlockchainHeader, blockNumber // decode it decoded, status, err := DecodeBlockRaw(raw) if failIfInvalid && err != nil { + fmt.Println("---- block invalid -----") return nil, err } + fmt.Println("updated") + // store the transferred block in the cache - multi.WriteBlock(header.PublicKey, header.Version, blockNumber, raw) + err = multi.WriteBlock(header.PublicKey, header.Version, blockNumber, raw) + if err != nil { + fmt.Println(err) + } + header.ListBlocks = append(header.ListBlocks, blockNumber) // update blockchain header stats if records were decoded @@ -285,6 +293,13 @@ func (multi *MultiStore) IngestBlock(header *MultiBlockchainHeader, blockNumber multi.UpdateBlockchainStatistics(header, decoded.RecordsDecoded) } + for _, recordRaw := range decoded.RecordsRaw { + if recordRaw.Type == 0 { + fmt.Println("Profile update detected") + } + } + fmt.Println("---------") + // update the blockchain header multi.WriteBlockchainHeader(header) diff --git a/blockchain/Profile.go b/blockchain/Profile.go index 5b80b74..75d36e3 100644 --- a/blockchain/Profile.go +++ b/blockchain/Profile.go @@ -6,8 +6,6 @@ Author: Peter Kleissner package blockchain -import "fmt" - // 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 @@ -44,8 +42,6 @@ func (blockchain *Blockchain) ProfileReadField(index uint16) (data []byte, statu func (blockchain *Blockchain) ProfileList() (fields []BlockRecordProfile, status int) { uniqueFields := make(map[uint16][]byte) - fmt.Println(blockchain.height) - status = blockchain.Iterate(func(block *Block) (statusI int) { fields, err := DecodeBlockRecordProfile(block.RecordsRaw) if err != nil { diff --git a/webapi/Blockchain.go b/webapi/Blockchain.go index a017ac1..7e3c6dd 100644 --- a/webapi/Blockchain.go +++ b/webapi/Blockchain.go @@ -8,10 +8,9 @@ package webapi import ( "encoding/hex" + "github.com/PeernetOfficial/core/blockchain" "net/http" "strconv" - - "github.com/PeernetOfficial/core/blockchain" ) type apiBlockchainHeader struct { diff --git a/webapi/Merge directory.go b/webapi/Merge directory.go index a92535f..59ee093 100644 --- a/webapi/Merge directory.go +++ b/webapi/Merge directory.go @@ -98,26 +98,6 @@ func (api *WebapiInstance) GreedySearchMergeDirection(nodeID *[][]byte, fileType var filesFromPeer uint64 - //var Name string - // - //ProfileNameFound := false - // - //// First iteration of the entire blockchain to search for the profile - //// image and Username of the user - //for blockN1 := peer.BlockchainHeight - 1; blockN1 > 0; blockN1-- { - // blockDecoded, _, found, _ := api.Backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN1) - // if !found { - // continue - // } - // // Adding profile image and Username to the output - // for raw := range blockDecoded.Block.RecordsRaw { - // if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfileName && !ProfileNameFound { - // Name = string(blockDecoded.Block.RecordsRaw[raw].Data[:]) - // ProfileNameFound = true - // } - // } - //} - // decode blocks from top down for blockN := peer.BlockchainHeight - 1; blockN > 0; blockN-- { blockDecoded, _, found, _ := api.Backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN) diff --git a/webapi/Profile.go b/webapi/Profile.go index 7b041ff..ec36551 100644 --- a/webapi/Profile.go +++ b/webapi/Profile.go @@ -10,6 +10,7 @@ import ( "bytes" "net/http" "strconv" + "time" "github.com/PeernetOfficial/core/blockchain" ) @@ -47,33 +48,26 @@ func (api *WebapiInstance) apiProfileList(w http.ResponseWriter, r *http.Request if valid && !bytes.Equal(NodeID, api.Backend.SelfNodeID()) { //_, node, _ := api.Backend.FindNode(NodeID, 100) - peers := api.Backend.PeerlistGet() + _, peers, _ := api.Backend.FindNode(NodeID, time.Second*5) + // First iteration of the entire blockchain to search for the profile + // image and Username of the user - for i, _ := range peers { - if bytes.Equal(peers[i].NodeID, NodeID) { - // First iteration of the entire blockchain to search for the profile - // image and Username of the user + for blockN1 := peers.BlockchainHeight; blockN1 > 0; blockN1-- { + blockDecoded, _, found, _ := api.Backend.ReadBlock(peers.PublicKey, peers.BlockchainVersion, blockN1) + if !found { + continue + } - for blockN1 := peers[i].BlockchainHeight - 1; blockN1 > 0; blockN1-- { - blockDecoded, _, found, _ := peers[i].Backend.ReadBlock(peers[i].PublicKey, peers[i].BlockchainVersion, blockN1) - if !found { - continue - } + profile, _ := blockchain.DecodeBlockRecordProfile(blockDecoded.Block.RecordsRaw) + // Adding profile image and Username to the output + for raw, _ := range profile { - profile, _ := blockchain.DecodeBlockRecordProfile(blockDecoded.Block.RecordsRaw) - // Adding profile image and Username to the output - for raw, _ := range profile { - - if profile[raw].Type == blockchain.ProfileName { - result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]})) - } - if profile[raw].Type == blockchain.ProfilePicture { - result.Status = 0 - result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]})) - } - } + if profile[raw].Type == blockchain.ProfileName { + result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]})) + } + if profile[raw].Type == blockchain.ProfilePicture { + result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]})) } - } } @@ -135,10 +129,6 @@ func (api *WebapiInstance) apiProfileWrite(w http.ResponseWriter, r *http.Reques return } - for _, field := range input.Fields { - api.Backend.LogError("apiProfileWrite(", "Upload type: %v\n", field.Type) - } - var fields []blockchain.BlockRecordProfile for n := range input.Fields { @@ -147,6 +137,8 @@ func (api *WebapiInstance) apiProfileWrite(w http.ResponseWriter, r *http.Reques newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileWrite(fields) + api.Backend.LogError("apiProfileWrite", "Height: %v, Version %v", newHeight, newVersion) + EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion}) }