Skip to content

Commit

Permalink
tidy up CInv::GetCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
morcos authored and sipa committed Apr 23, 2016
1 parent dbe6391 commit 97d7402
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
28 changes: 7 additions & 21 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,36 +149,22 @@ CInv::CInv(int typeIn, const uint256& hashIn)
hash = hashIn;
}

CInv::CInv(const std::string& strType, const uint256& hashIn)
{
if (strType == NetMsgType::TX)
type = MSG_TX;
else if (strType == NetMsgType::BLOCK)
type = MSG_BLOCK;
else
throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType));

hash = hashIn;
}

bool operator<(const CInv& a, const CInv& b)
{
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
}

bool CInv::IsKnownType() const
{
int masked = type & MSG_TYPE_MASK;
return (masked >= 1 && masked <= MSG_TYPE_MAX);
}

const char* CInv::GetCommand() const
std::string CInv::GetCommand() const
{
std::string cmd;
if (type & MSG_WITNESS_FLAG)
cmd.append("witness-");
int masked = type & MSG_TYPE_MASK;
switch (masked)
{
case MSG_TX: return NetMsgType::TX;
case MSG_BLOCK: return NetMsgType::BLOCK;
case MSG_TX: return cmd.append(NetMsgType::TX);
case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
default:
throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
}
Expand Down
4 changes: 1 addition & 3 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ class CInv
public:
CInv();
CInv(int typeIn, const uint256& hashIn);
CInv(const std::string& strType, const uint256& hashIn);

ADD_SERIALIZE_METHODS;

Expand All @@ -317,8 +316,7 @@ class CInv

friend bool operator<(const CInv& a, const CInv& b);

bool IsKnownType() const;
const char* GetCommand() const;
std::string GetCommand() const;
std::string ToString() const;

// TODO: make private (improves encapsulation)
Expand Down

0 comments on commit 97d7402

Please sign in to comment.