Skip to content

Commit

Permalink
Merge pull request #4 from KyberNetwork/feat/simulate-bundle
Browse files Browse the repository at this point in the history
feat(TRD-148): add simulation bundle endpoint
  • Loading branch information
tiennampham23 authored Feb 22, 2024
2 parents f5ca106 + 5aa310c commit 7640c10
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 21 deletions.
56 changes: 56 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,59 @@ func ProcessBeaconBlockRoot(beaconRoot common.Hash, vmenv *vm.EVM, statedb *stat
_, _, _ = vmenv.Call(vm.AccountRef(msg.From), *msg.To, msg.Data, 30_000_000, common.U2560)
statedb.Finalise(true)
}

func applyTransactionWithResult(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, *ExecutionResult, error) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb)

// Apply the transaction to the current state (included in the env).
result, err := ApplyMessage(evm, msg, gp)
if err != nil {
return nil, nil, err
}

// Update the state with pending changes.
var root []byte
if config.IsByzantium(blockNumber) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
}
*usedGas += result.UsedGas

// Create a new receipt for the transaction, storing the intermediate root and gas used
// by the tx.
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: *usedGas}
if result.Failed() {
receipt.Status = types.ReceiptStatusFailed
} else {
receipt.Status = types.ReceiptStatusSuccessful
}
receipt.TxHash = tx.Hash()
receipt.GasUsed = result.UsedGas

// If the transaction created a contract, store the creation address in the receipt.
if msg.To == nil {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
}

// Set the receipt logs and create the bloom filter.
receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = blockHash
receipt.BlockNumber = blockNumber
receipt.TransactionIndex = uint(statedb.TxIndex())
return receipt, result, err
}

func ApplyTransactionWithResult(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *ExecutionResult, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number, header.Time), header.BaseFee)
if err != nil {
return nil, nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
return applyTransactionWithResult(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
}
254 changes: 233 additions & 21 deletions eth/api_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package eth

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"math"
"math/big"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/crypto/sha3"

"github.com/ethereum/go-ethereum/core/txpool"

"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -27,6 +33,19 @@ type TraceInternalTransactionArgs struct {
Tx hexutil.Bytes `json:"tx"`
}

// CallBundleArgs represents the arguments for a call.
type CallBundleArgs struct {
Txs []hexutil.Bytes `json:"txs"`
BlockNumber rpc.BlockNumber `json:"blockNumber"`
StateBlockNumberOrHash rpc.BlockNumberOrHash `json:"stateBlockNumber"`
Coinbase *string `json:"coinbase"`
Timestamp *uint64 `json:"timestamp"`
Timeout *int64 `json:"timeout"`
GasLimit *uint64 `json:"gasLimit"`
Difficulty *big.Int `json:"difficulty"`
BaseFee *big.Int `json:"baseFee"`
}

type Backend interface {
BlockChain() *core.BlockChain
TxPool() *txpool.TxPool
Expand Down Expand Up @@ -67,6 +86,22 @@ func NewSimulationAPI(eth Backend) *SimulationAPIBackend {
chainHeadCh: make(chan core.ChainHeadEvent),
exitCh: make(chan struct{}),
}

// fetch the current block
header := simulationAPIBackend.eth.BlockChain().CurrentBlock()
if header == nil {
panic("Failed to get the header")
}
block := simulationAPIBackend.eth.BlockChain().GetBlockByHash(header.Hash())
if block == nil {
panic("Failed to get the block by hash")
}

err := simulationAPIBackend.setBlock(block)
if err != nil {
panic(fmt.Errorf("can't init block, err: %w", err))
}

simulationAPIBackend.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(simulationAPIBackend.chainHeadCh)
simulationAPIBackend.wg.Add(1)
go func() {
Expand Down Expand Up @@ -121,29 +156,12 @@ func (b *SimulationAPIBackend) loop() error {
for {
select {
case head := <-b.chainHeadCh:
currentBlock := head.Block
log.Info("Receive new head", "block", currentBlock.NumberU64())

blockTime := int64(currentBlock.Time())
if !b.isLatestBlock(blockTime) {
b.isCatchUpLatestBlock.Store(false)
log.Warn("The state of the block isn't up-to-date", "block", currentBlock.NumberU64(), "time", currentBlock.Time())
continue
}

signer := types.MakeSigner(b.eth.BlockChain().Config(), currentBlock.Number(), currentBlock.Time())
blockCtx := core.NewEVMBlockContext(currentBlock.Header(), b.eth.BlockChain(), nil)

stateDb, err := b.eth.BlockChain().StateAt(currentBlock.Root())
err := b.setBlock(head.Block)
if err != nil {
log.Error("Failed to get the read-only state of the blockchain", "hash", currentBlock.Hash().String(), "error", err)
log.Error("Failed to set block", "err", err)
return err
}
b.stateDb = stateDb
b.currentBlock = currentBlock
b.currentBlockCtx = blockCtx
b.currentSigner = signer
b.isCatchUpLatestBlock.Store(true)

case err := <-b.chainHeadSub.Err():
return err
case <-b.exitCh:
Expand All @@ -152,6 +170,32 @@ func (b *SimulationAPIBackend) loop() error {
}
}

func (b *SimulationAPIBackend) setBlock(currentBlock *types.Block) error {
log.Info("Receive new head", "block", currentBlock.NumberU64())

blockTime := int64(currentBlock.Time())
if !b.isLatestBlock(blockTime) {
b.isCatchUpLatestBlock.Store(false)
log.Warn("The state of the block isn't up-to-date", "block", currentBlock.NumberU64(), "time", currentBlock.Time())
return nil
}

signer := types.MakeSigner(b.eth.BlockChain().Config(), currentBlock.Number(), currentBlock.Time())
blockCtx := core.NewEVMBlockContext(currentBlock.Header(), b.eth.BlockChain(), nil)

stateDb, err := b.eth.BlockChain().StateAt(currentBlock.Root())
if err != nil {
log.Error("Failed to get the read-only state of the blockchain", "hash", currentBlock.Hash().String(), "error", err)
return err
}
b.stateDb = stateDb
b.currentBlock = currentBlock
b.currentBlockCtx = blockCtx
b.currentSigner = signer
b.isCatchUpLatestBlock.Store(true)
return nil
}

// simulate the single transaction into *types.SimulationTxResponse
// use stateDb as a param, stateDb isn't safe for concurrently
// need to make the copy version of stateDb and pass as the param
Expand Down Expand Up @@ -241,3 +285,171 @@ func (b *SimulationAPIBackend) isLatestBlock(blockTime int64) bool {
}
return false
}

// CallBundle will simulate a bundle of transactions at the top of a given block
// number with the state of another (or the same) block. This can be used to
// simulate future blocks with the current state, or it can be used to simulate
// a past block.
// The sender is responsible for signing the transactions and using the correct
// nonce and ensuring validity
func (b *SimulationAPIBackend) CallBundle(ctx context.Context, args CallBundleArgs) (map[string]interface{}, error) {
if len(args.Txs) == 0 {
return nil, errors.New("bundle missing txs")
}
if args.BlockNumber == 0 {
return nil, errors.New("bundle missing blockNumber")
}

var txs types.Transactions

for _, encodedTx := range args.Txs {
tx := new(types.Transaction)
if err := tx.UnmarshalBinary(encodedTx); err != nil {
return nil, err
}
txs = append(txs, tx)
}
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())

timeoutMilliSeconds := int64(5000)
if args.Timeout != nil {
timeoutMilliSeconds = *args.Timeout
}
timeout := time.Millisecond * time.Duration(timeoutMilliSeconds)

// Setup context so it may be cancelled the call has completed
// or, in case of unmetered gas, setup a context with a timeout.
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
} else {
ctx, cancel = context.WithCancel(ctx)
}
// Make sure the context is cancelled when the call has completed
// this makes sure resources are cleaned up.
defer cancel()

var (
stateDB = b.stateDb.Copy()
currentBlock = b.currentBlock
parent = b.currentBlock.Header()
chainConfig = b.eth.BlockChain().Config()
)

blockNumber := big.NewInt(int64(args.BlockNumber))

timestamp := parent.Time + 1
if args.Timestamp != nil {
timestamp = *args.Timestamp
}
coinbase := parent.Coinbase
if args.Coinbase != nil {
coinbase = common.HexToAddress(*args.Coinbase)
}
difficulty := parent.Difficulty
if args.Difficulty != nil {
difficulty = args.Difficulty
}
gasLimit := parent.GasLimit
if args.GasLimit != nil {
gasLimit = *args.GasLimit
}
var baseFee *big.Int
if args.BaseFee != nil {
baseFee = args.BaseFee
} else if chainConfig.IsLondon(big.NewInt(args.BlockNumber.Int64())) {
baseFee = eip1559.CalcBaseFee(chainConfig, parent)
}
header := &types.Header{
ParentHash: parent.Hash(),
Number: blockNumber,
GasLimit: gasLimit,
Time: timestamp,
Difficulty: difficulty,
Coinbase: coinbase,
BaseFee: baseFee,
}

vmconfig := vm.Config{}

// Setup the gas pool (also for unmetered requests)
// and apply the message.
gp := new(core.GasPool).AddGas(math.MaxUint64)

var results []map[string]interface{}
coinbaseBalanceBefore := stateDB.GetBalance(coinbase)

bundleHash := sha3.NewLegacyKeccak256()
signer := types.MakeSigner(b.eth.BlockChain().Config(), currentBlock.Number(), currentBlock.Time())
var totalGasUsed uint64
gasFees := new(big.Int)
for i, tx := range txs {
// Check if the context was cancelled (eg. timed-out)
if err := ctx.Err(); err != nil {
return nil, err
}

coinbaseBalanceBeforeTx := stateDB.GetBalance(coinbase)
stateDB.SetTxContext(tx.Hash(), i)

receipt, result, err := core.ApplyTransactionWithResult(chainConfig, b.eth.BlockChain(), &coinbase, gp, stateDB, header, tx, &header.GasUsed, vmconfig)
if err != nil {
return nil, fmt.Errorf("err: %w; txhash %s", err, tx.Hash())
}

txHash := tx.Hash().String()
from, err := types.Sender(signer, tx)
if err != nil {
return nil, fmt.Errorf("err: %w; txhash %s", err, tx.Hash())
}
to := "0x"
if tx.To() != nil {
to = tx.To().String()
}
jsonResult := map[string]interface{}{
"txHash": txHash,
"gasUsed": receipt.GasUsed,
"fromAddress": from.String(),
"toAddress": to,
}
totalGasUsed += receipt.GasUsed
gasPrice, err := tx.EffectiveGasTip(header.BaseFee)
if err != nil {
return nil, fmt.Errorf("err: %w; txhash %s", err, tx.Hash())
}
gasFeesTx := new(big.Int).Mul(big.NewInt(int64(receipt.GasUsed)), gasPrice)
gasFees.Add(gasFees, gasFeesTx)
bundleHash.Write(tx.Hash().Bytes())
if result.Err != nil {
jsonResult["error"] = result.Err.Error()
revert := result.Revert()
if len(revert) > 0 {
jsonResult["revert"] = string(revert)
}
} else {
dst := make([]byte, hex.EncodedLen(len(result.Return())))
hex.Encode(dst, result.Return())
jsonResult["value"] = "0x" + string(dst)
}
coinbaseDiffTx := new(big.Int).Sub(stateDB.GetBalance(coinbase).ToBig(), coinbaseBalanceBeforeTx.ToBig())
jsonResult["coinbaseDiff"] = coinbaseDiffTx.String()
jsonResult["gasFees"] = gasFeesTx.String()
jsonResult["ethSentToCoinbase"] = new(big.Int).Sub(coinbaseDiffTx, gasFeesTx).String()
jsonResult["gasPrice"] = new(big.Int).Div(coinbaseDiffTx, big.NewInt(int64(receipt.GasUsed))).String()
jsonResult["gasUsed"] = receipt.GasUsed
results = append(results, jsonResult)
}

ret := map[string]interface{}{}
ret["results"] = results
coinbaseDiff := new(big.Int).Sub(stateDB.GetBalance(coinbase).ToBig(), coinbaseBalanceBefore.ToBig())
ret["coinbaseDiff"] = coinbaseDiff.String()
ret["gasFees"] = gasFees.String()
ret["ethSentToCoinbase"] = new(big.Int).Sub(coinbaseDiff, gasFees).String()
ret["bundleGasPrice"] = new(big.Int).Div(coinbaseDiff, big.NewInt(int64(totalGasUsed))).String()
ret["totalGasUsed"] = totalGasUsed
ret["stateBlockNumber"] = parent.Number.Int64()

ret["bundleHash"] = "0x" + common.Bytes2Hex(bundleHash.Sum(nil))
return ret, nil
}

0 comments on commit 7640c10

Please sign in to comment.