From 042396e0d6914aa66b142b260bafa91df71b4acb Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Tue, 21 Nov 2023 13:44:59 +0800 Subject: [PATCH] update bs --- go.mod | 23 +- go.sum | 39 ++-- node/common.go | 69 ++++++ node/node.go | 2 - node/putHandle.go | 72 +++---- node/syncFile.go | 532 ++++++++++++++++++++++++---------------------- 6 files changed, 411 insertions(+), 326 deletions(-) diff --git a/go.mod b/go.mod index fe1365b..38c85c6 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,10 @@ module github.com/CESSProject/DeOSS go 1.20 require ( - github.com/AstaFrode/go-libp2p v0.26.4-0.20231025094835-732a19c0bd0b - github.com/CESSProject/cess-go-sdk v0.3.21-0.20231110033426-453245bdc270 + github.com/AstaFrode/go-libp2p v0.26.4-0.20231113143058-912296254d44 + github.com/CESSProject/cess-go-sdk v0.3.21-0.20231114061951-fc77d8141ff0 github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde - github.com/CESSProject/p2p-go v0.2.6-0.20231110024714-6f709106eff6 + github.com/CESSProject/p2p-go v0.2.6-0.20231117060004-cbb4a2e1792e github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce github.com/cbergoon/merkletree v0.2.0 github.com/centrifuge/go-substrate-rpc-client/v4 v4.1.0 @@ -15,6 +15,8 @@ require ( github.com/gin-gonic/gin v1.9.0 github.com/google/uuid v1.3.1 github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef + github.com/ipfs/go-block-format v0.1.2 + github.com/ipfs/go-cid v0.4.1 github.com/jedib0t/go-pretty/v6 v6.4.6 github.com/mr-tron/base58 v1.2.0 github.com/multiformats/go-multiaddr v0.11.0 @@ -25,17 +27,17 @@ require ( github.com/spf13/viper v1.15.0 github.com/stretchr/testify v1.8.4 github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a + github.com/vedhavyas/go-subkey/v2 v2.0.0 go.uber.org/zap v1.25.0 golang.org/x/crypto v0.14.0 golang.org/x/time v0.1.0 ) require ( - github.com/AstaFrode/boxo v0.12.1-0.20231109082032-75fa153a39f3 // indirect - github.com/AstaFrode/go-libp2p-gostream v0.5.1-0.20231109095351-8050802a5d8b // indirect - github.com/AstaFrode/go-libp2p-grpc v0.1.1-0.20231109100113-0af9e902e45f // indirect - github.com/AstaFrode/go-libp2p-kad-dht v0.23.1-0.20231109074947-0279c8612966 // indirect - github.com/AstaFrode/go-libp2p-kbucket v0.5.1-0.20231109065217-dde6a45fa7ce // indirect + github.com/AstaFrode/boxo v0.12.1-0.20231117024608-7d5a75b69cdd // indirect + github.com/AstaFrode/go-libp2p-kad-dht v0.23.1-0.20231117020818-8d3fc0a486ad // indirect + github.com/AstaFrode/go-libp2p-kbucket v0.5.1-0.20231113151623-a45fff026283 // indirect + github.com/AstaFrode/go-libp2p-record v0.2.1-0.20231113144445-2b6ba1a2d403 // indirect github.com/AstaFrode/go-peertaskqueue v0.8.2-0.20231108073729-990e433425a4 // indirect github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect @@ -88,9 +90,6 @@ require ( github.com/huin/goupnp v1.2.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/ipfs/bbloom v0.0.4 // indirect - github.com/ipfs/boxo v0.12.0 // indirect - github.com/ipfs/go-block-format v0.1.2 // indirect - github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect github.com/ipfs/go-ipfs-delay v0.0.1 // indirect github.com/ipfs/go-ipfs-pq v0.0.3 // indirect @@ -112,9 +111,7 @@ require ( github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-cidranger v1.1.0 // indirect github.com/libp2p/go-flow-metrics v0.1.0 // indirect - github.com/libp2p/go-libp2p v0.26.4 // indirect github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect - github.com/libp2p/go-libp2p-record v0.2.0 // indirect github.com/libp2p/go-msgio v0.3.0 // indirect github.com/libp2p/go-nat v0.1.0 // indirect github.com/libp2p/go-netroute v0.2.1 // indirect diff --git a/go.sum b/go.sum index 3f846d1..0cb5420 100644 --- a/go.sum +++ b/go.sum @@ -43,29 +43,27 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4= dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU= git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= -github.com/AstaFrode/boxo v0.12.1-0.20231109082032-75fa153a39f3 h1:eq7ocr4tA1vcvha6M5xSI/m6VVCneTR/tyFiVKIR7WI= -github.com/AstaFrode/boxo v0.12.1-0.20231109082032-75fa153a39f3/go.mod h1:V/cKqKfQBA04KQpA4dGpq4mwCBwT+e/fjyDDc2GT2gg= -github.com/AstaFrode/go-libp2p v0.26.4-0.20231025094835-732a19c0bd0b h1:4P16QVGgl7PulDFuDe67fUdPX4vls+/NL5yhuKHkv9k= -github.com/AstaFrode/go-libp2p v0.26.4-0.20231025094835-732a19c0bd0b/go.mod h1:RO0J/gHHqO0Wq16nM2Yv0byy9Z9cDBJ8VTzulots0kA= -github.com/AstaFrode/go-libp2p-gostream v0.5.1-0.20231109095351-8050802a5d8b h1:lOrOZr59T2tm+zkL8P0LKFmeI7tkNoW9dsJrrmCbwfk= -github.com/AstaFrode/go-libp2p-gostream v0.5.1-0.20231109095351-8050802a5d8b/go.mod h1:Wr9fDyJqrv8ATO/U+T6vtfiLYUutgi1/KJ1e6wkGDaA= -github.com/AstaFrode/go-libp2p-grpc v0.1.1-0.20231109100113-0af9e902e45f h1:W56eKv7chba4rcsokT66QJ1s4mMRFGvhbMXGPxK1fUo= -github.com/AstaFrode/go-libp2p-grpc v0.1.1-0.20231109100113-0af9e902e45f/go.mod h1:ngjEwR1IGTv20MwIvpMp12UNLbRwXq91a7TPW6m+2Mc= -github.com/AstaFrode/go-libp2p-kad-dht v0.23.1-0.20231109074947-0279c8612966 h1:4mqVjrugCK9HBInKBLJwqilar1y7LI7xT899MxzbHdA= -github.com/AstaFrode/go-libp2p-kad-dht v0.23.1-0.20231109074947-0279c8612966/go.mod h1:+DMTKOz/+WkZNaxdWDgrMQTt5d+x3QaK2eP1s7pmAL0= -github.com/AstaFrode/go-libp2p-kbucket v0.5.1-0.20231109065217-dde6a45fa7ce h1:LLSgCayG2gME+5XjK+oY5hS9nUhtff1+sw5tEzTGByU= -github.com/AstaFrode/go-libp2p-kbucket v0.5.1-0.20231109065217-dde6a45fa7ce/go.mod h1:guRAEj5PdSnmhX3xlfLFlcT2vI5sCItAZHNSapKkO6c= +github.com/AstaFrode/boxo v0.12.1-0.20231117024608-7d5a75b69cdd h1:HO6x4gA7nBUXhJ+cN6Oxq+5vb+QMGR6h1POpXj8O+iw= +github.com/AstaFrode/boxo v0.12.1-0.20231117024608-7d5a75b69cdd/go.mod h1:QAAe9MHNnh7pV9yZPDjzUeS2XFsJcZL1SVX/wwtOg/A= +github.com/AstaFrode/go-libp2p v0.26.4-0.20231113143058-912296254d44 h1:+cpQP/8OZDq7X5y45KsHC3Ndowwh0QFZImqXNLSVK1w= +github.com/AstaFrode/go-libp2p v0.26.4-0.20231113143058-912296254d44/go.mod h1:IwZXqQxpbAHV7orIa90XeCUGMKtzJnopON5gS1Avs/U= +github.com/AstaFrode/go-libp2p-kad-dht v0.23.1-0.20231117020818-8d3fc0a486ad h1:Ny8LSef08eEznNWPDsVxUlPBMlacgRcpUl+blucM2xs= +github.com/AstaFrode/go-libp2p-kad-dht v0.23.1-0.20231117020818-8d3fc0a486ad/go.mod h1:a3ag0Ke6hZseS0N2drhZwPcvtHbi67x+5xBNQmdRSNA= +github.com/AstaFrode/go-libp2p-kbucket v0.5.1-0.20231113151623-a45fff026283 h1:g3+4j9aH1s2bfMlLJ8SMe/+r0eGcEE2idGpVAz1nKk0= +github.com/AstaFrode/go-libp2p-kbucket v0.5.1-0.20231113151623-a45fff026283/go.mod h1:/ShEGW/hKSDSGKbDmBgCS6xGs5wBSURBBhIVrpU/2mo= +github.com/AstaFrode/go-libp2p-record v0.2.1-0.20231113144445-2b6ba1a2d403 h1:Mx/F2BYfwj8bLUiAK9IbdRgQHG6Ka3RE1FUysHNMT7o= +github.com/AstaFrode/go-libp2p-record v0.2.1-0.20231113144445-2b6ba1a2d403/go.mod h1:jyCMqtI9dzUu/2NToXCMQR4x+l+v+vqw3+S+QTOW1rg= github.com/AstaFrode/go-peertaskqueue v0.8.2-0.20231108073729-990e433425a4 h1:fuOv9I6pz+zZZgRU9TYCYP2wd0iXh4wsZCdzunmDDGg= github.com/AstaFrode/go-peertaskqueue v0.8.2-0.20231108073729-990e433425a4/go.mod h1:0YcQDsyTRKBTK9yE22DQgiwQndGFvenqMvD6Spkxl28= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/CESSProject/cess-go-sdk v0.3.21-0.20231110033426-453245bdc270 h1:ait40MYXw5eW26Uv5l9SWNLz+VHjp4+fxR3aNYxBz+g= -github.com/CESSProject/cess-go-sdk v0.3.21-0.20231110033426-453245bdc270/go.mod h1:KWlHxDKfinyfEI3w7BqMqgo+oi7grsUVayDw/2NYSks= +github.com/CESSProject/cess-go-sdk v0.3.21-0.20231114061951-fc77d8141ff0 h1:WelCunG4c8Ohmb4/ZiEzmJ9LCiU6cNSDMPxZb35ioGU= +github.com/CESSProject/cess-go-sdk v0.3.21-0.20231114061951-fc77d8141ff0/go.mod h1:KWlHxDKfinyfEI3w7BqMqgo+oi7grsUVayDw/2NYSks= github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde h1:5MDRjjtg6PEhqyVjupwaapN96cOZiddOGAYwKQeaTu0= github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde/go.mod h1:RUXBd3ROP98MYepEEa0Y0l/T0vQlIKqFJxI/ocdnRLM= -github.com/CESSProject/p2p-go v0.2.6-0.20231110024714-6f709106eff6 h1:TijSRt99nur0rt76KEpQlSLNOvnw0J5JeSHT/6QJhY8= -github.com/CESSProject/p2p-go v0.2.6-0.20231110024714-6f709106eff6/go.mod h1:0st7f0wgjxb6Y9IDq97pzF1+NPmsfLzjZEpaOKsdo2Q= +github.com/CESSProject/p2p-go v0.2.6-0.20231117060004-cbb4a2e1792e h1:r+SJZzcxb3so7W/6/98HGtT0BoB3YcK5Kf2uTp0q9Uc= +github.com/CESSProject/p2p-go v0.2.6-0.20231117060004-cbb4a2e1792e/go.mod h1:SMwJt5Zpk98k+d2J5gsN+0Forr7MbqOYWbHLd3mBLrI= github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM= github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= @@ -350,8 +348,6 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.12.0 h1:AXHg/1ONZdRQHQLgG5JHsSC3XoE4DjCAMgK+asZvUcQ= -github.com/ipfs/boxo v0.12.0/go.mod h1:xAnfiU6PtxWCnRqu7dcXQ10bB5/kvI1kXRotuGqGBhg= github.com/ipfs/go-block-format v0.1.2 h1:GAjkfhVx1f4YTODS6Esrj1wt2HhrtwTnhEr+DyPUaJo= github.com/ipfs/go-block-format v0.1.2/go.mod h1:mACVcrxarQKstUU3Yf/RdwbC4DzPV6++rO2a3d+a/KE= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= @@ -433,13 +429,8 @@ github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38y github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM= github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro= -github.com/libp2p/go-libp2p v0.26.4 h1:VA9ChjN0n1BwwfU/dqx4Zj9ezXtIxGk8FyJPwFONqxs= -github.com/libp2p/go-libp2p v0.26.4/go.mod h1:x75BN32YbwuY0Awm2Uix4d4KOz+/4piInkp4Wr3yOo8= github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s= github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w= -github.com/libp2p/go-libp2p-record v0.2.0 h1:oiNUOCWno2BFuxt3my4i1frNrt7PerzB3queqa1NkQ0= -github.com/libp2p/go-libp2p-record v0.2.0/go.mod h1:I+3zMkvvg5m2OcSdoL0KPljyJyvNDFGKX7QdlpYUcwk= -github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA= github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0= github.com/libp2p/go-msgio v0.3.0/go.mod h1:nyRM819GmVaF9LX3l03RMh10QdOroF++NBbxAb0mmDM= github.com/libp2p/go-nat v0.1.0 h1:MfVsH6DLcpa04Xr+p8hmVRG4juse0s3J8HyNWYHffXg= @@ -690,6 +681,8 @@ github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vedhavyas/go-subkey v1.0.4 h1:QwjBZx4w7qXC2lmqol2jJfhaNXPI9BsgLZiMiCwqGDU= github.com/vedhavyas/go-subkey v1.0.4/go.mod h1:aOIil/KS9hJlnr9ZSQKSoXdu/MbnkCxG4x9IOlLsMtI= +github.com/vedhavyas/go-subkey/v2 v2.0.0 h1:LemDIsrVtRSOkp0FA8HxP6ynfKjeOj3BY2U9UNfeDMA= +github.com/vedhavyas/go-subkey/v2 v2.0.0/go.mod h1:95aZ+XDCWAUUynjlmi7BtPExjXgXxByE0WfBwbmIRH4= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= diff --git a/node/common.go b/node/common.go index 385b34f..4968010 100644 --- a/node/common.go +++ b/node/common.go @@ -17,6 +17,7 @@ import ( jwt "github.com/dgrijalva/jwt-go" "github.com/mr-tron/base58" "github.com/pkg/errors" + "github.com/vedhavyas/go-subkey/v2/sr25519" ) // VerifyToken is used to parse and verify token @@ -108,6 +109,74 @@ func (n *Node) verifySignature(account, message, signature string) ([]byte, erro return nil, errors.New("signature verification failed") } +// VerifyToken is used to parse and verify token +func (n *Node) verifyJsSignature(account, message, signature string) ([]byte, error) { + if account == "" || signature == "" { + return nil, errors.New("no identity authentication information") + } + pkey, err := sutils.ParsingPublickey(account) + if err != nil { + return nil, err + } + + ss58, err := sutils.EncodePublicKeyAsSubstrateAccount(pkey) + if err != nil { + return nil, err + } + + verkr, _ := keyring.FromURI(ss58, keyring.NetSubstrate{}) + + sign_bytes, err := base58.Decode(signature) + if err != nil { + if strings.Contains(err.Error(), "zero length") { + return nil, errors.New("empty signature") + } + return nil, errors.New("signature not encoded with base58") + } + + if len(sign_bytes) != 64 { + return nil, errors.New("wrong signature length") + } + + var sign_array [64]byte + for i := 0; i < 64; i++ { + sign_array[i] = sign_bytes[i] + } + + // Verify signature + ok := verkr.Verify(verkr.SigningContext([]byte(""+message+"")), sign_array) + if ok { + return pkey, nil + } + return nil, errors.New("signature verification failed") +} + +// VerifyToken is used to parse and verify token +func (n *Node) verifySR25519Signature(account, message, signature string) ([]byte, error) { + if account == "" || signature == "" { + return nil, errors.New("no identity authentication information") + } + + pkey, err := sutils.ParsingPublickey(account) + if err != nil { + return nil, err + } + + pub, err := sr25519.Scheme{}.FromPublicKey(pkey) + if err != nil { + return pkey, err + } + sign_bytes, err := base58.Decode(signature) + if err != nil { + return pkey, err + } + ok := pub.Verify([]byte(message), sign_bytes) + if !ok { + return pkey, errors.New("signature verification failed") + } + return pkey, nil +} + func (n *Node) AccessControl(account string) bool { if account == "" { return false diff --git a/node/node.go b/node/node.go index 31972e1..efa3f88 100644 --- a/node/node.go +++ b/node/node.go @@ -321,8 +321,6 @@ func (n *Node) DelFromBlacklist(peerid string) { } func (n *Node) RebuildDirs() { - os.RemoveAll(n.GetDirs().FileDir) - os.RemoveAll(n.GetDirs().ServiceTagDir) os.RemoveAll(n.GetDirs().TmpDir) os.RemoveAll(filepath.Join(n.Workspace(), configs.Db)) os.RemoveAll(filepath.Join(n.Workspace(), configs.Log)) diff --git a/node/putHandle.go b/node/putHandle.go index ee71f64..da38a45 100644 --- a/node/putHandle.go +++ b/node/putHandle.go @@ -267,25 +267,25 @@ func (n *Node) putHandle(c *gin.Context) { return } - // for i := 0; i < len(segmentInfo); i++ { - // for j := 0; j < len(segmentInfo[i].FragmentHash); j++ { - // mycid, err := n.FidToCid(filepath.Base(segmentInfo[i].FragmentHash[j])) - // n.Upfile("info", fmt.Sprintf("[%v] my cid from hash-1: %v ,%v", clientIp, mycid, err)) - // } - // } + for i := 0; i < len(segmentInfo); i++ { + for j := 0; j < len(segmentInfo[i].FragmentHash); j++ { + mycid, err := n.FidToCid(filepath.Base(segmentInfo[i].FragmentHash[j])) + n.Upfile("info", fmt.Sprintf("[%v] my cid from hash-1: %v ,%v", clientIp, mycid, err)) + } + } - // n.Upfile("info", fmt.Sprintf("[%v] segmentInfo: %v", clientIp, segmentInfo)) - // savedCid, err := n.saveToBlockStore(segmentInfo) - // if err != nil { - // n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err)) - // c.JSON(http.StatusInternalServerError, err.Error()) - // return - // } + n.Upfile("info", fmt.Sprintf("[%v] segmentInfo: %v", clientIp, segmentInfo)) + savedCid, err := n.saveToBlockStore(segmentInfo) + if err != nil { + n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err)) + c.JSON(http.StatusInternalServerError, err.Error()) + return + } - // n.Upfile("info", fmt.Sprintf("[%v] save successed cids: %v", clientIp, savedCid)) + n.Upfile("info", fmt.Sprintf("[%v] save successed cids: %v", clientIp, savedCid)) // for i := 0; i < len(savedCid); i++ { - // buf, err := n.GetDataFromBlock(savedCid[i]) + // buf, err := n.GetDataFromBlock(n.GetCtxQueryFromCtxCancel(), savedCid[i]) // if err != nil { // n.Upfile("err", fmt.Sprintf("[%v] get data from %v failed", clientIp, savedCid[i])) // } else { @@ -443,24 +443,24 @@ func (n *Node) deduplication(pkey []byte, segmentInfo []pattern.SegmentDataInfo, return false, nil } -// func (n *Node) saveToBlockStore(segmentInfo []pattern.SegmentDataInfo) ([]string, error) { -// var err error -// var buf []byte -// var savedCid = make([]string, 0) -// for _, segment := range segmentInfo { -// for _, fragment := range segment.FragmentHash { -// buf, err = os.ReadFile(fragment) -// if err != nil { -// return savedCid, err -// } -// aCid, err := n.SaveAndNotifyDataBlock(buf) -// if err != nil { -// n.Upfile("err", fmt.Sprintf("save %v to block failed", fragment)) -// } else { -// n.Upfile("info", fmt.Sprintf("save %v to block suc: %v", fragment, aCid.String())) -// savedCid = append(savedCid, aCid.String()) -// } -// } -// } -// return savedCid, nil -// } +func (n *Node) saveToBlockStore(segmentInfo []pattern.SegmentDataInfo) ([]string, error) { + var err error + var buf []byte + var savedCid = make([]string, 0) + for _, segment := range segmentInfo { + for _, fragment := range segment.FragmentHash { + buf, err = os.ReadFile(fragment) + if err != nil { + return savedCid, err + } + aCid, err := n.SaveAndNotifyDataBlock(buf) + if err != nil { + n.Upfile("err", fmt.Sprintf("save %v to block failed", fragment)) + } else { + n.Upfile("info", fmt.Sprintf("save %v to block suc: %v", fragment, aCid.String())) + savedCid = append(savedCid, aCid.String()) + } + } + } + return savedCid, nil +} diff --git a/node/syncFile.go b/node/syncFile.go index d6c604b..7ff3495 100644 --- a/node/syncFile.go +++ b/node/syncFile.go @@ -8,15 +8,21 @@ package node import ( + "context" "encoding/json" "errors" + "fmt" "os" "path/filepath" "time" "github.com/CESSProject/DeOSS/pkg/db" + "github.com/CESSProject/DeOSS/pkg/utils" "github.com/CESSProject/cess-go-sdk/core/pattern" sutils "github.com/CESSProject/cess-go-sdk/core/utils" + "github.com/CESSProject/p2p-go/core" + blocks "github.com/ipfs/go-block-format" + "github.com/ipfs/go-cid" "github.com/mr-tron/base58" ) @@ -78,7 +84,7 @@ func (n *Node) syncFiles(ch chan<- bool) { if err != nil { continue } - + var fragmentlist []pattern.FileHash fmeta, err := n.QueryFileMetadata(wantfiles[i].File) if err != nil { n.Log("err", err.Error()) @@ -88,267 +94,289 @@ func (n *Node) syncFiles(ch chan<- bool) { continue } else { size = sorder.FileSize.Int64() + for _, segment := range sorder.SegmentList { + fragmentlist = append(fragmentlist, segment.FragmentHash...) + } } } else { size = fmeta.FileSize.Int64() + for _, segment := range fmeta.SegmentList { + for _, fragment := range segment.FragmentList { + fragmentlist = append(fragmentlist, fragment.Hash) + } + } } err = n.ReadDataAction(addr.ID, wantfiles[i].File, wantfiles[i].File, filepath.Join(n.GetDirs().FileDir, wantfiles[i].File), size) if err != nil { n.Log("err", err.Error()) } + + // + for _, fragment := range fragmentlist { + fid, err := n.FidToCid(string(fragment[:])) + if err != nil { + n.Log("err", err.Error()) + } + _, err = n.getBlockData(fid) + if err != nil { + n.Log("err", err.Error()) + } else { + n.Log("info", fmt.Sprintf("get block data suc and fid is:%s", fid)) + } + } + } + } +} + +func (n *Node) getBlocks(ch chan<- bool) { + defer func() { + ch <- true + }() + + time.Sleep(time.Second * 30) + + var wantList = []string{ + // 256k + // "QmXeE7NjTLviHDaf7ZvWhdTW2P43QY41DgxpeogQeBXnoZ", + // 1M + "QmRdTXKPV8VPCuPaawjJZZaACsDYRfVtZtNZTDLXrAQPx3", + // 2M + // "QmU9TPr52YZPngKq3AG21FfFztgfsW3LQQKnUWvF73fZjT", + // 3M + // "QmayoE5xG6tvgzTp2bZNErk7TSHW8ou4FKv2mamtGLNpMi", + // 3.5M + // "QmTZciSbB99gqWZknzcZ1HFFtbpnnEz4LTpxQe7zxBmpTF", + // 4M + "Qmay44Rd1U2Gcg3WB2BgTCezP9LNqrTzeDUZt2LKb7HSFh", + // 6M + // "QmadWN69GhfNWtuobno9v1XhXWjSfLkRXEdL32W4gaYunb", + // 8M + // "QmRQyZGdTjS2bYy1BisteG41NtWFSWTjE3smueqQpNZady", + // 10M + "QmdoP3JcdjeWXFDUrsvL3rqKejZJrRaBEEUEHyKwW6JV96", + // 12M + // "QmcKLqao5TcawyJdtDRFbHvMc1ktfepjpqXuu62o6ve1bx", + // 6Bytes + // "QmXsMQRrggM1pD2axFoscGSrANph6f1nsQm6oFm3AHzECy", + // 16M + // "QmcfxLnapUJbDXN68zTYQZX5eaCZ3nSqeG5PFwDCThRwnP", + // "QmauZXKtvXaz7j3Kqq3wA2NURgmJTQL5o8T4N5YProYzt8", + // "QmVLsR4VH3AwEMf9mvBhBGgcQcKLyjQdekJPKHXHWgVF4c", + // "Qma9hBSf9FobnK8N3cLxUuGHtMZfd4b2a66jCVQtYe5CHr", + // "QmdoJangkxFUQsMrvAsmN4vZVHUHqpREJfmbrSHk6XAsDm", + // "QmQUnEaKsHNLteYcQJDNVaHHV243sq8uWU6pVpQUbrSq1P", + // "QmWo6MkkYmKzToPzkXQu2aTS3vQj21fHQiFc3G3jNTzzWT", + // "QmUoJN78mHiCETZv3oRLLYSPAKfgWWgGvw18kCfF5TFH3d", + // "QmXPKrreCAhPMixUFQmCerAvNaMe2rDMqgzjaVrQTqocgZ", + // "QmYgYQSPeQuGxuQutsAoikYWXA7wmakQuUcqcLNuMGV6Y7", + // "QmfDcKHD6PuWfhZJPxaSBVCbmBzxnKzzNroUJoyncqL6Eq", + // "QmT3Pe3wiWpGpcYu4ANuSizzB3463svxSTz1VaqT276vrs", + // "QmV5TvFzDBfVmo8vyweVBvaNeHESdnUss81QEuKm5EeMdQ", + // "QmX3yLqBH78MHNmYjSR27vwGidFH8XYZ96zAYAZLdSkZxj", + // "QmZotvwGTFSaLaSueeLdWuvHgzpYY4d9g2L6ZdX2BpkE9V", + // "QmaqYqCLiEWdXdsV2HJdsTqfEJGUgk6Tbv8FDmhPcKsjm9", + // "QmP5ya9sCaMJDvMJKGXNAxfiS73DKm477nuvG6LwHBbQfz", + // "QmR9GRtE6JypMbQ8BD4bjAfETGuursZ5Q4GiTLppx5u7kc", + } + for i := 0; i < len(wantList); i++ { + _, err := n.getBlockData(wantList[i]) + if err != nil { + fmt.Println(err) } } } -// func (n *Node) getBlocks(ch chan<- bool) { -// defer func() { -// ch <- true -// }() - -// time.Sleep(time.Second * 30) - -// var wantList = []string{ -// // 256k -// //"QmXeE7NjTLviHDaf7ZvWhdTW2P43QY41DgxpeogQeBXnoZ", -// // 1M -// "QmRdTXKPV8VPCuPaawjJZZaACsDYRfVtZtNZTDLXrAQPx3", -// // 2M -// "QmU9TPr52YZPngKq3AG21FfFztgfsW3LQQKnUWvF73fZjT", -// // 3M -// "QmayoE5xG6tvgzTp2bZNErk7TSHW8ou4FKv2mamtGLNpMi", -// // 3.5M -// "QmTZciSbB99gqWZknzcZ1HFFtbpnnEz4LTpxQe7zxBmpTF", -// // 4M -// "Qmay44Rd1U2Gcg3WB2BgTCezP9LNqrTzeDUZt2LKb7HSFh", -// // 6M -// "QmadWN69GhfNWtuobno9v1XhXWjSfLkRXEdL32W4gaYunb", -// // 8M -// "QmRQyZGdTjS2bYy1BisteG41NtWFSWTjE3smueqQpNZady", -// // 10M -// "QmdoP3JcdjeWXFDUrsvL3rqKejZJrRaBEEUEHyKwW6JV96", -// // 12M -// "QmcKLqao5TcawyJdtDRFbHvMc1ktfepjpqXuu62o6ve1bx", -// // 6Bytes -// //"QmXsMQRrggM1pD2axFoscGSrANph6f1nsQm6oFm3AHzECy", -// // 16M -// // "QmcfxLnapUJbDXN68zTYQZX5eaCZ3nSqeG5PFwDCThRwnP", -// // "QmauZXKtvXaz7j3Kqq3wA2NURgmJTQL5o8T4N5YProYzt8", -// // "QmVLsR4VH3AwEMf9mvBhBGgcQcKLyjQdekJPKHXHWgVF4c", -// // "Qma9hBSf9FobnK8N3cLxUuGHtMZfd4b2a66jCVQtYe5CHr", -// // "QmdoJangkxFUQsMrvAsmN4vZVHUHqpREJfmbrSHk6XAsDm", -// // "QmQUnEaKsHNLteYcQJDNVaHHV243sq8uWU6pVpQUbrSq1P", -// // "QmWo6MkkYmKzToPzkXQu2aTS3vQj21fHQiFc3G3jNTzzWT", -// // "QmUoJN78mHiCETZv3oRLLYSPAKfgWWgGvw18kCfF5TFH3d", -// // "QmXPKrreCAhPMixUFQmCerAvNaMe2rDMqgzjaVrQTqocgZ", -// // "QmYgYQSPeQuGxuQutsAoikYWXA7wmakQuUcqcLNuMGV6Y7", -// // "QmfDcKHD6PuWfhZJPxaSBVCbmBzxnKzzNroUJoyncqL6Eq", -// // "QmT3Pe3wiWpGpcYu4ANuSizzB3463svxSTz1VaqT276vrs", -// // "QmV5TvFzDBfVmo8vyweVBvaNeHESdnUss81QEuKm5EeMdQ", -// // "QmX3yLqBH78MHNmYjSR27vwGidFH8XYZ96zAYAZLdSkZxj", -// // "QmZotvwGTFSaLaSueeLdWuvHgzpYY4d9g2L6ZdX2BpkE9V", -// // "QmaqYqCLiEWdXdsV2HJdsTqfEJGUgk6Tbv8FDmhPcKsjm9", -// // "QmP5ya9sCaMJDvMJKGXNAxfiS73DKm477nuvG6LwHBbQfz", -// // "QmR9GRtE6JypMbQ8BD4bjAfETGuursZ5Q4GiTLppx5u7kc", -// } -// for i := 0; i < len(wantList); i++ { -// _, err := n.getBlockData(wantList[i]) -// if err != nil { -// fmt.Println(err) -// } -// } -// } - -// func (n *Node) noticyBlocks(ch chan<- bool) { -// defer func() { -// ch <- true -// if err := recover(); err != nil { -// n.Pnc(utils.RecoverError(err)) -// } -// }() - -// //n.Discover("info", ">>>>> start discoverMgt <<<<<") -// fmt.Println("Start syncBlocks") -// var ok bool -// var blockMap = make(map[string]*blocks.BasicBlock, 0) - -// // // 1M -// // data_1M := make([]byte, 1024*1024) -// // var blockData1M = blocks.NewBlock(data_1M) -// // err := n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData1M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 1M data cid: ", blockData1M.Cid().String()) -// // blockMap[blockData1M.Cid().String()] = blockData1M - -// // // 2M -// // data_2M := make([]byte, 2*1024*1024) -// // var blockData2M = blocks.NewBlock(data_2M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData2M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 2M data cid: ", blockData2M.Cid().String()) -// // blockMap[blockData2M.Cid().String()] = blockData2M - -// // // 3M -// // data_3M := make([]byte, 3*1024*1024) -// // var blockData3M = blocks.NewBlock(data_3M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData3M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 3M data cid: ", blockData3M.Cid().String()) -// // blockMap[blockData3M.Cid().String()] = blockData3M - -// // // 3.5M -// // data_3_5M := make([]byte, 7*1024*1024/2) -// // var blockData3_5M = blocks.NewBlock(data_3_5M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData3_5M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 3.5M data cid: ", blockData3_5M.Cid().String()) -// // blockMap[blockData3_5M.Cid().String()] = blockData3_5M - -// // // 4M -// // data_4M := make([]byte, 4*1024*1024) -// // var blockData4M = blocks.NewBlock(data_4M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData4M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 4M data cid: ", blockData4M.Cid().String()) -// // blockMap[blockData4M.Cid().String()] = blockData4M - -// // // 6M -// // data_6M := make([]byte, 6*1024*1024) -// // var blockData6M = blocks.NewBlock(data_6M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData6M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 6M data cid: ", blockData6M.Cid().String()) -// // blockMap[blockData6M.Cid().String()] = blockData6M - -// // // 8M -// // data_8M := make([]byte, 8*1024*1024) -// // var blockData8M = blocks.NewBlock(data_8M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData8M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 8M data cid: ", blockData8M.Cid().String()) -// // blockMap[blockData8M.Cid().String()] = blockData8M - -// // // 10M -// // data_10M := make([]byte, 10*1024*1024) -// // var blockData10M = blocks.NewBlock(data_10M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData10M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 10M data cid: ", blockData10M.Cid().String()) -// // blockMap[blockData10M.Cid().String()] = blockData10M - -// // // 12M -// // data_12M := make([]byte, 12*1024*1024) -// // var blockData12M = blocks.NewBlock(data_12M) -// // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData12M) -// // if err != nil { -// // fmt.Println("[Put] err: ", err) -// // return -// // } -// // fmt.Println(">>> gen a new 12M data cid: ", blockData12M.Cid().String()) -// // blockMap[blockData12M.Cid().String()] = blockData12M - -// for { -// blockdirs, err := utils.DirDirs(filepath.Join(n.Workspace(), core.FileBlockDir), 0) -// if err != nil { -// fmt.Println("[noticyBlocks.DirDirs] err: ", err) -// return -// } - -// for i := 0; i < len(blockdirs); i++ { -// datadir := filepath.Join(blockdirs[i], ".data") -// fmt.Println("[datadir]: ", datadir) -// hash, err := sutils.CalcPathSHA256(datadir) -// if err != nil { -// fmt.Println("[CalcPathSHA256] err: ", err) -// continue -// } -// mycid, err := n.FidToCid(hash) -// if err != nil { -// fmt.Println("[FidToCid] err: ", err) -// continue -// } - -// fmt.Println("Local cid: ", mycid) - -// buf, err := n.GetLocalDataFromBlock(mycid) -// if err != nil { -// fmt.Println("[GetDataFromBlock] err: ", err) -// continue -// } -// var blockData = blocks.NewBlock(buf) - -// err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData) -// if err != nil { -// fmt.Println("[Put] err: ", err) -// continue -// } - -// _, ok = blockMap[mycid] -// if !ok { -// blockMap[mycid] = blockData -// } -// } - -// if len(blockdirs) == 0 { -// fmt.Println("----Local block data is empty------") -// time.Sleep(time.Minute) -// return -// } - -// count := 0 -// for count <= 10 { -// for k, v := range blockMap { -// err = n.GetBitSwap().NotifyNewBlocks(n.GetCtxQueryFromCtxCancel(), v) -// if err != nil { -// fmt.Println("[NotifyNewBlocks] ", k, " err: ", err) -// } else { -// fmt.Println("[NotifyNewBlocks] ", k, " suc") -// } -// count++ -// } -// time.Sleep(time.Second * 10) -// } -// } -// } - -// func (n *Node) getBlockData(wantcid string) ([]byte, error) { -// fmt.Println("Will GetDataFromBlock ", wantcid) -// acid, err := cid.Decode(wantcid) -// if err != nil { -// fmt.Println("[cid.Decode] err: ", err) -// return nil, err -// } -// ctxTout, cancelFunc := context.WithTimeout(context.Background(), time.Minute*5) -// defer cancelFunc() -// buf, err := n.GetBitSwap().GetBlock(ctxTout, acid) -// if err != nil { -// fmt.Println("GetBlock ", wantcid, " err: ", err) -// return nil, err -// } -// fmt.Println("GetBlock ", wantcid, " suc") -// return buf.RawData(), nil -// } +func (n *Node) noticyBlocks(ch chan<- bool) { + defer func() { + ch <- true + if err := recover(); err != nil { + n.Pnc(utils.RecoverError(err)) + } + }() + + //n.Discover("info", ">>>>> start discoverMgt <<<<<") + fmt.Println("Start syncBlocks") + var ok bool + var blockMap = make(map[string]*blocks.BasicBlock, 0) + + // 1M + data_1M := make([]byte, 1024*1024) + var blockData1M = blocks.NewBlock(data_1M) + err := n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData1M) + if err != nil { + fmt.Println("[Put] err: ", err) + return + } + fmt.Println(">>> gen a new 1M data cid: ", blockData1M.Cid().String()) + blockMap[blockData1M.Cid().String()] = blockData1M + + // // 2M + // data_2M := make([]byte, 2*1024*1024) + // var blockData2M = blocks.NewBlock(data_2M) + // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData2M) + // if err != nil { + // fmt.Println("[Put] err: ", err) + // return + // } + // fmt.Println(">>> gen a new 2M data cid: ", blockData2M.Cid().String()) + // blockMap[blockData2M.Cid().String()] = blockData2M + + // // 3M + // data_3M := make([]byte, 3*1024*1024) + // var blockData3M = blocks.NewBlock(data_3M) + // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData3M) + // if err != nil { + // fmt.Println("[Put] err: ", err) + // return + // } + // fmt.Println(">>> gen a new 3M data cid: ", blockData3M.Cid().String()) + // blockMap[blockData3M.Cid().String()] = blockData3M + + // // 3.5M + // data_3_5M := make([]byte, 7*1024*1024/2) + // var blockData3_5M = blocks.NewBlock(data_3_5M) + // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData3_5M) + // if err != nil { + // fmt.Println("[Put] err: ", err) + // return + // } + // fmt.Println(">>> gen a new 3.5M data cid: ", blockData3_5M.Cid().String()) + // blockMap[blockData3_5M.Cid().String()] = blockData3_5M + + // 4M + data_4M := make([]byte, 4*1024*1024) + var blockData4M = blocks.NewBlock(data_4M) + err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData4M) + if err != nil { + fmt.Println("[Put] err: ", err) + return + } + fmt.Println(">>> gen a new 4M data cid: ", blockData4M.Cid().String()) + blockMap[blockData4M.Cid().String()] = blockData4M + + // // 6M + // data_6M := make([]byte, 6*1024*1024) + // var blockData6M = blocks.NewBlock(data_6M) + // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData6M) + // if err != nil { + // fmt.Println("[Put] err: ", err) + // return + // } + // fmt.Println(">>> gen a new 6M data cid: ", blockData6M.Cid().String()) + // blockMap[blockData6M.Cid().String()] = blockData6M + + // // 8M + // data_8M := make([]byte, 8*1024*1024) + // var blockData8M = blocks.NewBlock(data_8M) + // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData8M) + // if err != nil { + // fmt.Println("[Put] err: ", err) + // return + // } + // fmt.Println(">>> gen a new 8M data cid: ", blockData8M.Cid().String()) + // blockMap[blockData8M.Cid().String()] = blockData8M + + // 10M + data_10M := make([]byte, 10*1024*1024) + var blockData10M = blocks.NewBlock(data_10M) + err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData10M) + if err != nil { + fmt.Println("[Put] err: ", err) + return + } + fmt.Println(">>> gen a new 10M data cid: ", blockData10M.Cid().String()) + blockMap[blockData10M.Cid().String()] = blockData10M + + // // 12M + // data_12M := make([]byte, 12*1024*1024) + // var blockData12M = blocks.NewBlock(data_12M) + // err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData12M) + // if err != nil { + // fmt.Println("[Put] err: ", err) + // return + // } + // fmt.Println(">>> gen a new 12M data cid: ", blockData12M.Cid().String()) + // blockMap[blockData12M.Cid().String()] = blockData12M + + for { + blockdirs, err := utils.DirDirs(filepath.Join(n.Workspace(), core.FileBlockDir), 0) + if err != nil { + fmt.Println("[noticyBlocks.DirDirs] err: ", err) + return + } + + for i := 0; i < len(blockdirs); i++ { + datadir := filepath.Join(blockdirs[i], ".data") + fmt.Println("[datadir]: ", datadir) + hash, err := sutils.CalcPathSHA256(datadir) + if err != nil { + fmt.Println("[CalcPathSHA256] err: ", err) + continue + } + mycid, err := n.FidToCid(hash) + if err != nil { + fmt.Println("[FidToCid] err: ", err) + continue + } + + fmt.Println("Local cid: ", mycid) + + buf, err := n.GetLocalDataFromBlock(mycid) + if err != nil { + fmt.Println("[GetDataFromBlock] err: ", err) + continue + } + var blockData = blocks.NewBlock(buf) + + err = n.GetBlockstore().Put(n.GetCtxQueryFromCtxCancel(), blockData) + if err != nil { + fmt.Println("[Put] err: ", err) + continue + } + + _, ok = blockMap[mycid] + if !ok { + blockMap[mycid] = blockData + } + } + + if len(blockdirs) == 0 { + fmt.Println("----Local block data is empty------") + time.Sleep(time.Minute) + return + } + + count := 0 + for count <= 10 { + for k, v := range blockMap { + err = n.GetBitSwap().NotifyNewBlocks(n.GetCtxQueryFromCtxCancel(), v) + if err != nil { + fmt.Println("[NotifyNewBlocks] ", k, " err: ", err) + } else { + fmt.Println("[NotifyNewBlocks] ", k, " suc") + } + count++ + } + time.Sleep(time.Second * 10) + } + } +} + +func (n *Node) getBlockData(wantcid string) ([]byte, error) { + fmt.Println("Will GetDataFromBlock ", wantcid) + acid, err := cid.Decode(wantcid) + if err != nil { + fmt.Println("[cid.Decode] err: ", err) + return nil, err + } + ctxTout, cancelFunc := context.WithTimeout(context.Background(), time.Minute) + defer cancelFunc() + buf, err := n.GetBitSwap().GetBlock(ctxTout, acid) + if err != nil { + fmt.Println("GetBlock ", wantcid, " err: ", err) + return nil, err + } + fmt.Println("GetBlock ", wantcid, " suc") + return buf.RawData(), nil +}