core/vm: per byte cost of CREATE2

This commit is contained in:
Martin Holst Swende 2018-10-02 14:22:05 +02:00
parent 7ae9bd5605
commit da9e9e8a6b
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 21 additions and 4 deletions

View file

@ -347,6 +347,17 @@ func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow { if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow {
return 0, errGasUintOverflow return 0, errGasUintOverflow
} }
wordGas, overflow := bigUint64(stack.Back(2))
if overflow {
return 0, errGasUintOverflow
}
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
return 0, errGasUintOverflow
}
if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
return 0, errGasUintOverflow
}
return gas, nil return gas, nil
} }

View file

@ -18,12 +18,11 @@ package vm
import ( import (
"bytes" "bytes"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
@ -544,14 +543,21 @@ func TestCreate2Addreses(t *testing.T) {
code: "0x", code: "0x",
expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0", expected: "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0",
}, },
} { } {
origin := common.BytesToAddress(common.FromHex(tt.origin)) origin := common.BytesToAddress(common.FromHex(tt.origin))
salt := common.BytesToHash(common.FromHex(tt.salt)) salt := common.BytesToHash(common.FromHex(tt.salt))
code := common.FromHex(tt.code) code := common.FromHex(tt.code)
address := crypto.CreateAddress2(origin, salt, code) address := crypto.CreateAddress2(origin, salt, code)
fmt.Printf("Example %d\n* address `0x%x`\n* salt `0x%x`\n* code `0x%x`\n* result: `%s`\n\n", i,origin, salt, code, address.String()) /*
stack := newstack()
// salt, but we don't need that for this test
stack.push(big.NewInt(int64(len(code)))) //size
stack.push(big.NewInt(0)) // memstart
stack.push(big.NewInt(0)) // value
gas, _ := gasCreate2(params.GasTable{}, nil, nil, stack, nil, 0)
fmt.Printf("Example %d\n* address `0x%x`\n* salt `0x%x`\n* init_code `0x%x`\n* gas (assuming no mem expansion): `%v`\n* result: `%s`\n\n", i,origin, salt, code, gas, address.String())
*/
expected := common.BytesToAddress(common.FromHex(tt.expected)) expected := common.BytesToAddress(common.FromHex(tt.expected))
if bytes.Compare(expected.Bytes(), address.Bytes()) != 0 { if bytes.Compare(expected.Bytes(), address.Bytes()) != 0 {
t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String()) t.Errorf("test %d: expected %s, got %s", i, expected.String(), address.String())