Skip to content

Commit

Permalink
Merge dev -> master (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl authored Mar 12, 2024
2 parents 0209ea5 + 730e5c3 commit 220a570
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- dev
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
- 'v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+'
- 'v[0-9]+.[0-9]+.[0-9]+-**'

concurrency:
group: ${{ github.workflow }}
Expand Down
14 changes: 5 additions & 9 deletions autopilot/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,18 @@ func (a *accounts) refillWorkerAccounts(ctx context.Context, w Worker) {

// refill accounts in separate goroutines
for _, c := range contracts {
// add logging for contracts in the set
_, inSet := inContractSet[c.ID]

// launch refill if not already in progress
if a.markRefillInProgress(workerID, c.HostKey) {
go func(contract api.ContractMetadata, inSet bool) {
go func(contract api.ContractMetadata) {
rCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
accountID, refilled, rerr := refillWorkerAccount(rCtx, a.a, w, workerID, contract)
if rerr != nil {
if inSet || rerr.Is(errMaxDriftExceeded) {
// register the alert on failure if the contract is in
// the set or the error is errMaxDriftExceeded
if rerr.Is(errMaxDriftExceeded) {
// register the alert if error is errMaxDriftExceeded
a.ap.RegisterAlert(ctx, newAccountRefillAlert(accountID, contract, *rerr))
a.l.Errorw(rerr.err.Error(), rerr.keysAndValues...)
}
a.l.Errorw(rerr.err.Error(), rerr.keysAndValues...)
} else {
// dismiss alerts on success
a.ap.DismissAlert(ctx, alertIDForAccount(alertAccountRefillID, accountID))
Expand All @@ -167,7 +163,7 @@ func (a *accounts) refillWorkerAccounts(ctx context.Context, w Worker) {
}

a.markRefillDone(workerID, contract.HostKey)
}(c, inSet)
}(c)
}
}
}
Expand Down
155 changes: 155 additions & 0 deletions autopilot/ipfilter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package autopilot

import (
"context"
"errors"
"net"
"testing"
"time"

"go.sia.tech/core/types"
"go.uber.org/zap"
)

var (
ipv4Localhost = net.IP{127, 0, 0, 1}
ipv6Localhost = net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
)

type testResolver struct {
addr map[string][]net.IPAddr
err error
}

func (r *testResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
// return error if set
if err := r.err; err != nil {
r.err = nil
return nil, err
}
// return IP addr if set
if addrs, ok := r.addr[host]; ok {
return addrs, nil
}
return nil, nil
}

func (r *testResolver) setNextErr(err error) { r.err = err }
func (r *testResolver) setAddr(host string, addrs []net.IPAddr) { r.addr[host] = addrs }

func newTestResolver() *testResolver {
return &testResolver{addr: make(map[string][]net.IPAddr)}
}

func newTestIPResolver(r resolver) *ipResolver {
ipr := newIPResolver(context.Background(), time.Minute, zap.NewNop().Sugar())
ipr.resolver = r
return ipr
}

func newTestIPFilter(r resolver) *ipFilter {
return &ipFilter{
subnetToHostKey: make(map[string]string),
resolver: newTestIPResolver(r),
logger: zap.NewNop().Sugar(),
}
}

func TestIPResolver(t *testing.T) {
r := newTestResolver()
ipr := newTestIPResolver(r)

// test lookup error
r.setNextErr(errors.New("unknown error"))
if _, err := ipr.lookup("example.com:1234"); !isErr(err, errors.New("unknown error")) {
t.Fatal("unexpected error", err)
}

// test IO timeout - no cache entry
r.setNextErr(errIOTimeout)
if _, err := ipr.lookup("example.com:1234"); !isErr(err, errIOTimeout) {
t.Fatal("unexpected error", err)
}

// test IO timeout - expired cache entry
ipr.cache["example.com:1234"] = ipCacheEntry{subnets: []string{"a"}}
r.setNextErr(errIOTimeout)
if _, err := ipr.lookup("example.com:1234"); !isErr(err, errIOTimeout) {
t.Fatal("unexpected error", err)
}

// test IO timeout - live cache entry
ipr.cache["example.com:1234"] = ipCacheEntry{created: time.Now(), subnets: []string{"a"}}
r.setNextErr(errIOTimeout)
if subnets, err := ipr.lookup("example.com:1234"); err != nil {
t.Fatal("unexpected error", err)
} else if len(subnets) != 1 || subnets[0] != "a" {
t.Fatal("unexpected subnets", subnets)
}

// test too many addresses - more than two
r.setAddr("example.com", []net.IPAddr{{}, {}, {}})
if _, err := ipr.lookup("example.com:1234"); !isErr(err, errTooManyAddresses) {
t.Fatal("unexpected error", err)
}

// test too many addresses - two of the same type
r.setAddr("example.com", []net.IPAddr{{IP: net.IPv4(1, 2, 3, 4)}, {IP: net.IPv4(1, 2, 3, 4)}})
if _, err := ipr.lookup("example.com:1234"); !isErr(err, errTooManyAddresses) {
t.Fatal("unexpected error", err)
}

// test invalid addresses
r.setAddr("example.com", []net.IPAddr{{IP: ipv4Localhost}, {IP: net.IP{127, 0, 0, 2}}})
if _, err := ipr.lookup("example.com:1234"); !isErr(err, errTooManyAddresses) {
t.Fatal("unexpected error", err)
}

// test valid addresses
r.setAddr("example.com", []net.IPAddr{{IP: ipv4Localhost}, {IP: ipv6Localhost}})
if subnets, err := ipr.lookup("example.com:1234"); err != nil {
t.Fatal("unexpected error", err)
} else if len(subnets) != 2 || subnets[0] != "127.0.0.0/24" || subnets[1] != "::/32" {
t.Fatal("unexpected subnets", subnets)
}
}

func TestIPFilter(t *testing.T) {
r := newTestResolver()
r.setAddr("host1.com", []net.IPAddr{{IP: net.IP{192, 168, 0, 1}}})
r.setAddr("host2.com", []net.IPAddr{{IP: net.IP{192, 168, 1, 1}}})
r.setAddr("host3.com", []net.IPAddr{{IP: net.IP{192, 168, 2, 1}}})
ipf := newTestIPFilter(r)

// add 3 hosts - unique IPs
r1 := ipf.IsRedundantIP("host1.com:1234", types.PublicKey{1})
r2 := ipf.IsRedundantIP("host2.com:1234", types.PublicKey{2})
r3 := ipf.IsRedundantIP("host3.com:1234", types.PublicKey{3})
if r1 || r2 || r3 {
t.Fatal("unexpected result", r1, r2, r3)
}

// try add 4th host - redundant IP
r.setAddr("host4.com", []net.IPAddr{{IP: net.IP{192, 168, 0, 12}}})
if redundant := ipf.IsRedundantIP("host4.com:1234", types.PublicKey{4}); !redundant {
t.Fatal("unexpected result", redundant)
}

// add 4th host - unique IP - 2 subnets
r.setAddr("host4.com", []net.IPAddr{{IP: net.IP{192, 168, 3, 1}}, {IP: net.ParseIP("2001:0db8:85a3::8a2e:0370:7334")}})
if redundant := ipf.IsRedundantIP("host4.com:1234", types.PublicKey{4}); redundant {
t.Fatal("unexpected result", redundant)
}

// try add 5th host - redundant IP based on the IPv6 subnet from host4
r.setAddr("host5.com", []net.IPAddr{{IP: net.ParseIP("2001:0db8:85b3::8a2e:0370:7335")}})
if redundant := ipf.IsRedundantIP("host5.com:1234", types.PublicKey{5}); !redundant {
t.Fatal("unexpected result", redundant)
}

// add 5th host - unique IP
r.setAddr("host5.com", []net.IPAddr{{IP: net.ParseIP("2001:0db9:85b3::8a2e:0370:7335")}})
if redundant := ipf.IsRedundantIP("host5.com:1234", types.PublicKey{5}); redundant {
t.Fatal("unexpected result", redundant)
}
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/gotd/contrib v0.19.0
github.com/klauspost/reedsolomon v1.12.1
github.com/minio/minio-go/v7 v7.0.68
github.com/minio/minio-go/v7 v7.0.69
github.com/montanaflynn/stats v0.7.1
gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe
go.sia.tech/core v0.2.1
Expand All @@ -20,8 +20,8 @@ require (
go.sia.tech/siad v1.5.10-0.20230228235644-3059c0b930ca
go.sia.tech/web/renterd v0.49.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.20.0
golang.org/x/term v0.17.0
golang.org/x/crypto v0.21.0
golang.org/x/term v0.18.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.4
gorm.io/driver/sqlite v1.5.5
Expand Down Expand Up @@ -75,7 +75,7 @@ require (
go.sia.tech/web v0.0.0-20231213145933-3f175a86abff // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.16.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.68 h1:hTqSIfLlpXaKuNy4baAp4Jjy2sqZEN9hRxD0M4aOfrQ=
github.com/minio/minio-go/v7 v7.0.68/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
github.com/minio/minio-go/v7 v7.0.69 h1:l8AnsQFyY1xiwa/DaQskY4NXSLA2yrGsW5iD9nRPVS0=
github.com/minio/minio-go/v7 v7.0.69/go.mod h1:XAvOPJQ5Xlzk5o3o/ArO2NMbhSGkimC+bpW/ngRKDmQ=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -274,8 +274,8 @@ golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
Expand Down Expand Up @@ -328,16 +328,16 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210421210424-b80969c67360/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down

0 comments on commit 220a570

Please sign in to comment.