Skip to content

Commit

Permalink
Merge branch 'tilen/cutoff_fix' into 'main'
Browse files Browse the repository at this point in the history
Cutoff fix

See merge request flarenetwork/fast-updates!28
  • Loading branch information
tilenflare committed Jun 12, 2024
2 parents 8a77341 + 1a690dd commit 2c4c47c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 2 additions & 0 deletions go-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ flare_system_manager = "0x919b4b4B561C72c990DC868F751328eF127c45F4"
incentive_manager_address = "0x919b4b4B561C72c990DC868F751328eF127c45F4"
# parameter defining when a fast update can be submitted
submission_window = 10
# url of the off-chain data provider
value_provider_url = "http://127.0.0.1:3101/feed-values/0"

[transactions]
gas_limit = 8000000
Expand Down
25 changes: 23 additions & 2 deletions go-client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -53,6 +54,7 @@ type UpdatesStats struct {

const (
refreshFeedsBlockInterval = 100
cutoffRepeats = 10
)

func CreateFastUpdatesClient(cfg *config.Config, valuesProvider provider.ValuesProvider) (*FastUpdatesClient, error) {
Expand Down Expand Up @@ -244,9 +246,10 @@ func (client *FastUpdatesClient) Run(startBlock, endBlock uint64) error {
}
logger.Info("new epoch, my weight weight %d, current block %d", weight, blockNum)
}
cutoff, err := client.GetBlockScoreCutoff(big.NewInt(int64(blockNum))) // todo

cutoff, err := client.GetBlockScoreCutoffWithRepeats(blockNum)
if err != nil {
return fmt.Errorf("Run: GetCurrentScoreCutoff for block %d: %w", blockNum, err)
return fmt.Errorf("Run: GetBlockScoreCutoffWithRepeats: %w", err)
}

updateProofs, err := sortition.FindUpdateProofs(client.key, seed, cutoff, big.NewInt(int64(blockNum)), weight)
Expand Down Expand Up @@ -292,6 +295,24 @@ func (client *FastUpdatesClient) Run(startBlock, endBlock uint64) error {
}
}


func (client *FastUpdatesClient) GetBlockScoreCutoffWithRepeats(blockNum uint64) (*big.Int, error) {
var cutoff *big.Int
var err error
for i := 0; i < cutoffRepeats; i++ {
cutoff, err = client.GetBlockScoreCutoff(big.NewInt(int64(blockNum)))
if err == nil {
break
}
if blockNum <= client.transactionQueue.CurrentBlockNum-uint64(client.params.SubmissionWindow)+1 || i == cutoffRepeats-1 {
return nil, fmt.Errorf("Run: GetCurrentScoreCutoff for block %d: %w", blockNum, err)
}
time.Sleep(200 * time.Millisecond)
}

return cutoff, err
}

func (client *FastUpdatesClient) WaitToEmptyRequests() {
client.transactionQueue.WaitToEmptyQueue()
}
Expand Down
16 changes: 6 additions & 10 deletions go-client/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
func TestClient(t *testing.T) {
chainNode := os.Getenv("CHAIN_NODE")
chainAddress := ""
valueProviderBaseUrl := ""
valueProviderUrl := ""
if chainNode == "docker_ganache" {
chainAddress = "http://ganache:8545"
valueProviderBaseUrl = "http://value-provider:3101"
valueProviderUrl = "http://value-provider:3101/feed-values/0"
} else {
// running a ganache node and an external provider that returns fixed values for testing
logger.Info("starting a ganache chain node and data provider")
Expand All @@ -34,7 +34,7 @@ func TestClient(t *testing.T) {
go cmd.Run() //nolint:errcheck

chainAddress = "http://127.0.0.1:8545"
valueProviderBaseUrl = "http://localhost:3101"
valueProviderUrl = "http://localhost:3101/feed-values/0"
}

// set chain parameters
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestClient(t *testing.T) {
cfg.Client.MockAddress = contracts.Mock.Hex()
cfg.Client.IncentiveManagerAddress = contracts.IncentiveManager.Hex()

updatesProvider := provider.NewHttpValueProvider(valueProviderBaseUrl)
updatesProvider := provider.NewHttpValueProvider(valueProviderUrl)

client, err := client.CreateFastUpdatesClient(&cfg, updatesProvider)
if err != nil {
Expand Down Expand Up @@ -131,15 +131,11 @@ func TestClient(t *testing.T) {

downDockerContainers()
if client.Stats.NumUpdates == 0 {
if err != nil {
t.Fatal("no updates submitted")
}
t.Fatal("no updates submitted")
}

if client.Stats.NumSuccessfulUpdates == 0 {
if err != nil {
t.Fatal("no successful update")
}
t.Fatal("no successful update")
}

for i, val := range feeds {
Expand Down

0 comments on commit 2c4c47c

Please sign in to comment.