Skip to content

Commit

Permalink
feat: append /webrtc-direct if /quic-v1 present
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Aug 1, 2024
1 parent bf242c2 commit 7a69a88
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 175 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Tests

env:
GO: 1.18
GO: 1.21

on:
push:
Expand All @@ -14,12 +14,12 @@ jobs:
name: Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO }}

Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MIG_DIRS = $(shell ls -d fs-repo-*-to-*)
IGNORED_DIRS := $(shell cat ignored-migrations)
ACTIVE_DIRS := $(filter-out $(IGNORED_DIRS),$(MIG_DIRS))

.PHONY: all build clean cmd sharness test test_go test_13_to_14 test_14_to_15
.PHONY: all build clean cmd sharness test test_go test_14_to_15 test_15_to_16

all: build

Expand All @@ -26,7 +26,7 @@ fs-repo-migrations/fs-repo-migrations:
sharness:
make -C sharness

test: test_go sharness test_13_to_14 test_14_to_15
test: test_go sharness test_14_to_15 test_15_to_16

clean: $(subst fs-repo,clean.fs-repo,$(ACTIVE_DIRS))
@make -C sharness clean
Expand All @@ -52,3 +52,6 @@ test_13_to_14:

test_14_to_15:
@cd fs-repo-14-to-15/not-sharness && ./test.sh

test_15_to_16:
@cd fs-repo-15-to-16/test-e2e && ./test.sh
3 changes: 3 additions & 0 deletions fs-repo-15-to-16/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build:

clean:
go clean

test:
@cd test-e2e && ./test.sh
4 changes: 2 additions & 2 deletions fs-repo-15-to-16/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/ipfs/fs-repo-migrations/fs-repo-14-to-15
module github.com/ipfs/fs-repo-migrations/fs-repo-15-to-16

go 1.20
go 1.22

require github.com/ipfs/fs-repo-migrations/tools v0.0.0-20211209222258-754a2dcb82ea
68 changes: 6 additions & 62 deletions fs-repo-15-to-16/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"path/filepath"
"regexp"
"strings"

migrate "github.com/ipfs/fs-repo-migrations/tools/go-migrate"
mfsr "github.com/ipfs/fs-repo-migrations/tools/mfsr"
Expand Down Expand Up @@ -150,8 +149,7 @@ func (m Migration) Revert(opts migrate.Options) error {
return nil
}

var quicRegex = regexp.MustCompilePOSIX("/quic(/|$)")
var quicEnd = regexp.MustCompilePOSIX("/quic$")
var quicRegex = regexp.MustCompilePOSIX("/quic-v1$")

// convert converts the config from one version to another
func convert(in io.Reader, out io.Writer) error {
Expand All @@ -160,21 +158,7 @@ func convert(in io.Reader, out io.Writer) error {
return err
}

// Upgrade bootstrapper QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ from /quic to /quic-v1
if b, ok := confMap["Bootstrap"]; ok {
bootstrap, ok := b.([]interface{})
if !ok {
return fmt.Errorf("invalid type for .Bootstrap got %T expected json array", b)
}

for i, v := range bootstrap {
if v == "/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" {
bootstrap[i] = "/ip4/104.131.131.82/udp/4001/quic-v1/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
}
}
}

// Remove /quic only addresses from the .Addresses fields
// Append /webrtc-direct listener if /udp/../quic-v1 is present in any of .Addresses fields
if err := func() error {
a, ok := confMap["Addresses"]
if !ok {
Expand Down Expand Up @@ -202,19 +186,20 @@ func convert(in io.Reader, out io.Writer) error {
uniq := map[string]struct{}{}
for _, v := range swarm {
if addr, ok := v.(string); ok {

// if /quic-v1, add /webrtc-direct under the same port
if quicRegex.MatchString(addr) {
newAddr := quicEnd.ReplaceAllString(addr, "/quic-v1")
newAddr = strings.ReplaceAll(newAddr, "/quic/", "/quic-v1/")
newAddr := quicRegex.ReplaceAllString(addr, "/webrtc-direct")

if _, ok := uniq[newAddr]; ok {
continue
}
uniq[newAddr] = struct{}{}

newSwarm = append(newSwarm, newAddr)
continue
}

// keep original addr
if _, ok := uniq[addr]; ok {
continue
}
Expand All @@ -232,47 +217,6 @@ func convert(in io.Reader, out io.Writer) error {
return err
}

// Remove legacy Gateway.HTTPHeaders values that were hardcoded since years ago, but no longer necessary
// (but leave as-is if user made any changes)
// https://github.com/ipfs/kubo/issues/10005
if err := func() error {
a, ok := confMap["Gateway"]
if !ok {
return nil
}
addresses, ok := a.(map[string]any)
if !ok {
fmt.Printf("invalid type for .Gateway got %T expected json map; skipping .Gateway\n", a)
return nil
}

s, ok := addresses["HTTPHeaders"]
if !ok {
return nil
}
headers, ok := s.(map[string]any)
if !ok {
fmt.Printf("invalid type for .Gateway.HTTPHeaders got %T expected json map; skipping .Gateway.HTTPHeaders\n", s)
return nil
}

if acaos, ok := headers["Access-Control-Allow-Origin"].([]interface{}); ok && len(acaos) == 1 && acaos[0] == "*" {
delete(headers, "Access-Control-Allow-Origin")
}

if acams, ok := headers["Access-Control-Allow-Methods"].([]interface{}); ok && len(acams) == 1 && acams[0] == "GET" {
delete(headers, "Access-Control-Allow-Methods")
}
if acahs, ok := headers["Access-Control-Allow-Headers"].([]interface{}); ok && len(acahs) == 3 {
if acahs[0] == "X-Requested-With" && acahs[1] == "Range" && acahs[2] == "User-Agent" {
delete(headers, "Access-Control-Allow-Headers")
}
}
return nil
}(); err != nil {
return err
}

// Save new config
fixed, err := json.MarshalIndent(confMap, "", " ")
if err != nil {
Expand Down
30 changes: 1 addition & 29 deletions fs-repo-15-to-16/test-e2e/repo-v15/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,27 @@
"Announce": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
],
"AppendAnnounce": null,
"NoAnnounce": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
],
"Swarm": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
]
},
"Bootstrap": [
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb"
],
"Gateway": {
"HTTPHeaders": {
"Access-Control-Allow-Headers": [
"X-Requested-With",
"Range",
"User-Agent"
],
"Access-Control-Allow-Methods": [
"GET"
],
"Access-Control-Allow-Origin": [
"*"
]
}
}
}
2 changes: 1 addition & 1 deletion fs-repo-15-to-16/test-e2e/repo-v15/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14
15
17 changes: 6 additions & 11 deletions fs-repo-15-to-16/test-e2e/repo-v16/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,33 @@
"Announce": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/webrtc-direct",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/webrtc-direct",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
],
"AppendAnnounce": null,
"NoAnnounce": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/webrtc-direct",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/webrtc-direct",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
],
"Swarm": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/webrtc-direct",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/webrtc-direct",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
]
},
"Bootstrap": [
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.131.131.82/udp/4001/quic-v1/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb"
],
"Gateway": {
"HTTPHeaders": {}
}
}
57 changes: 0 additions & 57 deletions fs-repo-15-to-16/test-e2e/repo-v16/config.14-to-15.bak

This file was deleted.

29 changes: 29 additions & 0 deletions fs-repo-15-to-16/test-e2e/repo-v16/config.15-to-16.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"Addresses": {
"Announce": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
],
"AppendAnnounce": null,
"NoAnnounce": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
],
"Swarm": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1/webtransport"
]
}
}
2 changes: 1 addition & 1 deletion fs-repo-15-to-16/test-e2e/repo-v16/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15
16
15 changes: 8 additions & 7 deletions fs-repo-15-to-16/test-e2e/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

set -x

echo "Migration 14 to 15" &&
cp -r repotest-init repotest && # init repo
go run .. -verbose -path=repotest && # run forward migration
diff -r repotest-golden repotest && # check forward migration against golden
go run .. -verbose -revert -path=repotest && # run backward migration
diff -r repotest-init repotest # check that after backward migration everything is back to how it used to be
echo "Migration 15 to 16" &&
cp -r repo-v15 repo-test && # init repo
go run .. -verbose -path=repo-test && # run forward migration
diff -r repo-v16 repo-test && # check forward migration against expected state
echo "Revert 16 to 15" &&
go run .. -verbose -revert -path=repo-test && # run backward migration
diff -r repo-v15 repo-test # check that after backward migration everything is back to how it used to be

FINISH="$?" # save exit code

rm -r repotest # cleanup
rm -r repo-test # cleanup

exit "$FINISH" # forward exit code

0 comments on commit 7a69a88

Please sign in to comment.