Skip to content

Commit

Permalink
cmd,core,internal,params: Introduce MintMe hardfork to avoid contract…
Browse files Browse the repository at this point in the history
… address collisions (#400)

* Introduce MintMe hardfork to avoid contract address collisions

* Introduce MintMe hardfork to avoid contract address collisions - code style fixes

* Introduce MintMe hardfork to avoid contract address collisions - fix import cycle

* Introduce MintMe hardfork to avoid contract address collisions - fix tests

* Update params/types/coregeth/chain_config.go

Co-authored-by: meowsbits <[email protected]>

* Update params/types/goethereum/goethereum.go

Co-authored-by: meowsbits <[email protected]>

* Update params/types/multigeth/multigethv0_chain_config.go

Co-authored-by: meowsbits <[email protected]>
  • Loading branch information
lukaszmatczak and meowsbits authored Aug 4, 2021
1 parent 01e9fa7 commit 047f112
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 7 deletions.
7 changes: 6 additions & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/params/mutations"
"github.com/ethereum/go-ethereum/params/types/ctypes"
"github.com/ethereum/go-ethereum/params/types/genesisT"
"github.com/ethereum/go-ethereum/params/vars"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/crypto/sha3"
Expand Down Expand Up @@ -187,7 +188,11 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig ctypes.ChainConfigura

// 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())
if chainConfig.IsEnabled(chainConfig.GetLyra2NonceTransition, vmContext.BlockNumber) {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce()+vars.Lyra2ContractNonceOffset)
} else {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
}
}

// Set the receipt logs and create the bloom filter.
Expand Down
5 changes: 3 additions & 2 deletions core/forkid/forkid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ func TestCreation(t *testing.T) {
params.MintMeChainConfig,
params.MintMeGenesisHash,
[]testcase{
{0, ID{Hash: checksumToBytes(0x02bf4180), Next: 0}},
{0, ID{Hash: checksumToBytes(0x02bf4180), Next: 252500}},
{252500, ID{Hash: checksumToBytes(0x50aed09f), Next: 0}},
},
},
}
Expand Down Expand Up @@ -372,7 +373,7 @@ func TestGatherForks(t *testing.T) {
{
"mintme",
params.MintMeChainConfig,
[]uint64{},
[]uint64{252_500},
},
}
sliceContains := func(sl []uint64, u uint64) bool {
Expand Down
7 changes: 6 additions & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params/mutations"
"github.com/ethereum/go-ethereum/params/types/ctypes"
"github.com/ethereum/go-ethereum/params/vars"
)

// StateProcessor is a basic Processor, which takes care of transitioning
Expand Down Expand Up @@ -126,7 +127,11 @@ func applyTransaction(msg types.Message, config ctypes.ChainConfigurator, bc Cha

// 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())
if config.IsEnabled(config.GetLyra2NonceTransition, header.Number) {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce()+vars.Lyra2ContractNonceOffset)
} else {
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
}
}

// Set the receipt logs and create the bloom filter.
Expand Down
7 changes: 6 additions & 1 deletion core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params/types/ctypes"
"github.com/ethereum/go-ethereum/params/vars"
"github.com/ethereum/go-ethereum/rlp"
)

Expand Down Expand Up @@ -376,7 +377,11 @@ func (r Receipts) DeriveFields(config ctypes.ChainConfigurator, hash common.Hash
if txs[i].To() == nil {
// Deriving the signer is expensive, only do if it's actually needed
from, _ := Sender(signer, txs[i])
r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce())
if config.IsEnabled(config.GetLyra2NonceTransition, r[i].BlockNumber) {
r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce()+vars.Lyra2ContractNonceOffset)
} else {
r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce())
}
}
// The used gas can be calculated based on previous r
if i == 0 {
Expand Down
6 changes: 5 additions & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,11 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,

// Create creates a new contract using code as deployment code.
func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
if evm.ChainConfig().IsEnabled(evm.ChainConfig().GetLyra2NonceTransition, evm.Context.BlockNumber) {
contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address())+vars.Lyra2ContractNonceOffset)
} else {
contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
}
return evm.create(caller, &codeAndHash{code: code}, gas, value, contractAddr)
}

Expand Down
9 changes: 8 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,11 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
if args.To != nil {
to = *args.To
} else {
to = crypto.CreateAddress(args.From, uint64(*args.Nonce))
if b.ChainConfig().IsEnabled(b.ChainConfig().GetLyra2NonceTransition, header.Number) {
to = crypto.CreateAddress(args.From, uint64(*args.Nonce)+vars.Lyra2ContractNonceOffset)
} else {
to = crypto.CreateAddress(args.From, uint64(*args.Nonce))
}
}
var input []byte
if args.Input != nil {
Expand Down Expand Up @@ -1871,6 +1875,9 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c

if tx.To() == nil {
addr := crypto.CreateAddress(from, tx.Nonce())
if b.ChainConfig().IsEnabled(b.ChainConfig().GetLyra2NonceTransition, b.CurrentBlock().Number()) {
addr = crypto.CreateAddress(from, tx.Nonce()+vars.Lyra2ContractNonceOffset)
}
log.Info("Submitted contract creation", "hash", tx.Hash().Hex(), "from", from, "nonce", tx.Nonce(), "contract", addr.Hex(), "value", tx.Value())
} else {
log.Info("Submitted transaction", "hash", tx.Hash().Hex(), "from", from, "nonce", tx.Nonce(), "recipient", tx.To(), "value", tx.Value())
Expand Down
2 changes: 2 additions & 0 deletions params/config_mintme.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ var (
ECIP1010Length: nil,
ECBP1100FBlock: nil, // ECBP1100 (MESS artificial finality)
RequireBlockHashes: map[uint64]common.Hash{},

Lyra2NonceTransitionBlock: big.NewInt(252500),
}
)
2 changes: 2 additions & 0 deletions params/types/coregeth/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ type CoreGethChainConfig struct {
BlockRewardSchedule ctypes.Uint64BigMapEncodesHex `json:"blockReward,omitempty"` // JSON tag matches Parity's

RequireBlockHashes map[uint64]common.Hash `json:"requireBlockHashes"`

Lyra2NonceTransitionBlock *big.Int `json:"lyra2NonceTransitionBlock,omitempty"`
}

// String implements the fmt.Stringer interface.
Expand Down
17 changes: 17 additions & 0 deletions params/types/coregeth/chain_config_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,3 +919,20 @@ func (c *CoreGethChainConfig) SetCliqueEpoch(n uint64) error {
c.Clique.Epoch = n
return nil
}

func (c *CoreGethChainConfig) GetLyra2NonceTransition() *uint64 {
if c.GetConsensusEngineType() != ctypes.ConsensusEngineT_Lyra2 {
return nil
}
return bigNewU64(c.Lyra2NonceTransitionBlock)
}

func (c *CoreGethChainConfig) SetLyra2NonceTransition(n *uint64) error {
if c.Lyra2 == nil {
return ctypes.ErrUnsupportedConfigFatal
}

c.Lyra2NonceTransitionBlock = setBig(c.Lyra2NonceTransitionBlock, n)

return nil
}
3 changes: 3 additions & 0 deletions params/types/ctypes/configurator_iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type ConsensusEnginator interface {
MustSetConsensusEngineType(t ConsensusEngineT) error
EthashConfigurator
CliqueConfigurator
Lyra2Configurator

// Catalyst: ETH -> ETH2 PoS transition helper
GetCatalystTransition() *uint64
Expand Down Expand Up @@ -228,6 +229,8 @@ type CliqueConfigurator interface {
}

type Lyra2Configurator interface {
GetLyra2NonceTransition() *uint64
SetLyra2NonceTransition(n *uint64) error
}

type BlockSealer interface {
Expand Down
8 changes: 8 additions & 0 deletions params/types/genesisT/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,11 @@ func (g *Genesis) GetCliqueEpoch() uint64 {
func (g *Genesis) SetCliqueEpoch(n uint64) error {
return g.Config.SetCliqueEpoch(n)
}

func (g *Genesis) GetLyra2NonceTransition() *uint64 {
return g.Config.GetLyra2NonceTransition()
}

func (g *Genesis) SetLyra2NonceTransition(n *uint64) error {
return g.Config.SetLyra2NonceTransition(n)
}
2 changes: 2 additions & 0 deletions params/types/goethereum/goethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type ChainConfig struct {

// Cache types for use with testing, but will not show up in config API.
ecbp1100Transition *big.Int

Lyra2NonceTransitionBlock *big.Int `json:"lyra2NonceTransitionBlock,omitempty"`
}

// String implements the fmt.Stringer interface.
Expand Down
17 changes: 17 additions & 0 deletions params/types/goethereum/goethereum_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,20 @@ func (c *ChainConfig) SetCliqueEpoch(n uint64) error {
c.Clique.Epoch = n
return nil
}

func (c *ChainConfig) GetLyra2NonceTransition() *uint64 {
if c.GetConsensusEngineType() != ctypes.ConsensusEngineT_Lyra2 {
return nil
}
return bigNewU64(c.Lyra2NonceTransitionBlock)
}

func (c *ChainConfig) SetLyra2NonceTransition(n *uint64) error {
if c.Lyra2 == nil {
return ctypes.ErrUnsupportedConfigFatal
}

c.Lyra2NonceTransitionBlock = setBig(c.Lyra2NonceTransitionBlock, n)

return nil
}
2 changes: 2 additions & 0 deletions params/types/multigeth/multigethv0_chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ type ChainConfig struct {
Ethash *ctypes.EthashConfig `json:"ethash,omitempty"`
Clique *ctypes.CliqueConfig `json:"clique,omitempty"`
Lyra2 *ctypes.Lyra2Config `json:"lyra2,omitempty"`

Lyra2NonceTransitionBlock *big.Int `json:"lyra2NonceTransitionBlock,omitempty"`
}
17 changes: 17 additions & 0 deletions params/types/multigeth/multigethv0_chain_config_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,20 @@ func (c *ChainConfig) SetCliqueEpoch(n uint64) error {
c.Clique.Epoch = n
return nil
}

func (c *ChainConfig) GetLyra2NonceTransition() *uint64 {
if c.GetConsensusEngineType() != ctypes.ConsensusEngineT_Lyra2 {
return nil
}
return bigNewU64(c.Lyra2NonceTransitionBlock)
}

func (c *ChainConfig) SetLyra2NonceTransition(n *uint64) error {
if c.Lyra2 == nil {
return ctypes.ErrUnsupportedConfigFatal
}

c.Lyra2NonceTransitionBlock = setBig(c.Lyra2NonceTransitionBlock, n)

return nil
}
1 change: 1 addition & 0 deletions params/types/parity/parity.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type ParityChainSpec struct {
EIP2565Transition *ParityU64 `json:"eip2565Transition,omitempty"` // FIXME, when and if i'm implemented in Parity
EIP2718Transition *ParityU64 `json:"eip2718Transition,omitempty"` // FIXME, when and if i'm implemented in Parity
ECIP1080Transition *ParityU64 `json:"ecip1080Transition,omitempty"` // FIXME, when and if i'm implemented in Parity
Lyra2NonceTransition *ParityU64 `json:"lyra2NonceTransition,omitempty"`

// supportedProtocolVersions is left here as a caching field only.
// I don't think this feature is supported by Parity, but
Expand Down
12 changes: 12 additions & 0 deletions params/types/parity/parity_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,3 +1263,15 @@ func (spec *ParityChainSpec) UpdateAccount(address common.Address, bal *big.Int,
}
return nil
}

func (spec *ParityChainSpec) GetLyra2NonceTransition() *uint64 {
if spec.GetConsensusEngineType() != ctypes.ConsensusEngineT_Lyra2 {
return nil
}
return spec.Params.Lyra2NonceTransition.Uint64P()
}

func (spec *ParityChainSpec) SetLyra2NonceTransition(n *uint64) error {
spec.Params.Lyra2NonceTransition = new(ParityU64).SetUint64(n)
return nil
}
3 changes: 3 additions & 0 deletions params/vars/lyra2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package vars

const Lyra2ContractNonceOffset uint64 = 0x00ffffff

0 comments on commit 047f112

Please sign in to comment.