Skip to content

Commit 11c05dc

Browse files
authored
all: configurable payment/disconnect thresholds (ethersphere#1729)
* Allow for configuration of disconnect and payment threshold * Make Swap flags override the config * Tests for the working of the disconnect/payment threshold * Move swap-related logic from swarm.go:NewSwarm to swap/swap.go:New
1 parent 5a391f8 commit 11c05dc

File tree

12 files changed

+324
-153
lines changed

12 files changed

+324
-153
lines changed

api/config.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethersphere/swarm/network"
3333
"github.com/ethersphere/swarm/pss"
3434
"github.com/ethersphere/swarm/storage"
35+
"github.com/ethersphere/swarm/swap"
3536
)
3637

3738
const (
@@ -52,12 +53,15 @@ type Config struct {
5253
BaseKey []byte
5354

5455
// Swap configs
55-
SwapBackendURL string
56-
SwapEnabled bool
56+
SwapBackendURL string // Ethereum API endpoint
57+
SwapEnabled bool // whether SWAP incentives are enabled
58+
SwapPaymentThreshold uint64 // honey amount at which a payment is triggered
59+
SwapDisconnectThreshold uint64 // honey amount at which a peer disconnects
60+
Contract common.Address // address of the chequebook contract
61+
// end of Swap configs
5762

5863
*network.HiveParams
5964
Pss *pss.Params
60-
Contract common.Address
6165
EnsRoot common.Address
6266
EnsAPIs []string
6367
Path string
@@ -81,26 +85,28 @@ type Config struct {
8185
privateKey *ecdsa.PrivateKey
8286
}
8387

84-
//create a default config with all parameters to set to defaults
88+
//NewConfig creates a default config with all parameters to set to defaults
8589
func NewConfig() (c *Config) {
8690

8791
c = &Config{
88-
FileStoreParams: storage.NewFileStoreParams(),
89-
HiveParams: network.NewHiveParams(),
90-
Pss: pss.NewParams(),
91-
ListenAddr: DefaultHTTPListenAddr,
92-
Port: DefaultHTTPPort,
93-
Path: node.DefaultDataDir(),
94-
EnsAPIs: nil,
95-
EnsRoot: ens.TestNetAddress,
96-
NetworkID: network.DefaultNetworkID,
97-
SyncEnabled: true,
98-
SyncingSkipCheck: false,
99-
MaxStreamPeerServers: 10000,
100-
DeliverySkipCheck: true,
101-
SyncUpdateDelay: 15 * time.Second,
102-
SwapEnabled: false,
103-
SwapBackendURL: "",
92+
FileStoreParams: storage.NewFileStoreParams(),
93+
SwapBackendURL: "",
94+
SwapEnabled: false,
95+
SwapPaymentThreshold: swap.DefaultPaymentThreshold,
96+
SwapDisconnectThreshold: swap.DefaultDisconnectThreshold,
97+
HiveParams: network.NewHiveParams(),
98+
Pss: pss.NewParams(),
99+
EnsRoot: ens.TestNetAddress,
100+
EnsAPIs: nil,
101+
Path: node.DefaultDataDir(),
102+
ListenAddr: DefaultHTTPListenAddr,
103+
Port: DefaultHTTPPort,
104+
NetworkID: network.DefaultNetworkID,
105+
SyncEnabled: true,
106+
SyncingSkipCheck: false,
107+
DeliverySkipCheck: true,
108+
MaxStreamPeerServers: 10000,
109+
SyncUpdateDelay: 15 * time.Second,
104110
}
105111

106112
return

cmd/swarm/config.go

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
"strings"
2828
"unicode"
2929

30+
"github.com/ethereum/go-ethereum/common"
3031
cli "gopkg.in/urfave/cli.v1"
3132

3233
"github.com/ethereum/go-ethereum/cmd/utils"
33-
"github.com/ethereum/go-ethereum/common"
3434
"github.com/ethereum/go-ethereum/log"
3535
"github.com/ethereum/go-ethereum/node"
3636
"github.com/naoina/toml"
@@ -59,33 +59,35 @@ var (
5959

6060
//constants for environment variables
6161
const (
62-
SwarmEnvChequebookAddr = "SWARM_CHEQUEBOOK_ADDR"
63-
SwarmEnvAccount = "SWARM_ACCOUNT"
64-
SwarmEnvBzzKeyHex = "SWARM_BZZ_KEY_HEX"
65-
SwarmEnvListenAddr = "SWARM_LISTEN_ADDR"
66-
SwarmEnvPort = "SWARM_PORT"
67-
SwarmEnvNetworkID = "SWARM_NETWORK_ID"
68-
SwarmEnvSwapEnable = "SWARM_SWAP_ENABLE"
69-
SwarmEnvSwapBackendURL = "SWARM_SWAP_BACKEND_URL"
70-
SwarmEnvSyncDisable = "SWARM_SYNC_DISABLE"
71-
SwarmEnvSyncUpdateDelay = "SWARM_ENV_SYNC_UPDATE_DELAY"
72-
SwarmEnvMaxStreamPeerServers = "SWARM_ENV_MAX_STREAM_PEER_SERVERS"
73-
SwarmEnvLightNodeEnable = "SWARM_LIGHT_NODE_ENABLE"
74-
SwarmEnvDeliverySkipCheck = "SWARM_DELIVERY_SKIP_CHECK"
75-
SwarmEnvENSAPI = "SWARM_ENS_API"
76-
SwarmEnvENSAddr = "SWARM_ENS_ADDR"
77-
SwarmEnvCORS = "SWARM_CORS"
78-
SwarmEnvBootnodes = "SWARM_BOOTNODES"
79-
SwarmEnvPSSEnable = "SWARM_PSS_ENABLE"
80-
SwarmEnvStorePath = "SWARM_STORE_PATH"
81-
SwarmEnvStoreCapacity = "SWARM_STORE_CAPACITY"
82-
SwarmEnvStoreCacheCapacity = "SWARM_STORE_CACHE_CAPACITY"
83-
SwarmEnvBootnodeMode = "SWARM_BOOTNODE_MODE"
84-
SwarmEnvNATInterface = "SWARM_NAT_INTERFACE"
85-
SwarmAccessPassword = "SWARM_ACCESS_PASSWORD"
86-
SwarmAutoDefaultPath = "SWARM_AUTO_DEFAULTPATH"
87-
SwarmGlobalstoreAPI = "SWARM_GLOBALSTORE_API"
88-
GethEnvDataDir = "GETH_DATADIR"
62+
SwarmEnvChequebookAddr = "SWARM_CHEQUEBOOK_ADDR"
63+
SwarmEnvAccount = "SWARM_ACCOUNT"
64+
SwarmEnvBzzKeyHex = "SWARM_BZZ_KEY_HEX"
65+
SwarmEnvListenAddr = "SWARM_LISTEN_ADDR"
66+
SwarmEnvPort = "SWARM_PORT"
67+
SwarmEnvNetworkID = "SWARM_NETWORK_ID"
68+
SwarmEnvSwapEnable = "SWARM_SWAP_ENABLE"
69+
SwarmEnvSwapBackendURL = "SWARM_SWAP_BACKEND_URL"
70+
SwarmEnvSwapPaymentThreshold = "SWARM_SWAP_PAYMENT_THRESHOLD"
71+
SwarmEnvSwapDisconnectThreshold = "SWARM_SWAP_DISCONNECT_THRESHOLD"
72+
SwarmEnvSyncDisable = "SWARM_SYNC_DISABLE"
73+
SwarmEnvSyncUpdateDelay = "SWARM_ENV_SYNC_UPDATE_DELAY"
74+
SwarmEnvMaxStreamPeerServers = "SWARM_ENV_MAX_STREAM_PEER_SERVERS"
75+
SwarmEnvLightNodeEnable = "SWARM_LIGHT_NODE_ENABLE"
76+
SwarmEnvDeliverySkipCheck = "SWARM_DELIVERY_SKIP_CHECK"
77+
SwarmEnvENSAPI = "SWARM_ENS_API"
78+
SwarmEnvENSAddr = "SWARM_ENS_ADDR"
79+
SwarmEnvCORS = "SWARM_CORS"
80+
SwarmEnvBootnodes = "SWARM_BOOTNODES"
81+
SwarmEnvPSSEnable = "SWARM_PSS_ENABLE"
82+
SwarmEnvStorePath = "SWARM_STORE_PATH"
83+
SwarmEnvStoreCapacity = "SWARM_STORE_CAPACITY"
84+
SwarmEnvStoreCacheCapacity = "SWARM_STORE_CACHE_CAPACITY"
85+
SwarmEnvBootnodeMode = "SWARM_BOOTNODE_MODE"
86+
SwarmEnvNATInterface = "SWARM_NAT_INTERFACE"
87+
SwarmAccessPassword = "SWARM_ACCESS_PASSWORD"
88+
SwarmAutoDefaultPath = "SWARM_AUTO_DEFAULTPATH"
89+
SwarmGlobalstoreAPI = "SWARM_GLOBALSTORE_API"
90+
GethEnvDataDir = "GETH_DATADIR"
8991
)
9092

9193
// These settings ensure that TOML keys use the same names as Go struct fields.
@@ -176,11 +178,9 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
176178
if keyid := ctx.GlobalString(SwarmAccountFlag.Name); keyid != "" {
177179
currentConfig.BzzAccount = keyid
178180
}
179-
180-
if chbookaddr := ctx.GlobalString(ChequebookAddrFlag.Name); chbookaddr != "" {
181+
if chbookaddr := ctx.GlobalString(SwarmSwapChequebookAddrFlag.Name); chbookaddr != "" {
181182
currentConfig.Contract = common.HexToAddress(chbookaddr)
182183
}
183-
184184
if networkid := ctx.GlobalString(SwarmNetworkIdFlag.Name); networkid != "" {
185185
id, err := strconv.ParseUint(networkid, 10, 64)
186186
if err != nil {
@@ -190,50 +190,44 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
190190
currentConfig.NetworkID = id
191191
}
192192
}
193-
194193
if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
195194
if datadir := ctx.GlobalString(utils.DataDirFlag.Name); datadir != "" {
196195
currentConfig.Path = expandPath(datadir)
197196
}
198197
}
199-
200198
bzzport := ctx.GlobalString(SwarmPortFlag.Name)
201199
if len(bzzport) > 0 {
202200
currentConfig.Port = bzzport
203201
}
204-
205202
if bzzaddr := ctx.GlobalString(SwarmListenAddrFlag.Name); bzzaddr != "" {
206203
currentConfig.ListenAddr = bzzaddr
207204
}
208-
209205
if ctx.GlobalIsSet(SwarmSwapEnabledFlag.Name) {
210206
currentConfig.SwapEnabled = true
211207
}
212-
208+
if swapBackendURL := ctx.GlobalString(SwarmSwapBackendURLFlag.Name); swapBackendURL != "" {
209+
currentConfig.SwapBackendURL = swapBackendURL
210+
}
211+
if paymentThreshold := ctx.GlobalUint64(SwarmSwapPaymentThresholdFlag.Name); paymentThreshold != 0 {
212+
currentConfig.SwapPaymentThreshold = paymentThreshold
213+
}
214+
if disconnectThreshold := ctx.GlobalUint64(SwarmSwapDisconnectThresholdFlag.Name); disconnectThreshold != 0 {
215+
currentConfig.SwapDisconnectThreshold = disconnectThreshold
216+
}
213217
if ctx.GlobalIsSet(SwarmSyncDisabledFlag.Name) {
214218
currentConfig.SyncEnabled = false
215219
}
216-
217220
if d := ctx.GlobalDuration(SwarmSyncUpdateDelay.Name); d > 0 {
218221
currentConfig.SyncUpdateDelay = d
219222
}
220-
221223
// any value including 0 is acceptable
222224
currentConfig.MaxStreamPeerServers = ctx.GlobalInt(SwarmMaxStreamPeerServersFlag.Name)
223-
224225
if ctx.GlobalIsSet(SwarmLightNodeEnabled.Name) {
225226
currentConfig.LightNodeEnabled = true
226227
}
227-
228228
if ctx.GlobalIsSet(SwarmDeliverySkipCheckFlag.Name) {
229229
currentConfig.DeliverySkipCheck = true
230230
}
231-
232-
currentConfig.SwapBackendURL = ctx.GlobalString(SwarmSwapBackendURLFlag.Name)
233-
if currentConfig.SwapEnabled && currentConfig.SwapBackendURL == "" {
234-
utils.Fatalf(SwarmErrSwapSetNoBackendURL)
235-
}
236-
237231
if ctx.GlobalIsSet(EnsAPIFlag.Name) {
238232
ensAPIs := ctx.GlobalStringSlice(EnsAPIFlag.Name)
239233
// preserve backward compatibility to disable ENS with --ens-api=""
@@ -243,40 +237,30 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
243237
for i := range ensAPIs {
244238
ensAPIs[i] = expandPath(ensAPIs[i])
245239
}
246-
247240
currentConfig.EnsAPIs = ensAPIs
248241
}
249-
250242
if cors := ctx.GlobalString(CorsStringFlag.Name); cors != "" {
251243
currentConfig.Cors = cors
252244
}
253-
254245
if storePath := ctx.GlobalString(SwarmStorePath.Name); storePath != "" {
255246
currentConfig.ChunkDbPath = storePath
256247
}
257-
258248
if storeCapacity := ctx.GlobalUint64(SwarmStoreCapacity.Name); storeCapacity != 0 {
259249
currentConfig.DbCapacity = storeCapacity
260250
}
261-
262251
if ctx.GlobalIsSet(SwarmStoreCacheCapacity.Name) {
263252
currentConfig.CacheCapacity = ctx.GlobalUint(SwarmStoreCacheCapacity.Name)
264253
}
265-
266254
if ctx.GlobalIsSet(SwarmBootnodeModeFlag.Name) {
267255
currentConfig.BootnodeMode = ctx.GlobalBool(SwarmBootnodeModeFlag.Name)
268256
}
269-
270257
if ctx.GlobalIsSet(SwarmDisableAutoConnectFlag.Name) {
271258
currentConfig.DisableAutoConnect = ctx.GlobalBool(SwarmDisableAutoConnectFlag.Name)
272259
}
273-
274260
if ctx.GlobalIsSet(SwarmGlobalStoreAPIFlag.Name) {
275261
currentConfig.GlobalStoreAPI = ctx.GlobalString(SwarmGlobalStoreAPIFlag.Name)
276262
}
277-
278263
return currentConfig
279-
280264
}
281265

282266
// dumpConfig is the dumpconfig command.

cmd/swarm/config_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net"
2525
"os"
2626
"os/exec"
27+
"strconv"
2728
"testing"
2829
"time"
2930

@@ -34,6 +35,7 @@ import (
3435
"github.com/ethereum/go-ethereum/rpc"
3536
"github.com/ethersphere/swarm"
3637
"github.com/ethersphere/swarm/api"
38+
"github.com/ethersphere/swarm/swap"
3739
"github.com/ethersphere/swarm/testutil"
3840
)
3941

@@ -48,20 +50,6 @@ func TestConfigDump(t *testing.T) {
4850
swarm.ExpectExit()
4951
}
5052

51-
func TestConfigFailsSwapEnabledNoBackendURL(t *testing.T) {
52-
flags := []string{
53-
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
54-
fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
55-
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
56-
fmt.Sprintf("--%s", SwarmSwapEnabledFlag.Name),
57-
"--verbosity", fmt.Sprintf("%d", *testutil.Loglevel),
58-
}
59-
60-
swarm := runSwarm(t, flags...)
61-
swarm.Expect("Fatal: " + SwarmErrSwapSetNoBackendURL + "\n")
62-
swarm.ExpectExit()
63-
}
64-
6553
func TestBzzKeyFlag(t *testing.T) {
6654
key, err := crypto.GenerateKey()
6755
if err != nil {
@@ -251,7 +239,10 @@ func TestConfigCmdLineOverrides(t *testing.T) {
251239
fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
252240
fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
253241
"--verbosity", fmt.Sprintf("%d", *testutil.Loglevel),
242+
fmt.Sprintf("--%s", SwarmSwapPaymentThresholdFlag.Name), strconv.Itoa(swap.DefaultPaymentThreshold + 1),
243+
fmt.Sprintf("--%s", SwarmSwapDisconnectThresholdFlag.Name), strconv.Itoa(swap.DefaultDisconnectThreshold + 1),
254244
}
245+
255246
node.Cmd = runSwarm(t, flags...)
256247
node.Cmd.InputLine(testPassphrase)
257248
defer func() {
@@ -296,6 +287,14 @@ func TestConfigCmdLineOverrides(t *testing.T) {
296287
t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
297288
}
298289

290+
if info.SwapPaymentThreshold != (swap.DefaultPaymentThreshold + 1) {
291+
t.Fatalf("Expected SwapPaymentThreshold to be %d, but got %d", swap.DefaultPaymentThreshold+1, info.SwapPaymentThreshold)
292+
}
293+
294+
if info.SwapDisconnectThreshold != (swap.DefaultDisconnectThreshold + 1) {
295+
t.Fatalf("Expected SwapDisconnectThreshold to be %d, but got %d", swap.DefaultDisconnectThreshold+1, info.SwapDisconnectThreshold)
296+
}
297+
299298
node.Shutdown()
300299
}
301300

cmd/swarm/flags.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package main
2020
import cli "gopkg.in/urfave/cli.v1"
2121

2222
var (
23-
ChequebookAddrFlag = cli.StringFlag{
23+
SwarmSwapChequebookAddrFlag = cli.StringFlag{
2424
Name: "chequebook",
2525
Usage: "chequebook contract address",
2626
EnvVar: SwarmEnvChequebookAddr,
@@ -65,6 +65,16 @@ var (
6565
Usage: "URL of the Ethereum API provider to use to settle SWAP payments",
6666
EnvVar: SwarmEnvSwapBackendURL,
6767
}
68+
SwarmSwapPaymentThresholdFlag = cli.Uint64Flag{
69+
Name: "swap-payment-threshold",
70+
Usage: "honey amount at which payment is triggered",
71+
EnvVar: SwarmEnvSwapPaymentThreshold,
72+
}
73+
SwarmSwapDisconnectThresholdFlag = cli.Uint64Flag{
74+
Name: "swap-disconnect-threshold",
75+
Usage: "honey amount at which a peer disconnects",
76+
EnvVar: SwarmEnvSwapDisconnectThreshold,
77+
}
6878
SwarmSyncDisabledFlag = cli.BoolTFlag{
6979
Name: "nosync",
7080
Usage: "Disable swarm syncing",

cmd/swarm/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ var gitCommit string
7878

7979
//declare a few constant error messages, useful for later error check comparisons in test
8080
var (
81-
SwarmErrNoBZZAccount = "bzzaccount option is required but not set; check your config file, command line or environment variables"
82-
SwarmErrSwapSetNoBackendURL = "SWAP is enabled but --swap-backend-url is not set"
81+
SwarmErrNoBZZAccount = "bzzaccount option is required but not set; check your config file, command line or environment variables"
8382
)
8483

8584
// this help command gets added to any subcommand that does not define it explicitly
@@ -177,8 +176,13 @@ func init() {
177176
CorsStringFlag,
178177
EnsAPIFlag,
179178
SwarmTomlConfigPathFlag,
179+
//swap flags
180180
SwarmSwapEnabledFlag,
181181
SwarmSwapBackendURLFlag,
182+
SwarmSwapDisconnectThresholdFlag,
183+
SwarmSwapPaymentThresholdFlag,
184+
SwarmSwapChequebookAddrFlag,
185+
// end of swap flags
182186
SwarmSyncDisabledFlag,
183187
SwarmSyncUpdateDelay,
184188
SwarmMaxStreamPeerServersFlag,
@@ -189,7 +193,6 @@ func init() {
189193
SwarmAccountFlag,
190194
SwarmBzzKeyHexFlag,
191195
SwarmNetworkIdFlag,
192-
ChequebookAddrFlag,
193196
// upload flags
194197
SwarmApiFlag,
195198
SwarmRecursiveFlag,

swap/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const (
3333
// This is the amount of time in seconds which an issuer has to wait to decrease the harddeposit of a beneficiary.
3434
// The smart-contract allows for setting this variable differently per beneficiary
3535
defaultHarddepositTimeoutDuration = 24 * time.Hour
36-
37-
// While Swap is unstable, it's only allowed to be run under a specific network ID
36+
// Until we deploy swap officially, it's only allowed to be enabled under a specific network ID (use the --bzznetworkid flag to set it)
3837
AllowedNetworkID = 5
3938
)

0 commit comments

Comments
 (0)