Skip to content

Commit

Permalink
banmen: Added new method to banstore, UnBanIPNet
Browse files Browse the repository at this point in the history
Signed-off-by: Ononiwu Maureen <[email protected]>
  • Loading branch information
Chinwendu20 committed Apr 19, 2023
1 parent 9badd7f commit 4ba9a15
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 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 unbans previously banned peer
UnBanIPNet(ipNet *net.IPNet) error
}

// NewStore returns a Store backed by a database.
Expand Down Expand Up @@ -136,6 +139,34 @@ 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

0 comments on commit 4ba9a15

Please sign in to comment.