Make Ethereum client as interface

This commit is contained in:
ChronosX88 2021-08-25 22:10:44 +03:00
parent f074007c75
commit 0dbcdbde70
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
8 changed files with 61 additions and 69 deletions

View File

@ -32,7 +32,7 @@ var (
type Miner struct { type Miner struct {
bus EventBus.Bus bus EventBus.Bus
address peer.ID address peer.ID
ethClient *ethclient.EthereumClient ethClient ethclient.EthereumSideAPI
minerStake *big.Int minerStake *big.Int
networkStake *big.Int networkStake *big.Int
privateKey crypto.PrivKey privateKey crypto.PrivKey
@ -43,7 +43,7 @@ type Miner struct {
func NewMiner( func NewMiner(
h host.Host, h host.Host,
ethClient *ethclient.EthereumClient, ethClient ethclient.EthereumSideAPI,
privateKey crypto.PrivKey, privateKey crypto.PrivKey,
mempool *pool.Mempool, mempool *pool.Mempool,
bus EventBus.Bus, bus EventBus.Bus,

View File

@ -8,6 +8,8 @@ import (
"sort" "sort"
"time" "time"
"github.com/Secured-Finance/dione/ethclient"
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
drand2 "github.com/Secured-Finance/dione/beacon/drand" drand2 "github.com/Secured-Finance/dione/beacon/drand"
@ -29,7 +31,6 @@ import (
"github.com/Secured-Finance/dione/consensus/types" "github.com/Secured-Finance/dione/consensus/types"
types2 "github.com/Secured-Finance/dione/types" types2 "github.com/Secured-Finance/dione/types"
"github.com/Secured-Finance/dione/ethclient"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/Secured-Finance/dione/pubsub" "github.com/Secured-Finance/dione/pubsub"
@ -44,7 +45,7 @@ type ConsensusHandler struct {
psb *pubsub.PubSubRouter psb *pubsub.PubSubRouter
privKey crypto.PrivKey privKey crypto.PrivKey
validator *ConsensusValidator validator *ConsensusValidator
ethereumClient *ethclient.EthereumClient ethereumClient ethclient.EthereumSideAPI
miner *blockchain.Miner miner *blockchain.Miner
consensus *ConsensusManager consensus *ConsensusManager
mempool *pool.Mempool mempool *pool.Mempool
@ -57,7 +58,7 @@ func NewConsensusHandler(
bus EventBus.Bus, bus EventBus.Bus,
psb *pubsub.PubSubRouter, psb *pubsub.PubSubRouter,
privKey crypto.PrivKey, privKey crypto.PrivKey,
ethereumClient *ethclient.EthereumClient, ethereumClient ethclient.EthereumSideAPI,
miner *blockchain.Miner, miner *blockchain.Miner,
bc *blockchain.BlockChain, bc *blockchain.BlockChain,
bp *ConsensusManager, bp *ConsensusManager,

View File

@ -6,6 +6,8 @@ import (
"math/big" "math/big"
"time" "time"
"github.com/Secured-Finance/dione/ethclient"
"github.com/asaskevich/EventBus" "github.com/asaskevich/EventBus"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -28,13 +30,12 @@ import (
"github.com/Secured-Finance/dione/contracts/dioneDispute" "github.com/Secured-Finance/dione/contracts/dioneDispute"
"github.com/Secured-Finance/dione/contracts/dioneOracle" "github.com/Secured-Finance/dione/contracts/dioneOracle"
"github.com/Secured-Finance/dione/ethclient"
) )
type DisputeManager struct { type DisputeManager struct {
ctx context.Context ctx context.Context
bus EventBus.Bus bus EventBus.Bus
ethClient *ethclient.EthereumClient ethClient ethclient.EthereumSideAPI
voteWindow time.Duration voteWindow time.Duration
blockchain *blockchain.BlockChain blockchain *blockchain.BlockChain
@ -64,7 +65,7 @@ type Submission struct {
Checked bool Checked bool
} }
func NewDisputeManager(bus EventBus.Bus, ethClient *ethclient.EthereumClient, bc *blockchain.BlockChain, cfg *config.Config, cm cache.CacheManager) (*DisputeManager, error) { func NewDisputeManager(bus EventBus.Bus, ethClient ethclient.EthereumSideAPI, bc *blockchain.BlockChain, cfg *config.Config, cm cache.CacheManager) (*DisputeManager, error) {
ctx := context.TODO() ctx := context.TODO()
submissionChan, submSubscription, err := ethClient.SubscribeOnNewSubmissions(ctx) submissionChan, submSubscription, err := ethClient.SubscribeOnNewSubmissions(ctx)

View File

@ -6,6 +6,8 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"github.com/sirupsen/logrus"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet" hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
"github.com/Secured-Finance/dione/config" "github.com/Secured-Finance/dione/config"
@ -21,8 +23,7 @@ import (
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
) )
// TODO: change artifacts for other contracts type ethereumClient struct {
type EthereumClient struct {
client *ethclient.Client client *ethclient.Client
ethAddress *common.Address ethAddress *common.Address
authTransactor *bind.TransactOpts authTransactor *bind.TransactOpts
@ -31,51 +32,50 @@ type EthereumClient struct {
dioneOracle *dioneOracle.DioneOracleSession dioneOracle *dioneOracle.DioneOracleSession
} }
type Ethereum interface { type EthereumSideAPI interface {
Initialize(ctx context.Context, url, connectionType, privateKey, oracleEmitterContractAddress, aggregatorContractAddress string) error SubmitRequestAnswer(reqID *big.Int, data []byte) error
Balance(context.Context, string) (*big.Int, error)
SubmitRequestAnswer(reqID *big.Int, data string, callbackAddress common.Address, callbackMethodID [4]byte) error
BeginDispute(miner common.Address, requestID *big.Int) error BeginDispute(miner common.Address, requestID *big.Int) error
VoteDispute(dhash string, voteStatus bool) error VoteDispute(dhash [32]byte, voteStatus bool) error
FinishDispute(dhash string) error FinishDispute(dhash [32]byte) error
SubscribeOnOracleEvents(ctx context.Context) (chan *dioneOracle.DioneOracleNewOracleRequest, event.Subscription, error)
SubscribeOnNewDisputes(ctx context.Context) (chan *dioneDispute.DioneDisputeNewDispute, event.Subscription, error) SubscribeOnNewDisputes(ctx context.Context) (chan *dioneDispute.DioneDisputeNewDispute, event.Subscription, error)
SubscribeOnNewSubmissions(ctx context.Context) (chan *dioneOracle.DioneOracleSubmittedOracleRequest, event.Subscription, error)
GetEthAddress() *common.Address
GetTotalStake() (*big.Int, error)
GetMinerStake(minerAddress common.Address) (*big.Int, error)
} }
func NewEthereumClient() *EthereumClient { func NewEthereumClient(cfg *config.Config) (EthereumSideAPI, error) {
ethereumClient := &EthereumClient{} c := &ethereumClient{}
return ethereumClient client, err := ethclient.Dial(cfg.Ethereum.GatewayAddress)
}
func (c *EthereumClient) Initialize(cfg *config.EthereumConfig) error {
client, err := ethclient.Dial(cfg.GatewayAddress)
if err != nil { if err != nil {
return err return nil, err
} }
c.client = client c.client = client
key, err := c.getPrivateKey(cfg) key, err := c.getPrivateKey(cfg)
if err != nil { if err != nil {
return err return nil, err
} }
authTransactor, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(int64(cfg.ChainID))) authTransactor, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(int64(cfg.Ethereum.ChainID)))
if err != nil { if err != nil {
return err return nil, err
} }
c.authTransactor = authTransactor c.authTransactor = authTransactor
c.ethAddress = &c.authTransactor.From c.ethAddress = &c.authTransactor.From
stakingContract, err := dioneStaking.NewDioneStaking(common.HexToAddress(cfg.DioneStakingContractAddress), client) stakingContract, err := dioneStaking.NewDioneStaking(common.HexToAddress(cfg.Ethereum.DioneStakingContractAddress), client)
if err != nil { if err != nil {
return err return nil, err
} }
oracleContract, err := dioneOracle.NewDioneOracle(common.HexToAddress(cfg.DioneOracleContractAddress), client) oracleContract, err := dioneOracle.NewDioneOracle(common.HexToAddress(cfg.Ethereum.DioneOracleContractAddress), client)
if err != nil { if err != nil {
return err return nil, err
} }
disputeContract, err := dioneDispute.NewDioneDispute(common.HexToAddress(cfg.DisputeContractAddress), client) disputeContract, err := dioneDispute.NewDioneDispute(common.HexToAddress(cfg.Ethereum.DisputeContractAddress), client)
if err != nil { if err != nil {
return err return nil, err
} }
c.dioneStaking = &dioneStaking.DioneStakingSession{ c.dioneStaking = &dioneStaking.DioneStakingSession{
Contract: stakingContract, Contract: stakingContract,
@ -122,24 +122,27 @@ func (c *EthereumClient) Initialize(cfg *config.EthereumConfig) error {
Context: context.Background(), Context: context.Background(),
}, },
} }
return nil
logrus.WithField("ethAddress", c.GetEthAddress().Hex()).Info("Ethereum client has been initialized!")
return c, nil
} }
func (c *EthereumClient) getPrivateKey(cfg *config.EthereumConfig) (*ecdsa.PrivateKey, error) { func (c *ethereumClient) getPrivateKey(cfg *config.Config) (*ecdsa.PrivateKey, error) {
if cfg.PrivateKey != "" { if cfg.Ethereum.PrivateKey != "" {
key, err := crypto.HexToECDSA(cfg.PrivateKey) key, err := crypto.HexToECDSA(cfg.Ethereum.PrivateKey)
return key, err return key, err
} }
if cfg.MnemonicPhrase != "" { if cfg.Ethereum.MnemonicPhrase != "" {
wallet, err := hdwallet.NewFromMnemonic(cfg.MnemonicPhrase) wallet, err := hdwallet.NewFromMnemonic(cfg.Ethereum.MnemonicPhrase)
if err != nil { if err != nil {
return nil, err return nil, err
} }
path := hdwallet.DefaultBaseDerivationPath path := hdwallet.DefaultBaseDerivationPath
if cfg.HDDerivationPath != "" { if cfg.Ethereum.HDDerivationPath != "" {
parsedPath, err := hdwallet.ParseDerivationPath(cfg.HDDerivationPath) parsedPath, err := hdwallet.ParseDerivationPath(cfg.Ethereum.HDDerivationPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -160,11 +163,11 @@ func (c *EthereumClient) getPrivateKey(cfg *config.EthereumConfig) (*ecdsa.Priva
return nil, fmt.Errorf("private key or mnemonic phrase isn't specified") return nil, fmt.Errorf("private key or mnemonic phrase isn't specified")
} }
func (c *EthereumClient) GetEthAddress() *common.Address { func (c *ethereumClient) GetEthAddress() *common.Address {
return c.ethAddress return c.ethAddress
} }
func (c *EthereumClient) SubscribeOnOracleEvents(ctx context.Context) (chan *dioneOracle.DioneOracleNewOracleRequest, event.Subscription, error) { func (c *ethereumClient) SubscribeOnOracleEvents(ctx context.Context) (chan *dioneOracle.DioneOracleNewOracleRequest, event.Subscription, error) {
resChan := make(chan *dioneOracle.DioneOracleNewOracleRequest) resChan := make(chan *dioneOracle.DioneOracleNewOracleRequest)
requestsFilter := c.dioneOracle.Contract.DioneOracleFilterer requestsFilter := c.dioneOracle.Contract.DioneOracleFilterer
subscription, err := requestsFilter.WatchNewOracleRequest(&bind.WatchOpts{ subscription, err := requestsFilter.WatchNewOracleRequest(&bind.WatchOpts{
@ -177,7 +180,7 @@ func (c *EthereumClient) SubscribeOnOracleEvents(ctx context.Context) (chan *dio
return resChan, subscription, err return resChan, subscription, err
} }
func (c *EthereumClient) SubmitRequestAnswer(reqID *big.Int, data []byte) error { func (c *ethereumClient) SubmitRequestAnswer(reqID *big.Int, data []byte) error {
_, err := c.dioneOracle.SubmitOracleRequest(reqID, data) _, err := c.dioneOracle.SubmitOracleRequest(reqID, data)
if err != nil { if err != nil {
return err return err
@ -186,7 +189,7 @@ func (c *EthereumClient) SubmitRequestAnswer(reqID *big.Int, data []byte) error
return nil return nil
} }
func (c *EthereumClient) BeginDispute(miner common.Address, requestID *big.Int) error { func (c *ethereumClient) BeginDispute(miner common.Address, requestID *big.Int) error {
_, err := c.disputeContract.BeginDispute(miner, requestID) _, err := c.disputeContract.BeginDispute(miner, requestID)
if err != nil { if err != nil {
return err return err
@ -195,7 +198,7 @@ func (c *EthereumClient) BeginDispute(miner common.Address, requestID *big.Int)
return nil return nil
} }
func (c *EthereumClient) VoteDispute(dhash [32]byte, voteStatus bool) error { func (c *ethereumClient) VoteDispute(dhash [32]byte, voteStatus bool) error {
_, err := c.disputeContract.Vote(dhash, voteStatus) _, err := c.disputeContract.Vote(dhash, voteStatus)
if err != nil { if err != nil {
return err return err
@ -204,7 +207,7 @@ func (c *EthereumClient) VoteDispute(dhash [32]byte, voteStatus bool) error {
return nil return nil
} }
func (c *EthereumClient) FinishDispute(dhash [32]byte) error { func (c *ethereumClient) FinishDispute(dhash [32]byte) error {
_, err := c.disputeContract.FinishDispute(dhash) _, err := c.disputeContract.FinishDispute(dhash)
if err != nil { if err != nil {
return err return err
@ -213,7 +216,7 @@ func (c *EthereumClient) FinishDispute(dhash [32]byte) error {
return nil return nil
} }
func (c *EthereumClient) SubscribeOnNewDisputes(ctx context.Context) (chan *dioneDispute.DioneDisputeNewDispute, event.Subscription, error) { func (c *ethereumClient) SubscribeOnNewDisputes(ctx context.Context) (chan *dioneDispute.DioneDisputeNewDispute, event.Subscription, error) {
resChan := make(chan *dioneDispute.DioneDisputeNewDispute) resChan := make(chan *dioneDispute.DioneDisputeNewDispute)
requestsFilter := c.disputeContract.Contract.DioneDisputeFilterer requestsFilter := c.disputeContract.Contract.DioneDisputeFilterer
subscription, err := requestsFilter.WatchNewDispute(&bind.WatchOpts{ subscription, err := requestsFilter.WatchNewDispute(&bind.WatchOpts{
@ -226,7 +229,7 @@ func (c *EthereumClient) SubscribeOnNewDisputes(ctx context.Context) (chan *dion
return resChan, subscription, err return resChan, subscription, err
} }
func (c *EthereumClient) SubscribeOnNewSubmissions(ctx context.Context) (chan *dioneOracle.DioneOracleSubmittedOracleRequest, event.Subscription, error) { func (c *ethereumClient) SubscribeOnNewSubmissions(ctx context.Context) (chan *dioneOracle.DioneOracleSubmittedOracleRequest, event.Subscription, error) {
resChan := make(chan *dioneOracle.DioneOracleSubmittedOracleRequest) resChan := make(chan *dioneOracle.DioneOracleSubmittedOracleRequest)
requestsFilter := c.dioneOracle.Contract.DioneOracleFilterer requestsFilter := c.dioneOracle.Contract.DioneOracleFilterer
subscription, err := requestsFilter.WatchSubmittedOracleRequest(&bind.WatchOpts{ subscription, err := requestsFilter.WatchSubmittedOracleRequest(&bind.WatchOpts{

View File

@ -11,7 +11,7 @@ const (
) )
// GetTotalStake for getting total stake in DioneStaking contract // GetTotalStake for getting total stake in DioneStaking contract
func (c *EthereumClient) GetTotalStake() (*big.Int, error) { func (c *ethereumClient) GetTotalStake() (*big.Int, error) {
totalStake, err := c.dioneStaking.TotalStake() totalStake, err := c.dioneStaking.TotalStake()
if err != nil { if err != nil {
return nil, err return nil, err
@ -21,7 +21,7 @@ func (c *EthereumClient) GetTotalStake() (*big.Int, error) {
} }
// GetMinerStake for getting specified miner stake in DioneStaking contract // GetMinerStake for getting specified miner stake in DioneStaking contract
func (c *EthereumClient) GetMinerStake(minerAddress common.Address) (*big.Int, error) { func (c *ethereumClient) GetMinerStake(minerAddress common.Address) (*big.Int, error) {
minerStake, err := c.dioneStaking.MinerStake(minerAddress) minerStake, err := c.dioneStaking.MinerStake(minerAddress)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -12,7 +12,7 @@ import (
) )
// // Balance returns the balance of the given ethereum address. // // Balance returns the balance of the given ethereum address.
func (c *EthereumClient) Balance(ctx context.Context, address string) (*big.Int, error) { func (c *ethereumClient) Balance(ctx context.Context, address string) (*big.Int, error) {
ethereumAddress := common.HexToAddress(address) ethereumAddress := common.HexToAddress(address)
value, err := c.client.BalanceAt(ctx, ethereumAddress, nil) value, err := c.client.BalanceAt(ctx, ethereumAddress, nil)
if err != nil { if err != nil {
@ -21,7 +21,7 @@ func (c *EthereumClient) Balance(ctx context.Context, address string) (*big.Int,
return value, nil return value, nil
} }
func (c *EthereumClient) SendTransaction(ctx context.Context, private_key, to string, amount int64) string { func (c *ethereumClient) SendTransaction(ctx context.Context, private_key, to string, amount int64) string {
privateKey, err := crypto.HexToECDSA(private_key) privateKey, err := crypto.HexToECDSA(private_key)
if err != nil { if err != nil {
logrus.Fatal("Failed to parse private key", err) logrus.Fatal("Failed to parse private key", err)

View File

@ -46,7 +46,7 @@ func runNode(
lc fx.Lifecycle, lc fx.Lifecycle,
cfg *config.Config, cfg *config.Config,
disco discovery.Discovery, disco discovery.Discovery,
ethClient *ethclient.EthereumClient, ethClient ethclient.EthereumSideAPI,
h host.Host, h host.Host,
mp *pool.Mempool, mp *pool.Mempool,
syncManager sync.SyncManager, syncManager sync.SyncManager,
@ -135,7 +135,7 @@ func runLibp2pAsync(ctx context.Context, h host.Host, cfg *config.Config, disco
return nil return nil
} }
func subscribeOnEthContractsAsync(ctx context.Context, ethClient *ethclient.EthereumClient, mp *pool.Mempool) error { func subscribeOnEthContractsAsync(ctx context.Context, ethClient ethclient.EthereumSideAPI, mp *pool.Mempool) error {
eventChan, subscription, err := ethClient.SubscribeOnOracleEvents(ctx) eventChan, subscription, err := ethClient.SubscribeOnOracleEvents(ctx)
if err != nil { if err != nil {
return err return err
@ -195,7 +195,7 @@ func Start() {
provideCacheManager, provideCacheManager,
providePrivateKey, providePrivateKey,
provideLibp2pHost, provideLibp2pHost,
provideEthereumClient, ethclient.NewEthereumClient,
providePubsub, providePubsub,
providePubsubRouter, providePubsubRouter,
provideBootstrapAddrs, provideBootstrapAddrs,

View File

@ -38,7 +38,6 @@ import (
"github.com/Secured-Finance/dione/cache" "github.com/Secured-Finance/dione/cache"
"github.com/Secured-Finance/dione/config" "github.com/Secured-Finance/dione/config"
"github.com/Secured-Finance/dione/ethclient"
"github.com/Secured-Finance/dione/pubsub" "github.com/Secured-Finance/dione/pubsub"
pex "github.com/Secured-Finance/go-libp2p-pex" pex "github.com/Secured-Finance/go-libp2p-pex"
"github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p"
@ -106,18 +105,6 @@ func provideBlockchainDatabase(cfg *config.Config) (database.Database, error) {
// return w, nil // return w, nil
//} //}
func provideEthereumClient(config *config.Config) *ethclient.EthereumClient {
ethereum := ethclient.NewEthereumClient()
err := ethereum.Initialize(&config.Ethereum)
if err != nil {
logrus.Fatal(err)
}
logrus.WithField("ethAddress", ethereum.GetEthAddress().Hex()).Info("Ethereum client has been initialized!")
return ethereum
}
func providePubsub(h host.Host) (*pubsub2.PubSub, error) { func providePubsub(h host.Host) (*pubsub2.PubSub, error) {
return pubsub2.NewFloodSub( return pubsub2.NewFloodSub(
context.TODO(), context.TODO(),