Skip to content

Commit

Permalink
new contract and more fixes (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-dionysos authored Dec 28, 2023
1 parent 9168e2a commit c30eef5
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 219 deletions.
3 changes: 2 additions & 1 deletion coin-distribution/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ INSERT INTO global (key,value)
('coin_collector_start_hour','0'),
('coin_collector_min_balance_required','0'),
('coin_collector_denied_countries',''),
('coin_distributer_gas_limit_units','300000')
('coin_distributer_gas_limit_units','30000000'),
('coin_distributer_gas_price_override','3000000000')
ON CONFLICT(key) DO NOTHING;

CREATE TABLE IF NOT EXISTS coin_distributions_by_earner (
Expand Down
29 changes: 25 additions & 4 deletions coin-distribution/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package coindistribution

import (
"context"
"fmt"
"math/big"
"net"
"net/http"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -65,7 +67,12 @@ func handleRPCError(ctx context.Context, target error) (retryAfter time.Duration
return 0
}

if errors.Is(target, core.ErrIntrinsicGas) {
// We may have two types of errors here:
// 1. Errors from ethereum RPC.
// 2. Errors from ethereum module (pre validation).
// The first type of errors are wrapped (see core.ErrXXX), the second type of errors are not wrapped. Just strings. As is.
// So check the second case with HasPrefix() and the first case with errors.Is().
if errors.Is(target, core.ErrIntrinsicGas) || strings.HasPrefix(target.Error(), core.ErrIntrinsicGas.Error()) {
log.Error(errors.Wrap(sendEthereumGasLimitTooLowSlackMessage(ctx, target.Error()), "failed to send slack message"))

return time.Minute * 10
Expand All @@ -81,7 +88,7 @@ func handleRPCError(ctx context.Context, target error) (retryAfter time.Duration
core.ErrSenderNoEOA,
core.ErrBlobFeeCapTooLow,
} {
if errors.Is(target, ethErr) {
if errors.Is(target, ethErr) || strings.HasPrefix(target.Error(), ethErr.Error()) {
return 0
}
}
Expand Down Expand Up @@ -128,11 +135,25 @@ func (ec *ethClientImpl) SuggestGasPrice(ctx context.Context) (*big.Int, error)
}

func (ec *ethClientImpl) AirdropToWallets(opts *bind.TransactOpts, recipients []common.Address, amounts []*big.Int) (*types.Transaction, error) {
// AirdropToWallets() uses PendingNonceAt() to get the next nonce internally which is not thread-safe.
// The slow zone, we **must** have `nonce` as a linear sequence, **without** gaps.
ec.Mutex.Lock()
defer ec.Mutex.Unlock()

return ec.AirDropper.AirdropToWallets(opts, recipients, amounts) //nolint:wrapcheck //.
tx, err := ec.AirDropper.AirdropToWallets(opts, recipients, amounts)
if err == nil && opts.Context.Err() == nil {
log.Info(fmt.Sprintf("airdropper: new transaction: %v | nonce %v | gas %v | cost %v | limit %v | recipients %v",
tx.Hash().String(),
tx.Nonce(),
tx.GasPrice().String(),
tx.Cost().String(),
tx.Gas(),
len(recipients),
))
// Wait a little to avoid nonce collision with pending transactions.
time.Sleep(time.Second)
}

return tx, err //nolint:wrapcheck //.
}

func (ec *ethClientImpl) CreateTransactionOpts(ctx context.Context, gasPrice, chanID *big.Int, gasLimit uint64) *bind.TransactOpts {
Expand Down
8 changes: 7 additions & 1 deletion coin-distribution/coin_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ func databaseGetValue[T bool | constraints.Integer](ctx context.Context, db stor
}

func (d *databaseConfig) GetGasLimit(ctx context.Context) (val uint64, err error) {
err = databaseGetValue(ctx, d.DB, configKeyoinDistributerGasLimit, &val)
err = databaseGetValue(ctx, d.DB, configKeyCoinDistributerGasLimit, &val)

return val, err
}

func (d *databaseConfig) GetGasPriceOverride(ctx context.Context) (val uint64, err error) {
err = databaseGetValue(ctx, d.DB, configKeyCoinDistributerGasPrice, &val)

return val, err
}
Expand Down
3 changes: 2 additions & 1 deletion coin-distribution/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const (

configKeyCoinDistributerEnabled = "coin_distributer_enabled"
configKeyCoinDistributerOnDemand = "coin_distributer_forced_execution"
configKeyoinDistributerGasLimit = "coin_distributer_gas_limit_units"
configKeyCoinDistributerGasLimit = "coin_distributer_gas_limit_units"
configKeyCoinDistributerGasPrice = "coin_distributer_gas_price_override"
)

// .
Expand Down
231 changes: 35 additions & 196 deletions coin-distribution/internal/ice_token.go

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion coin-distribution/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,20 @@ returning up.*
}

func (proc *coinProcessor) GetGasOptions(ctx context.Context) (price *big.Int, limit uint64, err error) {
price, err = proc.GetGasPrice(ctx)
gasOverride, _ := proc.GetGasPriceOverride(ctx)
if err != nil {
return nil, 0, err
}

if gasOverride == 0 {
price, err = proc.GetGasPrice(ctx)
if err != nil {
return nil, 0, err
}
} else {
price = big.NewInt(0).SetUint64(gasOverride)
}

limit, err = proc.GetGasLimit(ctx)
if err != nil {
return nil, 0, err
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.12.0 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -66,7 +66,7 @@ require (
github.com/crate-crypto/go-ipa v0.0.0-20231205143816-408dbffb2041 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.5.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/distribution/reference v0.5.0 // indirect
Expand All @@ -92,7 +92,7 @@ require (
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.13 // indirect
github.com/go-openapi/swag v0.22.6 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
Expand All @@ -104,7 +104,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/golang/snappy v0.0.5-0.20231225225746-43d5d4cd4e0e // indirect
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.5.0 // indirect
Expand Down Expand Up @@ -153,7 +153,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pierrec/lz4/v4 v4.1.19 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sx
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bits-and-blooms/bitset v1.12.0 h1:U/q1fAF7xXRhFCrhROzIfffYnu+dlS38vCZtmFVPHmA=
github.com/bits-and-blooms/bitset v1.12.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE=
github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
Expand Down Expand Up @@ -119,8 +119,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.5.0 h1:hn6cEZtQ0h3J8kFrHR/NrzyOoTnjgW1+FmNJzQ7y/sA=
github.com/deckarep/golang-set/v2 v2.5.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y=
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
Expand Down Expand Up @@ -196,8 +196,8 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
github.com/go-openapi/spec v0.20.13 h1:XJDIN+dLH6vqXgafnl5SUIMnzaChQ6QTo0/UPMbkIaE=
github.com/go-openapi/spec v0.20.13/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw=
github.com/go-openapi/swag v0.22.6 h1:dnqg1XfHXL9aBxSbktBqFR5CxVyVI+7fYWhAf1JOeTw=
github.com/go-openapi/swag v0.22.6/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
github.com/go-openapi/swag v0.22.7 h1:JWrc1uc/P9cSomxfnsFSVWoE1FW6bNbrVPmpQYpCcR8=
github.com/go-openapi/swag v0.22.7/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand Down Expand Up @@ -247,8 +247,8 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.5-0.20231225225746-43d5d4cd4e0e h1:4bw4WeyTYPp0smaXiJZCNnLrvVBqirQVreixayXezGc=
github.com/golang/snappy v0.0.5-0.20231225225746-43d5d4cd4e0e/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down Expand Up @@ -425,8 +425,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
Expand Down

0 comments on commit c30eef5

Please sign in to comment.