Page MenuHomePhabricator

Encode destination using Base58 in WalletDB
ClosedPublic

Authored by dagurval on Dec 2 2017, 21:19.

Details

Summary

Previously destinations were encoded with EncodeDestination, which
depends on user configuration. WalletDB needs consistent encoding.

Base58 encoding is used for backward compatibility.

Tests for Purpose, Name and DestData in CWalletDB

Test Plan

added tests

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

deadalnix requested changes to this revision.Dec 3 2017, 15:24
deadalnix added a subscriber: deadalnix.
deadalnix added inline comments.
src/wallet/test/walletdb_tests.cpp
16 ↗(On Diff #1900)

There is a facility to get temp files, isn't it ?

This will leave a ton of garbage behind itself.

src/wallet/walletdb.cpp
877 ↗(On Diff #1900)

Remove extra line.

880 ↗(On Diff #1900)

The fact you need to check this all over the place indicate this should probably be pushed down further. Write and Erase should probably take a destination as parameter.

This revision now requires changes to proceed.Dec 3 2017, 15:24
src/wallet/test/walletdb_tests.cpp
16 ↗(On Diff #1900)

This is the one I found, "pathTemp" provided by the TestingSetup fixture. It cleans up temp files.

src/wallet/walletdb.cpp
880 ↗(On Diff #1900)

Template specialization of Write/Erase? Are you sure you'd rather have that? :-)

src/wallet/walletdb.cpp
880 ↗(On Diff #1900)

What I'm sure of is that the code is telling us something. It needs the same check a bazilion times. That means it is fragile and that, even if you did not miss any check, eventually someone will.

I introduced WriteDstPair and EraseDstPair to avoid code repetition between WritePurpose and WriteName. Unfortunately WriteDestData creates its key in different way and cannot be generalized the same way.

This change is introduced in a new commit. If it's deemed an improvement, I'll squash the commits together.

deadalnix requested changes to this revision.Dec 6 2017, 21:52
deadalnix added inline comments.
src/wallet/walletdb.cpp
260 ↗(On Diff #1960)

Either use std::move or put the EncodeLegacyAddr call inline.

270 ↗(On Diff #1960)

dito

880 ↗(On Diff #1960)

Either EraseDstPair should use that one or the other way around. The same code is duplicated 4 times, witht he same problem 4 times.

892 ↗(On Diff #1960)

dito

src/wallet/walletdb.h
12 ↗(On Diff #1960)

It's unfortunate that this is required. I assume this is because CTxDestination is a typedef instead of a class.

It's probably too much to change this, but worth adding a comment to mention why this include is needed.

192 ↗(On Diff #1960)

Unless this require to access private data, this can be a static method in the cpp file.

This revision now requires changes to proceed.Dec 6 2017, 21:52
dagurval added inline comments.
src/wallet/walletdb.h
192 ↗(On Diff #1960)

We need access to protected methods Write/Erase

src/wallet/walletdb.h
192 ↗(On Diff #1960)

Ok :)

deadalnix requested changes to this revision.Dec 7 2017, 13:14
deadalnix added inline comments.
src/wallet/walletdb.cpp
36 ↗(On Diff #1981)

This is when you should stop and pause. What about something more along the lines of

bool buildShitToWrite(const CTxDestination &address, const std::string &key, std::pair<whatever...> &shit) {
    if (!IsValidDestination(dst)) { return false; }
    return {key, EncodeLegacyAddr(address, Params())};
}

bool CWalletDB::WritePurpose(const CTxDestination &address,
                             const std::string &strPurpose) {
    std::pair<whatever> shit;
    if (!buildShitToWrite(address, "purpose", shit)) { return false; }
    nWalletDBUpdateCounter++;
    return Write(shit, strPurpose);
}

In C++ this kind of thing tend to work better as policies: https://en.wikipedia.org/wiki/Policy-based_design

This revision now requires changes to proceed.Dec 7 2017, 13:14

back to first proposal, with some nits

After exploring alternatives, they indeed did not look like improvements. Let's go with that.

This revision is now accepted and ready to land.Dec 8 2017, 00:03
This revision was automatically updated to reflect the committed changes.