컨트랙 주소가 같을수 있나요?

말이 안되긴하는데,
테스트넷과, 메인넷에 같은 이름으로 NFT 컨트랙 발행했는데
컨트랙 주소가 일치합니다. 가능한 일인가요 ?

1개의 좋아요

@yong_jung
동일한 주소와 동일한 Nonce로 배포하셨다면 같을 수 있습니다.
해시라는 건 동일한 input에 대해 언제나 동일한 output이 나오기 때문이죠.

Contract를 배포할 때 어떤 함수가 호출되는지를 따라가보면 금방 이해하실 수 있습니다.
Contract 계정을 생성할 때는 아래와 같이 EVM의 Create 메서드가 호출이 되고

// source link: https://github.com/klaytn/klaytn/blob/8f3b25f3d8afdc99d5a7441554051572f44f5bda/blockchain/vm/evm.go#L522-L527
// Create creates a new contract using code as deployment code.
func (evm *EVM) Create(caller types.ContractRef, code []byte, gas uint64, value *big.Int, codeFormat params.CodeFormat) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
	codeAndHash := &codeAndHash{code: code}
	contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
	return evm.create(caller, codeAndHash, gas, value, contractAddr, false, codeFormat)
}

그 안에서 내부적으로 호출되는 CreateAddress를 보시면 Caller의 Address와 nonce를 RLP Encoding 하고 해당 값에 대해 Keccak256 해시를 구하는 것을 보실 수 있습니다.

// source link: https://github.com/klaytn/klaytn/blob/8f3b25f3d8afdc99d5a7441554051572f44f5bda/crypto/crypto.go#L87-L93
// CreateAddress creates a Klaytn address given the bytes and the nonce
func CreateAddress(b common.Address, nonce uint64) common.Address {
	data, _ := rlp.EncodeToBytes(struct {
		Addr  common.Address
		Nonce uint64
	}{b, nonce})
	return common.BytesToAddress(Keccak256(data)[12:])
}

스펙 대로 정확히 동작한 것이고 스펙에 따라 같은 Contract 주소가 생성된 것입니다 :slight_smile:

감사합니다.

2개의 좋아요

아 … nonce로 컨트랙 주소가 생성되는군요.
둘다 첫번째 tx(nonce)가 컨트랙 생성이라 같았나봅니다.
설명 감사드립니다!

1개의 좋아요