Skip to content

Commit

Permalink
finalize on TokenPermit replacement bytecode; auto call `initialize()…
Browse files Browse the repository at this point in the history
…` after bytecode-replaced
  • Loading branch information
simonzg committed Jan 30, 2024
1 parent 6599216 commit a5b1565
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 12 deletions.
11 changes: 8 additions & 3 deletions builtin/fork11_upgrades.go

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions meter/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,23 @@ var (
TeslaValidatorBenefitRatio = big.NewInt(1e18)

NewViewPeersLimit = 8

// in order to provide different addresses during testing and mainnet
// so defined all these vairables here
// will be different values during testing
USDC_eth_Address = MustParseAddress("0xd86e243fc0007e6226b07c9a50c9d70d78299eb5")
USDT_eth_Address = MustParseAddress("0x5fa41671c48e3c951afc30816947126ccc8c162e")
WBTC_eth_Address = MustParseAddress("0xc1f6c86abee8e2e0b6fd5bd80f0b51fef783635c")
)

func USDCAddress() Address {
return USDC_eth_Address
}

func USDTAddress() Address {
return USDT_eth_Address
}

func WBTCAddress() Address {
return WBTC_eth_Address
}
39 changes: 31 additions & 8 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (rt *Runtime) EnforceTeslaFork10_Corrections(stateDB *statedb.StateDB, bloc
}
}

func (rt *Runtime) EnforceTeslaFork11_Corrections(stateDB *statedb.StateDB, blockNum *big.Int, USDCAddr, USDTAddr, WBTCAddr meter.Address) {
func (rt *Runtime) EnforceTeslaFork11_Corrections(stateDB *statedb.StateDB, blockNum *big.Int, evm *vm.EVM) {
blockNumber := rt.Context().Number
log := log15.New("pkg", "fork11")
if blockNumber > 0 {
Expand All @@ -499,17 +499,40 @@ func (rt *Runtime) EnforceTeslaFork11_Corrections(stateDB *statedb.StateDB, bloc
if meter.IsMainNet() && meter.IsTeslaFork11(blockNumber) && (enforceFlag == nil || enforceFlag.Sign() == 0) {
log.Info("Start fork11 correction")

// initializeSelector, _ := hex.DecodeString("8129fc1c") // initialize

// update USDC.eth with ERC20MinterBurnerPauserPermit
rt.state.SetCode(USDCAddr, builtin.ERC20MinterBurnerPauserPermit_DeployedBytecode)
log.Info("Overriden USDC.eth with ERC20MinterBurnerPauserPermit bytecode", "addr", USDCAddr.String())
USDCAddress := meter.USDCAddress()
rt.state.SetCode(USDCAddress, builtin.USDCEthV2_DeployedBytecode)
log.Info("Overriden USDC.eth with ERC20MinterBurnerPauserPermit bytecode", "addr", USDCAddress)
// ret, leftOverGas, vmErr := evm.Call(vm.AccountRef(meter.ZeroAddress), common.Address(USDCAddress), initializeSelector, 350000, big.NewInt(0), 0)
// if vmErr != nil {
// log.Error("could not call initialize() on USDC.eth", "vmErr", vmErr, "ret", hex.EncodeToString(ret), "leftoverGas", leftOverGas)
// } else {
// log.Info("Called initialize() on USDC.eth")
// }

// update USDT.eth with ERC20MinterBurnerPauserPermit
rt.state.SetCode(USDTAddr, builtin.ERC20MinterBurnerPauserPermit_DeployedBytecode)
log.Info("Overriden USDT.eth with ERC20MinterBurnerPauserPermit bytecode", "addr", USDTAddr.String())
USDTAddress := meter.USDTAddress()
rt.state.SetCode(USDTAddress, builtin.USDTEthV2_DeployedBytecode)
log.Info("Overriden USDT.eth with ERC20MinterBurnerPauserPermit bytecode", "addr", USDTAddress)
// ret, leftOverGas, vmErr = evm.Call(vm.AccountRef(meter.ZeroAddress), common.Address(USDTAddress), initializeSelector, 350000, big.NewInt(0), 0)
// if vmErr != nil {
// log.Error("could not call initialize() on USDT.eth", "vmErr", vmErr, "ret", hex.EncodeToString(ret), "leftoverGas", leftOverGas)
// } else {
// log.Info("Called initialize() on USDT.eth")
// }

// update WBTC.eth with ERC20MinterBurnerPauserPermit
rt.state.SetCode(WBTCAddr, builtin.ERC20MinterBurnerPauserPermit_DeployedBytecode)
log.Info("Overriden WBTC.eth with ERC20MinterBurnerPauserPermit bytecode", "addr", WBTCAddr.String())
WBTCAddress := meter.WBTCAddress()
rt.state.SetCode(WBTCAddress, builtin.WBTCEthV2_DeployedBytecode)
log.Info("Overriden WBTC.eth with ERC20MinterBurnerPauserPermit bytecode", "addr", WBTCAddress)
// ret, leftOverGas, vmErr = evm.Call(vm.AccountRef(meter.ZeroAddress), common.Address(WBTCAddress), initializeSelector, 350000, big.NewInt(0), 0)
// if vmErr != nil {
// log.Error("could not call initialize() on WBTC.eth", "vmErr", vmErr, "ret", hex.EncodeToString(ret), "leftoverGas", leftOverGas)
// } else {
// log.Info("Called initialize() on WBTC.eth")
// }

// update baseSequenceAfterFork11
auctionCB := rt.state.GetAuctionCB()
Expand Down Expand Up @@ -919,7 +942,7 @@ func (rt *Runtime) PrepareClause(
rt.EnforceTeslaFork10_Corrections(stateDB, evm.BlockNumber)

// tesla fork11
rt.EnforceTeslaFork11_Corrections(stateDB, evm.BlockNumber, meter.MustParseAddress("0xd86e243fc0007e6226b07c9a50c9d70d78299eb5"), meter.MustParseAddress("0x5fa41671c48e3c951afc30816947126ccc8c162e"), meter.MustParseAddress("0xc1f6c86abee8e2e0b6fd5bd80f0b51fef783635c"))
rt.EnforceTeslaFork11_Corrections(stateDB, evm.BlockNumber, evm)

// check the restriction of transfer.
if rt.restrictTransfer(stateDB, txCtx.Origin, clause.Value(), clause.Token(), rt.ctx.Number) == true {
Expand Down
14 changes: 13 additions & 1 deletion tests/fork11/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,19 @@ func initRuntimeAfterFork11() *tests.TestEnv {

rt.EnforceTeslaFork8_LiquidStaking(sdb, big.NewInt(0))
rt.EnforceTeslaFork10_Corrections(sdb, big.NewInt(0))
rt.EnforceTeslaFork11_Corrections(sdb, big.NewInt(0), USDCAddr, USDTAddr, WBTCAddr)
meter.USDC_eth_Address = USDCAddr
meter.USDT_eth_Address = USDTAddr
meter.WBTC_eth_Address = WBTCAddr
transferAfterFork := tests.BuildTransferTx(c.Tag(), 1, tests.HolderAddr, big.NewInt(1), tests.VoterKey)
receipt, err := rt.ExecuteTransaction(transferAfterFork)
if err != nil {
panic(err)
}
if receipt.Reverted {
panic("could not execute after fork11")
}

// rt.EnforceTeslaFork11_Corrections(sdb, big.NewInt(0), evm, USDCAddr, USDTAddr, WBTCAddr)
return &tests.TestEnv{Runtime: rt, State: st, BktCreateTS: 0, CurrentTS: currentTs, ChainTag: c.Tag()}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/tx_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ func BuildVoteTx(chainTag byte, voterKey *ecdsa.PrivateKey, voterAddr meter.Addr
return BuildStakingTx(chainTag, 0, body, voterKey, 0)
}

func BuildTransferTx(chainTag byte, bestRef uint32, to meter.Address, amount *big.Int, signerKey *ecdsa.PrivateKey) *tx.Transaction {
builder := new(tx.Builder)
builder.ChainTag(chainTag).
BlockRef(tx.NewBlockRef(bestRef)).
Expiration(720).
GasPriceCoef(0).
Gas(meter.BaseTxGas * 2).
DependsOn(nil).
Nonce(uint64(rand.Intn(9999)))

builder.Clause(
tx.NewClause(&to).WithValue(amount).WithToken(meter.MTRG).WithData(make([]byte, 0)),
)
trx := builder.Build()
sig, _ := crypto.Sign(trx.SigningHash().Bytes(), signerKey)
trx = trx.WithSignature(sig)
return trx
}

func BuildContractCallTx(chainTag byte, bestRef uint32, to meter.Address, data []byte, signerKey *ecdsa.PrivateKey) *tx.Transaction {
builder := new(tx.Builder)
builder.ChainTag(chainTag).
Expand Down

0 comments on commit a5b1565

Please sign in to comment.