Skip to content

Commit

Permalink
Merge pull request #286 from Chinwendu20/UBPeer
Browse files Browse the repository at this point in the history
Added unban peer feature
  • Loading branch information
guggero authored May 23, 2024
2 parents 602843d + 3275e33 commit 1ea6f52
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
35 changes: 35 additions & 0 deletions banman/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Store interface {

// Status returns the ban status for a given IP network.
Status(*net.IPNet) (Status, error)

// UnbanIPNet removes the ban imposed on the specified peer.
UnbanIPNet(ipNet *net.IPNet) error
}

// NewStore returns a Store backed by a database.
Expand Down Expand Up @@ -136,6 +139,38 @@ func (s *banStore) BanIPNet(ipNet *net.IPNet, reason Reason, duration time.Durat
})
}

// UnbanIPNet removes a ban record for the IP network within the store.
func (s *banStore) UnbanIPNet(ipNet *net.IPNet) error {
err := walletdb.Update(s.db, func(tx walletdb.ReadWriteTx) error {
banStore := tx.ReadWriteBucket(banStoreBucket)
if banStore == nil {
return ErrCorruptedStore
}

banIndex := banStore.NestedReadWriteBucket(banBucket)
if banIndex == nil {
return ErrCorruptedStore
}

reasonIndex := banStore.NestedReadWriteBucket(reasonBucket)
if reasonIndex == nil {
return ErrCorruptedStore
}

var ipNetBuf bytes.Buffer
if err := encodeIPNet(&ipNetBuf, ipNet); err != nil {
return fmt.Errorf("unable to encode %v: %v", ipNet,
err)
}

k := ipNetBuf.Bytes()

return removeBannedIPNet(banIndex, reasonIndex, k)
})

return err
}

// addBannedIPNet adds an entry to the ban store for the given IP network.
func addBannedIPNet(banIndex, reasonIndex walletdb.ReadWriteBucket,
ipNetKey []byte, reason Reason, duration time.Duration) error {
Expand Down
7 changes: 7 additions & 0 deletions banman/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/lightninglabs/neutrino/banman"
"github.com/stretchr/testify/require"
)

// createTestBanStore creates a test Store backed by a boltdb instance.
Expand Down Expand Up @@ -123,4 +124,10 @@ func TestBanStore(t *testing.T) {
// We'll query for second IP network again as it should now be unknown
// to the BanStore. We should expect not to find anything regarding it.
checkBanStore(ipNet2, false, 0, 0)

// Test UnbanIPNet.
require.NoError(t, banStore.UnbanIPNet(ipNet1))

// We would now check that ipNet1 is indeed unbanned.
checkBanStore(ipNet1, false, 0, 0)
}
18 changes: 18 additions & 0 deletions neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,24 @@ func (s *ChainService) BanPeer(addr string, reason banman.Reason) error {
return s.banStore.BanIPNet(ipNet, reason, BanDuration)
}

// UnbanPeer connects and unbans a previously banned peer.
func (s *ChainService) UnbanPeer(addr string, parmanent bool) error {
log.Infof("UnBanning peer %v", addr)

ipNet, err := banman.ParseIPNet(addr, nil)
if err != nil {
return fmt.Errorf("unable to parse IP network for peer %v: %v",
addr, err)
}

err = s.banStore.UnbanIPNet(ipNet)
if err != nil {
return fmt.Errorf("unable to unban peer: %v", err)
}

return s.ConnectNode(addr, parmanent)
}

// IsBanned returns true if the peer is banned, and false otherwise.
func (s *ChainService) IsBanned(addr string) bool {
ipNet, err := banman.ParseIPNet(addr, nil)
Expand Down

0 comments on commit 1ea6f52

Please sign in to comment.