Skip to content

Commit

Permalink
Utilize a gas cache (#15)
Browse files Browse the repository at this point in the history
* use file cache

* utilize gas cache

* bump pickaxe

* temporarily disable mins

* more loggign

* mod update

* more logging

* bump

* remove space

* more logging

* work work

* more owrk

* more

* more debugging

* do not shadow err

* remove print debug

* enforce a min

* use registry value

* recover from panics

* move onto mainline pickaxe
  • Loading branch information
keefertaylor authored Mar 1, 2024
1 parent bc688ff commit d98b096
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/restake-go/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var startCmd = &cobra.Command{
}

// Create Gas Manager
gasPriceProvider, err := tx.NewInMemoryGasPriceProvider()
gasPriceProvider, err := tx.NewFileGasPriceProvider(logger, configurationDirectory)
if err != nil {
logger.Error().Err(err).Msg("unable to create a gas price provider")
return
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/evmos/evmos/v14 v14.0.0
github.com/rs/zerolog v1.30.0
github.com/spf13/cobra v1.7.0
github.com/tessellated-io/healthchecks v0.0.2
github.com/tessellated-io/pickaxe v1.1.6
github.com/tessellated-io/healthchecks v0.0.3
github.com/tessellated-io/pickaxe v1.1.7
github.com/tessellated-io/router v0.0.5
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -154,4 +154,5 @@ replace (

// Downgraded to avoid bugs in following commits which caused simulations to fail.
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7

)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,10 @@ github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/tessellated-io/healthchecks v0.0.2 h1:u/eGeg0rlb3tGOz0GXhHHPE5fgoETWnbR9NG5KdMHJI=
github.com/tessellated-io/healthchecks v0.0.2/go.mod h1:SNLJVFCD2Z9IFRYNiF7f4hoJ4+RsjHgrK7YvKCixfdI=
github.com/tessellated-io/pickaxe v1.1.6 h1:L/9GhwnIdjLekOldkA8mzy9ak8TC27fohxsZZBY4BTY=
github.com/tessellated-io/pickaxe v1.1.6/go.mod h1:G6FMTiWB8rLG4YH0THu2t2tZIMUC2Rykl22JotEIgLM=
github.com/tessellated-io/healthchecks v0.0.3 h1:k4y2/31ZhaZ6f7AQMCQQQOpIReJDNt8J/B0VxKchyMg=
github.com/tessellated-io/healthchecks v0.0.3/go.mod h1:SNLJVFCD2Z9IFRYNiF7f4hoJ4+RsjHgrK7YvKCixfdI=
github.com/tessellated-io/pickaxe v1.1.7 h1:iMiLWMA5Rl57JuSZNuBnqsrNwMZOvygOQ/qVj4bailE=
github.com/tessellated-io/pickaxe v1.1.7/go.mod h1:G6FMTiWB8rLG4YH0THu2t2tZIMUC2Rykl22JotEIgLM=
github.com/tessellated-io/router v0.0.5 h1:t6XXH4neABYljW4Nrf1u+orYXf1VUg249BwWMdbD3Bc=
github.com/tessellated-io/router v0.0.5/go.mod h1:Oj5Zw+/B5IwoL1ocuTsySz/1ZWEIxln50oHt0YbSjlQ=
github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg=
Expand Down
7 changes: 7 additions & 0 deletions restake/restake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ func NewRestakeClient(
}

func (rc *restakeClient) restake(ctx context.Context) ([]string, error) {
rc.logger.Info().Str("chain_id", rc.chainID).Msg("performing pre-flight sanity checks")
// 1. Perform sanity checks
err := rc.performSanityChecks(ctx)
if err != nil {
rc.logger.Error().Err(err).Str("chain_id", rc.chainID).Msg("failed sanity checks")
return nil, err
}
rc.logger.Info().Str("chain_id", rc.chainID).Msg("starting to restake")

// 2. Get all valid grants
rc.logger.Info().Str("chain_id", rc.chainID).Msg("fetching delegators and grants. this might take a while if there are many delegators...")
restakeDelegators, err := rc.grantManager.getRestakeDelegators(ctx, rc.minimumRequiredReward)
if err != nil {
rc.logger.Error().Err(err).Str("chain_id", rc.chainID).Msg("failed to fetch grants")
Expand All @@ -106,15 +109,19 @@ func (rc *restakeClient) restake(ctx context.Context) ([]string, error) {
if len(restakeDelegators) == 0 {
rc.logger.Warn().Str("chain_id", rc.chainID).Msg("no grants above minimum found, no restaking will be processed")
}
rc.logger.Info().Str("chain_id", rc.chainID).Msg("finished fetching delegators and grants")

// 3. Create restake messages
rc.logger.Info().Str("chain_id", rc.chainID).Msg("creating restake messages")
batches, err := rc.createRestakeMessages(ctx, restakeDelegators)
if err != nil {
rc.logger.Error().Err(err).Str("chain_id", rc.chainID).Msg("failed to generate restake messages")
return nil, err
}
rc.logger.Info().Str("chain_id", rc.chainID).Msg("finished creating messages")

// 4. Send in batches
rc.logger.Info().Str("chain_id", rc.chainID).Msg("sending restake batches")
txHashes := []string{}
for batchNum, batch := range batches {
rc.logger.Info().Uint("batch_size", rc.batchSize).Int("batch", batchNum+1).Int("total_batches", len(batches)).Msg("📬 sending a batch of messages")
Expand Down
83 changes: 54 additions & 29 deletions restake/restake_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (

"github.com/rs/zerolog"
"github.com/tessellated-io/healthchecks/health"
"github.com/tessellated-io/pickaxe/arrays"
chainregistry "github.com/tessellated-io/pickaxe/cosmos/chain-registry"
"github.com/tessellated-io/pickaxe/cosmos/rpc"
"github.com/tessellated-io/pickaxe/cosmos/tx"
"github.com/tessellated-io/pickaxe/crypto"
"github.com/tessellated-io/pickaxe/log"
"github.com/tessellated-io/pickaxe/util"
routertypes "github.com/tessellated-io/router/types"

"cosmossdk.io/math"
Expand Down Expand Up @@ -211,7 +213,12 @@ func (rm *RestakeManager) runRestakeForNetwork(
var err error

defer func() {
rm.logger.Debug().Str("chain_id", restakeChain.Name).Msg("restake finished")
// Ensure we didn't crash from panic
recoveredErr := recover()
if recoveredErr != nil {
err = util.InterfaceToError(recoveredErr)
}

if err != nil {
rm.logger.Error().Err(err).Str("chain_id", restakeChain.Name).Msg("restake failed with error")
}
Expand All @@ -224,12 +231,32 @@ func (rm *RestakeManager) runRestakeForNetwork(
}
results = append(results, result)

// Send health check if enabled
if !strings.EqualFold(localConfiguration.HealthChecksPingKey, "") {
healthClient := health.NewHealthClient(rm.logger, localConfiguration.HealthChecksPingKey, true)

if err == nil {
err = healthClient.SendSuccess(restakeChain.Name)
} else {
err = healthClient.SendFailure(restakeChain.Name)
}

if err != nil {
rm.logger.Error().Err(err).Str("chain_id", restakeChain.Name).Msg("unable to send healthchecks ping")
}

} else {
rm.logger.Info().Str("chain_id", restakeChain.Name).Msg("not sending healthchecks.io pings as they are disabled in config.")
}

// Leave wait group
defer wg.Done()
}()
prefixedLogger := rm.logger.ApplyPrefix(fmt.Sprintf(" [%s]", restakeChain.Name))
prefixedLogger := rm.logger.ApplyPrefix(fmt.Sprintf("[%s]", restakeChain.Name))
prefixedLogger.Info().Str("chain_name", restakeChain.Name).Msg("starting restake for network")

// Get the chain info
prefixedLogger.Info().Str("chain_name", restakeChain.Name).Msg("fetching chain registry data for chain...")
var chainInfo *chainregistry.ChainInfo
chainInfo, err = chainRegistryClient.ChainInfo(ctx, restakeChain.Name)
if err != nil {
Expand All @@ -242,14 +269,14 @@ func (rm *RestakeManager) runRestakeForNetwork(
var stakingDenom string
stakingDenom, err = chainInfo.StakingDenom()
if err != nil {
prefixedLogger.Error().Err(err).Str("chain_id", chainID).Msg("failed to get staking denom")
prefixedLogger.Error().Err(err).Str("chain_name", restakeChain.Name).Msg("failed to get staking denom")
return
}

var minimumRequiredReward math.LegacyDec
minimumRequiredReward, err = math.LegacyNewDecFromStr(restakeChain.Restake.MinimumReward.String())
if err != nil {
prefixedLogger.Error().Err(err).Str("chain_id", chainID).Str("minimum_reward", restakeChain.Restake.MinimumReward.String()).Msg("failed to parse minimum reward")
prefixedLogger.Error().Err(err).Str("chain_name", restakeChain.Name).Str("minimum_reward", restakeChain.Restake.MinimumReward.String()).Msg("failed to parse minimum reward")
return
}

Expand All @@ -263,6 +290,22 @@ func (rm *RestakeManager) runRestakeForNetwork(
validatorAddress := restakeChain.Address
botAddress := restakeChain.Restake.Address

// Fetch the asset list for the chain
var assetList *chainregistry.AssetList
assetList, err = chainRegistryClient.AssetList(ctx, restakeChain.Name)
if err != nil {
return
}

// Set minimum bot balance to be 1 fee token.
var minimumRequiredBotBalance *sdk.Coin
minimumRequiredBotBalance, err = assetList.OneToken(feeDenom)
if err != nil {
return
}

prefixedLogger.Info().Str("chain_name", restakeChain.Name).Msg("finished fetching chain registry data")

// Get an endpoint from the Router
var grpcEndpoint string
grpcEndpoint, err = rm.chainRouter.GrpcEndpoint(chainInfo.ChainID)
Expand All @@ -288,20 +331,6 @@ func (rm *RestakeManager) runRestakeForNetwork(
return
}

// Fetch the asset list for the chain
var assetList *chainregistry.AssetList
assetList, err = chainRegistryClient.AssetList(ctx, restakeChain.Name)
if err != nil {
return
}

// Set minimum bot balance to be 1 fee token.
var minimumRequiredBotBalance *sdk.Coin
minimumRequiredBotBalance, err = assetList.OneToken(feeDenom)
if err != nil {
return
}

// Create a Grant manager
var grantManager *grantManager
grantManager, err = NewGrantManager(restakeChain.Restake.Address, chainID, prefixedLogger, rpcClient, stakingDenom, validatorAddress)
Expand Down Expand Up @@ -380,19 +409,15 @@ func (rm *RestakeManager) runRestakeForNetwork(
}

txHashes, err = restakeClient.restake(ctx)
prefixedLogger.Info().Str("chain_name", restakeChain.Name).Msg("finished restaking")

// Send health check if enabled
if !strings.EqualFold(localConfiguration.HealthChecksPingKey, "") {
healthClient := health.NewHealthClient(rm.logger, localConfiguration.HealthChecksPingKey, true)

if err == nil {
err = healthClient.SendSuccess(restakeChain.Name)
} else {
err = healthClient.SendFailure(restakeChain.Name)
}
} else {
rm.logger.Info().Str("chain_id", restakeChain.Name).Msg("not sending healthchecks.io pings as they are disabled in config.")
stringifiedTxHashes := ""
if txHashes != nil {
stringifiedTxHashes = arrays.Reduce(txHashes, func(accumulated, next string) string {
return fmt.Sprintf("%s%s, ", accumulated, next)
}, "")
}
prefixedLogger.Debug().Str("chain_name", restakeChain.Name).Str("tx_hashes", stringifiedTxHashes).Err(err).Msg("verbose restake results")
}

// Results of running Restake on a given network
Expand Down

0 comments on commit d98b096

Please sign in to comment.