Skip to content

Commit

Permalink
Merge branch 'tilen/contract_fetch' into 'main'
Browse files Browse the repository at this point in the history
Automatic fetching of FastUpdater contract address.

See merge request flarenetwork/fast-updates!34
  • Loading branch information
tilenflare committed Oct 8, 2024
2 parents 6c655d1 + 5d799d3 commit f395e26
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
48 changes: 42 additions & 6 deletions go-client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ func CreateFastUpdatesClient(cfg *config.Config, valuesProvider provider.ValuesP
fastUpdatesClient.transactionAccounts[i].Address = crypto.PubkeyToAddress(*publicKeyECDSA)
}

fastUpdatesClient.fastUpdater, err = fast_updater.NewFastUpdater(
common.HexToAddress(cfg.Client.FastUpdaterAddress), fastUpdatesClient.chainClient,
)
if err != nil {
return nil, fmt.Errorf("CreateFastUpdatesClient: NewFastUpdater: %w", err)
if cfg.Client.FastUpdaterAddress != "" {
fastUpdatesClient.fastUpdater, err = fast_updater.NewFastUpdater(
common.HexToAddress(cfg.Client.FastUpdaterAddress), fastUpdatesClient.chainClient,
)
if err != nil {
return nil, fmt.Errorf("CreateFastUpdatesClient: NewFastUpdater: %w", err)
}
}

fastUpdatesClient.IncentiveManager, err = incentive.NewIncentive(
common.HexToAddress(cfg.Client.IncentiveManagerAddress), fastUpdatesClient.chainClient,
)
Expand All @@ -134,6 +137,11 @@ func CreateFastUpdatesClient(cfg *config.Config, valuesProvider provider.ValuesP
}
}

err = fastUpdatesClient.UpdateFastUpdaterContractAddress()
if err != nil {
return nil, err
}

fastUpdatesClient.flareSystemMock, err = mock.NewMock(
common.HexToAddress(cfg.Client.MockAddress), fastUpdatesClient.chainClient,
)
Expand Down Expand Up @@ -209,6 +217,11 @@ func (client *FastUpdatesClient) Run(startBlock, endBlock uint64) error {
logger.Info("Fetched feed ids: %v", client.allFeeds)

for {
err = client.UpdateFastUpdaterContractAddress()
if err != nil {
logger.Error("Failed attempt in updating the FastUpdater contract address: %s", err)
}

if blockNum%refreshFeedsBlockInterval == 0 {
client.allFeeds, err = client.GetCurrentFeedIds()
if err != nil {
Expand Down Expand Up @@ -295,7 +308,6 @@ func (client *FastUpdatesClient) Run(startBlock, endBlock uint64) error {
}
}


func (client *FastUpdatesClient) GetBlockScoreCutoffWithRepeats(blockNum uint64) (*big.Int, error) {
var cutoff *big.Int
var err error
Expand All @@ -320,3 +332,27 @@ func (client *FastUpdatesClient) WaitToEmptyRequests() {
func (client *FastUpdatesClient) Stop() {
client.transactionQueue.StopQueue()
}

func (client *FastUpdatesClient) UpdateFastUpdaterContractAddress() error {
if client.submission == nil {
return nil
}

newAddress, err := client.GetFastUpdaterContractAddress()
if err != nil {
return err
}
if newAddress != common.HexToAddress(client.params.FastUpdaterAddress) {
client.fastUpdater, err = fast_updater.NewFastUpdater(
newAddress, client.chainClient,
)
if err != nil {
return err
}

client.params.FastUpdaterAddress = newAddress.Hex()
logger.Info("Updated the FastUpdater address to %s", client.params.FastUpdaterAddress)
}

return nil
}
13 changes: 13 additions & 0 deletions go-client/client/client_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"

Expand Down Expand Up @@ -297,3 +298,15 @@ func (client *FastUpdatesClient) submitUpdates(updateProof *sortition.UpdateProo

return nil
}

func (client *FastUpdatesClient) GetFastUpdaterContractAddress() (common.Address, error) {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Duration(config.CallTimeoutMillisDefault)*time.Millisecond)
ops := &bind.CallOpts{Context: ctx}
fastUpdaterContractAddress, err := client.submission.SubmitAndPassContract(ops)
cancelFunc()
if err != nil {
return common.Address{}, err
}

return fastUpdaterContractAddress, nil
}

0 comments on commit f395e26

Please sign in to comment.