2022-07-04 04:49:07 +03:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
2023-10-24 16:51:30 +03:00
|
|
|
"crypto/rand"
|
2022-07-04 04:49:07 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testPeer is a simulated peer to allow testing direct network calls.
|
|
|
|
type testPeer struct {
|
|
|
|
*Peer
|
|
|
|
|
|
|
|
net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging
|
|
|
|
app *p2p.MsgPipeRW // Application layer reader/writer to simulate the local side
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestPeer creates a new peer registered at the given data backend.
|
|
|
|
func newTestPeer(name string, version uint, backend Backend) (*testPeer, <-chan error) {
|
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe()
|
|
|
|
|
|
|
|
// Start the peer on a new thread
|
|
|
|
var id enode.ID
|
|
|
|
rand.Read(id[:])
|
|
|
|
|
|
|
|
peer := NewPeer(version, p2p.NewPeer(id, name, nil), net)
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
errc <- backend.RunPeer(peer, func(peer *Peer) error {
|
|
|
|
return Handle(backend, peer)
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
return &testPeer{app: app, net: net, Peer: peer}, errc
|
|
|
|
}
|
|
|
|
|
|
|
|
// close terminates the local side of the peer, notifying the remote protocol
|
|
|
|
// manager of termination.
|
|
|
|
func (p *testPeer) close() {
|
|
|
|
p.Peer.Close()
|
|
|
|
p.app.Close()
|
|
|
|
}
|