Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

name_pending Qt GUI #406

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 165 additions & 9 deletions src/qt/nametablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ namespace {
};
}

const std::string NameTableEntry::NAME_STATUS_CONFIRMED = "Confirmed";
const std::string NameTableEntry::NAME_STATUS_EXPIRED = "Expired";
const std::string NameTableEntry::NAME_STATUS_TRANSFERRED_OUT = "Transferred out";
const std::string NameTableEntry::NAME_STATUS_REGISTRATION_PENDING = "Registration pending";
const std::string NameTableEntry::NAME_STATUS_INCOMING_TRANSFER_PENDING = "Incoming transfer pending";
const std::string NameTableEntry::NAME_STATUS_OUTGOING_TRANSFER_PENDING = "Outgoing transfer pending";
const std::string NameTableEntry::NAME_STATUS_RENEWAL_PENDING = "Renewal pending";
const std::string NameTableEntry::NAME_STATUS_UPDATE_PENDING = "Update pending";

// Returns true if new height is better
bool NameTableEntry::CompareHeight(int nOldHeight, int nNewHeight)
{
Expand Down Expand Up @@ -61,10 +70,6 @@ class NameTablePriv
std::map<std::string, NameTableEntry> vNamesO;

// confirmed names (name_list)
// TODO: Add unconfirmed names once support for this is added to
// name_list.
// TODO: Filter out expired=true and ismine=false once support for this
// is added to name_list.
// TODO: Set name and value encoding to hex, so that nonstandard
// encodings don't cause errors.

Expand Down Expand Up @@ -94,11 +99,162 @@ class NameTablePriv
continue;
}

std::string name = maybeName.get_str();
std::string data = maybeData.get_str();
int height = find_value ( v, "height").get_int();
int expiresIn = find_value ( v, "expires_in").get_int();
vNamesO[name] = NameTableEntry(name, data, height, expiresIn, "confirmed");
const std::string name = maybeName.get_str();
const std::string data = maybeData.get_str();
const int height = find_value ( v, "height").get_int();
const int expiresIn = find_value ( v, "expires_in").get_int();

const bool isMine = find_value ( v, "ismine").get_bool();
const bool isExpired = find_value ( v, "expired").get_bool();
// TODO: Check "op" field

std::string status = NameTableEntry::NAME_STATUS_CONFIRMED;
if (isExpired)
{
status = NameTableEntry::NAME_STATUS_EXPIRED;
}
if (!isMine)
{
status = NameTableEntry::NAME_STATUS_TRANSFERRED_OUT;
}

vNamesO[name] = NameTableEntry(name, data, height, expiresIn, status);
}
}

// unconfirmed names (name_pending)
// TODO: Set name and value encoding to hex, so that nonstandard
// encodings don't cause errors.

UniValue pendingNames;
try {
pendingNames = parent.walletModel->node().executeRpc("name_pending", NullUniValue, walletURI);
} catch (const UniValue& e) {
// although we shouldn't typically encounter error here, we
// should continue and try to add confirmed names and
// pending names. show error to user in case something
// actually went wrong so they can potentially recover
UniValue message = find_value( e, "message");
LogPrintf ("name_pending lookup error: %s\n", message.get_str());
}

// will be an object if name_pending command isn't available/other error
if(pendingNames.isArray())
{
for (const auto& v : pendingNames.getValues())
{
UniValue maybeName = find_value ( v, "name");
UniValue maybeData = find_value ( v, "value");
if (!maybeName.isStr() || !maybeData.isStr())
{
continue;
}

const std::string name = maybeName.get_str();
const std::string data = maybeData.get_str();

const bool isMine = find_value ( v, "ismine").get_bool();
const std::string op = find_value ( v, "op").get_str();

const bool confirmedEntryExists = vNamesO.count(name);

int height = 0;
int expiresIn = 0;
std::string status;

if (confirmedEntryExists)
{
// A confirmed entry exists.
auto confirmedEntry = vNamesO[name];
std::string confirmedData = confirmedEntry.value.toStdString();
std::string confirmedStatus = confirmedEntry.nameStatus.toStdString();
height = confirmedEntry.nHeight;
expiresIn = confirmedEntry.expiresIn;

if (confirmedStatus == NameTableEntry::NAME_STATUS_EXPIRED || confirmedStatus == NameTableEntry::NAME_STATUS_TRANSFERRED_OUT)
{
if (!isMine)
{
// This name isn't ours, it's just some random name
// that another user has pending.
continue;
}

// The name will be ours when it confirms, but it
// wasn't ours before.

if (op == "name_firstupdate")
{
// This is a registration, so the reason it wasn't
// ours before is that we hadn't registered it yet.
status = NameTableEntry::NAME_STATUS_REGISTRATION_PENDING;
}
else if (op == "name_update")
{
// This is an update, so we weren't the one to
// register it. (If we were, there would be a
// confirmed entry.) So this is an incoming
// transfer.
status = NameTableEntry::NAME_STATUS_INCOMING_TRANSFER_PENDING;
}
}
else if (confirmedStatus == NameTableEntry::NAME_STATUS_CONFIRMED)
{
if (!isMine)
{
// This name was ours, but won't be after this
// transaction confirms. So this is an outgoing
// transfer.
status = NameTableEntry::NAME_STATUS_OUTGOING_TRANSFER_PENDING;
}
else
{
// This name was ours, and still will be. So this
// is a pending update.

if (data == confirmedData)
{
// Data is unchanged, so this is a renewal.
status = NameTableEntry::NAME_STATUS_RENEWAL_PENDING;
}
else
{
// Data is changed.
status = NameTableEntry::NAME_STATUS_UPDATE_PENDING;
}
}
}
}
else
{
// A confirmed entry doesn't exist.

if (!isMine)
{
// This name isn't ours, it's just some random name
// that another user has pending.
continue;
}

// The name will be ours when it confirms, but it wasn't
// ours before.

if (op == "name_firstupdate")
{
// This is a registration, so the reason it wasn't ours
// before is that we hadn't registered it yet.
status = NameTableEntry::NAME_STATUS_REGISTRATION_PENDING;
}
else if (op == "name_update")
{
// This is an update, so we weren't the one to register
// it. (If we were, there would be a confirmed entry.)
// So this is an incoming transfer.
status = NameTableEntry::NAME_STATUS_INCOMING_TRANSFER_PENDING;
}
}

vNamesO[name] = NameTableEntry(name, data, height, expiresIn, status);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/qt/nametablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ struct NameTableEntry
static const int NAME_NON_EXISTING = -2; // Dummy nHeight value for unitinialized entries
static const int NAME_UNCONFIRMED = -3; // Dummy nHeight value for unconfirmed name transactions

static const std::string NAME_STATUS_CONFIRMED;
static const std::string NAME_STATUS_EXPIRED;
static const std::string NAME_STATUS_TRANSFERRED_OUT;
static const std::string NAME_STATUS_REGISTRATION_PENDING;
static const std::string NAME_STATUS_INCOMING_TRANSFER_PENDING;
static const std::string NAME_STATUS_OUTGOING_TRANSFER_PENDING;
static const std::string NAME_STATUS_RENEWAL_PENDING;
static const std::string NAME_STATUS_UPDATE_PENDING;

// NOTE: making this const throws warning indicating it will not be const
bool HeightValid() { return nHeight >= 0; }
static bool CompareHeight(int nOldHeight, int nNewHeight); // Returns true if new height is better
Expand Down