Skip to content

Commit

Permalink
Merge pull request #253 from YusukeShimizu/suspicious-csv-recovery
Browse files Browse the repository at this point in the history
swap: automatically add to the suspicious list If csv recovery
  • Loading branch information
nepet authored Nov 12, 2023
2 parents 5a35f88 + 023a2a5 commit c22211a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sample_policy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ reserve_onchain_msat=0
allowlisted_peers=peer1
allowlisted_peers=peer2
allowlisted_peers=peer3
accept_all_peers=0
accept_all_peers=0
14 changes: 14 additions & 0 deletions swap/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,3 +813,17 @@ func (c *CancelAction) Execute(services *SwapServices, swap *SwapData) EventType

return Event_Done
}

type AddSuspiciousPeerAction struct {
next Action
}

func (c *AddSuspiciousPeerAction) Execute(services *SwapServices, swap *SwapData) EventType {
if err := services.policy.AddToSuspiciousPeerList(swap.PeerNodeId); err != nil {
// Since retries are unlikely to succeed,log output and move to the next state.
log.Infof("error adding peer %s to suspicious peer list: %v", swap.PeerNodeId, err)
return c.next.Execute(services, swap)
}
log.Infof("added peer %s to suspicious peer list", swap.PeerNodeId)
return c.next.Execute(services, swap)
}
1 change: 1 addition & 0 deletions swap/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PeerMessage interface {
type Policy interface {
IsPeerAllowed(peer string) bool
IsPeerSuspicious(peer string) bool
AddToSuspiciousPeerList(pubkey string) error
GetReserveOnchainMsat() uint64
GetMinSwapAmountMsat() uint64
NewSwapsAllowed() bool
Expand Down
2 changes: 1 addition & 1 deletion swap/swap_in_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func getSwapInSenderStates() States {
Action: &NoOpDoneAction{},
},
State_ClaimedCsv: {
Action: &NoOpDoneAction{},
Action: &AddSuspiciousPeerAction{next: &NoOpDoneAction{}},
},
State_ClaimedCoop: {
Action: &NoOpDoneAction{},
Expand Down
2 changes: 1 addition & 1 deletion swap/swap_out_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func getSwapOutReceiverStates() States {
Action: &CancelAction{},
},
State_ClaimedCsv: {
Action: &NoOpDoneAction{},
Action: &AddSuspiciousPeerAction{next: &NoOpDoneAction{}},
},
State_ClaimedPreimage: {
Action: &NoOpDoneAction{},
Expand Down
4 changes: 4 additions & 0 deletions swap/swap_out_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ func (d *dummyPolicy) IsPeerAllowed(peer string) bool {
return true
}

func (d *dummyPolicy) AddToSuspiciousPeerList(pubkey string) error {
return nil
}

func (d *dummyPolicy) IsPeerSuspicious(peer string) bool {
d.isPeerSuspiciousParam = peer
return d.isPeerSuspiciousReturn
Expand Down
10 changes: 5 additions & 5 deletions test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func clnclnSetupWithConfig(t *testing.T, fundAmt uint64, clnConf []string) (*tes
if err != nil {
t.Fatal("could not create dir", err)
}
err = os.WriteFile(filepath.Join(lightningd.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1"), os.ModePerm)
err = os.WriteFile(filepath.Join(lightningd.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1\n"), os.ModePerm)
if err != nil {
t.Fatal("could not create policy file", err)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func mixedSetup(t *testing.T, fundAmt uint64, funder fundingNode) (*testframewor
if err != nil {
t.Fatal("could not create dir", err)
}
err = os.WriteFile(filepath.Join(cln.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1"), os.ModePerm)
err = os.WriteFile(filepath.Join(cln.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1\n"), os.ModePerm)
if err != nil {
t.Fatal("could not create policy file", err)
}
Expand Down Expand Up @@ -359,7 +359,7 @@ func clnclnElementsSetup(t *testing.T, fundAmt uint64) (*testframework.BitcoinNo
if err != nil {
t.Fatal("could not create dir", err)
}
err = os.WriteFile(filepath.Join(lightningd.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1"), os.ModePerm)
err = os.WriteFile(filepath.Join(lightningd.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1\n"), os.ModePerm)
if err != nil {
t.Fatal("could not create policy file", err)
}
Expand Down Expand Up @@ -524,7 +524,7 @@ func lndlndElementsSetup(t *testing.T, fundAmt uint64) (*testframework.BitcoinNo
t.Cleanup(peerswapd.Kill)

// Create policy file and accept all peers
err = os.WriteFile(filepath.Join(peerswapd.DataDir, "..", "policy.conf"), []byte("accept_all_peers=1"), os.ModePerm)
err = os.WriteFile(filepath.Join(peerswapd.DataDir, "..", "policy.conf"), []byte("accept_all_peers=1\n"), os.ModePerm)
if err != nil {
t.Fatal("could not create policy file", err)
}
Expand Down Expand Up @@ -623,7 +623,7 @@ func mixedElementsSetup(t *testing.T, fundAmt uint64, funder fundingNode) (*test
if err != nil {
t.Fatal("could not create dir", err)
}
err = os.WriteFile(filepath.Join(cln.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1"), os.ModePerm)
err = os.WriteFile(filepath.Join(cln.GetDataDir(), "peerswap", "policy.conf"), []byte("accept_all_peers=1\n"), os.ModePerm)
if err != nil {
t.Fatal("could not create policy file", err)
}
Expand Down
3 changes: 3 additions & 0 deletions test/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,7 @@ func csvClaimTest(t *testing.T, params *testParams) {
require.InDelta(params.origMakerWallet-commitFee-claimFee, balance, 1., "expected %d, got %d",
params.origMakerWallet-commitFee-claimFee, balance)

require.NoError(params.makerPeerswap.WaitForLog(
fmt.Sprintf("added peer %s to suspicious peer list", params.takerNode.Id()),
testframework.TIMEOUT))
}

0 comments on commit c22211a

Please sign in to comment.