Skip to content

Commit

Permalink
fix: Merge errors with v1.13.10.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-smith committed Jan 16, 2024
1 parent 65275db commit e1d6f4e
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 541 deletions.
27 changes: 0 additions & 27 deletions eth/catalyst/simulated_beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package catalyst
import (
"crypto/rand"
"errors"
"github.com/ethereum/go-ethereum/core"
"math/big"
"sync"
"time"
Expand Down Expand Up @@ -200,32 +199,6 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u
return nil
}

// loopOnDemand runs the block production loop for "on-demand" configuration (period = 0)
func (c *SimulatedBeacon) loopOnDemand() {
var (
newTxs = make(chan core.NewTxsEvent)
sub = c.eth.TxPool().SubscribeTransactions(newTxs, true)
)
defer sub.Unsubscribe()

for {
select {
case <-c.shutdownCh:
return
case w := <-c.withdrawals.pending:
withdrawals := append(c.withdrawals.gatherPending(9), w)
if err := c.sealBlock(withdrawals); err != nil {
log.Warn("Error performing sealing work", "err", err)
}
case <-newTxs:
withdrawals := c.withdrawals.gatherPending(10)
if err := c.sealBlock(withdrawals); err != nil {
log.Warn("Error performing sealing work", "err", err)
}
}
}
}

// loop runs the block production loop for non-zero period configuration
func (c *SimulatedBeacon) loop() {
timer := time.NewTimer(0)
Expand Down
31 changes: 15 additions & 16 deletions eth/tracers/blocknative/blocknative.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers/blocknative/decoder"
)
Expand Down Expand Up @@ -41,24 +40,24 @@ type Trace struct {

// BlockContext contains information about the block we simulate transactions in.
type BlockContext struct {
Number uint64 `json:"number"`
BaseFee uint64 `json:"baseFee"`
Time uint64 `json:"time"`
GasLimit uint64 `json:"gasLimit"`
Coinbase common.Address `json:"coinbase"`
StateRoot hexutil.Bytes `json:"stateRoot,omitempty"`
Random common.Hash `json:"random,omitempty"`
Number uint64 `json:"number"`
StateRoot string `json:"stateRoot,omitempty"`
BaseFee uint64 `json:"baseFee"`
Time uint64 `json:"time"`
Coinbase string `json:"coinbase"`
GasLimit uint64 `json:"gasLimit"`
Random string `json:"random,omitempty"`
}

type CallFrame struct {
Type string `json:"type"`
From common.Address `json:"from"`
To common.Address `json:"to,omitempty"`
Value Big `json:"value,omitempty"`
Gas Uint64 `json:"gas"`
GasUsed Uint64 `json:"gasUsed"`
Input hexutil.Bytes `json:"input"`
Output hexutil.Bytes `json:"output,omitempty"`
From string `json:"from"`
To string `json:"to,omitempty"`
Value string `json:"value,omitempty"`
Gas string `json:"gas"`
GasUsed string `json:"gasUsed"`
Input string `json:"input"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
ErrorReason string `json:"errorReason,omitempty"`
Calls []CallFrame `json:"calls,omitempty"`
Expand All @@ -71,7 +70,7 @@ type CallLog struct {
Address common.Address `json:"address"`

// Data is the encoded memory provided with the log.
Data hexutil.Bytes `json:"data"`
Data string `json:"data"`

// Topics is a slice of up to 4 32byte words provided with the log.
Topics []common.Hash `json:"topics"`
Expand Down
27 changes: 0 additions & 27 deletions eth/tracers/blocknative/cache/trace_cache.go

This file was deleted.

86 changes: 81 additions & 5 deletions eth/tracers/blocknative/decoder/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func decodeERC20Metadata(evmCall evmCallFn, addr common.Address) AssetMetadata {
var err error
metadata := AssetMetadata{Type: AssetTypeERC20}

if metadata.Name, err = evmCallMethodName(evmCall, addr); err != nil {
if metadata.Name, err = decodeMetadataName(evmCall, addr); err != nil {
log.Trace("failed to decode ERC20 name", "err", err)
}
if metadata.Symbol, err = evmCallMethodSymbol(evmCall, addr); err != nil {
if metadata.Symbol, err = decodeMetadataSymbol(evmCall, addr); err != nil {
log.Trace("failed to decode ERC20 symbol", "err", err)
}
if metadata.Decimals, err = evmCallMethodDecimals(evmCall, addr); err != nil {
if metadata.Decimals, err = decodeMetadataDecimals(evmCall, addr); err != nil {
log.Trace("failed to decode ERC20 decimals", "err", err)
}

Expand All @@ -64,10 +64,10 @@ func decodeERC721Metadata(evmCall evmCallFn, addr common.Address, tokenID *big.I
var err error
metadata := AssetMetadata{Type: AssetTypeERC721}

if metadata.Name, err = evmCallMethodName(evmCall, addr); err != nil {
if metadata.Name, err = decodeMetadataName(evmCall, addr); err != nil {
log.Trace("failed to decode ERC721 name", "err", err)
}
if metadata.Symbol, err = evmCallMethodSymbol(evmCall, addr); err != nil {
if metadata.Symbol, err = decodeMetadataSymbol(evmCall, addr); err != nil {
log.Trace("failed to decode ERC721 symbol", "err", err)
}

Expand All @@ -94,6 +94,82 @@ func decodeERC1155Metadata(evmCall evmCallFn, addr common.Address, tokenID *big.
return metadata
}

// decodeMetadataName decodes the name of an asset from the EVM.
func decodeMetadataName(evmCall evmCallFn, addr common.Address) (string, error) {
return callAndDecodeString(evmCall, addr, methodIDName)
}

// decodeMetadataSymbol decodes the symbol of an asset from the EVM.
func decodeMetadataSymbol(evmCall evmCallFn, addr common.Address) (string, error) {
return callAndDecodeString(evmCall, addr, methodIDSymbol)
}

// decodeMetadataDecimals decodes the decimals of an asset from the EVM.
func decodeMetadataDecimals(evmCall evmCallFn, addr common.Address) (uint8, error) {
return callAndDecodeUint8(evmCall, addr, methodIDDecimals)
}

// decodeMetadataTokenURI decodes the tokenURI of an asset from the EVM.
func decodeMetadataTokenURI(evmCall evmCallFn, addr common.Address, tokenID *big.Int) (string, error) {
tokenIDBytes := tokenID.Bytes()
if len(tokenIDBytes) > 32 {
return "", fmt.Errorf("tokenID is too large")
}
common.LeftPadBytes(tokenIDBytes, 32)
input := append(methodIDTokenURI, tokenIDBytes...)
return callAndDecodeString(evmCall, addr, input)
}

// decodeMetadataURI decodes the URI of an asset from the EVM.
func decodeMetadataURI(evmCall evmCallFn, addr common.Address, tokenID *big.Int) (string, error) {
tokenIDBytes := tokenID.Bytes()
if len(tokenIDBytes) > 32 {
return "", fmt.Errorf("tokenID is too large")
}
common.LeftPadBytes(tokenIDBytes, 32)
input := append(methodIDURI, tokenIDBytes...)
return callAndDecodeString(evmCall, addr, input)
}

// callAndDecodeString calls a method and decodes the result as a string.
func callAndDecodeString(evmCall evmCallFn, addr common.Address, method []byte) (string, error) {
// Load bytes from the EVM.
stringBytes, err := evmCall(addr, method)
if err != nil {
return "", err
}

// Parse into a string.
stringInterface, err := abiArgs.singleString.Unpack(stringBytes)
if err != nil {
return "", err
}
if len(stringInterface) < len(abiArgs.singleString) {
return "", fmt.Errorf("unexpected decoded size")
}
str, ok := stringInterface[0].(string)
if !ok {
return "", fmt.Errorf("unexpected type for decoded string")
}

return str, nil
}

// callAndDecodeUint8 calls a method and decodes the result as a uint8.
func callAndDecodeUint8(evmCall evmCallFn, addr common.Address, method []byte) (uint8, error) {
// Load bytes from the EVM.
uint8Bytes, err := evmCall(addr, method)
if err != nil {
return 0, err
}

// Parse into a uint8.
if len(uint8Bytes) < 1 {
return 0, fmt.Errorf("unexpected decoded size")
}
return uint8Bytes[len(uint8Bytes)-1], nil
}

// decodeArgsSafeBatchTransferFrom calls a method and decodes the result as a uint256[].
func decodeArgsSafeBatchTransferFrom(bytes []byte) ([]*Transfer, error) {
args, err := abiArgs.batchTransfer.UnpackValues(bytes)
Expand Down
12 changes: 8 additions & 4 deletions eth/tracers/blocknative/decoder/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package decoder

import (
"bytes"

"golang.org/x/exp/slices"
)

// decodeContract decodes the contract bytecode and determines all interfaces
func decodeContract(c *Contract, bytecode ByteCode) {
c.interfaces = bytecode.DecodeInterfaces()
c.Type = contractTypeForInterfaces(c.interfaces)
func decodeContract(bytecode ByteCode) (*Contract, error) {

interfaces := bytecode.DecodeInterfaces()

return &Contract{
interfaces: interfaces,
Type: contractTypeForInterfaces(interfaces),
}, nil
}

// IsERC20 returns true iff the contract interfaces contains ERC-20.
Expand Down
98 changes: 7 additions & 91 deletions eth/tracers/blocknative/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,95 +72,6 @@ func (d *Decoder) DecodeCallFrame(sender common.Address, receiver common.Address
return cf, nil
}

func (d *Decoder) DecodeCallFrameEnd(cf *CallFrame) error {
if cf == nil || cf.Contract == nil || cf.CallData == nil {
return nil
}

// Check updated balances for taxable transfers and look for active taxes.
// If we find them, add them as new transfers.
transfers := cf.CallData.Transfers
for _, transfer := range transfers {
// First check if we set a balanceBeforeTo. If we didn't then we can't
// utilize this inference.
if transfer.balanceBeforeTo == nil {
continue
}

// Get the balance after the transfer. At this point balanceOf worked
// once so we don't expect it to fail. If it does then we report it as
// an error so we can inspect later, and then we continue to the next
// transfer.
var (
err error
balanceAfterTo *big.Int
)
switch cf.Contract.Type {
case ContractTypeERC20:
if balanceAfterTo, err = evmCallMethodBalanceOf(d.evm.CallCode, cf.Contract.address, transfer.To); err != nil {
log.Error("failed to get balance after for receiver", "err", err)
continue
}
case ContractTypeERC1155:
if balanceAfterTo, err = evmCallMethodBalanceOf2(d.evm.CallCode, cf.Contract.address, transfer.To, transfer.TokenID); err != nil {
log.Error("failed to get balance after for receiver", "err", err)
continue
}
default:
continue
}

// If the increase in balance of the transfer recipient is less than we
// decoded it to be, then we know that the transfer was taxed.
//
// We don't know where the tax went but it will often go to the contract
// itself so we check the contract balance to see if that's the case.
decodedValue := transfer.Value.ToInt()
deltaTo := balanceAfterTo.Sub(balanceAfterTo, transfer.balanceBeforeTo)
if deltaTo.Cmp(decodedValue) < 0 {
// First, update the transfer value to the actual delta.
transfer.Value = NewAmount(deltaTo)

// Now let's see if the contract was the tax recipient.
balanceAfterContract, err := evmCallMethodBalanceOf(d.evm.CallCode, cf.Contract.address, cf.Contract.address)
if err != nil {
log.Error("failed to get balance after for contract", "err", err)
continue
}
deltaContract := balanceAfterContract.Sub(balanceAfterContract, transfer.balanceBeforeContract)
if deltaContract.Sign() == 1 {
// The contract balance increased so we know at least some of
// the tax went there. Add a tax transfer.
cf.Transfers = append(cf.Transfers, &Transfer{
Asset: transfer.Asset,
From: transfer.From,

To: cf.Contract.address,
Value: NewAmount(deltaContract),
})
}

// Check to see if there was a tax amount not accounted for by
// subtracting the known deltas from the decoded value.
unaccountedTax := new(big.Int).Add(deltaTo, deltaContract)
unaccountedTax.Sub(decodedValue, unaccountedTax)
if unaccountedTax.Sign() == 1 {
cf.Transfers = append(cf.Transfers, &Transfer{
Asset: transfer.Asset,
From: transfer.From,

To: common.Address{},
Value: NewAmount(unaccountedTax),
})
}
}
}

d.balances.captureCallFrameEnd(cf)

return nil
}

// DecodeContract decodes the contract at the given address.
func (d *Decoder) DecodeContract(addr common.Address) (*Contract, error) {
// Check the cache for an existing entry.
Expand All @@ -182,8 +93,10 @@ func (d *Decoder) DecodeContract(addr common.Address) (*Contract, error) {
}

// We have an unknown contract; decode itm, add it to the cache, and return it.
contract = &Contract{address: addr}
decodeContract(contract, bytecode)
contract, err := decodeContract(bytecode)
if err != nil {
return nil, err
}
d.caches.contracts.Add(addr, contract)
return contract, nil
}
Expand Down Expand Up @@ -217,11 +130,14 @@ func (d *Decoder) decodeAsset(contract *Contract, assetID AssetID) (*AssetMetada
}

// Cache miss; decode and add to the cache.

asset, err := DecodeAsset(d.evm.CallCode, contract, assetID)
if err != nil {
return nil, err
}

d.caches.assets.Add(assetID, asset)

return asset, nil
}

Expand Down
Loading

0 comments on commit e1d6f4e

Please sign in to comment.