Skip to content

Commit

Permalink
Some examples aren't working properly (#511)
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Walworth <[email protected]>
  • Loading branch information
rwalworth authored Oct 3, 2023
1 parent 92fef81 commit f6d3ade
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 135 deletions.
8 changes: 6 additions & 2 deletions sdk/examples/AccountCreateWithHtsExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ int main(int argc, char** argv)
/**
* Step 2: Mint the NFTs.
*/
TransactionReceipt txReceipt =
TokenMintTransaction().setTokenId(tokenId).setMetadata(CIDs).execute(client).getReceipt(client);
TransactionReceipt txReceipt = TokenMintTransaction()
.setMaxTransactionFee(Hbar(10LL))
.setTokenId(tokenId)
.setMetadata(CIDs)
.execute(client)
.getReceipt(client);
std::cout << "Minted " << txReceipt.mSerialNumbers.size() << " NFTs" << std::endl;

/**
Expand Down
7 changes: 4 additions & 3 deletions sdk/examples/ExemptCustomFeesExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ int main(int argc, char** argv)

/**
* Step 2: Create a fungible token that has three fractional fees.
* - Fee #1 sends 1/100 of the transferred value to collector 0.0.A.
* - Fee #2 sends 2/100 of the transferred value to collector 0.0.B.
* - Fee #3 sends 3/100 of the transferred value to collector 0.0.C.
* - Fee #1 sends 1/10th of the transferred value to collector 0.0.A.
* - Fee #2 sends 2/10th of the transferred value to collector 0.0.B.
* - Fee #3 sends 3/10th of the transferred value to collector 0.0.C.
*/
const TokenId createdTokenId =
TokenCreateTransaction()
.setMaxTransactionFee(Hbar(50LL))
.setTokenName("HIP-573 Token")
.setTokenSymbol("H573")
.setInitialSupply(100000000ULL)
Expand Down
1 change: 1 addition & 0 deletions sdk/examples/TransferCryptoExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "ED25519PrivateKey.h"
#include "Hbar.h"
#include "TransactionRecord.h"
#include "TransactionRecordQuery.h"
#include "TransactionResponse.h"
#include "TransferTransaction.h"

Expand Down
6 changes: 2 additions & 4 deletions sdk/main/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ class Client
void startNetworkUpdateThread(const std::chrono::duration<double>& period);

/**
* Schedule a network update a certain period of time from when this is called.
*
* @param period The period of time to wait before a network update is performed.
* Schedule a network update.
*/
void scheduleNetworkUpdate(const std::chrono::duration<double>& period);
void scheduleNetworkUpdate();

/**
* Cancel any scheduled network updates.
Expand Down
2 changes: 1 addition & 1 deletion sdk/main/include/TokenMintTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class TokenMintTransaction : public Transaction<TokenMintTransaction>
* The amount of the token to mint. This is for tokens of type FUNGIBLE_COMMON. The amount provided must be in the
* lowest denomination possible (i.e. if a token has 2 decimals, a value of 10,000 here will mint 100 tokens).
*/
uint64_t mAmount;
uint64_t mAmount = 0ULL;

/**
* The metadata of the NFTs to mint. This for tokens of type NON_FUNGIBLE_UNIQUE. Once an NFT is minted, its metadata
Expand Down
19 changes: 19 additions & 0 deletions sdk/main/include/impl/BaseNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#ifndef HEDERA_SDK_CPP_IMPL_BASE_NETWORK_H_
#define HEDERA_SDK_CPP_IMPL_BASE_NETWORK_H_

#include "AccountId.h"
#include "BaseNode.h"
#include "Defaults.h"
#include "LedgerId.h"
#include "TLSBehavior.h"

#include <chrono>
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -222,13 +224,25 @@ class BaseNetwork
{
return mNetwork;
}
[[nodiscard]] inline std::unordered_map<KeyType, std::unordered_set<std::shared_ptr<NodeType>>> getNetworkInternal()
{
return mNetwork;
}

/**
* Get the list of NodeTypes on this BaseNetwork.
*
* @return The list of NodeTypes on this BaseNetwork.
*/
[[nodiscard]] inline const std::unordered_set<std::shared_ptr<NodeType>>& getNodes() const { return mNodes; }
[[nodiscard]] inline std::unordered_set<std::shared_ptr<NodeType>>& getNodes() { return mNodes; }

/**
* Get this BaseNetwork's mutex.
*
* @return This BaseNetwork's mutex.
*/
[[nodiscard]] inline std::shared_ptr<std::mutex> getLock() const { return mMutex; }

private:
/**
Expand Down Expand Up @@ -317,6 +331,11 @@ class BaseNetwork
* The ledger ID of the network.
*/
LedgerId mLedgerId;

/**
* The mutex for this BaseNetwork, kept inside a std::shared_ptr to keep BaseNetwork copyable/movable.
*/
std::shared_ptr<std::mutex> mMutex = std::make_shared<std::mutex>();
};

} // namespace Hedera::internal
Expand Down
54 changes: 45 additions & 9 deletions sdk/main/include/impl/BaseNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <chrono>
#include <grpcpp/channel.h>
#include <grpcpp/security/credentials.h>
#include <mutex>

namespace Hedera::internal
{
Expand Down Expand Up @@ -99,35 +100,62 @@ class BaseNode
*
* @return address The BaseNodeAddress of this BaseNode.
*/
[[nodiscard]] inline BaseNodeAddress getAddress() const { return mAddress; }
[[nodiscard]] inline BaseNodeAddress getAddress() const
{
std::unique_lock lock(*mMutex);
return mAddress;
}

/**
* Get the minimum amount of time for this BaseNode to backoff after a bad gRPC status is received.
*
* @return The minimum amount of time for this BaseNode to backoff after a bad gRPC status is received.
*/
[[nodiscard]] inline std::chrono::duration<double> getMinNodeBackoff() const { return mMinNodeBackoff; }
[[nodiscard]] inline std::chrono::duration<double> getMinNodeBackoff() const
{
std::unique_lock lock(*mMutex);
return mMinNodeBackoff;
}

/**
* Get the maximum amount of time for this BaseNode to backoff after a bad gRPC status is received.
*
* @return The maximum amount of time for this BaseNode to backoff after a bad gRPC status is received.
*/
[[nodiscard]] inline std::chrono::duration<double> getMaxNodeBackoff() const { return mMaxNodeBackoff; }
[[nodiscard]] inline std::chrono::duration<double> getMaxNodeBackoff() const
{
std::unique_lock lock(*mMutex);
return mMaxNodeBackoff;
}

/**
* Get the number of times this BaseNode has received a bad gRPC status when attempting to submit a request.
*
* @return The number of times this BaseNode has received a bad gRPC status.
*/
[[nodiscard]] inline unsigned int getBadGrpcStatusCount() const { return mBadGrpcStatusCount; }
[[nodiscard]] inline unsigned int getBadGrpcStatusCount() const
{
std::unique_lock lock(*mMutex);
return mBadGrpcStatusCount;
}

/**
* Get the time at which this BaseNode will be considered "healthy".
*
* @return The time at which this BaseNode will be considered "healthy".
*/
[[nodiscard]] inline std::chrono::system_clock::time_point getReadmitTime() const { return mReadmitTime; }
[[nodiscard]] inline std::chrono::system_clock::time_point getReadmitTime() const
{
std::unique_lock lock(*mMutex);
return mReadmitTime;
}

/**
* Get this BaseNode's mutex.
*
* @return This BaseNode's mutex.
*/
[[nodiscard]] inline std::shared_ptr<std::mutex> getLock() const { return mMutex; }

protected:
~BaseNode() = default;
Expand Down Expand Up @@ -172,11 +200,9 @@ class BaseNode
[[nodiscard]] virtual std::shared_ptr<grpc::ChannelCredentials> getTlsChannelCredentials() const;

/**
* Initialize the stubs in this derived BaseNode with a gRPC channel.
*
* @param channel The gRPC channel with which to initialize the stubs.
* Initialize the stubs in this derived BaseNode with this BaseNode's gRPC channel.
*/
virtual void initializeStubs([[maybe_unused]] const std::shared_ptr<grpc::Channel>& channel)
virtual void initializeStubs()
{ // Intentionally unimplemented, derived BaseNodes that don't use stubs require no functionality.
}

Expand All @@ -194,6 +220,11 @@ class BaseNode
*/
[[nodiscard]] virtual inline std::string getAuthority() const { return "127.0.0.1"; }

/**
* Close this BaseNode's channel and any stubs using that channel.
*/
void closeChannel();

/**
* The address of this BaseNode.
*/
Expand Down Expand Up @@ -234,6 +265,11 @@ class BaseNode
* Is the gRPC channel being utilized by this BaseNode to communicate with its remote node initialized?
*/
bool mIsConnected = false;

/**
* The mutex for this BaseNode, kept inside a std::shared_ptr to keep BaseNetwork copyable/movable.
*/
std::shared_ptr<std::mutex> mMutex = std::make_shared<std::mutex>();
};

} // namespace Hedera::internal
Expand Down
6 changes: 2 additions & 4 deletions sdk/main/include/impl/MirrorNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ class MirrorNode : public BaseNode<MirrorNode, BaseNodeAddress>
[[nodiscard]] inline std::string getAuthority() const override { return {}; }

/**
* Derived from BaseNode. Initialize the stubs in this MirrorNode with a gRPC channel.
*
* @param channel The gRPC channel with which to initialize the stubs.
* Derived from BaseNode. Initialize the stubs in this MirrorNode with this MirrorNode's gRPC channel.
*/
void initializeStubs(const std::shared_ptr<grpc::Channel>& channel) override;
void initializeStubs() override;

/**
* Derived from BaseNode. Close the stubs in this MirrorNode.
Expand Down
29 changes: 23 additions & 6 deletions sdk/main/include/impl/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#define HEDERA_SDK_CPP_IMPL_NETWORK_H_

#include "BaseNetwork.h"
#include "Node.h"
#include "NodeAddressBook.h"
#include "TLSBehavior.h"

#include <memory>
#include <string>
Expand All @@ -32,7 +29,16 @@

namespace Hedera
{
namespace internal
{
class Node;
enum class TLSBehavior;
}

class AccountId;
class LedgerId;
class NodeAddress;
class NodeAddressBook;
}

namespace Hedera::internal
Expand Down Expand Up @@ -69,6 +75,17 @@ class Network : public BaseNetwork<Network, AccountId, Node>
*/
[[nodiscard]] static Network forNetwork(const std::unordered_map<std::string, AccountId>& network);

/**
* Construct a network map from a NodeAddressBook with a specific port for the endpoints.
*
* @param addressBook The NodeAddressBook from which to construct the network map.
* @param port The desired port.
* @return A network map that contains the nodes in the input NodeAddressBook.
*/
[[nodiscard]] static std::unordered_map<std::string, AccountId> getNetworkFromAddressBook(
const NodeAddressBook& addressBook,
unsigned int port);

/**
* Derived from BaseNetwork. Set the ledger ID of this Network.
*
Expand Down Expand Up @@ -141,7 +158,7 @@ class Network : public BaseNetwork<Network, AccountId, Node>
* @return The map of node addresses and AccountIds of the Nodes that exist on the network represented by the input
* LedgerId.
*/
[[nodiscard]] static std::unordered_map<AccountId, NodeAddress> getAddressBookForLedgerId(const LedgerId& ledgerId);
[[nodiscard]] static NodeAddressBook getAddressBookForLedgerId(const LedgerId& ledgerId);

/**
* Derived from BaseNetwork. Create a Node for this Network based on a network entry.
Expand All @@ -158,9 +175,9 @@ class Network : public BaseNetwork<Network, AccountId, Node>
* contained in the input map.
*
* @param ledgerId The new LedgerId of the Network.
* @param addressBook The address book with which to update this Network's Node's address book entry.
* @param addressBook The NodeAddressBook with which to update this Network's Node's address book entry.
*/
Network& setLedgerIdInternal(const LedgerId& ledgerId, const std::unordered_map<AccountId, NodeAddress>& addressBook);
Network& setLedgerIdInternal(const LedgerId& ledgerId, const NodeAddressBook& addressBook);

/**
* The maximum number of nodes to be returned for each request.
Expand Down
24 changes: 17 additions & 7 deletions sdk/main/include/impl/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,33 @@ class Node : public BaseNode<Node, AccountId>
*
* @return The AccountId of this Node.
*/
[[nodiscard]] inline AccountId getAccountId() const { return mAccountId; };
[[nodiscard]] inline AccountId getAccountId() const
{
std::unique_lock lock(*getLock());
return mAccountId;
};

/**
* Get the node certificate hash of this Node.
*
* @return The node certificate hash of this Node.
*/
[[nodiscard]] inline std::vector<std::byte> getNodeCertificateHash() const { return mNodeCertificateHash; }
[[nodiscard]] inline std::vector<std::byte> getNodeCertificateHash() const
{
std::unique_lock lock(*getLock());
return mNodeCertificateHash;
}

/**
* Get the certificate verification policy of this Node.
*
* @return \c TRUE if this Node is currently configured to verify certificates, otherwise \c FALSE.
*/
[[nodiscard]] inline bool getVerifyCertificates() const { return mVerifyCertificates; }
[[nodiscard]] inline bool getVerifyCertificates() const
{
std::unique_lock lock(*getLock());
return mVerifyCertificates;
}

private:
/**
Expand All @@ -169,11 +181,9 @@ class Node : public BaseNode<Node, AccountId>
[[nodiscard]] std::shared_ptr<grpc::ChannelCredentials> getTlsChannelCredentials() const override;

/**
* Derived from BaseNode. Initialize the stubs in this Node with a gRPC channel.
*
* @param channel The gRPC channel with which to initialize the stubs.
* Derived from BaseNode. Initialize the stubs in this Node with this Node's gRPC channel.
*/
void initializeStubs(const std::shared_ptr<grpc::Channel>& channel) override;
void initializeStubs() override;

/**
* Derived from BaseNode. Close the stubs in this Node.
Expand Down
Loading

0 comments on commit f6d3ade

Please sign in to comment.