Skip to content

Commit 8e8aefd

Browse files
authored
all: first working SWAP version (ethersphere#1554)
all: first working implementation of the Swarm Accounting Protocol (SWAP)
1 parent 3fa879f commit 8e8aefd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5040
-2699
lines changed

api/config.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/ethersphere/swarm/contracts/ens"
3232
"github.com/ethersphere/swarm/network"
3333
"github.com/ethersphere/swarm/pss"
34-
"github.com/ethersphere/swarm/services/swap"
3534
"github.com/ethersphere/swarm/storage"
3635
)
3736

@@ -52,8 +51,11 @@ type Config struct {
5251
CacheCapacity uint
5352
BaseKey []byte
5453

54+
// Swap configs
55+
SwapBackendURL string
56+
SwapEnabled bool
57+
5558
*network.HiveParams
56-
Swap *swap.LocalProfile
5759
Pss *pss.Params
5860
Contract common.Address
5961
EnsRoot common.Address
@@ -65,7 +67,6 @@ type Config struct {
6567
BzzKey string
6668
Enode *enode.Node `toml:"-"`
6769
NetworkID uint64
68-
SwapEnabled bool
6970
SyncEnabled bool
7071
SyncingSkipCheck bool
7172
DeliverySkipCheck bool
@@ -74,7 +75,6 @@ type Config struct {
7475
BootnodeMode bool
7576
DisableAutoConnect bool
7677
SyncUpdateDelay time.Duration
77-
SwapAPI string
7878
Cors string
7979
BzzAccount string
8080
GlobalStoreAPI string
@@ -87,21 +87,20 @@ func NewConfig() (c *Config) {
8787
c = &Config{
8888
FileStoreParams: storage.NewFileStoreParams(),
8989
HiveParams: network.NewHiveParams(),
90-
Swap: swap.NewDefaultSwapParams(),
9190
Pss: pss.NewParams(),
9291
ListenAddr: DefaultHTTPListenAddr,
9392
Port: DefaultHTTPPort,
9493
Path: node.DefaultDataDir(),
9594
EnsAPIs: nil,
9695
EnsRoot: ens.TestNetAddress,
9796
NetworkID: network.DefaultNetworkID,
98-
SwapEnabled: false,
9997
SyncEnabled: true,
10098
SyncingSkipCheck: false,
10199
MaxStreamPeerServers: 10000,
102100
DeliverySkipCheck: true,
103101
SyncUpdateDelay: 15 * time.Second,
104-
SwapAPI: "",
102+
SwapEnabled: false,
103+
SwapBackendURL: "",
105104
}
106105

107106
return
@@ -131,11 +130,6 @@ func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error
131130
return fmt.Errorf("Error creating enode: %v", err)
132131
}
133132

134-
// initialize components that depend on the swarm instance's private key
135-
if c.SwapEnabled {
136-
c.Swap.Init(c.Contract, prvKey)
137-
}
138-
139133
c.privateKey = prvKey
140134
c.ChunkDbPath = filepath.Join(c.Path, "chunks")
141135
c.BaseKey = common.FromHex(c.BzzKey)

api/config_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"reflect"
2121
"testing"
2222

23-
"github.com/ethereum/go-ethereum/common"
2423
"github.com/ethereum/go-ethereum/crypto"
2524
)
2625

@@ -57,7 +56,7 @@ func TestConfig(t *testing.T) {
5756
if one.PublicKey == "" {
5857
t.Fatal("Expected PublicKey to be set")
5958
}
60-
if one.Swap.PayProfile.Beneficiary == (common.Address{}) && one.SwapEnabled {
59+
if one.SwapEnabled && one.SwapBackendURL == "" {
6160
t.Fatal("Failed to correctly initialize SwapParams")
6261
}
6362
if one.ChunkDbPath == one.Path {

cmd/swarm/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const (
6666
SwarmEnvPort = "SWARM_PORT"
6767
SwarmEnvNetworkID = "SWARM_NETWORK_ID"
6868
SwarmEnvSwapEnable = "SWARM_SWAP_ENABLE"
69-
SwarmEnvSwapAPI = "SWARM_SWAP_API"
69+
SwarmEnvSwapBackendURL = "SWARM_SWAP_BACKEND_URL"
7070
SwarmEnvSyncDisable = "SWARM_SYNC_DISABLE"
7171
SwarmEnvSyncUpdateDelay = "SWARM_ENV_SYNC_UPDATE_DELAY"
7272
SwarmEnvMaxStreamPeerServers = "SWARM_ENV_MAX_STREAM_PEER_SERVERS"
@@ -229,9 +229,9 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
229229
currentConfig.DeliverySkipCheck = true
230230
}
231231

232-
currentConfig.SwapAPI = ctx.GlobalString(SwarmSwapAPIFlag.Name)
233-
if currentConfig.SwapEnabled && currentConfig.SwapAPI == "" {
234-
utils.Fatalf(SwarmErrSwapSetNoAPI)
232+
currentConfig.SwapBackendURL = ctx.GlobalString(SwarmSwapBackendURLFlag.Name)
233+
if currentConfig.SwapEnabled && currentConfig.SwapBackendURL == "" {
234+
utils.Fatalf(SwarmErrSwapSetNoBackendURL)
235235
}
236236

237237
if ctx.GlobalIsSet(EnsAPIFlag.Name) {

cmd/swarm/config_test.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestConfigDump(t *testing.T) {
4747
swarm.ExpectExit()
4848
}
4949

50-
func TestConfigFailsSwapEnabledNoSwapApi(t *testing.T) {
50+
func TestConfigFailsSwapEnabledNoBackendURL(t *testing.T) {
5151
flags := []string{
5252
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
5353
fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
@@ -56,7 +56,7 @@ func TestConfigFailsSwapEnabledNoSwapApi(t *testing.T) {
5656
}
5757

5858
swarm := runSwarm(t, flags...)
59-
swarm.Expect("Fatal: " + SwarmErrSwapSetNoAPI + "\n")
59+
swarm.Expect("Fatal: " + SwarmErrSwapSetNoBackendURL + "\n")
6060
swarm.ExpectExit()
6161
}
6262

@@ -310,7 +310,6 @@ func TestConfigFileOverrides(t *testing.T) {
310310
defaultConf.Port = httpPort
311311
defaultConf.DbCapacity = 9000000
312312
defaultConf.HiveParams.KeepAliveInterval = 6000000000
313-
defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
314313
//defaultConf.SyncParams.KeyBufferSize = 512
315314
//create a TOML string
316315
out, err := tomlSettings.Marshal(&defaultConf)
@@ -392,14 +391,6 @@ func TestConfigFileOverrides(t *testing.T) {
392391
t.Fatalf("Expected HiveParams KeepAliveInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.KeepAliveInterval))
393392
}
394393

395-
if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
396-
t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
397-
}
398-
399-
// if info.SyncParams.KeyBufferSize != 512 {
400-
// t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
401-
// }
402-
403394
node.Shutdown()
404395
}
405396

@@ -521,7 +512,6 @@ func TestConfigCmdLineOverridesFile(t *testing.T) {
521512
defaultConf.Port = "8588"
522513
defaultConf.DbCapacity = 9000000
523514
defaultConf.HiveParams.KeepAliveInterval = 6000000000
524-
defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
525515
//defaultConf.SyncParams.KeyBufferSize = 512
526516
//create a TOML file
527517
out, err := tomlSettings.Marshal(&defaultConf)
@@ -606,14 +596,6 @@ func TestConfigCmdLineOverridesFile(t *testing.T) {
606596
t.Fatalf("Expected HiveParams KeepAliveInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.KeepAliveInterval))
607597
}
608598

609-
if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
610-
t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
611-
}
612-
613-
// if info.SyncParams.KeyBufferSize != 512 {
614-
// t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
615-
// }
616-
617599
node.Shutdown()
618600
}
619601

cmd/swarm/flags.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ var (
6060
Usage: "Swarm SWAP enabled (default false)",
6161
EnvVar: SwarmEnvSwapEnable,
6262
}
63-
SwarmSwapAPIFlag = cli.StringFlag{
64-
Name: "swap-api",
63+
SwarmSwapBackendURLFlag = cli.StringFlag{
64+
Name: "swap-backend-url",
6565
Usage: "URL of the Ethereum API provider to use to settle SWAP payments",
66-
EnvVar: SwarmEnvSwapAPI,
66+
EnvVar: SwarmEnvSwapBackendURL,
6767
}
6868
SwarmSyncDisabledFlag = cli.BoolTFlag{
6969
Name: "nosync",

cmd/swarm/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ 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-
SwarmErrSwapSetNoAPI = "SWAP is enabled but --swap-api is not set"
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"
8383
)
8484

8585
// this help command gets added to any subcommand that does not define it explicitly
@@ -178,7 +178,7 @@ func init() {
178178
EnsAPIFlag,
179179
SwarmTomlConfigPathFlag,
180180
SwarmSwapEnabledFlag,
181-
SwarmSwapAPIFlag,
181+
SwarmSwapBackendURLFlag,
182182
SwarmSyncDisabledFlag,
183183
SwarmSyncUpdateDelay,
184184
SwarmMaxStreamPeerServersFlag,

cmd/swarm/run_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
332332

333333
func newTestNode(t *testing.T, dir string) *testNode {
334334

335+
t.Helper()
336+
335337
conf, account := getTestAccount(t, dir)
336338
ks := keystore.NewKeyStore(path.Join(dir, "keystore"), 1<<18, 1)
337339

@@ -342,7 +344,7 @@ func newTestNode(t *testing.T, dir string) *testNode {
342344
// assign ports
343345
ports, err := getAvailableTCPPorts(2)
344346
if err != nil {
345-
t.Fatal(err)
347+
return nil
346348
}
347349
p2pPort := ports[0]
348350
httpPort := ports[1]
@@ -386,7 +388,7 @@ func newTestNode(t *testing.T, dir string) *testNode {
386388
}
387389
}
388390
if node.Client == nil {
389-
t.Fatal(err)
391+
t.Fatal("Expected nil node")
390392
}
391393

392394
// load info

contracts/chequebook/api.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)