From 4ba9a154ea9d4e3d161c2c5af17c9d18dd5301aa Mon Sep 17 00:00:00 2001 From: Ononiwu Maureen Date: Wed, 19 Apr 2023 18:53:42 +0100 Subject: [PATCH] banmen: Added new method to banstore, UnBanIPNet Signed-off-by: Ononiwu Maureen --- banman/store.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/banman/store.go b/banman/store.go index 2eedfd349..2ae4ebeb6 100644 --- a/banman/store.go +++ b/banman/store.go @@ -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. @@ -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 {