Skip to content

Commit

Permalink
Merge pull request #490 from tablelandnetwork/bcalza/fixgaslimit
Browse files Browse the repository at this point in the history
Makes healthbot tx gas limit 10% greater than estimated
  • Loading branch information
brunocalza authored Mar 16, 2023
2 parents 01e912b + abb7260 commit 726f15f
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 169 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ HTTP_PORT ?= 8080
GO_BINDATA=go run github.com/go-bindata/go-bindata/v3/[email protected]
SQLC=go run github.com/kyleconroy/sqlc/cmd/[email protected]
GOVVV=go run github.com/ahmetb/[email protected]
ABIGEN=go run github.com/ethereum/go-ethereum/cmd/[email protected]

GOVVV_FLAGS=$(shell $(GOVVV) -flags -version $(BIN_VERSION) -pkg $(shell go list ./buildinfo))

# Code generation
ethereum: ethereum-testcontroller ethereum-testerc721 ethereum-testerc721a
go run github.com/ethereum/go-ethereum/cmd/[email protected] --abi ./pkg/tables/impl/ethereum/abi.json --pkg ethereum --type Contract --out pkg/tables/impl/ethereum/contract.go --bin pkg/tables/impl/ethereum/bytecode.bin
$(ABIGEN) --abi ./pkg/tables/impl/ethereum/abi.json --pkg ethereum --type Contract --out pkg/tables/impl/ethereum/contract.go --bin pkg/tables/impl/ethereum/bytecode.bin
.PHONY: ethereum

ethereum-testcontroller:
go run github.com/ethereum/go-ethereum/cmd/[email protected] --abi ./pkg/tables/impl/ethereum/test/controller/abi.json --pkg controller --type Contract --out pkg/tables/impl/ethereum/test/controller/controller.go --bin pkg/tables/impl/ethereum/test/controller/bytecode.bin
$(ABIGEN) --abi ./pkg/tables/impl/ethereum/test/controller/abi.json --pkg controller --type Contract --out pkg/tables/impl/ethereum/test/controller/controller.go --bin pkg/tables/impl/ethereum/test/controller/bytecode.bin
.PHONY: ethereum-testcontroller

ethereum-testerc721:
go run github.com/ethereum/go-ethereum/cmd/[email protected] --abi ./pkg/tables/impl/ethereum/test/erc721Enumerable/abi.json --pkg erc721Enumerable --type Contract --out pkg/tables/impl/ethereum/test/erc721Enumerable/erc721Enumerable.go --bin pkg/tables/impl/ethereum/test/erc721Enumerable/bytecode.bin
$(ABIGEN) --abi ./pkg/tables/impl/ethereum/test/erc721Enumerable/abi.json --pkg erc721Enumerable --type Contract --out pkg/tables/impl/ethereum/test/erc721Enumerable/erc721Enumerable.go --bin pkg/tables/impl/ethereum/test/erc721Enumerable/bytecode.bin
.PHONY: ethereum-testerc721

ethereum-testerc721a:
go run github.com/ethereum/go-ethereum/cmd/[email protected] --abi ./pkg/tables/impl/ethereum/test/erc721aQueryable/abi.json --pkg erc721aQueryable --type Contract --out pkg/tables/impl/ethereum/test/erc721aQueryable/erc721aQueryable.go --bin pkg/tables/impl/ethereum/test/erc721aQueryable/bytecode.bin
$(ABIGEN) --abi ./pkg/tables/impl/ethereum/test/erc721aQueryable/abi.json --pkg erc721aQueryable --type Contract --out pkg/tables/impl/ethereum/test/erc721aQueryable/erc721aQueryable.go --bin pkg/tables/impl/ethereum/test/erc721aQueryable/bytecode.bin
.PHONY: ethereum-testerc721a

system-sql-assets:
Expand Down
1 change: 1 addition & 0 deletions cmd/healthbot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ChainConfig struct {
GatewayEndpoint string
ContractAddr string
SuggestedGasPriceMultiplier float64
EstimatedGasLimitMultiplier float64
}
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/healthbot/counterprobe/counterprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CounterProbe struct {
receiptTimeout time.Duration
tableName string
suggGasPriceMultiplier float64
estGasLimitMultiplier float64

lock sync.RWMutex
mLastCounterValue int64
Expand All @@ -45,6 +46,7 @@ func New(
checkInterval time.Duration,
receiptTimeout time.Duration,
suggGasPriceMultiplier float64,
estGasLimitMultiplier float64,
) (*CounterProbe, error) {
log := logger.With().
Str("component", "healthbot").
Expand All @@ -65,6 +67,7 @@ func New(
tableName: tableName,
receiptTimeout: receiptTimeout,
suggGasPriceMultiplier: suggGasPriceMultiplier,
estGasLimitMultiplier: estGasLimitMultiplier,
}
if err := cp.initMetrics(chainName); err != nil {
return nil, fmt.Errorf("initializing metrics: %s", err)
Expand Down Expand Up @@ -142,7 +145,8 @@ func (cp *CounterProbe) increaseCounterValue(ctx context.Context) error {
txnHash, err := cp.client.Write(
ctx,
fmt.Sprintf("update %s set counter=counter+1", cp.tableName),
clientV1.WithSuggestedPriceMultiplier(cp.suggGasPriceMultiplier))
clientV1.WithSuggestedPriceMultiplier(cp.suggGasPriceMultiplier),
clientV1.WithEstimatedGasLimitMultiplier(cp.estGasLimitMultiplier))
if err != nil {
return fmt.Errorf("calling client Write: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/healthbot/counterprobe/counterprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestProduction(t *testing.T) {
client, err := clientV1.NewClient(ctx, wallet, clientV1.NewClientChain(chain))
require.NoError(t, err)

cp, err := New("optimism-mainnet", client, "Runbook_24", time.Second, time.Second*10, 1)
cp, err := New("optimism-mainnet", client, "Runbook_24", time.Second, time.Second*10, 1, 1)
require.NoError(t, err)

value, err := cp.healthCheck(context.Background())
Expand Down
6 changes: 6 additions & 0 deletions cmd/healthbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ func main() {
if chainCfg.OverrideClient.SuggestedGasPriceMultiplier > 0 {
suggestedGasPriceMultiplier = chainCfg.OverrideClient.SuggestedGasPriceMultiplier
}
estimatedGasLimitMultiplier := 1.0
if chainCfg.OverrideClient.EstimatedGasLimitMultiplier > 0 {
estimatedGasLimitMultiplier = chainCfg.OverrideClient.EstimatedGasLimitMultiplier
}

cp, err := counterprobe.New(
chain.Name,
client,
chainCfg.Probe.Tablename,
checkInterval,
receiptTimeout,
suggestedGasPriceMultiplier,
estimatedGasLimitMultiplier,
)
if err != nil {
log.Fatal().Err(err).Msg("initializing counter-probe")
Expand Down
128 changes: 41 additions & 87 deletions cmd/toolkit/smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
systemimpl "github.com/textileio/go-tableland/internal/system/impl"
"github.com/textileio/go-tableland/internal/tableland"
"github.com/textileio/go-tableland/pkg/nonce/impl"
parserimpl "github.com/textileio/go-tableland/pkg/parsing/impl"
"github.com/textileio/go-tableland/pkg/tables"
"github.com/textileio/go-tableland/pkg/tables/impl/ethereum"
"github.com/textileio/go-tableland/pkg/wallet"
)
Expand Down Expand Up @@ -55,38 +56,11 @@ var runSQLCmd = &cobra.Command{
return fmt.Errorf("dial: %s", err)
}

gasPrice, err := conn.SuggestGasPrice(ctx)
if err != nil {
return fmt.Errorf("suggest gas price: %s", err)
}

wallet, err := wallet.NewWallet(privateKey)
if err != nil {
return fmt.Errorf("new wallet: %s", err)
}

auth, err := bind.NewKeyedTransactorWithChainID(wallet.PrivateKey(), big.NewInt(int64(chainID)))
if err != nil {
return fmt.Errorf("new keyed transactor with chain id: %s", err)
}

nonce, err := conn.PendingNonceAt(ctx, wallet.Address())
if err != nil {
return fmt.Errorf("pending nonce at: %s", err)
}
opts := &bind.TransactOpts{
Context: ctx,
Signer: auth.Signer,
From: auth.From,
Nonce: big.NewInt(0).SetUint64(nonce),
GasPrice: gasPrice,
}

contract, err := ethereum.NewContract(common.HexToAddress(contractAddress), conn)
if err != nil {
return fmt.Errorf("new contract: %s", err)
}

parser, err := parserimpl.New([]string{
"sqlite_",
systemimpl.SystemTablesPrefix,
Expand All @@ -102,9 +76,20 @@ var runSQLCmd = &cobra.Command{
return fmt.Errorf("validating mutating query: %s", err)
}

tx, err := contract.RunSQL(opts,
client, err := ethereum.NewClient(
conn,
tableland.ChainID(chainID),
common.HexToAddress(contractAddress),
wallet,
impl.NewSimpleTracker(wallet, conn),
)
if err != nil {
return fmt.Errorf("creating ethereum client: %s", err)
}

tx, err := client.RunSQL(ctx,
wallet.Address(),
stmts[0].GetTableID().ToBigInt(),
stmts[0].GetTableID(),
query)
if err != nil {
return fmt.Errorf("run sql: %s", err)
Expand Down Expand Up @@ -146,38 +131,11 @@ var createTableCmd = &cobra.Command{
return fmt.Errorf("dial: %s", err)
}

gasPrice, err := conn.SuggestGasPrice(ctx)
if err != nil {
return fmt.Errorf("suggest gas price: %s", err)
}

wallet, err := wallet.NewWallet(privateKey)
if err != nil {
return fmt.Errorf("new wallet: %s", err)
}

auth, err := bind.NewKeyedTransactorWithChainID(wallet.PrivateKey(), big.NewInt(int64(chainID)))
if err != nil {
return fmt.Errorf("new keyed transactor with chain id: %s", err)
}

nonce, err := conn.PendingNonceAt(ctx, wallet.Address())
if err != nil {
return fmt.Errorf("pending nonce at: %s", err)
}
opts := &bind.TransactOpts{
Context: ctx,
Signer: auth.Signer,
From: auth.From,
Nonce: big.NewInt(0).SetUint64(nonce),
GasPrice: gasPrice,
}

contract, err := ethereum.NewContract(common.HexToAddress(contractAddress), conn)
if err != nil {
return fmt.Errorf("new contract: %s", err)
}

parser, err := parserimpl.New([]string{
"sqlite_",
systemimpl.SystemTablesPrefix,
Expand All @@ -192,7 +150,18 @@ var createTableCmd = &cobra.Command{
return fmt.Errorf("validate create table: %s", err)
}

tx, err := contract.CreateTable(opts, wallet.Address(), stmt)
client, err := ethereum.NewClient(
conn,
tableland.ChainID(chainID),
common.HexToAddress(contractAddress),
wallet,
impl.NewSimpleTracker(wallet, conn),
)
if err != nil {
return fmt.Errorf("creating ethereum client: %s", err)
}

tx, err := client.CreateTable(ctx, wallet.Address(), stmt)
if err != nil {
return fmt.Errorf("create table: %s", err)
}
Expand Down Expand Up @@ -234,38 +203,11 @@ var setControllerCmd = &cobra.Command{
return fmt.Errorf("dial: %s", err)
}

gasPrice, err := conn.SuggestGasPrice(ctx)
if err != nil {
return fmt.Errorf("suggest gas price: %s", err)
}

wallet, err := wallet.NewWallet(privateKey)
if err != nil {
return fmt.Errorf("new wallet: %s", err)
}

auth, err := bind.NewKeyedTransactorWithChainID(wallet.PrivateKey(), big.NewInt(int64(chainID)))
if err != nil {
return fmt.Errorf("new keyed transactor with chain id: %s", err)
}

nonce, err := conn.PendingNonceAt(ctx, wallet.Address())
if err != nil {
return fmt.Errorf("pending nonce at: %s", err)
}
opts := &bind.TransactOpts{
Context: ctx,
Signer: auth.Signer,
From: auth.From,
Nonce: big.NewInt(0).SetUint64(nonce),
GasPrice: gasPrice,
}

contract, err := ethereum.NewContract(common.HexToAddress(contractAddress), conn)
if err != nil {
return fmt.Errorf("new contract: %s", err)
}

tableIDStr := args[0]
controller := args[1]

Expand All @@ -275,9 +217,21 @@ var setControllerCmd = &cobra.Command{
return fmt.Errorf("set string: %s", err)
}

tx, err := contract.SetController(opts,
client, err := ethereum.NewClient(
conn,
tableland.ChainID(chainID),
common.HexToAddress(contractAddress),
wallet,
impl.NewSimpleTracker(wallet, conn),
)
if err != nil {
return fmt.Errorf("creating ethereum client: %s", err)
}

tx, err := client.SetController(
ctx,
wallet.Address(),
tableID,
tables.TableID(*tableID),
common.HexToAddress(controller),
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion docker/deployed/testnet/healthbot/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"Tablename": "${HEALTHBOT_ETHEREUM_GOERLI_TABLE}"
},
"OverrideClient": {
"SuggestedGasPriceMultiplier": 1.2
"SuggestedGasPriceMultiplier": 1.2,
"EstimatedGasLimitMultiplier": 1.1
}
},
{
Expand Down
18 changes: 17 additions & 1 deletion pkg/client/v1/writequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (c *Client) Write(ctx context.Context, query string, opts ...WriteOption) (
c.wallet.Address(),
tables.TableID(tableID),
query,
tables.WithSuggestedPriceMultiplier(config.suggestedGasPriceMultiplier))
tables.WithSuggestedPriceMultiplier(config.suggestedGasPriceMultiplier),
tables.WithEstimatedGasLimitMultiplier(config.estimatedGasLimitMultiplier))
if err != nil {
return "", fmt.Errorf("calling RunSQL: %v", err)
}
Expand All @@ -100,10 +101,12 @@ type WriteOption func(*WriteConfig) error
// WriteConfig contains configuration attributes to call Write.
type WriteConfig struct {
suggestedGasPriceMultiplier float64
estimatedGasLimitMultiplier float64
}

var defaultWriteConfig = WriteConfig{
suggestedGasPriceMultiplier: 1.0,
estimatedGasLimitMultiplier: 1.0,
}

// WithSuggestedPriceMultiplier allows to modify the gas priced to be used with respect with the suggested gas price.
Expand All @@ -118,3 +121,16 @@ func WithSuggestedPriceMultiplier(m float64) WriteOption {
return nil
}
}

// WithEstimatedGasLimitMultiplier allows to modify the gas limit to be used with respect with the estimated gas.
// For example, if `m=1.2` then the gas limit to be used will be `estimatedGas * 1.2`.
func WithEstimatedGasLimitMultiplier(m float64) WriteOption {
return func(wc *WriteConfig) error {
if m <= 0 {
return fmt.Errorf("multiplier should be positive")
}
wc.estimatedGasLimitMultiplier = m

return nil
}
}
Loading

0 comments on commit 726f15f

Please sign in to comment.