Skip to content

Commit

Permalink
Merge pull request #107 from renproject/feat/use-signatory-field-for-…
Browse files Browse the repository at this point in the history
…query

fix(store): index multiaddrs by signatory
  • Loading branch information
jazg committed Nov 6, 2020
2 parents edf47e9 + a10577a commit f9e1ec2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var _ = Describe("Resolver", func() {
resolver := init(ctx)
defer cleanup()

urlI, err := url.Parse("http://localhost/?id=localhost")
urlI, err := url.Parse("http://localhost/?id=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
Expect(err).ShouldNot(HaveOccurred())
params := jsonrpc.ParamsQueryConfig{}
req := http.Request{
Expand Down
25 changes: 18 additions & 7 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ type MultiAddrStore struct {

// New constructs a new `MultiAddrStore`.
func New(store db.Table, bootstrapAddrs []wire.Address) MultiAddrStore {
multiStore := MultiAddrStore{
store: store,
bootstrapAddrs: bootstrapAddrs,
}

for _, addr := range bootstrapAddrs {
if err := store.Insert(addr.Value, addr.String()); err != nil {
if err := multiStore.Insert(addr); err != nil {
panic(fmt.Sprintf("[MultiAddrStore] cannot initialize the store with bootstrap nodes addresses"))
}
}
return MultiAddrStore{
store: store,
bootstrapAddrs: bootstrapAddrs,
}
return multiStore
}

// Get retrieves a multi-address from the store.
Expand All @@ -38,12 +40,21 @@ func (multiStore *MultiAddrStore) Get(id string) (wire.Address, error) {

// Insert puts the given multi-address into the store.
func (multiStore *MultiAddrStore) Insert(addr wire.Address) error {
return multiStore.store.Insert(addr.Value, addr.String())
signatory, err := addr.Signatory()
if err != nil {
return err
}

return multiStore.store.Insert(signatory.String(), addr.String())
}

// Delete removes the given multi-address from the store.
func (multiStore *MultiAddrStore) Delete(addr wire.Address) error {
return multiStore.store.Delete(addr.Value)
signatory, err := addr.Signatory()
if err != nil {
return err
}
return multiStore.store.Delete(signatory.String())
}

// Size returns the number of entries in the store.
Expand Down
21 changes: 21 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,26 @@ var _ = Describe("Store", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(len(addrs)).To(Equal(randomSize))
})

It("should fetch the correct multiaddr for a given signatory", func() {
r := rand.New(rand.NewSource(GinkgoRandomSeed()))
expectedSize := rand.Intn(100) + 1
addrs := make([]wire.Address, expectedSize)
for i := 0; i < expectedSize; i++ {
addrs[i] = randomAddress(r)
}
addrStore := New(kv.NewTable(kv.NewMemDB(kv.JSONCodec), "addresses"), nil)

for i := 0; i < expectedSize; i++ {
Expect(addrStore.Insert(addrs[i])).ShouldNot(HaveOccurred())
signatory, err := addrs[i].Signatory()
Expect(err).ShouldNot(HaveOccurred())
fetchedAddr, err := addrStore.Get(signatory.String())
Expect(err).ShouldNot(HaveOccurred())
Expect(fetchedAddr).To(Equal(addrs[i]))

}
Expect(len(addrs)).To(Equal(expectedSize))
})
})
})
6 changes: 4 additions & 2 deletions testutils/darknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func NewMockDarknode(serverURL string, store store.MultiAddrStore) *MockDarknode
panic(err)
}
addr := wire.NewUnsignedAddress(wire.TCP, fmt.Sprintf("%v:%v", host, port), uint64(time.Now().Unix()))
if err := store.Insert(addr); err != nil {

if err := addr.Sign((*id.PrivKey)(key)); err != nil {
panic(err)
}
if err := addr.Sign((*id.PrivKey)(key)); err != nil {
if err := store.Insert(addr); err != nil {
panic(err)
}

return &MockDarknode{
Me: addr,
Store: store,
Expand Down

0 comments on commit f9e1ec2

Please sign in to comment.