Skip to content

Commit

Permalink
fix: use specific addr when creating protected storage
Browse files Browse the repository at this point in the history
this avoids increasing the nonce of the caller by two
  • Loading branch information
youben11 committed Jan 18, 2024
1 parent a7a9fc0 commit 9445919
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions fhevm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
fhevm_crypto "github.com/zama-ai/fhevm-go/crypto"
)

var protectedStorageAddrCallerAddr common.Address
var defaultProtectedStorageAddrCallerAddr = []byte{93}

// Set the addr to be used as caller when creating protected storage contracts
func SetProtectedStorageAddrCallerAddr(addr []byte) {
protectedStorageAddrCallerAddr = common.BytesToAddress(addr)
}

func init() {
SetProtectedStorageAddrCallerAddr(defaultProtectedStorageAddrCallerAddr)
}

// A Logger interface for the EVM.
type Logger interface {
Debug(msg string, keyvals ...interface{})
Expand Down Expand Up @@ -191,7 +203,7 @@ func padArrayTo32Multiple(input []byte) []byte {
func Create(evm EVMEnvironment, caller common.Address, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
contractAddr = crypto.CreateAddress(caller, evm.GetNonce(caller))
protectedStorageAddr := fhevm_crypto.CreateProtectedStorageContractAddress(contractAddr)
_, _, leftOverGas, err = evm.CreateContract(caller, nil, gas, big.NewInt(0), protectedStorageAddr)
_, _, leftOverGas, err = evm.CreateContract(protectedStorageAddrCallerAddr, nil, gas, big.NewInt(0), protectedStorageAddr)
if err != nil {
ret = nil
contractAddr = common.Address{}
Expand All @@ -205,7 +217,7 @@ func Create2(evm EVMEnvironment, caller common.Address, code []byte, gas uint64,
codeHash := crypto.Keccak256Hash(code)
contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), codeHash.Bytes())
protectedStorageAddr := fhevm_crypto.CreateProtectedStorageContractAddress(contractAddr)
_, _, leftOverGas, err = evm.CreateContract2(caller, nil, common.Hash{}, gas, big.NewInt(0), protectedStorageAddr)
_, _, leftOverGas, err = evm.CreateContract2(protectedStorageAddrCallerAddr, nil, common.Hash{}, gas, big.NewInt(0), protectedStorageAddr)
if err != nil {
ret = nil
contractAddr = common.Address{}
Expand Down

0 comments on commit 9445919

Please sign in to comment.