Skip to content

Commit

Permalink
banman: Added UnbanIPNet method to banstore
Browse files Browse the repository at this point in the history
Signed-off-by: Ononiwu Maureen <amaka013@gmail.com>
  • Loading branch information
Chinwendu20 committed May 10, 2024
1 parent 42a196f commit eb5942f
Show file tree
Hide file tree
Showing 2 changed files with 42 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(ipNet2, false, 0, 0)
}

0 comments on commit eb5942f

Please sign in to comment.