Skip to content

Commit

Permalink
feat(multiledger): Add new test case for egoistic funding.
Browse files Browse the repository at this point in the history
Signed-off-by: Sophia Koehler <[email protected]>
  • Loading branch information
sophia1ch committed Mar 26, 2024
1 parent 318905c commit e2a24c8
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 14 deletions.
8 changes: 8 additions & 0 deletions client/multiledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ func TestMultiLedgerDispute(t *testing.T) {
mlt := test.SetupMultiLedgerTest(t, testDuration)
ctest.TestMultiLedgerDispute(ctx, t, mlt, challengeDuration)
}

func TestEgoisticMultiLedger(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testDuration)
defer cancel()

mlt := test.EgoisticTest(t, testDuration)
ctest.TestMultiLedgerDispute(ctx, t, mlt, challengeDuration)
}
132 changes: 118 additions & 14 deletions client/test/multiledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,58 @@ func SetupMultiLedgerTest(t *testing.T, testDuration time.Duration) ctest.MultiL
bus := wire.NewLocalBus()

// Setup clients.
c1 := setupClient(t, rng, l1, l2, bus, true)
c2 := setupClient(t, rng, l1, l2, bus, false)
c1 := setupClient(t, rng, l1, l2, bus)
c2 := setupClient(t, rng, l1, l2, bus)

// Fund accounts.
l1.simSetup.SimBackend.FundAddress(ctx, wallet.AsEthAddr(c1.WalletAddress))
l1.simSetup.SimBackend.FundAddress(ctx, wallet.AsEthAddr(c2.WalletAddress))
l2.simSetup.SimBackend.FundAddress(ctx, wallet.AsEthAddr(c1.WalletAddress))
l2.simSetup.SimBackend.FundAddress(ctx, wallet.AsEthAddr(c2.WalletAddress))

//nolint:gomnd
return ctest.MultiLedgerSetup{
Client1: c1,
Client2: c2,
Asset1: l1.asset,
Asset2: l2.asset,
InitBalances: channel.Balances{
{EtherToWei(8), EtherToWei(2)}, // Asset 1.
{EtherToWei(2), EtherToWei(8)}, // Asset 2.
},
UpdateBalances1: channel.Balances{
{EtherToWei(5), EtherToWei(5)}, // Asset 1.
{EtherToWei(3), EtherToWei(7)}, // Asset 2.
},
UpdateBalances2: channel.Balances{
{EtherToWei(1), EtherToWei(9)}, // Asset 1.
{EtherToWei(5), EtherToWei(5)}, // Asset 2.
},
BalanceDelta: EtherToWei(0.00012),
}
}

// SetupMultiLedgerTest creates a multi-ledger test setup.

Check warning on line 95 in client/test/multiledger.go

View workflow job for this annotation

GitHub Actions / Lint

exported: comment on exported function EgoisticTest should be of the form "EgoisticTest ..." (revive)
func EgoisticTest(t *testing.T, testDuration time.Duration) ctest.MultiLedgerSetup {
t.Helper()
rng := test.Prng(t)

ctx, cancel := context.WithTimeout(context.Background(), testDuration)
defer cancel()

l1 := setupLedger(ctx, t, rng, big.NewInt(1337)) //nolint:gomnd
l2 := setupLedger(ctx, t, rng, big.NewInt(1338)) //nolint:gomnd
l3 := setupLedger(ctx, t, rng, big.NewInt(134)) //nolint:gomnd

// Setup message bus.
bus := wire.NewLocalBus()

// Setup clients.
c1 := setupClient(t, rng, l1, l2, bus)
c2 := setupClient(t, rng, l1, l2, bus)

// Setup clients.
TestEgoisticLedger(t, rng, l1, l2, l3)

// Fund accounts.
l1.simSetup.SimBackend.FundAddress(ctx, wallet.AsEthAddr(c1.WalletAddress))
Expand Down Expand Up @@ -126,7 +176,7 @@ func setupLedger(ctx context.Context, t *testing.T, rng *rand.Rand, chainID *big
}
}

func setupClient(t *testing.T, rng *rand.Rand, l1, l2 testLedger, bus wire.Bus, egoistic bool) ctest.MultiLedgerClient {
func setupClient(t *testing.T, rng *rand.Rand, l1, l2 testLedger, bus wire.Bus) ctest.MultiLedgerClient {
t.Helper()
require := require.New(t)

Expand Down Expand Up @@ -154,17 +204,7 @@ func setupClient(t *testing.T, rng *rand.Rand, l1, l2 testLedger, bus wire.Bus,
multiFunder := multi.NewFunder()
funderL1 := ethchannel.NewFunder(cb1)
funderL2 := ethchannel.NewFunder(cb2)
if egoistic {
egoisticPart := make([]bool, 2)
egoisticPart[1] = true
funderL2.SetEgoisticPart(1, 2)
require.Equal(egoisticPart, funderL2.EgoisticPart)

egoisticChains := make(map[multi.LedgerIDMapKey]bool)
egoisticChains[funderL2.ChainID().MapKey()] = true
multiFunder.SetEgoisticChain(funderL2.ChainID(), true)
require.Equal(egoisticChains, multiFunder.EgoisticChains)
}

registered := funderL1.RegisterAsset(*l1.asset, ethchannel.NewETHDepositor(defaultETHGasLimit), acc.Account)
require.True(registered)
registered = funderL1.RegisterAsset(*l2.asset, ethchannel.NewNoOpDepositor(), acc.Account)
Expand Down Expand Up @@ -212,6 +252,70 @@ func setupClient(t *testing.T, rng *rand.Rand, l1, l2 testLedger, bus wire.Bus,
}
}

func TestEgoisticLedger(t *testing.T, rng *rand.Rand, l1, l2, l3 testLedger) {

Check warning on line 255 in client/test/multiledger.go

View workflow job for this annotation

GitHub Actions / Lint

exported: exported function TestEgoisticLedger should have comment or be unexported (revive)
t.Helper()
require := require.New(t)

// Setup wallet and account.
w := wtest.RandomWallet().(*keystore.Wallet)
acc := w.NewRandomAccount(rng).(*keystore.Account)

// Setup contract backends.
signer1 := l1.simSetup.SimBackend.Signer
cb1 := ethchannel.NewContractBackend(
l1.simSetup.CB,
l1.ChainID(),
keystore.NewTransactor(*w, signer1),
l1.simSetup.CB.TxFinalityDepth(),
)
signer2 := l2.simSetup.SimBackend.Signer
cb2 := ethchannel.NewContractBackend(
l2.simSetup.CB,
l2.ChainID(),
keystore.NewTransactor(*w, signer2),
l2.simSetup.CB.TxFinalityDepth(),
)

signer3 := l3.simSetup.SimBackend.Signer
cb3 := ethchannel.NewContractBackend(
l3.simSetup.CB,
l3.ChainID(),
keystore.NewTransactor(*w, signer3),
l3.simSetup.CB.TxFinalityDepth(),
)

// Setup funder.
multiFunder := multi.NewFunder()
funderL1 := ethchannel.NewFunder(cb1)
funderL2 := ethchannel.NewFunder(cb2)
funderL3 := ethchannel.NewFunder(cb3)
egoisticPart := make([]bool, 3)

Check failure on line 292 in client/test/multiledger.go

View workflow job for this annotation

GitHub Actions / Lint

mnd: Magic number: 3, in <argument> detected (gomnd)
egoisticPart[1] = true
funderL2.SetEgoisticPart(1, 3)

Check failure on line 294 in client/test/multiledger.go

View workflow job for this annotation

GitHub Actions / Lint

mnd: Magic number: 3, in <argument> detected (gomnd)
require.Equal(egoisticPart, funderL2.EgoisticPart)

egoisticChains := make(map[multi.LedgerIDMapKey]bool)
egoisticChains[funderL2.ChainID().MapKey()] = true
multiFunder.SetEgoisticChain(funderL2.ChainID(), true)
require.Equal(egoisticChains, multiFunder.EgoisticChains)

registered := funderL1.RegisterAsset(*l1.asset, ethchannel.NewETHDepositor(defaultETHGasLimit), acc.Account)
require.True(registered)
registered = funderL1.RegisterAsset(*l2.asset, ethchannel.NewNoOpDepositor(), acc.Account)
require.True(registered)
registered = funderL2.RegisterAsset(*l1.asset, ethchannel.NewNoOpDepositor(), acc.Account)
require.True(registered)
registered = funderL2.RegisterAsset(*l2.asset, ethchannel.NewETHDepositor(defaultETHGasLimit), acc.Account)
require.True(registered)
registered = funderL3.RegisterAsset(*l1.asset, ethchannel.NewNoOpDepositor(), acc.Account)
require.True(registered)
registered = funderL3.RegisterAsset(*l2.asset, ethchannel.NewETHDepositor(defaultETHGasLimit), acc.Account)
require.True(registered)
multiFunder.RegisterFunder(l1.ChainID(), funderL1)
multiFunder.RegisterFunder(l2.ChainID(), funderL2)
multiFunder.RegisterFunder(l3.ChainID(), funderL3)
}

// EtherToWei converts eth to wei.
func EtherToWei(eth float64) *big.Int {
weiFloat := new(big.Float).Mul(big.NewFloat(eth), new(big.Float).SetFloat64(params.Ether))
Expand Down

0 comments on commit e2a24c8

Please sign in to comment.