Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sidecar
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
sidecar
Commits
a4bfa5bc
Commit
a4bfa5bc
authored
Sep 07, 2021
by
suyanlong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add HandleIBTPX and getRemotePeerInfo function
parent
a2e0aa28
Pipeline
#7952
canceled with stages
Changes
7
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
99 additions
and
26 deletions
+99
-26
appchain.go
internal/appchain/appchain.go
+1
-1
handler.go
internal/peermgr/handler.go
+0
-2
local.go
internal/peermgr/local.go
+14
-0
swarm.go
internal/peermgr/swarm.go
+75
-22
init.go
internal/router/init.go
+1
-1
message.pb.go
model/pb/message.pb.go
+0
-0
message.proto
model/pb/message.proto
+8
-0
No files found.
internal/appchain/appchain.go
View file @
a4bfa5bc
...
@@ -3,13 +3,13 @@ package appchain
...
@@ -3,13 +3,13 @@ package appchain
import
(
import
(
"context"
"context"
"fmt"
"fmt"
"github.com/link33/sidecar/internal/port"
"strconv"
"strconv"
"strings"
"strings"
"time"
"time"
"github.com/Rican7/retry"
"github.com/Rican7/retry"
"github.com/Rican7/retry/strategy"
"github.com/Rican7/retry/strategy"
"github.com/link33/sidecar/internal/port"
"github.com/link33/sidecar/internal/txcrypto"
"github.com/link33/sidecar/internal/txcrypto"
"github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/pkg/plugins"
"github.com/link33/sidecar/pkg/plugins"
...
...
internal/peermgr/handler.go
View file @
a4bfa5bc
...
@@ -32,8 +32,6 @@ func (swarm *Swarm) RegisterMsgHandler(messageType pb.Message_Type, handler Mess
...
@@ -32,8 +32,6 @@ func (swarm *Swarm) RegisterMsgHandler(messageType pb.Message_Type, handler Mess
func
(
swarm
*
Swarm
)
RegisterConnectHandler
(
handler
ConnectHandler
)
error
{
func
(
swarm
*
Swarm
)
RegisterConnectHandler
(
handler
ConnectHandler
)
error
{
swarm
.
lock
.
Lock
()
swarm
.
lock
.
Lock
()
defer
swarm
.
lock
.
Unlock
()
defer
swarm
.
lock
.
Unlock
()
swarm
.
connectHandlers
=
append
(
swarm
.
connectHandlers
,
handler
)
swarm
.
connectHandlers
=
append
(
swarm
.
connectHandlers
,
handler
)
return
nil
return
nil
}
}
internal/peermgr/local.go
View file @
a4bfa5bc
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"github.com/link33/sidecar/internal/router"
"github.com/link33/sidecar/internal/router"
"github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/model/pb"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/sirupsen/logrus"
)
)
type
local
struct
{
type
local
struct
{
...
@@ -14,6 +15,7 @@ type local struct {
...
@@ -14,6 +15,7 @@ type local struct {
tag
string
tag
string
rev
chan
*
pb
.
Message
rev
chan
*
pb
.
Message
rout
router
.
Router
rout
router
.
Router
logger
logrus
.
FieldLogger
}
}
func
(
l
*
local
)
ID
()
string
{
func
(
l
*
local
)
ID
()
string
{
...
@@ -58,3 +60,15 @@ func (l *local) ListenIBTPX() <-chan *pb.Message {
...
@@ -58,3 +60,15 @@ func (l *local) ListenIBTPX() <-chan *pb.Message {
}
}
// sidecar 节点
// sidecar 节点
func
(
l
*
local
)
HandleGetPeerInfoMessage
(
p
port
.
Port
,
message
*
pb
.
Message
)
{
data
:=
&
pb
.
PeerInfo
{
ID
:
l
.
ID
(),
Tag
:
l
.
Tag
(),
}
msg
,
_
:=
data
.
Marshal
()
retMsg
:=
Message
(
pb
.
Message_ACK
,
true
,
msg
)
err
:=
p
.
AsyncSend
(
retMsg
)
if
err
!=
nil
{
l
.
logger
.
Error
(
err
)
}
}
internal/peermgr/swarm.go
View file @
a4bfa5bc
...
@@ -107,6 +107,19 @@ func (swarm *Swarm) Start() error {
...
@@ -107,6 +107,19 @@ func (swarm *Swarm) Start() error {
return
fmt
.
Errorf
(
"register get address msg handler: %w"
,
err
)
return
fmt
.
Errorf
(
"register get address msg handler: %w"
,
err
)
}
}
if
err
:=
swarm
.
RegisterMsgHandler
(
pb
.
Message_PEER_INFO_GET
,
l
.
HandleGetPeerInfoMessage
);
err
!=
nil
{
return
fmt
.
Errorf
(
"register get peer info msg handler: %w"
,
err
)
}
if
err
:=
swarm
.
RegisterMultiMsgHandler
([]
pb
.
Message_Type
{
pb
.
Message_IBTP_SEND
,
pb
.
Message_IBTP_GET
,
pb
.
Message_IBTP_RECEIPT_SEND
,
pb
.
Message_IBTP_RECEIPT_GET
,
},
swarm
.
HandleIBTPX
);
err
!=
nil
{
return
fmt
.
Errorf
(
"register handle IBTPX msg handler: %w"
,
err
)
}
if
err
:=
swarm
.
p2p
.
Start
();
err
!=
nil
{
if
err
:=
swarm
.
p2p
.
Start
();
err
!=
nil
{
return
fmt
.
Errorf
(
"p2p module start: %w"
,
err
)
return
fmt
.
Errorf
(
"p2p module start: %w"
,
err
)
}
}
...
@@ -181,30 +194,51 @@ func (swarm *Swarm) handleMessage(s network.Stream, data []byte) {
...
@@ -181,30 +194,51 @@ func (swarm *Swarm) handleMessage(s network.Stream, data []byte) {
return
return
}
}
t
:=
m
.
Type
handler
,
ok
:=
swarm
.
msgHandlers
.
Load
(
m
.
Type
)
switch
{
if
!
ok
{
// 接收其它sidecar节点发过来的交易、请求等。主要是IBTP结构相关数据。
swarm
.
logger
.
WithFields
(
logrus
.
Fields
{
case
t
==
pb
.
Message_IBTP_SEND
||
t
==
pb
.
Message_IBTP_GET
||
t
==
pb
.
Message_IBTP_RECEIPT_SEND
||
t
==
pb
.
Message_IBTP_RECEIPT_GET
:
"error"
:
fmt
.
Errorf
(
"can't handle msg[type: %v]"
,
m
.
Type
),
p
,
is
:=
swarm
.
router
.
Load
(
s
.
RemotePeerID
())
"type"
:
m
.
Type
.
String
(),
if
is
{
})
.
Error
(
"Handle message"
)
ps
,
iss
:=
p
.
(
*
sidecar
)
return
if
iss
{
}
ps
.
rev
<-
m
return
msgHandler
,
ok
:=
handler
.
(
MessageHandler
)
}
if
!
ok
{
swarm
.
logger
.
WithFields
(
logrus
.
Fields
{
"error"
:
fmt
.
Errorf
(
"invalid handler for msg [type: %v]"
,
m
.
Type
),
"type"
:
m
.
Type
.
String
(),
})
.
Error
(
"Handle message"
)
return
}
rec
:=
make
(
chan
*
pb
.
Message
)
p
:=
&
sidecar
{
id
:
s
.
RemotePeerID
(),
swarm
:
swarm
,
tag
:
""
,
rev
:
rec
,
}
msgHandler
(
p
,
m
)
}
// 接收其它sidecar节点发过来的交易、请求等。主要是IBTP结构相关数据。
func
(
swarm
*
Swarm
)
HandleIBTPX
(
pt
port
.
Port
,
m
*
pb
.
Message
)
{
p
,
is
:=
swarm
.
router
.
Load
(
pt
.
ID
())
if
is
{
ps
,
iss
:=
p
.
(
*
sidecar
)
if
iss
{
ps
.
rev
<-
m
}
}
addr
,
_
:=
peer
.
AddrInfoFromP2pAddr
(
s
.
RemotePeerAddr
())
}
else
{
rec
:=
make
(
chan
*
pb
.
Message
)
ppt
:=
pt
.
(
*
sidecar
)
newPort
:=
&
sidecar
{
info
,
err
:=
swarm
.
getRemotePeerInfo
(
ppt
.
ID
())
id
:
addr
.
ID
.
String
(),
if
err
!=
nil
{
swarm
:
swarm
,
swarm
.
logger
.
Error
(
err
)
tag
:
""
,
rev
:
rec
,
}
}
swarm
.
router
.
Add
(
newPort
)
ppt
.
id
=
info
.
GetID
()
rec
<-
m
_
=
swarm
.
router
.
Add
(
ppt
)
default
:
//非IBTP结构相关数据,转发给local处理
ppt
.
rev
<-
m
//TODO
}
}
}
}
...
@@ -361,6 +395,25 @@ func (swarm *Swarm) getRemoteAddress(id peer.ID) (string, error) {
...
@@ -361,6 +395,25 @@ func (swarm *Swarm) getRemoteAddress(id peer.ID) (string, error) {
return
string
(
ret
.
Payload
.
Data
),
nil
return
string
(
ret
.
Payload
.
Data
),
nil
}
}
func
(
swarm
*
Swarm
)
getRemotePeerInfo
(
id
string
)
(
*
pb
.
PeerInfo
,
error
)
{
msg
:=
Message
(
pb
.
Message_PEER_INFO_GET
,
true
,
nil
)
reqData
,
err
:=
msg
.
Marshal
()
if
err
!=
nil
{
return
nil
,
err
}
retData
,
err
:=
swarm
.
p2p
.
Send
(
id
.
String
(),
reqData
)
//同步获取数据
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"sync send: %w"
,
err
)
}
ret
:=
&
pb
.
Message
{}
if
err
:=
ret
.
Unmarshal
(
retData
);
err
!=
nil
{
return
nil
,
err
}
info
:=
&
pb
.
PeerInfo
{}
err
=
info
.
Unmarshal
(
ret
.
Payload
.
Data
)
return
info
,
err
}
func
(
swarm
*
Swarm
)
FindProviders
(
id
string
)
(
string
,
error
)
{
func
(
swarm
*
Swarm
)
FindProviders
(
id
string
)
(
string
,
error
)
{
format
:=
cid
.
V0Builder
{}
format
:=
cid
.
V0Builder
{}
toCid
,
err
:=
format
.
Sum
([]
byte
(
id
))
toCid
,
err
:=
format
.
Sum
([]
byte
(
id
))
...
...
internal/router/init.go
View file @
a4bfa5bc
...
@@ -151,7 +151,7 @@ func (r *router) Route(msg *pb.Message) error {
...
@@ -151,7 +151,7 @@ func (r *router) Route(msg *pb.Message) error {
return
nil
return
nil
}
}
func
(
r
*
router
)
firstRoute
(
ibtp
*
pb
.
Message
)
{
func
(
r
*
router
)
firstRoute
(
msg
*
pb
.
Message
)
{
panic
(
"implement me"
)
panic
(
"implement me"
)
}
}
...
...
model/pb/message.pb.go
View file @
a4bfa5bc
This diff is collapsed.
Click to expand it.
model/pb/message.proto
View file @
a4bfa5bc
...
@@ -16,6 +16,7 @@ message Message {
...
@@ -16,6 +16,7 @@ message Message {
IBTP_SEND
=
8
;
IBTP_SEND
=
8
;
IBTP_RECEIPT_SEND
=
9
;
IBTP_RECEIPT_SEND
=
9
;
IBTP_RECEIPT_GET
=
10
;
IBTP_RECEIPT_GET
=
10
;
PEER_INFO_GET
=
11
;
}
}
Type
type
=
1
;
Type
type
=
1
;
Pack
payload
=
2
;
Pack
payload
=
2
;
...
@@ -26,3 +27,10 @@ message Pack {
...
@@ -26,3 +27,10 @@ message Pack {
bool
ok
=
1
;
bool
ok
=
1
;
bytes
data
=
2
;
bytes
data
=
2
;
}
}
message
PeerInfo
{
string
ID
=
1
;
string
Tag
=
2
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment