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

swap: unify variable types, pt. 4 #2087

Open
wants to merge 91 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
eca2e54
uint256: add first implementation for Int256
mortelli Jan 24, 2020
08df85b
uint256: iterate implementation for Int256, refactor Uint256
mortelli Jan 24, 2020
c6e0960
uint256: rename package to boundedint
mortelli Jan 24, 2020
58beba2
Merge remote-tracking branch 'origin/swap-unify-variable-types-ii' in…
mortelli Jan 27, 2020
ece8ceb
swap: fix formatting
mortelli Jan 27, 2020
3ab3cda
swap: fix formatting
mortelli Jan 27, 2020
6e8bbcb
swap: rename FromUint64 func to Uint64ToUint256
mortelli Jan 27, 2020
b3013ea
swap: rename FromUint64 to Uint64ToUint256
mortelli Jan 29, 2020
b26159a
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Jan 30, 2020
1442c1b
boundedint: small change to comments
mortelli Jan 31, 2020
560c13d
boundedint: add Int64ToInt256 func
mortelli Jan 31, 2020
d2ad8f2
boundedint, swarm, swap, main: add BoundedInt interface, change Cmp s…
mortelli Feb 3, 2020
ec696e8
swap: fix compile errors in api test
mortelli Feb 3, 2020
440e4c6
swap: fix compile errors in protocol test
mortelli Feb 4, 2020
2b81a9e
swap: fix compile errors in simulations test
mortelli Feb 4, 2020
0678e50
swap: fix compile errors in swap test
mortelli Feb 5, 2020
0cfebfe
swap: fix linter errors
mortelli Feb 5, 2020
904cfab
boundedint: change initial value for boundedints to 0
mortelli Feb 6, 2020
4cee832
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Feb 6, 2020
cc86c61
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Feb 6, 2020
8a26add
main: update config tests for boundedint thresholds
mortelli Feb 6, 2020
e16b8f0
boundedint: change initial value for boundedints to 0
mortelli Feb 6, 2020
fe2924a
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Feb 6, 2020
14bb3b3
boundedint: rename Uint256TestCase structure to BoundedIntTestCase
mortelli Feb 6, 2020
a628927
boundedint: add tests for Int256 type
mortelli Feb 7, 2020
e688755
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Feb 7, 2020
fbd16c6
boundedint: iterate Int256 tests
mortelli Feb 7, 2020
d71e7c0
boundedint: iterate boundedint tests
mortelli Feb 7, 2020
264b9ad
swap: fix bug with wrong package name
mortelli Feb 7, 2020
0bd16a1
boundedint: iterate boundedint tests
mortelli Feb 7, 2020
fff2c10
boundedint: iterate boundedint tests
mortelli Feb 7, 2020
386fc63
boundedint: fix linter errors
mortelli Feb 10, 2020
65f5104
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Feb 10, 2020
af7339a
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Feb 21, 2020
de7f1dd
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Feb 21, 2020
91b56c7
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Feb 24, 2020
6bff02e
swap: fix import order
mortelli Feb 24, 2020
97fe637
swap: fix wrong backend import
mortelli Feb 24, 2020
d59666e
swap, boundedint: rename boundedint package to int256
mortelli Feb 24, 2020
ed4af97
int256: rename test case struct
mortelli Feb 24, 2020
b7f9ed7
int256: rename Uint64ToUint256 func to Uint256From, add Int256From fu…
mortelli Feb 24, 2020
c0ddb1b
int256: small change to test cases names
mortelli Feb 24, 2020
2fe9327
int256: add comments to test cases array
mortelli Feb 24, 2020
f5261bd
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Feb 26, 2020
51de061
int256: rename Int64ToInt256 function to Int256From
mortelli Feb 26, 2020
cc0ed2a
api, main, swap: remove de-reference for default swap thresholds
mortelli Feb 27, 2020
9254abe
main: small refactor to TestConfigCmdLineOverrides function
mortelli Feb 27, 2020
1fc4558
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Mar 10, 2020
ce58332
int256: rename Cmp to cmp to make it private since it was not used ou…
mortelli Mar 11, 2020
308e98c
int256: hard-code int256 and uint256 test cases value fields in which…
mortelli Mar 11, 2020
05ecc7c
int256: change signature of Int256 and Uint256 Copy methods to receiv…
mortelli Mar 11, 2020
25704d3
int256: make Int256 Set function private, instead allow the construct…
mortelli Mar 11, 2020
0dc050a
int256: make Uint256 Set function private, instead allow the construc…
mortelli Mar 11, 2020
f401aa5
swap: replace constructor calls for Uint256 struct with the new signa…
mortelli Mar 11, 2020
7084544
int256: fix docstring for NewUint256 func
mortelli Mar 12, 2020
68d0679
int256: reorder Int256 and Uint256 funcs for more clarity and consist…
mortelli Mar 12, 2020
142a6b5
int256: fix casing in Uint256 set docstring
mortelli Mar 12, 2020
7e66082
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Mar 12, 2020
04bb9a5
swap: replace constructor calls for Uint256 struct with the new signa…
mortelli Mar 12, 2020
95f0d54
cmd/swarm, swap: swap replace constructor calls in tests for Uint256 …
mortelli Mar 12, 2020
f4a2197
int256: change param type of Equals func for Uint256 type to BigIntWr…
mortelli Mar 12, 2020
53d2d26
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Mar 13, 2020
1bbdbf8
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Mar 13, 2020
1ba6969
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Mar 17, 2020
0cb9464
int256: change MarshalJSON to use value semantics
mortelli Mar 19, 2020
3767d3e
int256: change MarshalJSON to use value semantics
mortelli Mar 19, 2020
2aaac70
int256: change 2^8 int256 test case to hard-coded value
mortelli Mar 19, 2020
6f7ba29
int256: remove unnecessary allocations in Int256From, Uint256From, Co…
mortelli Mar 23, 2020
307d6b9
int256: add TestIn256ValueInjection and TestUin256ValueInjection funcs
mortelli Mar 24, 2020
6e2f7e4
int256: change Int256 value field and funcs to user pointer semantics
mortelli Mar 24, 2020
968a3b4
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Mar 24, 2020
7ad6424
int256: change Uint256 value field and funcs to user pointer semantics
mortelli Mar 24, 2020
208ef2f
int256: fix Uint256 tests to work with pointer semantics
mortelli Mar 25, 2020
23dad45
swap, contracts/swap: fix swap code to work with new int256 pointer s…
mortelli Mar 25, 2020
82549c0
swap: fix swap tests to work with new int256 pointer semantics
mortelli Mar 25, 2020
8ef80df
int256: make sure all big.Int Set calls are done from pointer receive…
mortelli Mar 25, 2020
8206716
int256: extract checkUint256Bounds and checkInt256Bounds funcs, fix e…
mortelli Mar 25, 2020
ddbe749
int256: fix wrong call of boundary-checking in `Int256`'s `set` func
mortelli Mar 25, 2020
1b684e6
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Mar 25, 2020
1418124
int256: update BigIntWrapper interface to use pointer semantics
mortelli Mar 25, 2020
e591aff
int256: add nil value checking to checkInt256Bounds and checkUint256B…
mortelli Mar 25, 2020
ac6bcb7
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Mar 29, 2020
138fcbe
int256: quote and unquote Int256 and Uint256 values in marshaling/unm…
mortelli Mar 29, 2020
f7bb5c2
int256: disallow nil values when unmarshaling Int256 and Uint256 structs
mortelli Mar 29, 2020
48b4707
int256: remove unneeded code in Uint256 UnmarshalJSON func
mortelli Mar 29, 2020
acc7d11
Merge remote-tracking branch 'origin/swap-unify-variable-types-iii' i…
mortelli Mar 29, 2020
2d0837f
main: simplify TestConfigCmdLineOverrides thresholds printing
mortelli Mar 29, 2020
159baee
Merge remote-tracking branch 'origin/master' into swap-unify-variable…
mortelli Mar 30, 2020
b57d27a
int256: extend Int256 and Uint256 Value funcs to return nil if possib…
mortelli Mar 30, 2020
8be556e
remove unneeded change to go.mod
mortelli Mar 30, 2020
0f6e721
swap: small refactor to tests
mortelli Mar 30, 2020
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
21 changes: 11 additions & 10 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethersphere/swarm/pss"
"github.com/ethersphere/swarm/storage"
"github.com/ethersphere/swarm/swap"
"github.com/ethersphere/swarm/swap/int256"
)

const (
Expand All @@ -52,16 +53,16 @@ type Config struct {
BaseKey []byte

// Swap configs
SwapBackendURL string // Ethereum API endpoint
SwapEnabled bool // whether SWAP incentives are enabled
SwapPaymentThreshold uint64 // honey amount at which a payment is triggered
SwapDisconnectThreshold uint64 // honey amount at which a peer disconnects
SwapSkipDeposit bool // do not ask the user to deposit during boot sequence
SwapDepositAmount uint64 // deposit amount to the chequebook
SwapLogPath string // dir to swap related audit logs
SwapLogLevel int // log level of swap related audit logs
Contract common.Address // address of the chequebook contract
SwapChequebookFactory common.Address // address of the chequebook factory contract
SwapBackendURL string // Ethereum API endpoint
SwapEnabled bool // whether SWAP incentives are enabled
SwapPaymentThreshold *int256.Uint256 // honey amount at which a payment is triggered
SwapDisconnectThreshold *int256.Uint256 // honey amount at which a peer disconnects
SwapSkipDeposit bool // do not ask the user to deposit during boot sequence
SwapDepositAmount uint64 // deposit amount to the chequebook
SwapLogPath string // dir to swap related audit logs
SwapLogLevel int // log level of swap related audit logs
Contract common.Address // address of the chequebook contract
SwapChequebookFactory common.Address // address of the chequebook factory contract
// end of Swap configs

*network.HiveParams
Expand Down
5 changes: 3 additions & 2 deletions cmd/swarm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

bzzapi "github.com/ethersphere/swarm/api"
"github.com/ethersphere/swarm/network"
"github.com/ethersphere/swarm/swap/int256"
)

var (
Expand Down Expand Up @@ -223,10 +224,10 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
currentConfig.SwapDepositAmount = deposit
}
if paymentThreshold := ctx.GlobalUint64(SwarmSwapPaymentThresholdFlag.Name); paymentThreshold != 0 {
currentConfig.SwapPaymentThreshold = paymentThreshold
currentConfig.SwapPaymentThreshold = int256.Uint256From(paymentThreshold)
}
if disconnectThreshold := ctx.GlobalUint64(SwarmSwapDisconnectThresholdFlag.Name); disconnectThreshold != 0 {
currentConfig.SwapDisconnectThreshold = disconnectThreshold
currentConfig.SwapDisconnectThreshold = int256.Uint256From(disconnectThreshold)
}
if ctx.GlobalIsSet(SwarmNoSyncFlag.Name) {
val := !ctx.GlobalBool(SwarmNoSyncFlag.Name)
Expand Down
28 changes: 21 additions & 7 deletions cmd/swarm/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"net"
"os"
"os/exec"
"strconv"
"testing"
"time"

Expand All @@ -36,6 +35,7 @@ import (
"github.com/ethersphere/swarm"
"github.com/ethersphere/swarm/api"
"github.com/ethersphere/swarm/swap"
"github.com/ethersphere/swarm/swap/int256"
"github.com/ethersphere/swarm/testutil"
)

Expand Down Expand Up @@ -227,6 +227,20 @@ func TestConfigCmdLineOverrides(t *testing.T) {
t.Fatal(err)
}

// add 1 to payment threshold
paymentThresholdOverride, err := new(int256.Uint256).Add(swap.DefaultPaymentThreshold, int256.Uint256From(1))
if err != nil {
t.Fatal(err)
}
paymentThreshold := paymentThresholdOverride.Value()

// add 1 to disconnect threshold
disconnectThresholdOverride, err := new(int256.Uint256).Add(swap.DefaultDisconnectThreshold, int256.Uint256From(1))
if err != nil {
t.Fatal(err)
}
disconnectThreshold := disconnectThresholdOverride.Value()

flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
Expand All @@ -238,8 +252,8 @@ func TestConfigCmdLineOverrides(t *testing.T) {
fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
"--verbosity", fmt.Sprintf("%d", *testutil.Loglevel),
fmt.Sprintf("--%s", SwarmSwapPaymentThresholdFlag.Name), strconv.FormatUint(swap.DefaultPaymentThreshold+1, 10),
fmt.Sprintf("--%s", SwarmSwapDisconnectThresholdFlag.Name), strconv.FormatUint(swap.DefaultDisconnectThreshold+1, 10),
fmt.Sprintf("--%s", SwarmSwapPaymentThresholdFlag.Name), paymentThreshold.String(),
fmt.Sprintf("--%s", SwarmSwapDisconnectThresholdFlag.Name), disconnectThreshold.String(),
fmt.Sprintf("--%s", SwarmEnablePinningFlag.Name),
}

Expand Down Expand Up @@ -287,12 +301,12 @@ func TestConfigCmdLineOverrides(t *testing.T) {
t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
}

if info.SwapPaymentThreshold != (swap.DefaultPaymentThreshold + 1) {
t.Fatalf("Expected SwapPaymentThreshold to be %d, but got %d", swap.DefaultPaymentThreshold+1, info.SwapPaymentThreshold)
if !info.SwapPaymentThreshold.Equals(paymentThresholdOverride) {
t.Fatalf("Expected SwapPaymentThreshold to be %v, but got %v", paymentThresholdOverride, info.SwapPaymentThreshold)
}

if info.SwapDisconnectThreshold != (swap.DefaultDisconnectThreshold + 1) {
t.Fatalf("Expected SwapDisconnectThreshold to be %d, but got %d", swap.DefaultDisconnectThreshold+1, info.SwapDisconnectThreshold)
if !info.SwapDisconnectThreshold.Equals(disconnectThresholdOverride) {
t.Fatalf("Expected SwapDisconnectThreshold to be %v, but got %v", disconnectThresholdOverride, info.SwapDisconnectThreshold)
}

if info.EnablePinning != true {
Expand Down
12 changes: 6 additions & 6 deletions swap/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (s *Swap) APIs() []rpc.API {

type swapAPI interface {
AvailableBalance() (*int256.Uint256, error)
PeerBalance(peer enode.ID) (int64, error)
Balances() (map[enode.ID]int64, error)
PeerBalance(peer enode.ID) (*int256.Int256, error)
Balances() (map[enode.ID]*int256.Int256, error)
PeerCheques(peer enode.ID) (PeerCheques, error)
Cheques() (map[enode.ID]*PeerCheques, error)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *Swap) AvailableBalance() (*int256.Uint256, error) {
}

// PeerBalance returns the balance for a given peer
func (s *Swap) PeerBalance(peer enode.ID) (balance int64, err error) {
func (s *Swap) PeerBalance(peer enode.ID) (balance *int256.Int256, err error) {
if swapPeer := s.getPeer(peer); swapPeer != nil {
swapPeer.lock.Lock()
defer swapPeer.lock.Unlock()
Expand All @@ -106,8 +106,8 @@ func (s *Swap) PeerBalance(peer enode.ID) (balance int64, err error) {
}

// Balances returns the balances for all known SWAP peers
func (s *Swap) Balances() (map[enode.ID]int64, error) {
balances := make(map[enode.ID]int64)
func (s *Swap) Balances() (map[enode.ID]*int256.Int256, error) {
balances := make(map[enode.ID]*int256.Int256)

s.peersLock.Lock()
for peer, swapPeer := range s.peers {
Expand All @@ -121,7 +121,7 @@ func (s *Swap) Balances() (map[enode.ID]int64, error) {
balanceIterFunction := func(key []byte, value []byte) (stop bool, err error) {
peer := keyToID(string(key), balancePrefix)
if _, peerHasBalance := balances[peer]; !peerHasBalance {
var peerBalance int64
var peerBalance *int256.Int256
err = json.Unmarshal(value, &peerBalance)
if err == nil {
balances[peer] = peerBalance
Expand Down
51 changes: 28 additions & 23 deletions swap/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
"github.com/ethersphere/swarm/p2p/protocols"
"github.com/ethersphere/swarm/state"
"github.com/ethersphere/swarm/swap/int256"
)

type peerChequesTestCase struct {
Expand Down Expand Up @@ -40,19 +41,19 @@ func TestPeerBalance(t *testing.T) {
defer clean()

// test balance
setBalance(t, testPeer, 888)
testPeerBalance(t, swap, testPeerID, 888)
setBalance(t, testPeer, int256.Int256From(888))
testPeerBalance(t, swap, testPeerID, int256.Int256From(888))

// test balance after change
setBalance(t, testPeer, 17000)
testPeerBalance(t, swap, testPeerID, 17000)
setBalance(t, testPeer, int256.Int256From(17000))
testPeerBalance(t, swap, testPeerID, int256.Int256From(17000))

// test balance for second peer
testPeer2 := addPeer(t, swap)
testPeer2ID := testPeer2.ID()

setBalance(t, testPeer2, 4)
testPeerBalance(t, swap, testPeer2ID, 4)
setBalance(t, testPeer2, int256.Int256From(4))
testPeerBalance(t, swap, testPeer2ID, int256.Int256From(4))

// test balance for inexistent node
invalidPeerID := adapters.RandomNodeConfig().ID
Expand All @@ -67,23 +68,23 @@ func TestPeerBalance(t *testing.T) {
// test balance for disconnected node
testPeer3 := newDummyPeer().Peer
testPeer3ID := testPeer3.ID()
err = swap.saveBalance(testPeer3ID, 777)
testPeerBalance(t, swap, testPeer3ID, 777)
err = swap.saveBalance(testPeer3ID, int256.Int256From(777))
testPeerBalance(t, swap, testPeer3ID, int256.Int256From(777))

// test previous results are still correct
testPeerBalance(t, swap, testPeerID, 17000)
testPeerBalance(t, swap, testPeer2ID, 4)
testPeerBalance(t, swap, testPeerID, int256.Int256From(17000))
testPeerBalance(t, swap, testPeer2ID, int256.Int256From(4))
}

// tests that expected balance for peer matches the result of the Balance function
func testPeerBalance(t *testing.T, s *Swap, id enode.ID, expectedBalance int64) {
func testPeerBalance(t *testing.T, s *Swap, id enode.ID, expectedBalance *int256.Int256) {
t.Helper()
b, err := s.PeerBalance(id)
if err != nil {
t.Fatal(err)
}
if b != expectedBalance {
t.Fatalf("Expected peer's balance to be %d, but is %d", expectedBalance, b)
if !b.Equals(expectedBalance) {
t.Fatalf("Expected peer's balance to be %v, but is %v", expectedBalance, b)
}
}

Expand All @@ -97,7 +98,7 @@ func addPeer(t *testing.T, s *Swap) *Peer {
}

// sets the given balance for the given peer, fails if there are errors
func setBalance(t *testing.T, p *Peer, balance int64) {
func setBalance(t *testing.T, p *Peer, balance *int256.Int256) {
t.Helper()
err := p.setBalance(balance)
if err != nil {
Expand All @@ -112,38 +113,42 @@ func TestBalances(t *testing.T) {
defer clean()

// test balances are empty
testBalances(t, swap, map[enode.ID]int64{})
testBalances(t, swap, map[enode.ID]*int256.Int256{})

// add peer
testPeer := addPeer(t, swap)
testPeerID := testPeer.ID()

// test balances with one peer
setBalance(t, testPeer, 808)
testBalances(t, swap, map[enode.ID]int64{testPeerID: 808})
balancePeer := int256.Int256From(808)
setBalance(t, testPeer, int256.Int256From(808))
testBalances(t, swap, map[enode.ID]*int256.Int256{testPeerID: balancePeer})

// add second peer
testPeer2 := addPeer(t, swap)
testPeer2ID := testPeer2.ID()

// test balances with second peer
setBalance(t, testPeer2, 123)
testBalances(t, swap, map[enode.ID]int64{testPeerID: 808, testPeer2ID: 123})
balancePeer2 := int256.Int256From(123)
setBalance(t, testPeer2, int256.Int256From(123))
testBalances(t, swap, map[enode.ID]*int256.Int256{testPeerID: balancePeer, testPeer2ID: balancePeer2})

// test balances after balance change for peer
setBalance(t, testPeer, 303)
testBalances(t, swap, map[enode.ID]int64{testPeerID: 303, testPeer2ID: 123})
balancePeer = int256.Int256From(303)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my favorite :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am somewhat partial to 909 as well 😛

balancePeer2 = int256.Int256From(123)
setBalance(t, testPeer, int256.Int256From(303))
testBalances(t, swap, map[enode.ID]*int256.Int256{testPeerID: balancePeer, testPeer2ID: balancePeer2})
}

// tests that a map of peerID:balance matches the result of the Balances function
func testBalances(t *testing.T, s *Swap, expectedBalances map[enode.ID]int64) {
func testBalances(t *testing.T, s *Swap, expectedBalances map[enode.ID]*int256.Int256) {
t.Helper()
actualBalances, err := s.Balances()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(actualBalances, expectedBalances) {
t.Fatalf("Expected node's balances to be %d, but are %d", expectedBalances, actualBalances)
t.Fatalf("Expected node's balances to be %v, but are %v", expectedBalances, actualBalances)
}
}

Expand Down
4 changes: 2 additions & 2 deletions swap/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func newDefaultParams(t *testing.T) *Params {
BaseAddrs: network.NewBzzAddr(baseKey, nil),
LogPath: emptyLogPath,
LogLevel: DefaultSwapLogLevel, //Info level
PaymentThreshold: int64(DefaultPaymentThreshold),
DisconnectThreshold: int64(DefaultDisconnectThreshold),
PaymentThreshold: DefaultPaymentThreshold,
DisconnectThreshold: DefaultDisconnectThreshold,
}
}

Expand Down
11 changes: 8 additions & 3 deletions swap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ package swap

import (
"time"

"github.com/ethersphere/swarm/swap/int256"
)

// These are currently arbitrary values which have not been verified nor tested
// Need experimentation to arrive to values which make sense
const (
// Thresholds which trigger payment or disconnection. The unit is in honey (internal accounting unit)
// DefaultPaymentThreshold is set to be equivalent to requesting and serving 10mb of data (2441 chunks (4096 bytes) = 10 mb, 10^7 bytes = 10 mb)
DefaultPaymentThreshold = 2441*RetrieveRequestPrice + (10^7)*ChunkDeliveryPrice // 4096 * 2441 = 10 mb,
DefaultDisconnectThreshold = 20 * DefaultPaymentThreshold
defaultPaymentThreshold = 2441*RetrieveRequestPrice + (10^7)*ChunkDeliveryPrice // 4096 * 2441 = 10 mb,
defaultDisconnectThreshold = 20 * defaultPaymentThreshold
// ChequeDebtTolerance is the lowest resulting balance a node is willing to accept when receiving a cheque
// the value is meant to be used below 0, as positive resulting balances should always be accepted when receiving cheques
ChequeDebtTolerance = DefaultPaymentThreshold * 20 / 100 // roughly 20% of the payment threshold
ChequeDebtTolerance = defaultPaymentThreshold * 20 / 100 // roughly 20% of the payment threshold
// DefaultDepositAmount is the default amount to send to the contract when initially deploying
// NOTE: deliberate value for now; needs experimentation
DefaultDepositAmount = 0
Expand All @@ -40,3 +42,6 @@ const (
AllowedNetworkID = 5
DefaultTransactionTimeout = 10 * time.Minute
)

var DefaultPaymentThreshold = int256.Uint256From(defaultPaymentThreshold)
var DefaultDisconnectThreshold = int256.Uint256From(defaultDisconnectThreshold)
17 changes: 13 additions & 4 deletions swap/int256/int256.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Int256 struct {
value *big.Int
}

// BigIntWrapper represents a struct with an underlying big.Int value
type BigIntWrapper interface {
Value() *big.Int
acud marked this conversation as resolved.
Show resolved Hide resolved
}

var minInt256 = new(big.Int).Mul(big.NewInt(-1), new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil)) // -(2^255)
var maxInt256 = new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(1)) // 2^255 - 1

Expand Down Expand Up @@ -57,6 +62,10 @@ func (u *Int256) Copy() *Int256 {

// Value returns the underlying private value for a Int256 struct
func (u *Int256) Value() *big.Int {
if u.value == nil {
return nil
}
// clone the value to avoid external modification
return new(big.Int).Set(u.value)
}

Expand Down Expand Up @@ -110,13 +119,13 @@ func (u *Int256) Mul(multiplicand, multiplier *Int256) (*Int256, error) {
}

// cmp calls the underlying Cmp method for the big.Int stored in a Int256 struct as its value field
func (u *Int256) cmp(v *Int256) int {
return u.value.Cmp(v.value)
func (u *Int256) Cmp(v BigIntWrapper) int {
return u.value.Cmp(v.Value())
}

// Equals returns true if the two Int256 structs have the same underlying values, false otherwise
func (u *Int256) Equals(v *Int256) bool {
return u.cmp(v) == 0
func (u *Int256) Equals(v BigIntWrapper) bool {
return u.Cmp(v) == 0
}

// String returns the string representation for Int256 structs
Expand Down
10 changes: 7 additions & 3 deletions swap/int256/uint256.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (u *Uint256) Copy() *Uint256 {

// Value returns the underlying private value for a Uint256 struct
func (u *Uint256) Value() *big.Int {
if u.value == nil {
return nil
}
// clone the value to avoid external modification
return new(big.Int).Set(u.value)
}

Expand Down Expand Up @@ -110,12 +114,12 @@ func (u *Uint256) Mul(multiplicand, multiplier *Uint256) (*Uint256, error) {
}

// Cmp calls the underlying Cmp method for the big.Int stored in a Uint256 struct as its value field
func (u *Uint256) Cmp(v *Uint256) int {
return u.value.Cmp(v.value)
func (u *Uint256) Cmp(v BigIntWrapper) int {
return u.value.Cmp(v.Value())
}

// Equals returns true if the two Uint256 structs have the same underlying values, false otherwise
func (u *Uint256) Equals(v *Uint256) bool {
func (u *Uint256) Equals(v BigIntWrapper) bool {
return u.Cmp(v) == 0
}

Expand Down
Loading