Skip to content
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
16 changes: 15 additions & 1 deletion l2geth/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/mantlenetworkio/mantle/l2geth/log"
"github.com/mantlenetworkio/mantle/l2geth/params"
"github.com/mantlenetworkio/mantle/l2geth/rollup/fees"
"github.com/mantlenetworkio/mantle/l2geth/rollup/rcfg"
)

// StateProcessor is a basic Processor, which takes care of transitioning
Expand Down Expand Up @@ -127,10 +128,23 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
}
*usedGas += gas

// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
// based on the eip phase, we're passing whether the root touch-delete accounts.
receipt := types.NewReceipt(root, failed, *usedGas)
daSwitch := statedb.GetState(rcfg.L2GasPriceOracleAddress, rcfg.DaSwitchSlot).Big()
if err != nil {
return nil, err
}
if daSwitch.Cmp(common.Big1) == 0 {
daFee, daGasPrice, daGasUsed, _, err := fees.DeriveDAGasInfo(msg, statedb)
if err != nil {
return nil, err
}
receipt.DAGasPrice = daGasPrice
receipt.DAFee = daFee
receipt.DAGasUsed = daGasUsed

}
receipt.L1GasPrice = l1GasPrice
receipt.L1GasUsed = l1GasUsed
receipt.L1Fee = l1Fee
Expand Down
7 changes: 7 additions & 0 deletions l2geth/core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type StateTransition struct {
evm *vm.EVM
// UsingBVM
l1Fee *big.Int
daFee *big.Int
}

// Message represents a message sent to a contract.
Expand Down Expand Up @@ -131,6 +132,7 @@ func IntrinsicGas(data []byte, contractCreation, isHomestead bool, isEIP2028 boo
// NewStateTransition initialises and returns a new state transition object.
func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition {
l1Fee := new(big.Int)
daFee := new(big.Int)
gasPrice := msg.GasPrice()
if rcfg.UsingBVM {
if msg.GasPrice().Cmp(common.Big0) != 0 {
Expand All @@ -143,6 +145,10 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
} else if charge.Cmp(common.Big1) == 1 {
panic(fmt.Sprintf("charge:%v is invaild", charge))
}
daCharge := evm.StateDB.GetState(rcfg.L2GasPriceOracleAddress, rcfg.DaSwitchSlot).Big()
if daCharge.Cmp(common.Big1) == 0 {
daFee, _ = fees.CalculateDAMsgFee(msg, evm.StateDB, nil)
}
}
}

Expand All @@ -155,6 +161,7 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
data: msg.Data(),
state: evm.StateDB,
l1Fee: l1Fee,
daFee: daFee,
}
}

Expand Down
3 changes: 3 additions & 0 deletions l2geth/core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type Receipt struct {
L1GasUsed *big.Int `json:"l1GasUsed" gencodec:"required"`
L1Fee *big.Int `json:"l1Fee" gencodec:"required"`
FeeScalar *big.Float `json:"l1FeeScalar" gencodec:"required"`
DAGasPrice *big.Int `json:"daGasPrice" `
DAGasUsed *big.Int `json:"daGasUsed" `
DAFee *big.Int `json:"daFee"`
}

type receiptMarshaling struct {
Expand Down
19 changes: 19 additions & 0 deletions l2geth/eth/gasprice/rollup_gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
type RollupOracle struct {
l1GasPrice *big.Int
l2GasPrice *big.Int
daGasPrice *big.Int
overhead *big.Int
scalar *big.Float
isBurning *big.Int
charge *big.Int
sccAddress common.Address
l1GasPriceLock sync.RWMutex
l2GasPriceLock sync.RWMutex
daGasPriceLock sync.RWMutex
overheadLock sync.RWMutex
scalarLock sync.RWMutex
isBurningLock sync.RWMutex
Expand Down Expand Up @@ -74,6 +76,23 @@ func (gpo *RollupOracle) SetL2GasPrice(gasPrice *big.Int) error {
return nil
}

// SuggestDAGasPrice returns the gas price which should be charged per byte of published
// data by the sequencer.
func (gpo *RollupOracle) SuggestDAGasPrice(ctx context.Context) (*big.Int, error) {
gpo.daGasPriceLock.RLock()
defer gpo.daGasPriceLock.RUnlock()
return gpo.daGasPrice, nil
}

// SetDAGasPrice returns the current DA gas price
func (gpo *RollupOracle) SetDAGasPrice(daGasPrice *big.Int) error {
gpo.daGasPriceLock.Lock()
defer gpo.daGasPriceLock.Unlock()
gpo.daGasPrice = daGasPrice
log.Info("Set DA Gas Price", "daGasprice", gpo.daGasPrice)
return nil
}

// SuggestOverhead returns the cached overhead value from the
// BVM_GasPriceOracle
func (gpo *RollupOracle) SuggestOverhead(ctx context.Context) (*big.Int, error) {
Expand Down
62 changes: 62 additions & 0 deletions l2geth/rollup/client_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package rollup

import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"math/big"
"testing"
"time"

"github.com/influxdata/influxdb/pkg/testing/assert"
"github.com/mantlenetworkio/mantle/l2geth/accounts/abi/bind"
"github.com/mantlenetworkio/mantle/l2geth/common"
"github.com/mantlenetworkio/mantle/l2geth/core/types"
"github.com/mantlenetworkio/mantle/l2geth/crypto"
"github.com/mantlenetworkio/mantle/l2geth/ethclient"

"github.com/jarcoal/httpmock"
)
Expand Down Expand Up @@ -73,3 +83,55 @@ func TestDecodedJSON(t *testing.T) {
t.Fatal("Cannot decode")
}
}

type ExtAcc struct {
Key *ecdsa.PrivateKey
Addr common.Address
}

func FromHexKey(hexkey string) (ExtAcc, error) {
key, err := crypto.HexToECDSA(hexkey)
if err != nil {
return ExtAcc{}, err
}
pubKey := key.Public()
pubKeyECDSA, ok := pubKey.(*ecdsa.PublicKey)
if !ok {
err = fmt.Errorf("publicKey is not of type *ecdsa.PublicKey")
return ExtAcc{}, err
}
addr := crypto.PubkeyToAddress(*pubKeyECDSA)
return ExtAcc{key, addr}, nil
}

func TestBatchTransactions(t *testing.T) {
account, _ := FromHexKey("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
txOpt := bind.NewKeyedTransactor(account.Key)

client, err := ethclient.Dial("http://localhost:8545")
assert.NoError(t, err)

receiveAccount := common.HexToAddress("0x76EFAac78C011D24BC975a5dAcC9a91245397e1a")
txOpt.Value = big.NewInt(5e18)
txOpt.GasLimit = uint64(21000)
txOpt.GasPrice = big.NewInt(1)

for i := 0; i < 1000; i++ {
nonce, err := client.PendingNonceAt(context.Background(), account.Addr)
assert.NoError(t, err)

txOpt.Value = big.NewInt(1).Mul(big.NewInt(1e18), big.NewInt(int64(i)))
rawTx := types.NewTransaction(nonce, receiveAccount, txOpt.Value, txOpt.GasLimit, txOpt.GasPrice, nil)

signedTx, err := txOpt.Signer(types.HomesteadSigner{}, txOpt.From, rawTx)
assert.NoError(t, err)

err = client.SendTransaction(context.Background(), signedTx)
assert.NoError(t, err)

t.Logf("index %d, txHash %s", i, signedTx.Hash().String())

time.Sleep(100 * time.Millisecond)
}
}

13 changes: 13 additions & 0 deletions l2geth/rollup/fees/bindings/gaspriceoracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestCalculateFee(t *testing.T) {

opts, _ := NewKeyedTransactor(key)
addr, _, gpo, err := DeployGasPriceOracle(opts, sim, opts.From)
// TODO da contract
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -88,19 +89,31 @@ func TestCalculateFee(t *testing.T) {

gasOracle.SetL1GasPrice(l1BaseFee)
gasOracle.SetL2GasPrice(l2GasPrice)
// TODO
//gasOracle.SetDAGasPrice(daGasPrice)
gasOracle.SetOverhead(overhead)
gasOracle.SetScalar(scalar, decimals)

l1Fee, err := gpo.GetL1Fee(&callopts, raw.Bytes())
if err != nil {
t.Fatal("cannot get l1 fee")
}
// TODO daFee
//daFee, err := gpo.GetDAFee(&callopts, raw.Bytes())
//if err != nil {
// t.Fatal("cannot get l1 fee")
//}

scaled := fees.ScaleDecimals(scalar, decimals)
expectL1Fee := fees.CalculateL1Fee(raw.Bytes(), overhead, l1BaseFee, scaled)
if expectL1Fee.Cmp(l1Fee) != 0 {
t.Fatal("solidity does not match go")
}
// TODO
//expectDAFee := fees.CalculateDAFee(raw.Bytes(), overhead, l1BaseFee, scaled)
//if expectL1Fee.Cmp(l1Fee) != 0 {
// t.Fatal("solidity does not match go")
//}

state, err := chain.State()
if err != nil {
Expand Down
Loading