Skip to content

Commit

Permalink
Do not clobber address_book.seed file
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Oct 23, 2024
1 parent 89fee4b commit 07555ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
69 changes: 45 additions & 24 deletions storage/localstate_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

const keyFragmentSeed = "TSS_FRAGMENT_SEED"
const addressBookName = "address_book"

type LocalPartySaveData interface {
}
Expand Down Expand Up @@ -173,7 +174,7 @@ func (fsm *FileStateMgr) SaveAddressBook(address map[peer.ID][]maddr.Multiaddr)
if len(fsm.folder) < 1 {
return errors.New("base file path is invalid")
}
filePathName := filepath.Join(fsm.folder, "address_book.seed")
filePathName := filepath.Join(fsm.folder, addressBookName)
var buf bytes.Buffer

for peer, addrs := range address {
Expand All @@ -191,41 +192,35 @@ func (fsm *FileStateMgr) SaveAddressBook(address map[peer.ID][]maddr.Multiaddr)
}
fsm.writeLock.Lock()
defer fsm.writeLock.Unlock()
return ioutil.WriteFile(filePathName, buf.Bytes(), 0o600)
return os.WriteFile(filePathName, buf.Bytes(), 0o600)
}

// RetrieveP2PAddresses loads addresses from both the seed address book
// and state address book
func (fsm *FileStateMgr) RetrieveP2PAddresses() ([]maddr.Multiaddr, error) {
if len(fsm.folder) < 1 {
return nil, errors.New("base file path is invalid")
}
filePathName := filepath.Join(fsm.folder, "address_book.seed")
filePathName = filepath.Clean(filePathName)

_, err := os.Stat(filePathName)
if err != nil {
return nil, err
}
fsm.writeLock.RLock()
input, err := ioutil.ReadFile(filePathName)
if err != nil {
fsm.writeLock.RUnlock()
defer fsm.writeLock.RUnlock()

seedPath := filepath.Join(fsm.folder, "address_book.seed")
seedAddresses, err := loadAddressBook(seedPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
fsm.writeLock.RUnlock()
data := strings.Split(string(input), "\n")
var peerAddresses []maddr.Multiaddr
for _, el := range data {
// we skip the empty entry
if len(el) == 0 {
continue
}
addr, err := maddr.NewMultiaddr(el)
if err != nil {
return nil, fmt.Errorf("invalid address in address book %w", err)

savePath := filepath.Join(fsm.folder, addressBookName)
savedAddresses, err := loadAddressBook(savePath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrNotExist) {
return seedAddresses, nil
}
peerAddresses = append(peerAddresses, addr)
return seedAddresses, err
}
return peerAddresses, nil

return append(seedAddresses, savedAddresses...), nil
}

func (fsm *FileStateMgr) encryptFragment(plainText []byte) ([]byte, error) {
Expand Down Expand Up @@ -287,3 +282,29 @@ func getFragmentSeed(password string) ([]byte, error) {
seed := h.Sum(nil)
return seed, nil
}

func loadAddressBook(path string) ([]maddr.Multiaddr, error) {
path = filepath.Clean(path)
_, err := os.Stat(path)
if err != nil {
return nil, err
}
input, err := os.ReadFile(path)
if err != nil {
return nil, err
}
data := strings.Split(string(input), "\n")
var peerAddresses []maddr.Multiaddr
for _, el := range data {
// we skip the empty entry
if len(el) == 0 {
continue
}
addr, err := maddr.NewMultiaddr(el)
if err != nil {
return nil, fmt.Errorf("invalid address in address book: %w", err)
}
peerAddresses = append(peerAddresses, addr)
}
return peerAddresses, nil
}
2 changes: 1 addition & 1 deletion storage/localstate_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *FileStateMgrTestSuite) TestSaveAddressBook(c *C) {
c.Assert(err, IsNil)
c.Assert(fsm, NotNil)
c.Assert(fsm.SaveAddressBook(testAddresses), IsNil)
filePathName := filepath.Join(f, "address_book.seed")
filePathName := filepath.Join(f, addressBookName)
_, err = os.Stat(filePathName)
c.Assert(err, IsNil)
item, err := fsm.RetrieveP2PAddresses()
Expand Down

0 comments on commit 07555ce

Please sign in to comment.