| 1 | package httprouting |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/ipfs/boxo/ipns" |
| 10 | "github.com/ipfs/boxo/routing/http/server" |
| 11 | "github.com/ipfs/boxo/routing/http/types" |
| 12 | "github.com/ipfs/boxo/routing/http/types/iter" |
| 13 | "github.com/ipfs/go-cid" |
| 14 | "github.com/libp2p/go-libp2p/core/peer" |
| 15 | "github.com/libp2p/go-libp2p/core/routing" |
| 16 | ) |
| 17 | |
| 18 | // MockHTTPContentRouter provides /routing/v1 |
| 19 | // (https://specs.ipfs.tech/routing/http-routing-v1/) server implementation |
| 20 | // based on github.com/ipfs/boxo/routing/http/server |
| 21 | type MockHTTPContentRouter struct { |
| 22 | m sync.Mutex |
| 23 | provideBitswapCalls int |
| 24 | findProvidersCalls int |
| 25 | findPeersCalls int |
| 26 | getClosestPeersCalls int |
| 27 | providers map[cid.Cid][]types.Record |
| 28 | peers map[peer.ID][]*types.PeerRecord |
| 29 | Debug bool |
| 30 | } |
| 31 | |
| 32 | func (r *MockHTTPContentRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) { |
| 33 | if r.Debug { |
| 34 | fmt.Printf("MockHTTPContentRouter.FindProviders(%s)\n", key.String()) |
| 35 | } |
| 36 | r.m.Lock() |
| 37 | defer r.m.Unlock() |
| 38 | r.findProvidersCalls++ |
| 39 | if r.providers == nil { |
| 40 | r.providers = make(map[cid.Cid][]types.Record) |
| 41 | } |
| 42 | records, found := r.providers[key] |
| 43 | if !found { |
| 44 | return iter.FromSlice([]iter.Result[types.Record]{}), nil |
| 45 | } |
| 46 | results := make([]iter.Result[types.Record], len(records)) |
| 47 | for i, rec := range records { |
| 48 | results[i] = iter.Result[types.Record]{Val: rec} |
| 49 | if r.Debug { |
| 50 | fmt.Printf("MockHTTPContentRouter.FindProviders(%s) result: %+v\n", key.String(), rec) |
| 51 | } |
| 52 | } |
| 53 | return iter.FromSlice(results), nil |
| 54 | } |
| 55 | |
| 56 | // nolint deprecated |
| 57 | func (r *MockHTTPContentRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) { |
| 58 | r.m.Lock() |
| 59 | defer r.m.Unlock() |
| 60 | r.provideBitswapCalls++ |
| 61 | return 0, nil |
| 62 | } |
| 63 | |
| 64 | func (r *MockHTTPContentRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) { |
| 65 | r.m.Lock() |
| 66 | defer r.m.Unlock() |
| 67 | r.findPeersCalls++ |
| 68 | |
| 69 | if r.peers == nil { |
| 70 | r.peers = make(map[peer.ID][]*types.PeerRecord) |
| 71 | } |
| 72 | records, found := r.peers[pid] |
| 73 | if !found { |
| 74 | return iter.FromSlice([]iter.Result[*types.PeerRecord]{}), nil |
| 75 | } |
| 76 | |
| 77 | results := make([]iter.Result[*types.PeerRecord], len(records)) |
| 78 | for i, rec := range records { |
| 79 | results[i] = iter.Result[*types.PeerRecord]{Val: rec} |
| 80 | if r.Debug { |
| 81 | fmt.Printf("MockHTTPContentRouter.FindPeers(%s) result: %+v\n", pid.String(), rec) |
| 82 | } |
| 83 | } |
| 84 | return iter.FromSlice(results), nil |
| 85 | } |
| 86 | |
| 87 | func (r *MockHTTPContentRouter) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) { |
| 88 | return nil, routing.ErrNotSupported |
| 89 | } |
| 90 | |
| 91 | func (r *MockHTTPContentRouter) PutIPNS(ctx context.Context, name ipns.Name, rec *ipns.Record) error { |
| 92 | return routing.ErrNotSupported |
| 93 | } |
| 94 | |
| 95 | func (r *MockHTTPContentRouter) NumFindProvidersCalls() int { |
| 96 | r.m.Lock() |
| 97 | defer r.m.Unlock() |
| 98 | return r.findProvidersCalls |
| 99 | } |
| 100 | |
| 101 | // AddProvider adds a record for a given CID |
| 102 | func (r *MockHTTPContentRouter) AddProvider(key cid.Cid, record types.Record) { |
| 103 | r.m.Lock() |
| 104 | defer r.m.Unlock() |
| 105 | if r.providers == nil { |
| 106 | r.providers = make(map[cid.Cid][]types.Record) |
| 107 | } |
| 108 | r.providers[key] = append(r.providers[key], record) |
| 109 | |
| 110 | peerRecord, ok := record.(*types.PeerRecord) |
| 111 | if ok { |
| 112 | if r.peers == nil { |
| 113 | r.peers = make(map[peer.ID][]*types.PeerRecord) |
| 114 | } |
| 115 | pid := peerRecord.ID |
| 116 | r.peers[*pid] = append(r.peers[*pid], peerRecord) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func (r *MockHTTPContentRouter) GetClosestPeers(ctx context.Context, key cid.Cid) (iter.ResultIter[*types.PeerRecord], error) { |
| 121 | r.m.Lock() |
| 122 | defer r.m.Unlock() |
| 123 | r.getClosestPeersCalls++ |
| 124 | |
| 125 | if r.peers == nil { |
| 126 | r.peers = make(map[peer.ID][]*types.PeerRecord) |
| 127 | } |
| 128 | pid, err := peer.FromCid(key) |
| 129 | if err != nil { |
| 130 | return iter.FromSlice([]iter.Result[*types.PeerRecord]{}), nil |
| 131 | } |
| 132 | records, found := r.peers[pid] |
| 133 | if !found { |
| 134 | return iter.FromSlice([]iter.Result[*types.PeerRecord]{}), nil |
| 135 | } |
| 136 | |
| 137 | results := make([]iter.Result[*types.PeerRecord], len(records)) |
| 138 | for i, rec := range records { |
| 139 | results[i] = iter.Result[*types.PeerRecord]{Val: rec} |
| 140 | if r.Debug { |
| 141 | fmt.Printf("MockHTTPContentRouter.GetPeers(%s) result: %+v\n", pid.String(), rec) |
| 142 | } |
| 143 | } |
| 144 | return iter.FromSlice(results), nil |
| 145 | } |