Skip to content

Update modules on config update #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: load-config-urls-from-zos-config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ require (
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
github.com/rs/zerolog v1.34.0
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/stretchr/testify v1.10.0
github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241127100051-77e684bcb1b2
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.16.1-0.20241229121208-76ac3fea5e67
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.16.8
github.com/threefoldtech/zbus v1.0.1
github.com/tyler-smith/go-bip39 v1.1.0
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852
Expand All @@ -67,9 +67,7 @@ require (
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/tools v0.30.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287 // indirect
)
Expand Down Expand Up @@ -158,13 +156,13 @@ require (
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.zx2c4.com/wireguard v0.0.20200320 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
gonum.org/v1/gonum v0.16.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/grpc v1.70.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/djherbis/times.v1 v1.2.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
34 changes: 15 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,15 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.14.3/go.mod h1:3WXPzbXEEliJ+a6UFE4vhIxV8qR1EML6ngzP9ug4eYg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/safchain/ethtool v0.0.0-20201023143004-874930cb3ce0 h1:eskphjc5kRCykOJyX7HHVbJCs25/8knprttvrVvEd8o=
Expand Down Expand Up @@ -564,8 +564,8 @@ github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56 h1:uWd8JfE8N3
github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56/go.mod h1:lZjR32SiNo3dP70inVFxaLMyZjmKX1ucS+5O31dbPNM=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241127100051-77e684bcb1b2 h1:VW2J36F8g/kJn4IkY0JiRFmb1gFcdjiOyltfJLJ0mYU=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20241127100051-77e684bcb1b2/go.mod h1:cOL5YgHUmDG5SAXrsZxFjUECRQQuAqOoqvXhZG5sEUw=
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.16.1-0.20241229121208-76ac3fea5e67 h1:Ii9TmXPBC1GYxRirReSygRZvEGXfAsQRaIipMEzGik0=
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.16.1-0.20241229121208-76ac3fea5e67/go.mod h1:93SROfr+QjgaJ5/jIWtIpLkhaD8Pv8WbdfwvwMNG2p4=
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.16.8 h1:BDuus/zqEBDsmPQA0h3inJyPnM83O6l4oMe6hqn00xA=
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.16.8/go.mod h1:93SROfr+QjgaJ5/jIWtIpLkhaD8Pv8WbdfwvwMNG2p4=
github.com/threefoldtech/zbus v1.0.1 h1:3KaEpyOiDYAw+lrAyoQUGIvY9BcjVRXlQ1beBRqhRNk=
github.com/threefoldtech/zbus v1.0.1/go.mod h1:E/v/xEvG/l6z/Oj0aDkuSUXFm/1RVJkhKBwDTAIdsHo=
github.com/tinylib/msgp v1.1.5 h1:2gXmtWueD2HefZHQe1QOy9HVzmFrLOVvsXwXBQ0ayy0=
Expand Down Expand Up @@ -635,8 +635,6 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
Expand All @@ -657,8 +655,6 @@ golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down Expand Up @@ -714,8 +710,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -786,8 +782,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down Expand Up @@ -822,8 +818,8 @@ golang.zx2c4.com/wireguard v0.0.20200320/go.mod h1:lDian4Sw4poJ04SgHh35nzMVwGSYl
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200609130330-bd2cb7843e1b h1:l4mBVCYinjzZuR5DtxHuBD6wyd4348TGiavJ5vLrhEc=
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200609130330-bd2cb7843e1b/go.mod h1:UdS9frhv65KTfwxME1xE8+rHYoFpbm36gOud1GhBe9c=
golang.zx2c4.com/wireguard/windows v0.3.14/go.mod h1:3P4IEAsb+BjlKZmpUXgy74c0iX9AVwwr3WcVJ8nPgME=
gonum.org/v1/gonum v0.15.0 h1:2lYxjRbTYyxkJxlhC+LvJIx3SsANPdRybu1tGj9/OrQ=
gonum.org/v1/gonum v0.15.0/go.mod h1:xzZVBJBtS+Mz4q0Yl2LJTk+OxOg4jiXZ7qBoM0uISGo=
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down Expand Up @@ -857,8 +853,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions pkg/api_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
//go:generate zbusc -module api-gateway -version 0.0.1 -name api-gateway -package stubs github.com/threefoldtech/zosbase/pkg+SubstrateGateway stubs/api_gateway_stub.go

type SubstrateGateway interface {
UpdateSubstrateGatewayConnection(manager substrate.Manager) (err error)
CreateNode(node substrate.Node) (uint32, error)
CreateTwin(relay string, pk []byte) (uint32, error)
EnsureAccount(activationURL []string, termsAndConditionsLink string, termsAndConditionsHash string) (info substrate.AccountInfo, err error)
Expand Down
8 changes: 4 additions & 4 deletions pkg/environment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ type Config struct {
BinRepo string `json:"bin_repo"`
GeoipURLs []string `json:"geoip_urls"`

FlistURL string `json:"flist_url"`
V4FlistURL string `json:"v4_flist_url"`
// FlistURL string `json:"flist_url"`
// V4FlistURL string `json:"v4_flist_url"`

HubURL string `json:"hub_url"`
V4HubURL string `json:"v4_hub_url"`

HubStorage string `json:"hub_storage"`
V4HubStorage string `json:"v4_hub_storage"`
// HubStorage string `json:"hub_storage"`
// V4HubStorage string `json:"v4_hub_storage"`
}

// Merge, updates current config with cfg merging and override config
Expand Down
48 changes: 29 additions & 19 deletions pkg/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"slices"
"strconv"
"sync"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -126,8 +125,8 @@ const (
)

var (
pool substrate.Manager
poolOnce sync.Once
pool substrate.Manager
subURLs []string

envDev = Environment{
RunningMode: RunningDev,
Expand Down Expand Up @@ -288,9 +287,19 @@ func GetSubstrate() (substrate.Manager, error) {
return nil, errors.Wrap(err, "failed to get boot environment")
}

poolOnce.Do(func() {
slices.Sort(subURLs)
slices.Sort(env.SubstrateURL)

// if substrate url changed then update subURLs and update pool with new manager
if !slices.Equal(subURLs, env.SubstrateURL) {
log.Debug().Strs("substrate_urls", env.SubstrateURL).Msg("updating to sub manager with url")
subURLs = env.SubstrateURL
pool = substrate.NewManager(env.SubstrateURL...)
})
}

// poolOnce.Do(func() {
// pool = substrate.NewManager(env.SubstrateURL...)
// })

return pool, nil
}
Expand Down Expand Up @@ -368,14 +377,15 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
if geoip := config.GeoipURLs; len(geoip) > 0 {
env.GeoipURLs = geoip
}

if flist := config.FlistURL; len(flist) > 0 {
env.FlistURL = flist
}

if storage := config.HubStorage; len(storage) > 0 {
env.HubStorage = storage
}
// flist url and hub urls shouldn't listen to changes in config as long as we can't change it at run time.
// it would cause breakage in vmd that needs a reboot to be recovered.
// if flist := config.FlistURL; len(flist) > 0 {
// env.FlistURL = flist
// }
//
// if storage := config.HubStorage; len(storage) > 0 {
// env.HubStorage = storage
// }

if hub := config.HubURL; len(hub) > 0 {
env.HubURL = hub
Expand All @@ -389,14 +399,14 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
// if the node running v4 chage urls to use v4 hub
if params.IsV4() {
env.FlistURL = defaultV4FlistURL
if flist := config.V4FlistURL; len(flist) > 0 {
env.FlistURL = flist
}
// if flist := config.V4FlistURL; len(flist) > 0 {
// env.FlistURL = flist
// }

env.HubStorage = defaultV4HubStorage
if storage := config.V4HubStorage; len(storage) > 0 {
env.HubStorage = storage
}
// if storage := config.V4HubStorage; len(storage) > 0 {
// env.HubStorage = storage
// }

}

Expand Down
30 changes: 28 additions & 2 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"os"
"sync/atomic"
"time"

gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
Expand Down Expand Up @@ -66,7 +67,8 @@ type Callback func(events *substrate.EventRecords)
// Events processor receives all events starting from the given state
// and for each set of events calls callback cb
type Processor struct {
sub substrate.Manager
sub substrate.Manager
updated atomic.Bool

cb Callback
state State
Expand Down Expand Up @@ -97,6 +99,7 @@ func (e *Processor) process(changes []types.StorageChangeSet, meta *types.Metada
}
}
}

func (e *Processor) eventsTo(cl *gsrpc.SubstrateAPI, meta *types.Metadata, block types.Header) error {
//
last, err := e.state.Get(cl)
Expand All @@ -120,7 +123,7 @@ func (e *Processor) eventsTo(cl *gsrpc.SubstrateAPI, meta *types.Metadata, block
return errors.Wrapf(err, "failed to get block hash '%d'", start)
}

//state.ErrUnknownBlock
// state.ErrUnknownBlock
changes, err := cl.RPC.State.QueryStorageAt([]types.StorageKey{key}, hash)
if err, ok := err.(rpc.Error); ok {
if err.ErrorCode() == -32000 { // block is too old not in archive anymore
Expand Down Expand Up @@ -169,6 +172,24 @@ func (e *Processor) subscribe(ctx context.Context) error {
if err := e.state.Set(block.Number); err != nil {
return errors.Wrap(err, "failed to commit last block number")
}
default:
if e.updated.Load() {
e.updated.Swap(false)

newCL, newMeta, err := e.sub.Raw()
if err != nil {
log.Debug().Err(err).Msg("failed to update substrate connection")
break
}

// only update cl and mata after creating the new connection successfully
cl.Client.Close()
cl = newCL
meta = newMeta

log.Debug().Msg("done updating sub connection for substrate events listener")

}
}
}
}
Expand All @@ -184,3 +205,8 @@ func (e *Processor) Start(ctx context.Context) {
return
}
}

func (e *Processor) updateSubstrateConn(sub substrate.Manager) {
e.sub = sub
e.updated.Swap(true)
}
20 changes: 12 additions & 8 deletions pkg/events/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ const (
)

type RedisStream struct {
sub substrate.Manager
state string
farm pkg.FarmID
node uint32
pool *redis.Pool
sub substrate.Manager
state string
farm pkg.FarmID
node uint32
pool *redis.Pool
processor *Processor
}

func NewRedisStream(sub substrate.Manager, address string, farm pkg.FarmID, node uint32, state string) (*RedisStream, error) {
Expand All @@ -51,6 +52,11 @@ func NewRedisStream(sub substrate.Manager, address string, farm pkg.FarmID, node
}, nil
}

func (r *RedisStream) UpdateSubstrateManager(sub substrate.Manager) {
r.sub = sub
r.processor.updateSubstrateConn(sub)
}

func (r *RedisStream) push(con redis.Conn, queue string, event interface{}) error {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
Expand Down Expand Up @@ -139,11 +145,11 @@ func (r *RedisStream) process(events *substrate.EventRecords) {
log.Error().Err(err).Msg("failed to push event")
}
}

}

func (r *RedisStream) Start(ctx context.Context) {
ps := NewProcessor(r.sub, r.process, NewFileState(r.state))
r.processor = ps
ps.Start(ctx)
}

Expand Down Expand Up @@ -184,7 +190,6 @@ func (r *RedisConsumer) pop(con redis.Conn, group, stream string) ([]payload, er
"BLOCK", 0,
"STREAMS", stream,
0))

if err != nil {
return nil, err
}
Expand All @@ -202,7 +207,6 @@ func (r *RedisConsumer) pop(con redis.Conn, group, stream string) ([]payload, er
"BLOCK", 3000,
"STREAMS", stream,
">"))

if err != nil {
return nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/stubs/api_gateway_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,18 @@ func (s *SubstrateGatewayStub) UpdateNodeUptimeV2(ctx context.Context, arg0 uint
}
return
}

func (s *SubstrateGatewayStub) UpdateSubstrateGatewayConnection(ctx context.Context, arg0 tfchainclientgo.Manager) (ret0 error) {
args := []interface{}{arg0}
result, err := s.client.RequestContext(ctx, s.module, s.object, "UpdateSubstrateGatewayConnection", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}
Loading
Loading