Create & Create2

Create create在用的時候主要是根據 sender address sender address’s nonce 這兩個東西來產生address 產生的公式如下 keccak256(rlp.encode(senderAddress, senderAddressNonce))[12:] go-ethereum(v1.9.23)裡面是這樣實作的 // CreateAddress creates an ethereum address given the bytes and the nonce func CreateAddress(b common.Address, nonce uint64) common.Address { data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) return common.BytesToAddress(Keccak256(data)[12:]) } Create2 後來新加入的opcode,擺脫了nonce的限制,讓address可以更加靈活的被運用。像是可以有如BTC一般產生一堆收款地址,等待想要回收時,再自己部署一個合約的code回收那些錢即可。create2的必要元素如下: sender address the hash of byte code being deployed a random salt (32 byte string), supplied by the creator keccak256(0xff ++ deployingAddr ++ salt ++ keccak256(bytecode))[12:] go-ethereum(v1.9.23)裡面是這樣實作的 // CreateAddress2 creates an ethereum address given the address bytes, initial // contract code hash and a salt. func CreateAddress2(b common.Address, salt [32]byte, inithash []byte) common.Address { return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash)[12:]) } 以下有一個solidity的使用方法,可以在remix操作看看,理論上在create2Addr得到的address,用相同的參數放進去create2裡面會得到一樣的address ...

Ethereum Virtual Machine操作

本篇使用go-ethereum Version: 1.9.23-stable 概述 EVM跑起來就像是一個stack machine,stack整體是 1024 * 256 bit 個空間。 運作期間可以對Storage或是Memory做儲存或是讀取的操作。 至於各個OPCODE的操作可以搭配參照這裡。 1 + 1 首先我們來執行一個小小的程式 1 + 1 ❯ evm --code 6001600101 --debug run 0x #### TRACE #### PUSH1 pc=00000000 gas=10000000000 cost=3 PUSH1 pc=00000002 gas=9999999997 cost=3 Stack: 00000000 0000000000000000000000000000000000000000000000000000000000000001 ADD pc=00000004 gas=9999999994 cost=3 Stack: 00000000 0000000000000000000000000000000000000000000000000000000000000001 00000001 0000000000000000000000000000000000000000000000000000000000000001 STOP pc=00000005 gas=9999999991 cost=0 Stack: 00000000 0000000000000000000000000000000000000000000000000000000000000002 #### LOGS #### 這邊的code可以拆成三個部分來看,分別是6001 6001 01 所對應到的指令會像是這樣 00000: PUSH1 0x01 // PUSH 1 byte長度的數值 0x01 00002: PUSH1 0x01 // PUSH 1 byte長度的數值 0x01 00004: ADD // 相加Stack最上面的兩個元素後,把結果放在頂端 如上面的debug最後一個步驟,我們可以看到0x02就在stack的頂端。 ...