Skip to content

Commit

Permalink
feat: implement ClientForNetwork() and add mirror support to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
janaakhterov committed Sep 29, 2020
1 parent 33a48ab commit 289c638
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 61 deletions.
2 changes: 1 addition & 1 deletion account_balance_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestNewAccountBalanceQuery_ForContract(t *testing.T) {
}

func TestAccountBalanceQuery_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion account_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestSerializeAccountCreateTransaction(t *testing.T) {
}

func TestAccountCreateTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion account_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSerializeAccountDeleteTransaction(t *testing.T) {
}

func TestAccountDeleteTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion account_info_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNewAccountInfoQuery(t *testing.T) {
}

func TestAccountInfoQuery_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion account_update_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSerializeAccountUpdateTransaction(t *testing.T) {
}

func TestAccountUpdateTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
94 changes: 59 additions & 35 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Client struct {

networkNodes map[AccountID]*node
networkNodeIds []AccountID

mirrorChannels map[string]*grpc.ClientConn
mirrorNetwork []string
}

type node struct {
Expand All @@ -44,68 +47,79 @@ type operator struct {
signer TransactionSigner
}

var mainnetNodes = map[string]AccountID{
"35.237.200.180:50211": {Account: 3},
"35.186.191.247:50211": {Account: 4},
"35.192.2.25:50211": {Account: 5},
"35.199.161.108:50211": {Account: 6},
"35.203.82.240:50211": {Account: 7},
"35.236.5.219:50211": {Account: 8},
"35.197.192.225:50211": {Account: 9},
"35.242.233.154:50211": {Account: 10},
"35.240.118.96:50211": {Account: 11},
"35.204.86.32:50211": {Account: 12},
var mainnetNodes = map[AccountID]string{
{Account: 3}: "35.237.200.180:50211",
{Account: 4}: "35.186.191.247:50211",
{Account: 5}: "35.192.2.25:50211",
{Account: 6}: "35.199.161.108:50211",
{Account: 7}: "35.203.82.240:50211",
{Account: 8}: "35.236.5.219:50211",
{Account: 9}: "35.197.192.225:50211",
{Account: 10}: "35.242.233.154:50211",
{Account: 11}: "35.240.118.96:50211",
{Account: 12}: "35.204.86.32:50211",
}

var testnetNodes = map[string]AccountID{
"0.testnet.hedera.com:50211": {Account: 3},
"1.testnet.hedera.com:50211": {Account: 4},
"2.testnet.hedera.com:50211": {Account: 5},
"3.testnet.hedera.com:50211": {Account: 6},
var testnetNodes = map[AccountID]string{
{Account: 3}: "0.testnet.hedera.com:50211",
{Account: 4}: "1.testnet.hedera.com:50211",
{Account: 5}: "2.testnet.hedera.com:50211",
{Account: 6}: "3.testnet.hedera.com:50211",
}

var previewnetNodes = map[string]AccountID{
"0.previewnet.hedera.com:50211": {Account: 3},
"1.previewnet.hedera.com:50211": {Account: 4},
"2.previewnet.hedera.com:50211": {Account: 5},
"3.previewnet.hedera.com:50211": {Account: 6},
var previewnetNodes = map[AccountID]string{
{Account: 3}: "0.previewnet.hedera.com:50211",
{Account: 4}: "1.previewnet.hedera.com:50211",
{Account: 5}: "2.previewnet.hedera.com:50211",
{Account: 6}: "3.previewnet.hedera.com:50211",
}

var mainnetMirror = []string{"hcs.mainnet.mirrornode.hedera.com:5600"}
var testnetMirror = []string{"hcs.testnet.mirrornode.hedera.com:5600"}
var previewnetMirror = []string{"hcs.previewnet.mirrornode.hedera.com:5600"}

func ClientForNetwork(network map[AccountID]string) *Client {
return newClient(network, []string{})
}

// ClientForMainnet returns a preconfigured client for use with the standard
// Hedera mainnet.
// Most users will want to set an operator account with .SetOperator so
// transactions can be automatically given TransactionIDs and signed.
func ClientForMainnet() *Client {
return NewClient(mainnetNodes)
return newClient(mainnetNodes, mainnetMirror)
}

// ClientForTestnet returns a preconfigured client for use with the standard
// Hedera testnet.
// Most users will want to set an operator account with .SetOperator so
// transactions can be automatically given TransactionIDs and signed.
func ClientForTestnet() *Client {
return NewClient(testnetNodes)
return newClient(testnetNodes, testnetMirror)
}

// ClientForPreviewnet returns a preconfigured client for use with the standard
// Hedera previewnet.
// Most users will want to set an operator account with .SetOperator so
// transactions can be automatically given TransactionIDs and signed.
func ClientForPreviewnet() *Client {
return NewClient(previewnetNodes)
return newClient(previewnetNodes, previewnetMirror)
}

// NewClient takes in a map of node addresses to their respective IDS (network)
// newClient takes in a map of node addresses to their respective IDS (network)
// and returns a Client instance which can be used to
func NewClient(network map[string]AccountID) *Client {
func newClient(network map[AccountID]string, mirrorNetwork []string) *Client {
client := &Client{
maxQueryPayment: defaultMaxQueryPayment,
maxTransactionFee: defaultMaxTransactionFee,
networkNodes: map[AccountID]*node{},
networkNodeIds: []AccountID{},
mirrorChannels: map[string]*grpc.ClientConn{},
mirrorNetwork: []string{},
}

client.ReplaceNodes(network)
client.SetNetwork(network)
client.SetMirrorNetwork(mirrorNetwork)

return client
}
Expand All @@ -115,8 +129,10 @@ type configOperator struct {
PrivateKey string `json:"privateKey"`
}

// TODO: Implement complete spec: https://gitlab.com/launchbadge/hedera/sdk/python/-/issues/45
type clientConfig struct {
Network map[string]string `json:"network"`
MirrorNetwork []string `json:"mirrorNetwork"`
Operator *configOperator `json:"operator"`
}

Expand All @@ -130,18 +146,18 @@ func ClientFromJSON(jsonBytes []byte) (*Client, error) {
return nil, err
}

var network map[string]AccountID = make(map[string]AccountID)
var network map[AccountID]string = make(map[AccountID]string)

for id, url := range clientConfig.Network {
accountID, err := AccountIDFromString(id)
if err != nil {
return nil, err
}

network[url] = accountID
network[accountID] = url
}

client := NewClient(network)
client := newClient(network, clientConfig.MirrorNetwork)

// if the operator is not provided, finish here
if clientConfig.Operator == nil {
Expand Down Expand Up @@ -170,9 +186,9 @@ func ClientFromJSON(jsonBytes []byte) (*Client, error) {
return client, nil
}

// ClientFromFile takes a filename string representing the path to a JSON encoded
// ClientFromJsonFile takes a filename string representing the path to a JSON encoded
// Client file and returns a Client based on the configuration.
func ClientFromFile(filename string) (*Client, error) {
func ClientFromJsonFile(filename string) (*Client, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
Expand Down Expand Up @@ -204,10 +220,10 @@ func (client *Client) Close() error {
return nil
}

// ReplaceNodes replaces all nodes in the Client with a new set of nodes.
// SetNetwork replaces all nodes in the Client with a new set of nodes.
// (e.g. for an Address Book update).
func (client *Client) ReplaceNodes(network map[string]AccountID) *Client {
for address, id := range network {
func (client *Client) SetNetwork(network map[AccountID]string) *Client {
for id, address := range network {
client.networkNodeIds = append(client.networkNodeIds, id)
client.networkNodes[id] = &node{
id: id,
Expand All @@ -218,6 +234,14 @@ func (client *Client) ReplaceNodes(network map[string]AccountID) *Client {
return client
}

// SetNetwork replaces all nodes in the Client with a new set of nodes.
// (e.g. for an Address Book update).
func (client *Client) SetMirrorNetwork(mirrorNetwork []string) *Client {
client.mirrorNetwork= mirrorNetwork

return client
}

// SetOperator sets that account that will, by default, be paying for
// transactions and queries built with the client and the associated key
// with which to automatically sign transactions.
Expand Down
2 changes: 1 addition & 1 deletion consensus_message_submit_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSerializeConsensusMessageSubmitTransaction(t *testing.T) {
}

func TestConsensusMessageSubmitTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion consensus_topic_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSerializeConsensusTopicCreateTransaction(t *testing.T) {
}

func TestConsensusTopicCreateTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion consensus_topic_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSerializeConsensusTopicDeleteTransaction(t *testing.T) {
}

func TestConsensusTopicDeleteTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion consensus_topic_info_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestConsensusTopicInfoQuery(t *testing.T) {
}

func TestConsensusTopicInfoQuery_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion consensus_topic_update_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestSerializeConsensusTopicUpdateTransaction(t *testing.T) {
}

func TestConsensusTopicUpdateTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion contract_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestContractCreateTransaction_Execute(t *testing.T) {
// Note: this is the bytecode for the contract found in the example for ./examples/create_simple_contract
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion contract_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestContractDeleteTransaction_Execute(t *testing.T) {
// Note: this is the bytecode for the contract found in the example for ./examples/create_simple_contract
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion contract_execute_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestContractExecuteTransaction_Execute(t *testing.T) {

testContractByteCode := []byte(smartContract.Contracts["stateful.sol:StatefulContract"].Bin)

client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion contract_info_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestContractInfoQuery_Execute(t *testing.T) {
// Note: this is the bytecode for the contract found in the example for ./examples/create_simple_contract
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion crypto_transfer_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestSerializeCryptoTransferTransaction(t *testing.T) {
}

func TestCryptoTransferTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion file_append_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSerializeFileAppendTransaction(t *testing.T) {
}

func TestFileAppendTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion file_contents_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNewFileContentsQuery(t *testing.T) {
}

func TestFileContentsQuery_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion file_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSerializeFileCreateTransaction(t *testing.T) {
}

func TestFileCreateTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion file_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSerializeFileDeleteTransaction(t *testing.T) {
}

func TestFileDeleteTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion file_info_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNewFileInfoQuery(t *testing.T) {
}

func TestFileInfoQueryTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
2 changes: 1 addition & 1 deletion file_update_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSerializeFileUpdateTransaction(t *testing.T) {
}

func TestFileUpdateTransaction_Execute(t *testing.T) {
client, err := ClientFromFile(os.Getenv("CONFIG_FILE"))
client, err := ClientFromJsonFile(os.Getenv("CONFIG_FILE"))

if err != nil {
client = ClientForTestnet()
Expand Down
4 changes: 2 additions & 2 deletions query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (builder *QueryBuilder) GetCost(client *Client) (Hbar, error) {
return ZeroHbar, err
}

tx = tx.signWithOperator(*client.operator)
tx = tx.SignWithOperator(*client.operator)

builder.pbHeader.Payment = tx.pb

Expand Down Expand Up @@ -187,7 +187,7 @@ func (builder *QueryBuilder) generatePaymentTransaction(client *Client, node *no
}

if client.operator != nil {
tx = tx.signWithOperator(*client.operator)
tx = tx.SignWithOperator(*client.operator)
}

builder.pbHeader.Payment = tx.pb
Expand Down
Loading

0 comments on commit 289c638

Please sign in to comment.