-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist and load peers from separate database (#1935)
- Loading branch information
Showing
5 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package p2p | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/fxamacker/cbor/v2" | ||
"github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// EncodeAddrs encodes a slice of multiaddrs into a byte slice | ||
func EncodeAddrs(addrs []multiaddr.Multiaddr) ([]byte, error) { | ||
multiAddrBytes := make([][]byte, len(addrs)) | ||
for i, addr := range addrs { | ||
multiAddrBytes[i] = addr.Bytes() | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := cbor.NewEncoder(&buf).Encode(multiAddrBytes); err != nil { | ||
return nil, fmt.Errorf("encode addresses: %w", err) | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
// decodeAddrs decodes a byte slice into a slice of multiaddrs | ||
func decodeAddrs(b []byte) ([]multiaddr.Multiaddr, error) { | ||
var multiAddrBytes [][]byte | ||
if err := cbor.NewDecoder(bytes.NewReader(b)).Decode(&multiAddrBytes); err != nil { | ||
return nil, fmt.Errorf("decode addresses: %w", err) | ||
} | ||
|
||
addrs := make([]multiaddr.Multiaddr, 0, len(multiAddrBytes)) | ||
for _, addrBytes := range multiAddrBytes { | ||
addr, err := multiaddr.NewMultiaddrBytes(addrBytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse multiaddr: %w", err) | ||
} | ||
addrs = append(addrs, addr) | ||
} | ||
|
||
return addrs, nil | ||
} |