欢迎所有爱好区块链技术的人加入。

ARCHIVES / 1298.HTML

以太坊部署合约时的地址是怎么计算的

Surou Ethereum-新手教程

先看代码 core/state_processor.go

func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, modOptions ...ModifyProcessOptionFunc) (*types.Receipt, error) {
...
	// 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())
	}

其中的evm.TxContext.Origin 为当前部署合约交易发起地址
core/evm.go

// NewEVMTxContext creates a new transaction context for a single transaction.
func NewEVMTxContext(msg Message) vm.TxContext {
	return vm.TxContext{
		Origin:   msg.From(),
		GasPrice: new(big.Int).Set(msg.GasPrice()),
	}
}

结论

合约地址ContractAddress通过部署合约发起地址和当前发起交易所在nonce计算所得

所以如果知道合约部署地址,就可以猜出对应的合约部署时地址了,可以用于一些提前抢单操作。。