Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
all: first working SWAP version (#1554)
Browse files Browse the repository at this point in the history
all: first working implementation of the Swarm Accounting Protocol (SWAP)
  • Loading branch information
mortelli authored Aug 27, 2019
1 parent 3fa879f commit 8e8aefd
Show file tree
Hide file tree
Showing 41 changed files with 5,040 additions and 2,699 deletions.
18 changes: 6 additions & 12 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/ethersphere/swarm/contracts/ens"
"github.com/ethersphere/swarm/network"
"github.com/ethersphere/swarm/pss"
"github.com/ethersphere/swarm/services/swap"
"github.com/ethersphere/swarm/storage"
)

Expand All @@ -52,8 +51,11 @@ type Config struct {
CacheCapacity uint
BaseKey []byte

// Swap configs
SwapBackendURL string
SwapEnabled bool

*network.HiveParams
Swap *swap.LocalProfile
Pss *pss.Params
Contract common.Address
EnsRoot common.Address
Expand All @@ -65,7 +67,6 @@ type Config struct {
BzzKey string
Enode *enode.Node `toml:"-"`
NetworkID uint64
SwapEnabled bool
SyncEnabled bool
SyncingSkipCheck bool
DeliverySkipCheck bool
Expand All @@ -74,7 +75,6 @@ type Config struct {
BootnodeMode bool
DisableAutoConnect bool
SyncUpdateDelay time.Duration
SwapAPI string
Cors string
BzzAccount string
GlobalStoreAPI string
Expand All @@ -87,21 +87,20 @@ func NewConfig() (c *Config) {
c = &Config{
FileStoreParams: storage.NewFileStoreParams(),
HiveParams: network.NewHiveParams(),
Swap: swap.NewDefaultSwapParams(),
Pss: pss.NewParams(),
ListenAddr: DefaultHTTPListenAddr,
Port: DefaultHTTPPort,
Path: node.DefaultDataDir(),
EnsAPIs: nil,
EnsRoot: ens.TestNetAddress,
NetworkID: network.DefaultNetworkID,
SwapEnabled: false,
SyncEnabled: true,
SyncingSkipCheck: false,
MaxStreamPeerServers: 10000,
DeliverySkipCheck: true,
SyncUpdateDelay: 15 * time.Second,
SwapAPI: "",
SwapEnabled: false,
SwapBackendURL: "",
}

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

// initialize components that depend on the swarm instance's private key
if c.SwapEnabled {
c.Swap.Init(c.Contract, prvKey)
}

c.privateKey = prvKey
c.ChunkDbPath = filepath.Join(c.Path, "chunks")
c.BaseKey = common.FromHex(c.BzzKey)
Expand Down
3 changes: 1 addition & 2 deletions api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"reflect"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ func TestConfig(t *testing.T) {
if one.PublicKey == "" {
t.Fatal("Expected PublicKey to be set")
}
if one.Swap.PayProfile.Beneficiary == (common.Address{}) && one.SwapEnabled {
if one.SwapEnabled && one.SwapBackendURL == "" {
t.Fatal("Failed to correctly initialize SwapParams")
}
if one.ChunkDbPath == one.Path {
Expand Down
8 changes: 4 additions & 4 deletions cmd/swarm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const (
SwarmEnvPort = "SWARM_PORT"
SwarmEnvNetworkID = "SWARM_NETWORK_ID"
SwarmEnvSwapEnable = "SWARM_SWAP_ENABLE"
SwarmEnvSwapAPI = "SWARM_SWAP_API"
SwarmEnvSwapBackendURL = "SWARM_SWAP_BACKEND_URL"
SwarmEnvSyncDisable = "SWARM_SYNC_DISABLE"
SwarmEnvSyncUpdateDelay = "SWARM_ENV_SYNC_UPDATE_DELAY"
SwarmEnvMaxStreamPeerServers = "SWARM_ENV_MAX_STREAM_PEER_SERVERS"
Expand Down Expand Up @@ -229,9 +229,9 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
currentConfig.DeliverySkipCheck = true
}

currentConfig.SwapAPI = ctx.GlobalString(SwarmSwapAPIFlag.Name)
if currentConfig.SwapEnabled && currentConfig.SwapAPI == "" {
utils.Fatalf(SwarmErrSwapSetNoAPI)
currentConfig.SwapBackendURL = ctx.GlobalString(SwarmSwapBackendURLFlag.Name)
if currentConfig.SwapEnabled && currentConfig.SwapBackendURL == "" {
utils.Fatalf(SwarmErrSwapSetNoBackendURL)
}

if ctx.GlobalIsSet(EnsAPIFlag.Name) {
Expand Down
22 changes: 2 additions & 20 deletions cmd/swarm/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestConfigDump(t *testing.T) {
swarm.ExpectExit()
}

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

swarm := runSwarm(t, flags...)
swarm.Expect("Fatal: " + SwarmErrSwapSetNoAPI + "\n")
swarm.Expect("Fatal: " + SwarmErrSwapSetNoBackendURL + "\n")
swarm.ExpectExit()
}

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

if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
}

// if info.SyncParams.KeyBufferSize != 512 {
// t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
// }

node.Shutdown()
}

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

if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
}

// if info.SyncParams.KeyBufferSize != 512 {
// t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
// }

node.Shutdown()
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/swarm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ var (
Usage: "Swarm SWAP enabled (default false)",
EnvVar: SwarmEnvSwapEnable,
}
SwarmSwapAPIFlag = cli.StringFlag{
Name: "swap-api",
SwarmSwapBackendURLFlag = cli.StringFlag{
Name: "swap-backend-url",
Usage: "URL of the Ethereum API provider to use to settle SWAP payments",
EnvVar: SwarmEnvSwapAPI,
EnvVar: SwarmEnvSwapBackendURL,
}
SwarmSyncDisabledFlag = cli.BoolTFlag{
Name: "nosync",
Expand Down
6 changes: 3 additions & 3 deletions cmd/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ var gitCommit string

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

// this help command gets added to any subcommand that does not define it explicitly
Expand Down Expand Up @@ -178,7 +178,7 @@ func init() {
EnsAPIFlag,
SwarmTomlConfigPathFlag,
SwarmSwapEnabledFlag,
SwarmSwapAPIFlag,
SwarmSwapBackendURLFlag,
SwarmSyncDisabledFlag,
SwarmSyncUpdateDelay,
SwarmMaxStreamPeerServersFlag,
Expand Down
6 changes: 4 additions & 2 deletions cmd/swarm/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {

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

t.Helper()

conf, account := getTestAccount(t, dir)
ks := keystore.NewKeyStore(path.Join(dir, "keystore"), 1<<18, 1)

Expand All @@ -342,7 +344,7 @@ func newTestNode(t *testing.T, dir string) *testNode {
// assign ports
ports, err := getAvailableTCPPorts(2)
if err != nil {
t.Fatal(err)
return nil
}
p2pPort := ports[0]
httpPort := ports[1]
Expand Down Expand Up @@ -386,7 +388,7 @@ func newTestNode(t *testing.T, dir string) *testNode {
}
}
if node.Client == nil {
t.Fatal(err)
t.Fatal("Expected nil node")
}

// load info
Expand Down
68 changes: 0 additions & 68 deletions contracts/chequebook/api.go

This file was deleted.

Loading

0 comments on commit 8e8aefd

Please sign in to comment.