Skip to content

Commit

Permalink
Update mint flow (implement charging gas, update handling invalid ins…
Browse files Browse the repository at this point in the history
…tructions), rollback changes in fee collecting
  • Loading branch information
whitebit-robot committed Jun 23, 2023
2 parents 1516733 + 239a3a3 commit 6ca9efb
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 214 deletions.
49 changes: 27 additions & 22 deletions core/mint/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,45 @@ import (
"strings"
)

const (
BurnNetworkEthereum byte = iota
BurnNetworkTron
)

const EventName = "Mint"
const eventAbiString = `[{
"anonymous": false,
"inputs": [
{"indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256"},
{"indexed": false, "internalType": "bytes32", "name": "burnTxHash", "type": "bytes32"},
{"indexed": false, "internalType": "uint8", "name": "burnTxNetwork", "type": "uint8"}
],
"name": "Mint",
"type": "event"
}]`

var EventAbi, _ = abi.JSON(strings.NewReader(eventAbiString))

type storageLayout struct {
Owner common.Hash
MintLimit common.Hash
}

type contract struct {
// Contract is an object representing predefined mint contract params.
// Specified bytecode is a result of MintState contract compilation (see core/mint/contract/MintState.sol).
// Standard JSON input that is used for compilation is located near contract and can be used to reproduce the bytecode.
// BytecodeHash is a result of crypto.Keccak256Hash(bytecode).
var Contract = struct {
Address common.Address
Bytecode []byte
BytecodeHash common.Hash
StorageLayout storageLayout
}

// Contract is an object representing predefined mint contract params.
var Contract = contract{
}{
Address: common.HexToAddress("0x0000000000000000000000000000000000001000"),
Bytecode: common.Hex2Bytes("608060405234801561001057600080fd5b506004361061004c5760003560e01c806334259b5c146100515780638da5cb5b14610066578063996517cf14610096578063f2fde38b146100ad575b600080fd5b61006461005f366004610247565b6100c0565b005b600054610079906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b61009f60015481565b60405190815260200161008d565b6100646100bb366004610260565b610170565b6000546001600160a01b0316331461011a5760405162461bcd60e51b815260206004820152601860248201527720bb30b4b630b136329037b7363c903337b91037bbb732b960411b60448201526064015b60405180910390fd5b600154811061016b5760405162461bcd60e51b815260206004820152601e60248201527f4d696e74206c696d69742063616e6e6f7420626520696e6372656173656400006044820152606401610111565b600155565b6000546001600160a01b031633146101c55760405162461bcd60e51b815260206004820152601860248201527720bb30b4b630b136329037b7363c903337b91037bbb732b960411b6044820152606401610111565b6001600160a01b0381166102255760405162461bcd60e51b815260206004820152602160248201527f5a65726f2061646472657373206973206e6f7420612076616c6964206f776e656044820152603960f91b6064820152608401610111565b600080546001600160a01b0319166001600160a01b0392909216919091179055565b60006020828403121561025957600080fd5b5035919050565b60006020828403121561027257600080fd5b81356001600160a01b038116811461028957600080fd5b939250505056fea2646970667358221220e47c633a678b6c6fa00b437fc21a7ce743a31ba848d0163e41cee1256bdccb7d64736f6c63430008130033"),
BytecodeHash: common.HexToHash("0x094c2e08801f704f590fc847deec6076c880cdc6062f87c32614a4ff213fdf9c"), // result of crypto.Keccak256Hash(bytecode)
Bytecode: common.Hex2Bytes("608060405234801561001057600080fd5b506004361061004c5760003560e01c806334259b5c146100515780638da5cb5b1461006d578063996517cf1461008b578063f2fde38b146100a9575b600080fd5b61006b60048036038101906100669190610346565b6100c5565b005b6100756101a1565b60405161008291906103b4565b60405180910390f35b6100936101c5565b6040516100a091906103de565b60405180910390f35b6100c360048036038101906100be9190610425565b6101cb565b005b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610153576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161014a906104af565b60405180910390fd5b6001548110610197576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161018e9061051b565b60405180910390fd5b8060018190555050565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60015481565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610259576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610250906104af565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16036102c8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016102bf906105ad565b60405180910390fd5b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600080fd5b6000819050919050565b61032381610310565b811461032e57600080fd5b50565b6000813590506103408161031a565b92915050565b60006020828403121561035c5761035b61030b565b5b600061036a84828501610331565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061039e82610373565b9050919050565b6103ae81610393565b82525050565b60006020820190506103c960008301846103a5565b92915050565b6103d881610310565b82525050565b60006020820190506103f360008301846103cf565b92915050565b61040281610393565b811461040d57600080fd5b50565b60008135905061041f816103f9565b92915050565b60006020828403121561043b5761043a61030b565b5b600061044984828501610410565b91505092915050565b600082825260208201905092915050565b7f417661696c61626c65206f6e6c7920666f72206f776e65720000000000000000600082015250565b6000610499601883610452565b91506104a482610463565b602082019050919050565b600060208201905081810360008301526104c88161048c565b9050919050565b7f4d696e74206c696d69742063616e6e6f7420626520696e637265617365640000600082015250565b6000610505601e83610452565b9150610510826104cf565b602082019050919050565b60006020820190508181036000830152610534816104f8565b9050919050565b7f5a65726f2061646472657373206973206e6f7420612076616c6964206f776e6560008201527f7200000000000000000000000000000000000000000000000000000000000000602082015250565b6000610597602183610452565b91506105a28261053b565b604082019050919050565b600060208201905081810360008301526105c68161058a565b905091905056fea2646970667358221220f6ba05603c2183eab4fcaf34ef756bc7b9865308ce3373edd3d9ecd4ecef228864736f6c63430008130033"),
BytecodeHash: common.HexToHash("0xfd239f4b73e7ca9e286f4e3190bfa283fc96af4bb89ad03283c760d5431af53b"),
StorageLayout: storageLayout{
Owner: common.Hash{},
MintLimit: common.BytesToHash([]byte{1}),
},
}

const mintEventAbiString = `[{
"anonymous": false,
"inputs": [
{"indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256"},
{"indexed": false, "internalType": "bytes32", "name": "burnTxHash", "type": "bytes32"},
{"indexed": false, "internalType": "uint8", "name": "burnTxNetwork", "type": "uint8"}
],
"name": "Mint",
"type": "event"
}]`

var mintEventAbi, _ = abi.JSON(strings.NewReader(mintEventAbiString))

const mintEventName = "Mint"
14 changes: 0 additions & 14 deletions core/mint/contract/compiler_config.json

This file was deleted.

19 changes: 19 additions & 0 deletions core/mint/contract/standard_json_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"language": "Solidity",
"sources": {
"MintState.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity 0.8.19;\n\ncontract MintState {\n address public owner;\n uint256 public mintLimit;\n\n event Mint(uint256 amount, bytes32 burnTxHash, uint8 burnTxNetwork);\n\n modifier onlyOwner {\n require(msg.sender == owner, \"Available only for owner\");\n _;\n }\n\n function transferOwnership(address newOwner) external onlyOwner {\n require(newOwner != address(0), \"Zero address is not a valid owner\");\n owner = newOwner;\n }\n\n function changeMintLimit(uint256 newLimit) external onlyOwner {\n require(newLimit < mintLimit, \"Mint limit cannot be increased\");\n mintLimit = newLimit;\n }\n}\n"
}
},
"settings": {
"optimizer": {
"enabled": false,
"runs": 200
},
"outputSelection": {
"*": {
"*": ["*"]
}
}
}
}
100 changes: 0 additions & 100 deletions core/mint/instruction.go

This file was deleted.

33 changes: 0 additions & 33 deletions core/mint/state.go

This file was deleted.

2 changes: 1 addition & 1 deletion core/state/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestApplyMintContractMigration(t *testing.T) {

expectedOwner := common.HexToHash("0x0000000000000000000000001000000000000000000000000000000000000000")
expectedMintLimit := common.HexToHash("0x0000000000000000000000000000000000000000000000000de0b6b3a7640000")
expectedBytecodeHash := common.HexToHash("0x094c2e08801f704f590fc847deec6076c880cdc6062f87c32614a4ff213fdf9c")
expectedBytecodeHash := common.HexToHash("0xfd239f4b73e7ca9e286f4e3190bfa283fc96af4bb89ad03283c760d5431af53b")

contractStateObject := state.getStateObject(common.BytesToAddress([]byte{0x10, 0x00}))
assert.Equal(t, new(big.Int), contractStateObject.Balance())
Expand Down
16 changes: 6 additions & 10 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core

import (
"fmt"
"github.com/ethereum/go-ethereum/core/mint"
"math"
"math/big"

Expand Down Expand Up @@ -328,8 +327,11 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
)
if contractCreation {
ret, _, st.gas, vmerr = st.evm.Create(sender, st.data, st.gas, st.value)
} else if mintInstruction := mint.ParseInstruction(st.evm, msg.From(), st.to(), st.data); mintInstruction != nil {
mintInstruction.Apply(st.evm, st.gas)
} else if st.evm.IsMintInstruction(st.to(), st.data) {
var data [65]byte
copy(data[:], st.data)

st.gas, vmerr = st.evm.Mint(msg.From(), data, st.gas)
} else {
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
Expand All @@ -355,13 +357,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
} else {
fee := new(big.Int).SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTip)

feeCollectorAddress := st.evm.Context.Coinbase
if st.evm.ChainConfig().FeeCollectorAddress != nil {
feeCollectorAddress = *st.evm.ChainConfig().FeeCollectorAddress
}

st.state.AddBalance(feeCollectorAddress, fee)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
}

return &ExecutionResult{
Expand Down
Loading

0 comments on commit 6ca9efb

Please sign in to comment.