-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from mysteriumnetwork/reconnectable-ethclient
Add reconnactable ethereum client
- Loading branch information
Showing
3 changed files
with
145 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2020 The "MysteriumNetwork/payments" Authors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package client | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
// NewReconnectableEthClient creates new ethereum client that can reconnect. | ||
func NewReconnectableEthClient(address string) (*ReconnectableEthClient, error) { | ||
ec, err := ethclient.Dial(address) | ||
if err != nil { | ||
return nil, fmt.Errorf("ethereum client failed to connect: %w", err) | ||
} | ||
|
||
return &ReconnectableEthClient{ | ||
address: address, | ||
client: ec, | ||
}, nil | ||
} | ||
|
||
// ReconnectableEthClient is a ethereum client that can reconnect. | ||
type ReconnectableEthClient struct { | ||
address string | ||
mu sync.Mutex | ||
client *ethclient.Client | ||
} | ||
|
||
// Client returns the currently connected ethereum client. | ||
func (c *ReconnectableEthClient) Client() *ethclient.Client { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
return c.client | ||
} | ||
|
||
// Reconnect creates new ethereum client and replaces the current one. | ||
func (c *ReconnectableEthClient) Reconnect() error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
client, err := ethclient.Dial(c.address) | ||
if err != nil { | ||
return fmt.Errorf("ethereum client failed to dial: %w", err) | ||
} | ||
|
||
c.client.Close() | ||
c.client = client | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2020 The "MysteriumNetwork/payments" Authors. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package client | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReconnectableEthClientCreatesNewClient(t *testing.T) { | ||
client, err := NewReconnectableEthClient("http://127.0.0.1:1234") | ||
assert.Nil(t, err) | ||
|
||
c1 := client.Client() | ||
c2 := client.Client() | ||
assert.Equal(t, c1, c2) | ||
|
||
err = client.Reconnect() | ||
assert.Nil(t, err) | ||
|
||
c3 := client.Client() | ||
assert.NotEqual(t, c1, c3) | ||
} |