mirror of
https://github.com/vacp2p/go-multiprotocol.git
synced 2026-01-09 14:57:56 -05:00
updated
This commit is contained in:
13
README.md
13
README.md
@@ -1,3 +1,14 @@
|
||||
# go-multiprotocol
|
||||
|
||||
A lot of this code has been adapted from [multiaddr](https://github.com/multiformats/go-multiaddr).
|
||||
A lot of this code has been adapted from [multiaddr](https://github.com/multiformats/go-multiaddr).
|
||||
|
||||
Multiprotocol can be initialized using a CSV file, for the CSV format see the [specification](https://github.com/vacp2p/multiprotocol).
|
||||
|
||||
```go
|
||||
err := Init("testdata/multiprotocol.csv")
|
||||
if err != nil {
|
||||
print(err)
|
||||
}
|
||||
```
|
||||
|
||||
Protocols can also be added programatically using the ```AddProtocol``` function.
|
||||
6
codec.go
6
codec.go
@@ -17,21 +17,21 @@ func stringToBytes(s string) ([]byte, error) {
|
||||
sp := strings.Split(s, "/")
|
||||
|
||||
if sp[0] != "" {
|
||||
return nil, fmt.Errorf("failed to parse multiaddr %q: must begin with /", s)
|
||||
return nil, fmt.Errorf("failed to parse multiprotocol %q: must begin with /", s)
|
||||
}
|
||||
|
||||
// consume first empty elem
|
||||
sp = sp[1:]
|
||||
|
||||
if len(sp) == 0 {
|
||||
return nil, fmt.Errorf("failed to parse multiaddr %q: empty multiaddr", s)
|
||||
return nil, fmt.Errorf("failed to parse multiprotocol %q: empty multiprotocol", s)
|
||||
}
|
||||
|
||||
for len(sp) > 0 {
|
||||
name := sp[0]
|
||||
p := ProtocolWithName(name)
|
||||
if p.Code == 0 {
|
||||
return nil, fmt.Errorf("failed to parse multiaddr %q: unknown protocol %s", s, sp[0])
|
||||
return nil, fmt.Errorf("failed to parse multiprotocol %q: unknown protocol %s", s, sp[0])
|
||||
}
|
||||
_, _ = b.Write(p.VCode)
|
||||
sp = sp[1:]
|
||||
|
||||
18
init.go
18
init.go
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
@@ -14,7 +15,7 @@ func Init(path string) error {
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
r := csv.NewReader(file)
|
||||
@@ -29,6 +30,10 @@ func Init(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if record[0] == "code" {
|
||||
continue
|
||||
}
|
||||
|
||||
p, err := protocol(record)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -44,26 +49,29 @@ func Init(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func protocol(strings []string) (*Protocol, error) {
|
||||
code, err := strconv.Atoi(strings[0])
|
||||
func protocol(vals []string) (*Protocol, error) {
|
||||
code, err := strconv.Atoi(strings.TrimSpace(vals[0]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
size, err := size(strings[1])
|
||||
size, err := size(vals[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Protocol{
|
||||
Name: strings[2],
|
||||
Name: strings.TrimSpace(vals[2]),
|
||||
Code: code,
|
||||
VCode: multiaddr.CodeToVarint(code),
|
||||
Size: size,
|
||||
Transcoder: multiaddr.TranscoderUnix,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func size(u string) (int, error) {
|
||||
u = strings.TrimSpace(u)
|
||||
|
||||
if u == "0" {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type multiprotocol struct {
|
||||
func NewMultiprotocol(s string) (p Multiprotocol, err error) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
log.Printf("Panic in NewMultiaddr on input %q: %s", s, e)
|
||||
log.Printf("Panic in NewMultiProtocol on input %q: %s", s, e)
|
||||
err = fmt.Errorf("%v", e)
|
||||
}
|
||||
}()
|
||||
|
||||
17
multiprotocol_test.go
Normal file
17
multiprotocol_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package multiprotocol
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
|
||||
err := Init("testdata/multiprotocol.csv")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %s", err.Error())
|
||||
}
|
||||
|
||||
if len(Protocols) != 4 {
|
||||
t.Errorf("unexpected amount of protocols parsed. expected: 4 got: %d", len(Protocols))
|
||||
}
|
||||
}
|
||||
17
protocol.go
17
protocol.go
@@ -45,23 +45,6 @@ var protocolsByCode = map[int]Protocol{}
|
||||
// Protocols is the list of multiaddr protocols supported by this module.
|
||||
var Protocols = []Protocol{}
|
||||
|
||||
// SwapToP2pMultiaddrs is a function to make the transition from /ipfs/...
|
||||
// multiaddrs to /p2p/... multiaddrs easier
|
||||
// The first stage of the rollout is to ship this package to all users so
|
||||
// that all users of multiaddr can parse both /ipfs/ and /p2p/ multiaddrs
|
||||
// as the same code (P_P2P). During this stage of the rollout, all addresses
|
||||
// with P_P2P will continue printing as /ipfs/, so that older clients without
|
||||
// the new parsing code won't break.
|
||||
// Once the network has adopted the new parsing code broadly enough, users of
|
||||
// multiaddr can add a call to this method to an init function in their codebase.
|
||||
// This will cause any P_P2P multiaddr to print out as /p2p/ instead of /ipfs/.
|
||||
// Note that the binary serialization of this multiaddr does not change at any
|
||||
// point. This means that this code is not a breaking network change at any point
|
||||
//
|
||||
// DEPRECATED: this is now the default
|
||||
func SwapToP2pMultiaddrs() {
|
||||
}
|
||||
|
||||
func AddProtocol(p Protocol) error {
|
||||
if _, ok := protocolsByName[p.Name]; ok {
|
||||
return fmt.Errorf("protocol by the name %q already exists", p.Name)
|
||||
|
||||
5
testdata/multiprotocol.csv
vendored
Normal file
5
testdata/multiprotocol.csv
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
code, size, name, comment
|
||||
42, 0, vac, namespace
|
||||
2, V, waku,
|
||||
3, V, store, a node will store messages.
|
||||
4, V, relay, a node will relay messages.
|
||||
|
Reference in New Issue
Block a user