Skip to content

Commit

Permalink
Merge branch 'resequence' into banana
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Sep 12, 2024
2 parents c05bdfa + eac6c21 commit b98c098
Show file tree
Hide file tree
Showing 41 changed files with 1,074 additions and 607 deletions.
6 changes: 3 additions & 3 deletions cmd/rpcdaemon/commands/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
if transfer == nil {
transfer = new(hexutil.Big)
}
log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
log.Debug("Gas estimation capped by limited funds", "original", hi, "balance", balance,
"sent", transfer.ToInt(), "maxFeePerGas", feeCap, "fundable", allowance)
hi = allowance.Uint64()
}
}

// Recap the highest gas allowance with specified gascap.
if hi > api.GasCap {
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", api.GasCap)
log.Debug("Caller gas above allowance, capping", "requested", hi, "cap", api.GasCap)
hi = api.GasCap
}
gasCap = hi
Expand Down Expand Up @@ -340,7 +340,7 @@ func (api *APIImpl) GetProof(ctx context.Context, address libcommon.Address, sto
return nil, err
}

latestBlock, err := rpchelper.GetLatestBlockNumber(tx)
latestBlock, err := rpchelper.GetLatestFinishedBlockNumber(tx)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/rpcdaemon/commands/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1450,15 +1450,19 @@ func populateBatchDataSlimDetails(batches []*types.BatchDataSlim) (json.RawMessa
jBatches := make([]map[string]interface{}, 0, len(batches))
for _, b := range batches {
jBatch := map[string]interface{}{}
jBatch["number"] = b.Number
jBatch["number"] = hexutil.EncodeUint64(b.Number)
jBatch["empty"] = b.Empty
if !b.Empty {
jBatch["batchL2Data"] = b.BatchL2Data
}
jBatches = append(jBatches, jBatch)
}

return json.Marshal(jBatches)
data := map[string]interface{}{
"data": jBatches,
}

return json.Marshal(data)
}

// GetProof
Expand Down
83 changes: 56 additions & 27 deletions cmd/rpcdaemon/commands/zkevm_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ import (
"github.com/ledgerwatch/erigon/zk/hermez_db"
)

const (
//used if address not specified
defaultSenderAddress = "0x1111111111111111111111111111111111111111"

defaultV = "0x1c"
defaultR = "0xa54492cfacf71aef702421b7fbc70636537a7b2fbe5718c5ed970a001bb7756b"
defaultS = "0x2e9fb27acc75955b898f0b12ec52aa34bf08f01db654374484b80bf12f0d841e"
)

type zkevmRPCTransaction struct {
Gas hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice,omitempty"`
Expand All @@ -36,53 +45,75 @@ type zkevmRPCTransaction struct {
To *common.Address `json:"to"`
From *common.Address `json:"from"`
Value *hexutil.Big `json:"value"`
V *hexutil.Big `json:"v"`
R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"`
Data hexutility.Bytes `json:"data"`
}

// Tx return types.Transaction from rpcTransaction
func (tx *zkevmRPCTransaction) Tx() types.Transaction {
func (tx *zkevmRPCTransaction) Tx(sr state.StateReader) (types.Transaction, error) {
if tx == nil {
return nil
return nil, nil
}

sender := common.HexToAddress(defaultSenderAddress)
if tx.From != nil {
sender = *tx.From
}
nonce := uint64(0)

ad, err := sr.ReadAccountData(sender)
if err != nil {
return nil, err
}
if ad != nil {
nonce = ad.Nonce
}

if tx.Value == nil {
// set this to something non nil
tx.Value = &hexutil.Big{}
}

gasPrice := uint256.NewInt(1)
if tx.GasPrice != nil {
gasPrice = uint256.MustFromBig(tx.GasPrice.ToInt())
}

var data []byte
if tx.Data != nil {
data = tx.Data
} else if tx.Input != nil {
data = tx.Input
} else if tx.To == nil {
return nil, fmt.Errorf("contract creation without data provided")
}

var legacy *types.LegacyTx
if tx.To == nil {
legacy = types.NewContractCreation(
uint64(tx.Nonce),
nonce,
uint256.MustFromBig(tx.Value.ToInt()),
uint64(tx.Gas),
gasPrice,
tx.Input,
data,
)
} else {
legacy = types.NewTransaction(
uint64(tx.Nonce),
nonce,
*tx.To,
uint256.MustFromBig(tx.Value.ToInt()),
uint64(tx.Gas),
gasPrice,
tx.Input,
data,
)
}

if tx.From != nil {
legacy.SetSender(*tx.From)
}
legacy.SetSender(sender)

if tx.V != nil && tx.R != nil && tx.S != nil {
// parse signature raw values V, R, S from local hex strings
legacy.V = *uint256.MustFromBig(tx.V.ToInt())
legacy.R = *uint256.MustFromBig(tx.R.ToInt())
legacy.S = *uint256.MustFromBig(tx.S.ToInt())
}
legacy.V = *uint256.MustFromHex(defaultV)
legacy.R = *uint256.MustFromHex(defaultR)
legacy.S = *uint256.MustFromHex(defaultS)

return legacy
return legacy, nil
}

// EstimateGas implements eth_estimateGas. Returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain.
Expand All @@ -95,13 +126,6 @@ func (zkapi *ZkEvmAPIImpl) EstimateCounters(ctx context.Context, rpcTx *zkevmRPC
}
defer dbtx.Rollback()

if rpcTx.Value == nil {
// set this to something non nil
rpcTx.Value = &hexutil.Big{}
}

tx := rpcTx.Tx()

chainConfig, err := api.chainConfig(dbtx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -139,6 +163,11 @@ func (zkapi *ZkEvmAPIImpl) EstimateCounters(ctx context.Context, rpcTx *zkevmRPC

signer := types.MakeSigner(chainConfig, header.Number.Uint64())

tx, err := rpcTx.Tx(stateReader)
if err != nil {
return nil, err
}

msg, err := tx.AsMessage(*signer, header.BaseFee, rules)
if err != nil {
return nil, err
Expand Down Expand Up @@ -381,7 +410,7 @@ func (api *ZkEvmAPIImpl) TraceTransactionCounters(ctx context.Context, hash comm
if config == nil {
config = &tracers.TraceConfig_ZkEvm{}
}
config.CounterCollector = txCounters.ExecutionCounters()
config.CounterCollector = txCounters

// Trace the transaction and return
return transactions.TraceTx(ctx, txEnv.Msg, txEnv.BlockContext, txEnv.TxContext, txEnv.Ibs, config, chainConfig, stream, api.ethApi.evmCallTimeout)
Expand Down
11 changes: 9 additions & 2 deletions core/vm/contracts_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,17 @@ func (c *ripemd160hash_zkevm) Run(input []byte) ([]byte, error) {

// data copy implemented as a native contract.
type dataCopy_zkevm struct {
enabled bool
cc *CounterCollector
enabled bool
cc *CounterCollector
outLength int
}

func (c *dataCopy_zkevm) SetCounterCollector(cc *CounterCollector) {
c.cc = cc
}

func (c *dataCopy_zkevm) SetOutputLength(outLength int) {
c.outLength = outLength
}

// RequiredGas returns the gas required to execute the pre-compiled contract.
Expand All @@ -241,6 +243,11 @@ func (c *dataCopy_zkevm) Run(in []byte) ([]byte, error) {
if !c.enabled {
return []byte{}, ErrUnsupportedPrecompile
}

if c.cc != nil {
c.cc.preIdentity(len(in), c.outLength)
}

return in, nil
}

Expand Down
17 changes: 3 additions & 14 deletions core/vm/zk_batch_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ type BatchCounterCollector struct {
rlpCombinedCounters Counters
executionCombinedCounters Counters
processingCombinedCounters Counters

rlpCombinedCountersCache Counters
executionCombinedCountersCache Counters
processingCombinedCountersCache Counters
}

func NewBatchCounterCollector(smtMaxLevel int, forkId uint16, mcpReduction float64, unlimitedCounters bool, addonCounters *Counters) *BatchCounterCollector {
Expand Down Expand Up @@ -221,7 +217,7 @@ func (bcc *BatchCounterCollector) CombineCollectors(verifyMerkleProof bool) (Cou
}
}

for k, _ := range combined {
for k := range combined {
val := bcc.rlpCombinedCounters[k].used + bcc.executionCombinedCounters[k].used + bcc.processingCombinedCounters[k].used
combined[k].used += val
combined[k].remaining -= val
Expand Down Expand Up @@ -254,15 +250,8 @@ func (bcc *BatchCounterCollector) CombineCollectorsNoChanges() Counters {
}

for _, tx := range bcc.transactions {
for k, v := range tx.rlpCounters.counters {
combined[k].used += v.used
combined[k].remaining -= v.used
}
for k, v := range tx.executionCounters.counters {
combined[k].used += v.used
combined[k].remaining -= v.used
}
for k, v := range tx.processingCounters.counters {
txCounters := tx.CombineCounters()
for k, v := range txCounters {
combined[k].used += v.used
combined[k].remaining -= v.used
}
Expand Down
Loading

0 comments on commit b98c098

Please sign in to comment.