Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-1608] Add Chainlink Image Variations (and lots of linting) #1145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ func (h *SafeEVMHeader) UnmarshalJSON(bs []byte) error {

h.Hash = jsonHead.Hash
h.Number = (*big.Int)(jsonHead.Number)
h.Timestamp = time.Unix(int64(jsonHead.Timestamp), 0).UTC()

jsonTimestamp := jsonHead.Timestamp
h.Timestamp = time.Unix(int64(jsonTimestamp), 0).UTC() // nolint gosec
h.BaseFee = (*big.Int)(jsonHead.BaseFee)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion lib/blockchain/celo.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (e *CeloClient) TransactionOpts(from *EthereumWallet) (*bind.TransactOpts,
if err != nil {
return nil, err
}
opts.Nonce = big.NewInt(int64(nonce))
opts.Nonce = new(big.Int).SetUint64(nonce)

gasPrice, err := e.Client.SuggestGasPrice(context.Background())
if err != nil {
Expand Down
31 changes: 19 additions & 12 deletions lib/blockchain/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"
"math/big"
"regexp"
"strconv"
Expand Down Expand Up @@ -263,7 +264,7 @@ func (e *EthereumClient) HeaderTimestampByNumber(ctx context.Context, bn *big.In
if err != nil {
return 0, err
}
return uint64(h.Timestamp.UTC().Unix()), nil
return uint64(h.Timestamp.UTC().Unix()), nil // nolint gosec
}

// BlockNumber gets latest block number
Expand Down Expand Up @@ -539,7 +540,7 @@ func (e *EthereumClient) TransactionOpts(from *EthereumWallet) (*bind.TransactOp
if err != nil {
return nil, err
}
opts.Nonce = big.NewInt(int64(nonce))
opts.Nonce = new(big.Int).SetUint64(nonce)

if e.NetworkConfig.MinimumConfirmations <= 0 { // Wait for your turn to send on an L2 chain
<-e.NonceSettings.registerInstantTransaction(from.Address(), nonce)
Expand Down Expand Up @@ -707,8 +708,7 @@ func (e *EthereumClient) WaitForFinalizedTx(txHash common.Hash) (*big.Int, time.
// if the tx is finalized it returns true, the finalized header number by which the tx was considered finalized and the time at which it was finalized
func (e *EthereumClient) IsTxHeadFinalized(txHdr, header *SafeEVMHeader) (bool, *big.Int, time.Time, error) {
if e.NetworkConfig.FinalityDepth > 0 {
if header.Number.Cmp(new(big.Int).Add(txHdr.Number,
big.NewInt(int64(e.NetworkConfig.FinalityDepth)))) > 0 {
if header.Number.Cmp(new(big.Int).Add(txHdr.Number, new(big.Int).SetUint64(e.NetworkConfig.FinalityDepth))) > 0 {
return true, header.Number, header.Timestamp, nil
}
return false, nil, time.Time{}, nil
Expand Down Expand Up @@ -1100,7 +1100,7 @@ func (e *EthereumClient) GetLatestFinalizedBlockHeader(ctx context.Context) (*ty
}
latestBlockNumber := header.Number.Uint64()
finalizedBlockNumber := latestBlockNumber - e.NetworkConfig.FinalityDepth
return e.Client.HeaderByNumber(ctx, big.NewInt(int64(finalizedBlockNumber)))
return e.Client.HeaderByNumber(ctx, new(big.Int).SetUint64(finalizedBlockNumber))
}

// EstimatedFinalizationTime returns the estimated time it takes for a block to be finalized
Expand All @@ -1118,10 +1118,14 @@ func (e *EthereumClient) EstimatedFinalizationTime(ctx context.Context) (time.Du
if err != nil {
return 0, err
}
if e.NetworkConfig.FinalityDepth == 0 {
finDepth := e.NetworkConfig.FinalityDepth
if finDepth == 0 {
return 0, fmt.Errorf("finality depth is 0 and finality tag is not enabled")
}
timeBetween := time.Duration(e.NetworkConfig.FinalityDepth) * blckTime
if finDepth > math.MaxInt64 {
return 0, fmt.Errorf("finality depth %d is larger than the max value of int64", finDepth)
}
timeBetween := time.Duration(finDepth) * blckTime
e.l.Info().
Str("Time", timeBetween.String()).
Str("Network", e.GetNetworkName()).
Expand Down Expand Up @@ -1159,7 +1163,7 @@ func (e *EthereumClient) TimeBetweenFinalizedBlocks(ctx context.Context, maxTime
return 0, err
}
if nextFinalizedHeader.Number.Cmp(currentFinalizedHeader.Number) > 0 {
timeBetween := time.Unix(int64(nextFinalizedHeader.Time), 0).Sub(time.Unix(int64(currentFinalizedHeader.Time), 0))
timeBetween := time.Unix(int64(nextFinalizedHeader.Time), 0).Sub(time.Unix(int64(currentFinalizedHeader.Time), 0)) // nolint gosec
e.l.Info().
Str("Time", timeBetween.String()).
Str("Network", e.GetNetworkName()).
Expand Down Expand Up @@ -1190,23 +1194,26 @@ func (e *EthereumClient) AvgBlockTime(ctx context.Context) (time.Duration, error
}
totalTime := time.Duration(0)
var previousHeader *types.Header
previousHeader, err = e.Client.HeaderByNumber(ctx, big.NewInt(int64(startBlockNumber-1)))
previousHeader, err = e.Client.HeaderByNumber(ctx, new(big.Int).SetUint64(startBlockNumber-1))
if err != nil {
return totalTime, err
}
for i := startBlockNumber; i <= latestBlockNumber; i++ {
hdr, err := e.Client.HeaderByNumber(ctx, big.NewInt(int64(i)))
hdr, err := e.Client.HeaderByNumber(ctx, new(big.Int).SetUint64(i))
if err != nil {
return totalTime, err
}

blockTime := time.Unix(int64(hdr.Time), 0)
previousBlockTime := time.Unix(int64(previousHeader.Time), 0)
blockTime := time.Unix(int64(hdr.Time), 0) // nolint gosec
previousBlockTime := time.Unix(int64(previousHeader.Time), 0) // nolint gosec
blockDuration := blockTime.Sub(previousBlockTime)
totalTime += blockDuration
previousHeader = hdr
}

if numBlocks > math.MaxInt64 {
return 0, fmt.Errorf("numBlocks %d is larger than the max value of int64", numBlocks)
}
averageBlockTime := totalTime / time.Duration(numBlocks)

return averageBlockTime, nil
Expand Down
2 changes: 1 addition & 1 deletion lib/blockchain/transaction_confirmers.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (e *EthereumClient) backfillMissedBlocks(lastBlockSeen uint64, headerChanne
Uint64("Latest Block", latestBlockNumber).
Msg("Backfilling missed blocks since RPC connection issues")
for i := lastBlockSeen + 1; i <= latestBlockNumber; i++ {
header, err := e.HeaderByNumber(context.Background(), big.NewInt(int64(i)))
header, err := e.HeaderByNumber(context.Background(), new(big.Int).SetUint64(i))
if err != nil {
e.l.Err(err).Uint64("Number", i).Msg("Error getting header, unable to backfill and process it")
return
Expand Down
6 changes: 3 additions & 3 deletions lib/client/rpc_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestRPCSuite(t *testing.T) {
require.NoError(t, err)
blockNumber, err := client.BlockNumber(context.Background())
require.NoError(t, err)
block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
block, err := client.BlockByNumber(context.Background(), new(big.Int).SetUint64(blockNumber))
require.NoError(t, err)
// check the base fee of the block
require.Equal(t, "2000000000", block.BaseFee().String(), "expected base fee to be 20 gwei")
Expand All @@ -41,7 +41,7 @@ func TestRPCSuite(t *testing.T) {
require.NoError(t, err)
blockNumber, err = client.BlockNumber(context.Background())
require.NoError(t, err)
block, err = client.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
block, err = client.BlockByNumber(context.Background(), new(big.Int).SetUint64(blockNumber))
require.NoError(t, err)
// check the base fee of the block
require.Equal(t, "3000000000", block.BaseFee().String(), "expected base fee to be 30 gwei")
Expand All @@ -52,7 +52,7 @@ func TestRPCSuite(t *testing.T) {
require.NoError(t, err)
blockNumber, err = client.BlockNumber(context.Background())
require.NoError(t, err)
block, err = client.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
block, err = client.BlockByNumber(context.Background(), new(big.Int).SetUint64(blockNumber))
require.NoError(t, err)
// check the base fee of the block
require.Equal(t, "2250000000", block.BaseFee().String(), "expected base fee to be 30 gwei")
Expand Down
6 changes: 3 additions & 3 deletions lib/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func TestRPCAPI(t *testing.T) {
pm.Stop()
bn, err := client.BlockNumber(context.Background())
require.NoError(t, err)
require.GreaterOrEqual(t, uint64(iterations), bn-1)
require.LessOrEqual(t, uint64(iterations), bn+1)
require.GreaterOrEqual(t, uint64(iterations), bn-1) // nolint gosec
require.LessOrEqual(t, uint64(iterations), bn+1) // nolint gosec
})

t.Run("(anvil) test we can mine blocks with strictly N+ transactions", func(t *testing.T) {
Expand All @@ -211,7 +211,7 @@ func TestRPCAPI(t *testing.T) {
}
bn, err := client.BlockNumber(context.Background())
require.NoError(t, err)
for i := 1; i <= int(bn); i++ {
for i := 1; i <= int(bn); i++ { // nolint gosec
block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(i)))
require.NoError(t, err)
require.GreaterOrEqual(t, int64(block.Transactions().Len()), txnInBlock)
Expand Down
6 changes: 3 additions & 3 deletions lib/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,21 @@ func (n *NetworkConfig) OverrideURLsAndKeysFromEVMNetwork() {
return
}
for name, evmNetwork := range n.EVMNetworks {
if evmNetwork.URLs != nil && len(evmNetwork.URLs) > 0 {
if len(evmNetwork.URLs) > 0 {
logging.L.Warn().Str("network", name).Msg("found URLs in EVMNetwork. overriding RPC URLs in RpcWsUrls with EVMNetwork URLs")
if n.RpcWsUrls == nil {
n.RpcWsUrls = make(map[string][]string)
}
n.RpcWsUrls[name] = evmNetwork.URLs
}
if evmNetwork.HTTPURLs != nil && len(evmNetwork.HTTPURLs) > 0 {
if len(evmNetwork.HTTPURLs) > 0 {
logging.L.Warn().Str("network", name).Msg("found HTTPURLs in EVMNetwork. overriding RPC URLs in RpcHttpUrls with EVMNetwork HTTP URLs")
if n.RpcHttpUrls == nil {
n.RpcHttpUrls = make(map[string][]string)
}
n.RpcHttpUrls[name] = evmNetwork.HTTPURLs
}
if evmNetwork.PrivateKeys != nil && len(evmNetwork.PrivateKeys) > 0 {
if len(evmNetwork.PrivateKeys) > 0 {
logging.L.Warn().Str("network", name).Msg("found PrivateKeys in EVMNetwork. overriding wallet keys in WalletKeys with EVMNetwork private keys")
if n.WalletKeys == nil {
n.WalletKeys = make(map[string][]string)
Expand Down
4 changes: 2 additions & 2 deletions lib/gauntlet/gauntlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (g *Gauntlet) GenerateRandomNetwork() {
type ExecCommandOptions struct {
ErrHandling []string
CheckErrorsInRead bool
RetryCount int
RetryCount int // TODO: set to uint
RetryDelay time.Duration
}

Expand Down Expand Up @@ -154,7 +154,7 @@ func (g *Gauntlet) ExecCommandWithRetries(args []string, options ExecCommandOpti
},
retry.Delay(options.RetryDelay),
retry.MaxDelay(options.RetryDelay),
retry.Attempts(uint(options.RetryCount)),
retry.Attempts(uint(options.RetryCount)), // nolint gosec
)

return output, err
Expand Down
4 changes: 2 additions & 2 deletions lib/logstream/logstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (m *LogStream) ConnectContainer(ctx context.Context, container LogProducing
startErr := retry.Do(func() error {
return container.StartLogProducer(ctx, tc.WithLogProductionTimeout(timeout))
},
retry.Attempts(uint(retryLimit)),
retry.Attempts(uint(retryLimit)), // nolint gosec
retry.Delay(1*time.Second),
retry.OnRetry(func(n uint, err error) {
m.log.Info().
Expand Down Expand Up @@ -298,7 +298,7 @@ func (m *LogStream) ConnectContainer(ctx context.Context, container LogProducing
return
}
}
}(cons.logListeningDone, m.loggingConfig.LogStream.LogProducerTimeout.Duration, int(*m.loggingConfig.LogStream.LogProducerRetryLimit))
}(cons.logListeningDone, m.loggingConfig.LogStream.LogProducerTimeout.Duration, int(*m.loggingConfig.LogStream.LogProducerRetryLimit)) // nolint gosec

return err
}
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/seth/seth.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ func ValidateSethNetworkConfig(cfg *pkg_seth.Network) error {
if cfg == nil {
return errors.New("network cannot be nil")
}
if cfg.URLs == nil || len(cfg.URLs) == 0 {
if len(cfg.URLs) == 0 {
return errors.New("URLs are required")
}
if cfg.PrivateKeys == nil || len(cfg.PrivateKeys) == 0 {
if len(cfg.PrivateKeys) == 0 {
return errors.New("PrivateKeys are required")
}
if cfg.TransferGasFee == 0 {
Expand Down
11 changes: 9 additions & 2 deletions seth/block_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ func (cs *BlockStats) CalculateBlockDurations(blocks []*types.Block) error {
totalSize := uint64(0)

for i := 1; i < len(blocks); i++ {
duration := time.Unix(int64(blocks[i].Time()), 0).Sub(time.Unix(int64(blocks[i-1].Time()), 0))
// Handle possible overflow
currentBlockTime := blocks[i].Time()
previousBlockTime := blocks[i-1].Time()
if currentBlockTime > math.MaxInt64 || previousBlockTime > math.MaxInt64 {
return fmt.Errorf("block time exceeds int64 range")
}

duration := time.Unix(int64(currentBlockTime), 0).Sub(time.Unix(int64(previousBlockTime), 0))
durations = append(durations, duration)
totalDuration += duration

Expand Down Expand Up @@ -156,7 +163,7 @@ func (cs *BlockStats) CalculateBlockDurations(blocks []*types.Block) error {

L.Debug().
Uint64("BlockNumber", blocks[i].Number().Uint64()).
Time("BlockTime", time.Unix(int64(blocks[i].Time()), 0)).
Time("BlockTime", time.Unix(int64(currentBlockTime), 0)).
Str("Duration", duration.String()).
Float64("GasUsedPercentage", calculateRatioPercentage(blocks[i].GasUsed(), blocks[i].GasLimit())).
Float64("TPS", tps).
Expand Down
34 changes: 26 additions & 8 deletions seth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"math"
"math/big"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -58,7 +59,7 @@ type Client struct {
Client *ethclient.Client
Addresses []common.Address
PrivateKeys []*ecdsa.PrivateKey
ChainID int64
ChainID int64 // TODO: Use uint64
URL string
Context context.Context
CancelFunc context.CancelFunc
Expand Down Expand Up @@ -253,13 +254,20 @@ func NewClientRaw(
cfg.Network.ChainID = chainId.Uint64()
}
ctx, cancelFunc := context.WithCancel(context.Background())

// Protect from overflow
chainId := cfg.Network.ChainID
if chainId > math.MaxInt64 {
cancelFunc()
return nil, fmt.Errorf("chain ID is too big: %d", chainId)
}
c := &Client{
Cfg: cfg,
Client: client,
Addresses: addrs,
PrivateKeys: pkeys,
URL: cfg.FirstNetworkURL(),
ChainID: int64(cfg.Network.ChainID),
ChainID: int64(chainId),
Context: ctx,
CancelFunc: cancelFunc,
}
Expand Down Expand Up @@ -433,13 +441,18 @@ func (m *Client) TransferETHFromKey(ctx context.Context, fromKeyNum int, to stri
return errors.Wrap(err, "failed to get network ID")
}

var gasLimit int64
var gasLimit uint64
//nolint
gasLimitRaw, err := m.EstimateGasLimitForFundTransfer(m.Addresses[fromKeyNum], common.HexToAddress(to), value)
if err != nil {
gasLimit = m.Cfg.Network.TransferGasFee
// protect from overflow
gasFee := m.Cfg.Network.TransferGasFee
if gasFee < 0 {
return fmt.Errorf("transferGasFee is negative, and gas limit estimation failed: %w", err)
}
gasLimit = uint64(gasFee)
} else {
gasLimit = int64(gasLimitRaw)
gasLimit = gasLimitRaw
}

if gasPrice == nil {
Expand Down Expand Up @@ -565,7 +578,7 @@ func WithPending(pending bool) CallOpt {
// WithBlockNumber sets blockNumber option for bind.CallOpts
func WithBlockNumber(bn uint64) CallOpt {
return func(o *bind.CallOpts) {
o.BlockNumber = big.NewInt(int64(bn))
o.BlockNumber = new(big.Int).SetUint64(bn)
}
}

Expand Down Expand Up @@ -916,7 +929,7 @@ func (m *Client) configureTransactionOpts(
estimations GasEstimations,
o ...TransactOpt,
) *bind.TransactOpts {
opts.Nonce = big.NewInt(int64(nonce))
opts.Nonce = new(big.Int).SetUint64(nonce)
opts.GasPrice = estimations.GasPrice
opts.GasLimit = m.Cfg.Network.GasLimit

Expand Down Expand Up @@ -945,7 +958,12 @@ func NewContractLoader[T any](client *Client) *ContractLoader[T] {

// LoadContract loads contract by name, address, ABI loader and wrapper init function, it adds contract ABI to Seth Contract Store and address to Contract Map. Thanks to that we can easily
// trace and debug interactions with the contract. Signatures of functions passed to this method were chosen to conform to Geth wrappers' GetAbi() and NewXXXContract() functions.
func (cl *ContractLoader[T]) LoadContract(name string, address common.Address, abiLoadFn func() (*abi.ABI, error), wrapperInitFn func(common.Address, bind.ContractBackend) (*T, error)) (*T, error) {
func (cl *ContractLoader[T]) LoadContract(
name string,
address common.Address,
abiLoadFn func() (*abi.ABI, error),
wrapperInitFn func(common.Address, bind.ContractBackend) (*T, error),
) (*T, error) {
abiData, err := abiLoadFn()
if err != nil {
return new(T), err
Expand Down
2 changes: 1 addition & 1 deletion seth/client_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestAPINonces(t *testing.T) {
{
name: "with nonce override",
transactionOpts: []seth.TransactOpt{
seth.WithNonce(big.NewInt(int64(pnonce))),
seth.WithNonce(new(big.Int).SetUint64(pnonce)),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion seth/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m *GasEstimator) Stats(fromNumber uint64, priorityPerc float64) (GasSugges
fromNumber = 1
}
}
hist, err := m.Client.Client.FeeHistory(context.Background(), fromNumber, big.NewInt(int64(bn)), []float64{priorityPerc})
hist, err := m.Client.Client.FeeHistory(context.Background(), fromNumber, new(big.Int).SetUint64(bn), []float64{priorityPerc})
if err != nil {
return GasSuggestions{}, err
}
Expand Down
Loading
Loading