Skip to content

Commit 1ea6f52

Browse files
authored
Merge pull request #286 from Chinwendu20/UBPeer
Added unban peer feature
2 parents 602843d + 3275e33 commit 1ea6f52

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

banman/store.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ type Store interface {
6666

6767
// Status returns the ban status for a given IP network.
6868
Status(*net.IPNet) (Status, error)
69+
70+
// UnbanIPNet removes the ban imposed on the specified peer.
71+
UnbanIPNet(ipNet *net.IPNet) error
6972
}
7073

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

142+
// UnbanIPNet removes a ban record for the IP network within the store.
143+
func (s *banStore) UnbanIPNet(ipNet *net.IPNet) error {
144+
err := walletdb.Update(s.db, func(tx walletdb.ReadWriteTx) error {
145+
banStore := tx.ReadWriteBucket(banStoreBucket)
146+
if banStore == nil {
147+
return ErrCorruptedStore
148+
}
149+
150+
banIndex := banStore.NestedReadWriteBucket(banBucket)
151+
if banIndex == nil {
152+
return ErrCorruptedStore
153+
}
154+
155+
reasonIndex := banStore.NestedReadWriteBucket(reasonBucket)
156+
if reasonIndex == nil {
157+
return ErrCorruptedStore
158+
}
159+
160+
var ipNetBuf bytes.Buffer
161+
if err := encodeIPNet(&ipNetBuf, ipNet); err != nil {
162+
return fmt.Errorf("unable to encode %v: %v", ipNet,
163+
err)
164+
}
165+
166+
k := ipNetBuf.Bytes()
167+
168+
return removeBannedIPNet(banIndex, reasonIndex, k)
169+
})
170+
171+
return err
172+
}
173+
139174
// addBannedIPNet adds an entry to the ban store for the given IP network.
140175
func addBannedIPNet(banIndex, reasonIndex walletdb.ReadWriteBucket,
141176
ipNetKey []byte, reason Reason, duration time.Duration) error {

banman/store_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/btcsuite/btcwallet/walletdb"
1212
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
1313
"github.com/lightninglabs/neutrino/banman"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
// createTestBanStore creates a test Store backed by a boltdb instance.
@@ -123,4 +124,10 @@ func TestBanStore(t *testing.T) {
123124
// We'll query for second IP network again as it should now be unknown
124125
// to the BanStore. We should expect not to find anything regarding it.
125126
checkBanStore(ipNet2, false, 0, 0)
127+
128+
// Test UnbanIPNet.
129+
require.NoError(t, banStore.UnbanIPNet(ipNet1))
130+
131+
// We would now check that ipNet1 is indeed unbanned.
132+
checkBanStore(ipNet1, false, 0, 0)
126133
}

neutrino.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,24 @@ func (s *ChainService) BanPeer(addr string, reason banman.Reason) error {
10811081
return s.banStore.BanIPNet(ipNet, reason, BanDuration)
10821082
}
10831083

1084+
// UnbanPeer connects and unbans a previously banned peer.
1085+
func (s *ChainService) UnbanPeer(addr string, parmanent bool) error {
1086+
log.Infof("UnBanning peer %v", addr)
1087+
1088+
ipNet, err := banman.ParseIPNet(addr, nil)
1089+
if err != nil {
1090+
return fmt.Errorf("unable to parse IP network for peer %v: %v",
1091+
addr, err)
1092+
}
1093+
1094+
err = s.banStore.UnbanIPNet(ipNet)
1095+
if err != nil {
1096+
return fmt.Errorf("unable to unban peer: %v", err)
1097+
}
1098+
1099+
return s.ConnectNode(addr, parmanent)
1100+
}
1101+
10841102
// IsBanned returns true if the peer is banned, and false otherwise.
10851103
func (s *ChainService) IsBanned(addr string) bool {
10861104
ipNet, err := banman.ParseIPNet(addr, nil)

0 commit comments

Comments
 (0)