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

Bugfix/3078 maxgas not cached #3089

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/apps/chifra/pkg/rpcClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,3 @@ func Id_2_BlockHash(chain, arg string, isBlockHash func(arg string) bool) (strin
}
return BlockHashFromNumber(provider, blockNum)
}

func mustParseUint(input any) (result uint64) {
result, _ = strconv.ParseUint(fmt.Sprint(input), 0, 64)
return
}
49 changes: 10 additions & 39 deletions src/apps/chifra/pkg/rpcClient/get_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package rpcClient
import (
"errors"
"fmt"
"math/big"
"strconv"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
Expand Down Expand Up @@ -71,60 +70,32 @@ func GetBlockByNumberWithTxs(chain string, bn uint64, options *Options) (types.S
block.Transactions = make([]types.SimpleTransaction, 0, len(rawBlock.Transactions))
for _, rawTx := range rawBlock.Transactions {
// cast transaction to a concrete type
t, ok := rawTx.(map[string]any)
rawData, ok := rawTx.(map[string]any)
if !ok {
err = errors.New("cannot cast raw block transaction into map")
return block, err
}

txHash := base.HexToHash(fmt.Sprint(t["hash"]))
txGasPrice := mustParseUint(t["gasPrice"])
input := fmt.Sprint(t["input"])
value := big.NewInt(0)
value.SetString(fmt.Sprint(t["value"]), 0)
txIndex := mustParseUint(t["transactionIndex"])
hasToken := IsTokenRelated(input)
raw := types.NewRawTransactionFromMap(rawData)

// Get the receipt
var receipt types.SimpleReceipt
receipt, err = GetTransactionReceipt(chain, ReceiptQuery{
Bn: uint64(bn),
Txid: uint64(txIndex),
TxHash: &txHash,
GasPrice: txGasPrice,
Txid: uint64(raw.TxIndex()),
TxHash: raw.TxHash(),
GasPrice: raw.TxGasPrice(),
NeedsTs: true,
Ts: ts,
}, options)
if err != nil {
return block, err
}

tx := types.SimpleTransaction{
Hash: txHash,
BlockHash: base.HexToHash(fmt.Sprint(t["blockHash"])),
BlockNumber: mustParseUint(t["blockNumber"]),
TransactionIndex: mustParseUint(t["transactionIndex"]),
Nonce: mustParseUint(t["nonce"]),
Timestamp: ts,
From: base.HexToAddress(fmt.Sprint(t["from"])),
To: base.HexToAddress(fmt.Sprint(t["to"])),
Value: *value,
Gas: mustParseUint(t["gas"]),
GasPrice: txGasPrice,
GasUsed: receipt.GasUsed,
MaxFeePerGas: mustParseUint(t["maxFeePerGas"]),
MaxPriorityFeePerGas: mustParseUint(t["maxPriorityFeePerGas"]),
Input: input,
IsError: receipt.IsError,
HasToken: hasToken,
Receipt: &receipt,
// Traces []SimpleTrace `json:"traces"`
// ArticulatedTx *SimpleFunction `json:"articulatedTx"`
}
tx.SetGasCost(&receipt)
block.Transactions = append(block.Transactions, tx)
tx := types.NewSimpleTransaction(raw, &receipt, ts, IsTokenRelated(raw.Input))
block.Transactions = append(block.Transactions, *tx)

if options.HasStore() && !options.TransactionWriteDisabled {
options.Store.Write(&tx, writeOptions)
options.Store.Write(tx, writeOptions)
}
}

Expand Down Expand Up @@ -233,7 +204,7 @@ func getRawBlock(chain string, bn uint64, withTxs bool) (*types.RawBlock, error)
if bn == 0 {
// The RPC does not return a timestamp for the zero block, so we make one
block.Timestamp = fmt.Sprintf("0x%x", rpc.GetBlockTimestamp(chain, utils.PointerOf(uint64(0))))
} else if mustParseUint(block.Timestamp) == 0 {
} else if utils.MustParseUint(block.Timestamp) == 0 {
return &types.RawBlock{}, fmt.Errorf("block at %s returned an error: %w", fmt.Sprintf("%d", bn), ethereum.NotFound)
}

Expand Down
8 changes: 4 additions & 4 deletions src/apps/chifra/pkg/rpcClient/get_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func getSimpleLogs(chain string, filter types.SimpleLogFilter) ([]types.SimpleLo
curDate := gostradamus.FromUnixTimestamp(0)
var ret []types.SimpleLog
for _, rawLog := range rawLogs {
bn := mustParseUint(rawLog.BlockNumber)
bn := utils.MustParseUint(rawLog.BlockNumber)
if bn != curBlock {
curTs = rpc.GetBlockTimestamp(chain, &bn)
curDate = gostradamus.FromUnixTimestamp(curTs)
Expand All @@ -47,13 +47,13 @@ func getSimpleLogs(chain string, filter types.SimpleLogFilter) ([]types.SimpleLo
log := types.SimpleLog{
Address: base.HexToAddress(rawLog.Address),
BlockHash: base.HexToHash(rawLog.BlockHash),
BlockNumber: mustParseUint(rawLog.BlockNumber),
BlockNumber: utils.MustParseUint(rawLog.BlockNumber),
Data: rawLog.Data,
LogIndex: mustParseUint(rawLog.LogIndex),
LogIndex: utils.MustParseUint(rawLog.LogIndex),
Timestamp: curTs,
Date: curDate.String(),
TransactionHash: base.HexToHash(rawLog.TransactionHash),
TransactionIndex: mustParseUint(rawLog.TransactionIndex),
TransactionIndex: utils.MustParseUint(rawLog.TransactionIndex),
}
for _, topic := range rawLog.Topics {
log.Topics = append(log.Topics, base.HexToHash(topic))
Expand Down
16 changes: 8 additions & 8 deletions src/apps/chifra/pkg/rpcClient/get_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func GetTransactionReceipt(chain string, query ReceiptQuery, rpcOptions *Options
rawLog := rawLog
log := types.SimpleLog{
Address: base.HexToAddress(rawLog.Address),
LogIndex: mustParseUint(rawLog.LogIndex),
BlockNumber: mustParseUint(rawLog.BlockNumber),
LogIndex: utils.MustParseUint(rawLog.LogIndex),
BlockNumber: utils.MustParseUint(rawLog.BlockNumber),
BlockHash: base.HexToHash(rawLog.BlockHash),
TransactionIndex: mustParseUint(rawLog.TransactionIndex),
TransactionIndex: utils.MustParseUint(rawLog.TransactionIndex),
TransactionHash: base.HexToHash(tx.Hash().Hex()),
Timestamp: query.Ts,
Date: utils.FormattedDate(query.Ts),
Expand All @@ -78,15 +78,15 @@ func GetTransactionReceipt(chain string, query ReceiptQuery, rpcOptions *Options

receipt = types.SimpleReceipt{
BlockHash: base.HexToHash(rawReceipt.BlockHash),
BlockNumber: mustParseUint(rawReceipt.BlockNumber),
BlockNumber: utils.MustParseUint(rawReceipt.BlockNumber),
ContractAddress: base.HexToAddress(rawReceipt.ContractAddress),
CumulativeGasUsed: fmt.Sprint(cumulativeGasUsed),
GasUsed: mustParseUint(rawReceipt.GasUsed),
GasUsed: utils.MustParseUint(rawReceipt.GasUsed),
Logs: logs,
Status: uint32(mustParseUint(rawReceipt.Status)),
IsError: mustParseUint(rawReceipt.Status) == 0,
Status: uint32(utils.MustParseUint(rawReceipt.Status)),
IsError: utils.MustParseUint(rawReceipt.Status) == 0,
TransactionHash: base.HexToHash(rawReceipt.TransactionHash),
TransactionIndex: mustParseUint(rawReceipt.TransactionIndex),
TransactionIndex: utils.MustParseUint(rawReceipt.TransactionIndex),
}
receipt.SetRaw(rawReceipt)

Expand Down
18 changes: 9 additions & 9 deletions src/apps/chifra/pkg/rpcClient/get_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ func GetTracesByBlockNumber(chain string, bn uint64) ([]types.SimpleTrace, error
traceAction := types.SimpleTraceAction{
Address: base.HexToAddress(rawTrace.Action.Address),
Author: base.HexToAddress(rawTrace.Action.Author),
Balance: *big.NewInt(0).SetUint64(mustParseUint(rawTrace.Action.Balance)),
Balance: *big.NewInt(0).SetUint64(utils.MustParseUint(rawTrace.Action.Balance)),
CallType: rawTrace.Action.CallType,
From: base.HexToAddress(rawTrace.Action.From),
Gas: mustParseUint(rawTrace.Action.Gas),
Gas: utils.MustParseUint(rawTrace.Action.Gas),
Init: rawTrace.Action.Init,
Input: rawTrace.Action.Input,
RefundAddress: base.HexToAddress(rawTrace.Action.RefundAddress),
RewardType: rawTrace.Action.RewardType,
SelfDestructed: base.HexToAddress(rawTrace.Action.SelfDestructed),
To: base.HexToAddress(rawTrace.Action.To),
Value: *big.NewInt(0).SetUint64(mustParseUint(rawTrace.Action.Value)),
Value: *big.NewInt(0).SetUint64(utils.MustParseUint(rawTrace.Action.Value)),
}
traceResult := types.SimpleTraceResult{}
if rawTrace.Result != nil {
traceResult.Address = base.HexToAddress(rawTrace.Result.Address)
traceResult.Code = rawTrace.Result.Code
traceResult.GasUsed = mustParseUint(rawTrace.Result.GasUsed)
traceResult.GasUsed = utils.MustParseUint(rawTrace.Result.GasUsed)
traceResult.Output = rawTrace.Result.Output
}
trace := types.SimpleTrace{
Expand Down Expand Up @@ -144,7 +144,7 @@ func GetTracesByFilter(chain string, filter string) ([]types.SimpleTrace, error)
return ret, fmt.Errorf("trace filter %s returned an error: %w", filter, ethereum.NotFound)
} else {
curApp := types.SimpleAppearance{BlockNumber: uint32(^uint32(0))}
curTs := rpc.GetBlockTimestamp(chain, utils.PointerOf(mustParseUint(f.FromBlock)))
curTs := rpc.GetBlockTimestamp(chain, utils.PointerOf(utils.MustParseUint(f.FromBlock)))
var idx uint64

// TODO: This could be loadTrace in the same way loadBlocks works
Expand All @@ -160,7 +160,7 @@ func GetTracesByFilter(chain string, filter string) ([]types.SimpleTrace, error)
action := types.SimpleTraceAction{
CallType: rawTrace.Action.CallType,
From: base.HexToAddress(rawTrace.Action.From),
Gas: mustParseUint(rawTrace.Action.Gas),
Gas: utils.MustParseUint(rawTrace.Action.Gas),
Input: rawTrace.Action.Input,
To: base.HexToAddress(rawTrace.Action.To),
Value: *value,
Expand All @@ -175,7 +175,7 @@ func GetTracesByFilter(chain string, filter string) ([]types.SimpleTrace, error)
var result *types.SimpleTraceResult
if rawTrace.Result != nil {
result = &types.SimpleTraceResult{
GasUsed: mustParseUint(rawTrace.Result.GasUsed),
GasUsed: utils.MustParseUint(rawTrace.Result.GasUsed),
Output: rawTrace.Result.Output,
Code: rawTrace.Result.Code,
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func GetTracesByTransactionHash(chain string, txHash string, transaction *types.
action := types.SimpleTraceAction{
CallType: rawTrace.Action.CallType,
From: base.HexToAddress(rawTrace.Action.From),
Gas: mustParseUint(rawTrace.Action.Gas),
Gas: utils.MustParseUint(rawTrace.Action.Gas),
Input: rawTrace.Action.Input,
To: base.HexToAddress(rawTrace.Action.To),
Value: *value,
Expand All @@ -265,7 +265,7 @@ func GetTracesByTransactionHash(chain string, txHash string, transaction *types.
var result *types.SimpleTraceResult
if rawTrace.Result != nil {
result = &types.SimpleTraceResult{
GasUsed: mustParseUint(rawTrace.Result.GasUsed),
GasUsed: utils.MustParseUint(rawTrace.Result.GasUsed),
Output: rawTrace.Result.Output,
Code: rawTrace.Result.Code,
}
Expand Down
45 changes: 3 additions & 42 deletions src/apps/chifra/pkg/rpcClient/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GetAppearanceFromHash(chain string, hash string) (uint64, uint64, error) {
if rawTx, err := GetRawTransactionByHash(chain, base.HexToHash(hash)); err != nil {
return 0, 0, err
} else {
return mustParseUint(rawTx.BlockNumber), mustParseUint(rawTx.TransactionIndex), nil
return utils.MustParseUint(rawTx.BlockNumber), utils.MustParseUint(rawTx.TransactionIndex), nil
}
}

Expand Down Expand Up @@ -292,26 +292,7 @@ func GetTransactionByAppearance(chain string, appearance *types.RawAppearance, f
return
}

tx = &types.SimpleTransaction{
Hash: base.HexToHash(rawTx.Hash),
BlockHash: base.HexToHash(rawTx.BlockHash),
BlockNumber: bn,
TransactionIndex: txid,
Nonce: mustParseUint(rawTx.Nonce),
Timestamp: blockTs,
From: base.HexToAddress(rawTx.From),
To: base.HexToAddress(rawTx.To),
Gas: mustParseUint(rawTx.Gas),
GasPrice: mustParseUint(rawTx.GasPrice),
GasUsed: receipt.GasUsed,
Input: rawTx.Input,
IsError: receipt.IsError,
HasToken: IsTokenRelated(rawTx.Input),
Receipt: &receipt,
}
tx.Value.SetString(rawTx.Value, 0)
tx.SetGasCost(&receipt)
tx.SetRaw(rawTx)
tx = types.NewSimpleTransaction(rawTx, &receipt, blockTs, IsTokenRelated(rawTx.Input))

if rpcOptions.HasStore() && !rpcOptions.TransactionWriteDisabled {
rpcOptions.Store.Write(tx, writeOptions)
Expand Down Expand Up @@ -365,27 +346,7 @@ func GetTransactionByBlockAndId(chain string, bn base.Blknum, txid uint64, rpcOp
return
}

// TODO: this gets repeated
tx = &types.SimpleTransaction{
Hash: base.HexToHash(rawTx.Hash),
BlockHash: base.HexToHash(rawTx.BlockHash),
BlockNumber: bn,
TransactionIndex: txid,
Nonce: mustParseUint(rawTx.Nonce),
Timestamp: blockTs,
From: base.HexToAddress(rawTx.From),
To: base.HexToAddress(rawTx.To),
Gas: mustParseUint(rawTx.Gas),
GasPrice: mustParseUint(rawTx.GasPrice),
GasUsed: receipt.GasUsed,
Input: rawTx.Input,
IsError: receipt.IsError,
HasToken: IsTokenRelated(rawTx.Input),
Receipt: &receipt,
}
tx.Value.SetString(rawTx.Value, 0)
tx.SetGasCost(&receipt)
tx.SetRaw(rawTx)
tx = types.NewSimpleTransaction(rawTx, &receipt, blockTs, IsTokenRelated(rawTx.Input))

if rpcOptions.HasStore() && !rpcOptions.TransactionWriteDisabled {
rpcOptions.Store.Write(tx, writeOptions)
Expand Down
5 changes: 3 additions & 2 deletions src/apps/chifra/pkg/rpcClient/get_uncle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
)

// GetUncleCountByNumber returns the number of uncles in a block.
Expand Down Expand Up @@ -69,11 +70,11 @@ func GetUnclesByNumber(chain string, bn uint64) ([]types.SimpleBlock[types.Simpl
} else {
// TODO: expand other fields if we ever need them (probably not)
ret = append(ret, types.SimpleBlock[types.SimpleTransaction]{
BlockNumber: mustParseUint(rawUncle.BlockNumber),
BlockNumber: utils.MustParseUint(rawUncle.BlockNumber),
Hash: base.HexToHash(rawUncle.Hash),
Miner: base.HexToAddress(rawUncle.Miner),
ParentHash: base.HexToHash(rawUncle.ParentHash),
Timestamp: int64(mustParseUint(rawUncle.Timestamp)),
Timestamp: int64(utils.MustParseUint(rawUncle.Timestamp)),
// Transactions: rawUncle.Transactions,
// BaseFeePerGas: rawUncle.BaseFeePerGas,
// Difficulty: rawUncle.Difficulty,
Expand Down
74 changes: 74 additions & 0 deletions src/apps/chifra/pkg/types/types_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,80 @@ func (s *SimpleTransaction) ReadFrom(r io.Reader) (n int64, err error) {
// EXISTING_CODE
//

// NewRawTransactionFromMap is useful when we get a map of transaction properties, e.g.
// from a call to eth_getBlockByHash [0x..., true]
func NewRawTransactionFromMap(input map[string]any) (r *RawTransaction) {
r = &RawTransaction{}

// TODO: I wonder why we make copies here
r.BlockHash = fmt.Sprint(input["blockHash"])
r.BlockNumber = fmt.Sprint(input["blockNumber"])
// Missing in block query
if _, ok := input["chainId"]; ok {
r.ChainId = fmt.Sprint(input["chainId"])
}
r.From = fmt.Sprint(input["from"])
r.Gas = fmt.Sprint(input["gas"])
r.GasPrice = fmt.Sprint(input["gasPrice"])
r.Hash = fmt.Sprint(input["hash"])
r.Input = fmt.Sprint(input["input"])
r.MaxFeePerGas = fmt.Sprint(input["maxFeePerGas"])
r.MaxPriorityFeePerGas = fmt.Sprint(input["maxPriorityFeePerGas"])
r.Nonce = fmt.Sprint(input["nonce"])
r.To = fmt.Sprint(input["to"])
r.TransactionIndex = fmt.Sprint(input["transactionIndex"])
r.Value = fmt.Sprint(input["value"])

return
}

func (r *RawTransaction) TxIndex() uint64 {
return utils.MustParseUint(r.TransactionIndex)
}

func (r *RawTransaction) TxGasPrice() uint64 {
return utils.MustParseUint(r.GasPrice)
}

func (r *RawTransaction) TxHash() *base.Hash {
hash := base.HexToHash(r.Hash)
return &hash
}

// NewSimpleTransaction builds SimpleTransaction using data from raw and receipt. Receipt can be nil.
// Transaction timestamp and HasToken flag will be set to timestamp and hasToken.
func NewSimpleTransaction(raw *RawTransaction, receipt *SimpleReceipt, timestamp base.Timestamp, hasToken bool) (s *SimpleTransaction) {
s = &SimpleTransaction{}

// TODO: use RawTransaction methods
s.Hash = base.HexToHash(raw.Hash)
s.BlockHash = base.HexToHash(raw.BlockHash)
s.BlockNumber = utils.MustParseUint(raw.BlockNumber)
s.TransactionIndex = utils.MustParseUint(raw.TransactionIndex)
s.Nonce = utils.MustParseUint(raw.Nonce)
s.Timestamp = timestamp
s.From = base.HexToAddress(raw.From)
s.To = base.HexToAddress(raw.To)
s.Value.SetString(raw.Value, 0)
s.Gas = utils.MustParseUint(raw.Gas)
s.GasPrice = utils.MustParseUint(raw.GasPrice)
s.MaxFeePerGas = utils.MustParseUint(raw.MaxFeePerGas)
s.MaxPriorityFeePerGas = utils.MustParseUint(raw.MaxPriorityFeePerGas)
s.Input = raw.Input

s.HasToken = hasToken
if receipt != nil {
s.GasUsed = receipt.GasUsed
s.IsError = receipt.IsError
s.Receipt = receipt

s.SetGasCost(receipt)
}
s.SetRaw(raw)

return
}

func (s *SimpleTransaction) SetGasCost(receipt *SimpleReceipt) base.Gas {
if receipt == nil {
return 0
Expand Down