Skip to content

Commit

Permalink
Fix schedule network recursive call
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuel Pargov <[email protected]>
  • Loading branch information
bamzedev committed Nov 15, 2022
1 parent 4485dd7 commit d521c1f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion address_book_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (query *AddressBookQuery) Execute(client *Client) (NodeAddressBook, error)
}

if subClient == nil {
ctx, cancel = context.WithCancel(context.TODO())
ctx, cancel = context.WithCancel(client.networkUpdateContext)

subClient, err = (*channel).GetNodes(ctx, pb)
if err != nil {
Expand Down
56 changes: 32 additions & 24 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ type Client struct {
minBackoff time.Duration

requestTimeout *time.Duration
networkUpdateInitialDelay time.Duration
defaultNetworkUpdatePeriod time.Duration
networkUpdateContext context.Context
cancelNetworkUpdate context.CancelFunc
Expand All @@ -87,7 +86,7 @@ var previewnetMirror = []string{"hcs.previewnet.mirrornode.hedera.com:5600"}

func ClientForNetwork(network map[string]AccountID) *Client {
net := _NewNetwork()
client := _NewClient(net, []string{}, "mainnet", false)
client := _NewClient(net, []string{}, "mainnet")
_ = client.SetNetwork(network)
net._SetLedgerID(*NewLedgerIDMainnet())
return client
Expand All @@ -98,28 +97,28 @@ func ClientForNetwork(network map[string]AccountID) *Client {
// 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(*_NetworkForTestnet(mainnetNodes._ToMap()), mainnetMirror, NetworkNameMainnet, true)
return _NewClient(*_NetworkForTestnet(mainnetNodes._ToMap()), mainnetMirror, NetworkNameMainnet)
}

// 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(*_NetworkForPreviewnet(testnetNodes._ToMap()), testnetMirror, NetworkNameTestnet, true)
return _NewClient(*_NetworkForPreviewnet(testnetNodes._ToMap()), testnetMirror, NetworkNameTestnet)
}

// 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(*_NetworkForMainnet(previewnetNodes._ToMap()), previewnetMirror, NetworkNamePreviewnet, true)
return _NewClient(*_NetworkForMainnet(previewnetNodes._ToMap()), previewnetMirror, NetworkNamePreviewnet)
}

// 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 _Network, mirrorNetwork []string, name NetworkName, networkUpdate bool) *Client {
func _NewClient(network _Network, mirrorNetwork []string, name NetworkName) *Client {
ctx, cancel := context.WithCancel(context.Background())
client := Client{
maxQueryPayment: defaultMaxQueryPayment,
Expand All @@ -132,33 +131,40 @@ func _NewClient(network _Network, mirrorNetwork []string, name NetworkName, netw
maxBackoff: 8 * time.Second,
defaultRegenerateTransactionIDs: true,
defaultNetworkUpdatePeriod: 24 * time.Hour,
networkUpdateInitialDelay: 10 * time.Second,
networkUpdateContext: ctx,
cancelNetworkUpdate: cancel,
}

client.SetMirrorNetwork(mirrorNetwork)
client.network._SetNetworkName(name)

if networkUpdate {
go client._ScheduleNetworkUpdate(ctx, 0)
//We can't ask for AddressBook from non existent Mirror node
if len(mirrorNetwork)>0 {
go client._ScheduleNetworkUpdate(ctx, client.defaultNetworkUpdatePeriod)
}

return &client
}

func (client *Client) _UpdateAddressBook() {
addressbook, err := NewAddressBookQuery().
SetFileID(FileIDForAddressBook()).
Execute(client)
if err == nil && len(addressbook.NodeAddresses) > 0 {
client.SetNetworkFromAddressBook(addressbook)
}
//Log the error/addressbook
}

func (client *Client) _ScheduleNetworkUpdate(ctx context.Context, duration time.Duration) {
select {
case <-ctx.Done():
return
case <-time.After(duration):
addressbook, err := NewAddressBookQuery().
SetFileID(FileIDForAddressBook()).
Execute(client)
if err == nil && len(addressbook.NodeAddresses) > 0 {
client.SetNetworkFromAddressBook(addressbook)
client._UpdateAddressBook()
for {
select {
case <-ctx.Done():
return
case <-time.After(duration):
client._UpdateAddressBook()
}
client._ScheduleNetworkUpdate(ctx, client.defaultNetworkUpdatePeriod)
}
}

Expand All @@ -168,7 +174,9 @@ func (client *Client) CancelScheduledNetworkUpdate() {

func (client *Client) SetNetworkUpdatePeriod(period time.Duration) *Client {
client.defaultNetworkUpdatePeriod = period
client._ScheduleNetworkUpdate(client.networkUpdateContext, period)
client.CancelScheduledNetworkUpdate()
client.networkUpdateContext, client.cancelNetworkUpdate = context.WithCancel(context.Background())
go client._ScheduleNetworkUpdate(client.networkUpdateContext, period)
return client
}

Expand Down Expand Up @@ -259,16 +267,16 @@ func ClientFromConfig(jsonBytes []byte) (*Client, error) {
return client, errors.New("mirrorNetwork is expected to be either string or an array of strings")
}
}
client = _NewClient(network, arr, NetworkNameMainnet, false)
client = _NewClient(network, arr, NetworkNameMainnet)
case string:
if len(mirror) > 0 {
switch mirror {
case string(NetworkNameMainnet):
client = _NewClient(network, mainnetMirror, NetworkNameMainnet, true)
client = _NewClient(network, mainnetMirror, NetworkNameMainnet)
case string(NetworkNamePreviewnet):
client = _NewClient(network, previewnetMirror, NetworkNamePreviewnet, true)
client = _NewClient(network, previewnetMirror, NetworkNamePreviewnet)
case string(NetworkNameTestnet):
client = _NewClient(network, testnetMirror, NetworkNameTestnet, true)
client = _NewClient(network, testnetMirror, NetworkNameTestnet)
}
}
default:
Expand Down
4 changes: 3 additions & 1 deletion mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ func NewMockClientAndServer(allNodeResponses [][]interface{}) (*Client, *MockSer
network := map[string]AccountID{}
mirrorNetwork := make([]string, len(allNodeResponses))
servers := make([]*MockServer, len(allNodeResponses))
ctx, cancel := context.WithCancel(context.Background())
client := &Client{
maxQueryPayment: defaultMaxQueryPayment,
maxTransactionFee: defaultMaxTransactionFee,
Expand All @@ -467,7 +468,8 @@ func NewMockClientAndServer(allNodeResponses [][]interface{}) (*Client, *MockSer
maxBackoff: 8 * time.Second,
defaultRegenerateTransactionIDs: true,
defaultNetworkUpdatePeriod: 24 * time.Hour,
networkUpdateInitialDelay: 1 * time.Millisecond,
networkUpdateContext: ctx,
cancelNetworkUpdate: cancel,
}

for i, responses := range allNodeResponses {
Expand Down

0 comments on commit d521c1f

Please sign in to comment.