Skip to content

Commit c12f4df

Browse files
committed
params: core, core/vm, miner: 64bit gas instructions
Reworked the EVM gas instructions to use 64bit integers rather than arbitrary size big ints. All gas operations, be it additions, multiplications or divisions, are checked and guarded against 64 bit integer overflows. In additon, most of the protocol paramaters in the params package have been converted to uint64 and are now constants rather than variables. * common/math: added overflow check ops * core: vmenv, env renamed to evm * eth, internal/ethapi, les: unmetered eth_call and cancel methods * core/vm: implemented big.Int pool for evm instructions * core/vm: unexported intPool methods & verification methods * core/vm: added memoryGasCost overflow check and test
1 parent 72dcd3c commit c12f4df

47 files changed

Lines changed: 1087 additions & 826 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/evm/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func run(ctx *cli.Context) error {
156156
ret, _, err = runtime.Create(input, &runtime.Config{
157157
Origin: sender.Address(),
158158
State: statedb,
159-
GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)),
159+
GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)).Uint64(),
160160
GasPrice: common.Big(ctx.GlobalString(PriceFlag.Name)),
161161
Value: common.Big(ctx.GlobalString(ValueFlag.Name)),
162162
EVMConfig: vm.Config{
@@ -172,7 +172,7 @@ func run(ctx *cli.Context) error {
172172
ret, err = runtime.Call(receiver.Address(), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
173173
Origin: sender.Address(),
174174
State: statedb,
175-
GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)),
175+
GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)).Uint64(),
176176
GasPrice: common.Big(ctx.GlobalString(PriceFlag.Name)),
177177
Value: common.Big(ctx.GlobalString(ValueFlag.Name)),
178178
EVMConfig: vm.Config{

‎cmd/geth/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
205205
if err != nil {
206206
glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
207207
}
208-
if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
208+
if uint64(len(extra)) > params.MaximumExtraDataSize {
209209
glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
210210
glog.V(logger.Debug).Infof("extra: %x\n", extra)
211211
extra = nil

‎common/math/integer.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package math
2+
3+
import gmath "math"
4+
5+
/*
6+
* NOTE: The following methods need to be optimised using either bit checking or asm
7+
*/
8+
9+
// SafeSub returns subtraction result and whether overflow occurred.
10+
func SafeSub(x, y uint64) (uint64, bool) {
11+
return x - y, x < y
12+
}
13+
14+
// SafeAdd returns the result and whether overflow occurred.
15+
func SafeAdd(x, y uint64) (uint64, bool) {
16+
return x + y, y > gmath.MaxUint64-x
17+
}
18+
19+
// SafeMul returns multiplication result and whether overflow occurred.
20+
func SafeMul(x, y uint64) (uint64, bool) {
21+
if x == 0 {
22+
return 0, false
23+
}
24+
return x * y, x != 0 && y != 0 && y > gmath.MaxUint64/x
25+
}

‎common/math/integer_test.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package math
2+
3+
import (
4+
gmath "math"
5+
"testing"
6+
)
7+
8+
type operation byte
9+
10+
const (
11+
sub operation = iota
12+
add
13+
mul
14+
)
15+
16+
func TestOverflow(t *testing.T) {
17+
for i, test := range []struct {
18+
x uint64
19+
y uint64
20+
overflow bool
21+
op operation
22+
}{
23+
// add operations
24+
{gmath.MaxUint64, 1, true, add},
25+
{gmath.MaxUint64 - 1, 1, false, add},
26+
27+
// sub operations
28+
{0, 1, true, sub},
29+
{0, 0, false, sub},
30+
31+
// mul operations
32+
{10, 10, false, mul},
33+
{gmath.MaxUint64, 2, true, mul},
34+
{gmath.MaxUint64, 1, false, mul},
35+
} {
36+
var overflows bool
37+
switch test.op {
38+
case sub:
39+
_, overflows = SafeSub(test.x, test.y)
40+
case add:
41+
_, overflows = SafeAdd(test.x, test.y)
42+
case mul:
43+
_, overflows = SafeMul(test.x, test.y)
44+
}
45+
46+
if test.overflow != overflows {
47+
t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
48+
}
49+
}
50+
}

‎core/bench_test.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
9292
var (
9393
ringKeys = make([]*ecdsa.PrivateKey, 1000)
9494
ringAddrs = make([]common.Address, len(ringKeys))
95+
bigTxGas = new(big.Int).SetUint64(params.TxGas)
9596
)
9697

9798
func init() {
@@ -111,16 +112,16 @@ func genTxRing(naccounts int) func(int, *BlockGen) {
111112
return func(i int, gen *BlockGen) {
112113
gas := CalcGasLimit(gen.PrevBlock(i - 1))
113114
for {
114-
gas.Sub(gas, params.TxGas)
115-
if gas.Cmp(params.TxGas) < 0 {
115+
gas.Sub(gas, bigTxGas)
116+
if gas.Cmp(bigTxGas) < 0 {
116117
break
117118
}
118119
to := (from + 1) % naccounts
119120
tx := types.NewTransaction(
120121
gen.TxNonce(ringAddrs[from]),
121122
ringAddrs[to],
122123
benchRootFunds,
123-
params.TxGas,
124+
bigTxGas,
124125
nil,
125126
nil,
126127
)

‎core/block_validator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (v *BlockValidator) ValidateHeader(header, parent *types.Header, checkPow b
204204
//
205205
// See YP section 4.3.4. "Block Header Validity"
206206
func ValidateHeader(config *params.ChainConfig, pow pow.PoW, header *types.Header, parent *types.Header, checkPow, uncle bool) error {
207-
if big.NewInt(int64(len(header.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
207+
if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
208208
return fmt.Errorf("Header extra data too long (%d)", len(header.Extra))
209209
}
210210

‎core/blockchain_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ func TestFastVsFullChains(t *testing.T) {
720720
// If the block number is multiple of 3, send a few bonus transactions to the miner
721721
if i%3 == 2 {
722722
for j := 0; j < i%4+1; j++ {
723-
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, nil, nil), signer, key)
723+
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), bigTxGas, nil, nil), signer, key)
724724
if err != nil {
725725
panic(err)
726726
}
@@ -884,8 +884,8 @@ func TestChainTxReorgs(t *testing.T) {
884884
// Create two transactions shared between the chains:
885885
// - postponed: transaction included at a later block in the forked chain
886886
// - swapped: transaction included at the same block number in the forked chain
887-
postponed, _ := types.SignTx(types.NewTransaction(0, addr1, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
888-
swapped, _ := types.SignTx(types.NewTransaction(1, addr1, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
887+
postponed, _ := types.SignTx(types.NewTransaction(0, addr1, big.NewInt(1000), bigTxGas, nil, nil), signer, key1)
888+
swapped, _ := types.SignTx(types.NewTransaction(1, addr1, big.NewInt(1000), bigTxGas, nil, nil), signer, key1)
889889

890890
// Create two transactions that will be dropped by the forked chain:
891891
// - pastDrop: transaction dropped retroactively from a past block
@@ -901,13 +901,13 @@ func TestChainTxReorgs(t *testing.T) {
901901
chain, _ := GenerateChain(params.TestChainConfig, genesis, db, 3, func(i int, gen *BlockGen) {
902902
switch i {
903903
case 0:
904-
pastDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
904+
pastDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), bigTxGas, nil, nil), signer, key2)
905905

906906
gen.AddTx(pastDrop) // This transaction will be dropped in the fork from below the split point
907907
gen.AddTx(postponed) // This transaction will be postponed till block #3 in the fork
908908

909909
case 2:
910-
freshDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
910+
freshDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), bigTxGas, nil, nil), signer, key2)
911911

912912
gen.AddTx(freshDrop) // This transaction will be dropped in the fork from exactly at the split point
913913
gen.AddTx(swapped) // This transaction will be swapped out at the exact height
@@ -926,18 +926,18 @@ func TestChainTxReorgs(t *testing.T) {
926926
chain, _ = GenerateChain(params.TestChainConfig, genesis, db, 5, func(i int, gen *BlockGen) {
927927
switch i {
928928
case 0:
929-
pastAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key3)
929+
pastAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), bigTxGas, nil, nil), signer, key3)
930930
gen.AddTx(pastAdd) // This transaction needs to be injected during reorg
931931

932932
case 2:
933933
gen.AddTx(postponed) // This transaction was postponed from block #1 in the original chain
934934
gen.AddTx(swapped) // This transaction was swapped from the exact current spot in the original chain
935935

936-
freshAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key3)
936+
freshAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), bigTxGas, nil, nil), signer, key3)
937937
gen.AddTx(freshAdd) // This transaction will be added exactly at reorg time
938938

939939
case 3:
940-
futureAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key3)
940+
futureAdd, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), bigTxGas, nil, nil), signer, key3)
941941
gen.AddTx(futureAdd) // This transaction will be added after a full reorg
942942
}
943943
})

‎core/chain_makers_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ func ExampleGenerateChain() {
5656
switch i {
5757
case 0:
5858
// In block 1, addr1 sends addr2 some ether.
59-
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1)
59+
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), bigTxGas, nil, nil), signer, key1)
6060
gen.AddTx(tx)
6161
case 1:
6262
// In block 2, addr1 sends some more ether to addr2.
6363
// addr2 passes it on to addr3.
64-
tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
65-
tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
64+
tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), bigTxGas, nil, nil), signer, key1)
65+
tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), bigTxGas, nil, nil), signer, key2)
6666
gen.AddTx(tx1)
6767
gen.AddTx(tx2)
6868
case 2:

0 commit comments

Comments
 (0)