aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbojie <bojie@dexon.org>2019-01-31 18:23:46 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:57 +0800
commit2e939de0678b7ef8da6a0306270e0ef126a8df01 (patch)
tree8d55a14cb2b988610b0dab2323c3ec3b995df4ff
parente5327ae2da93eb3c4784971157db4300d17fb35b (diff)
downloaddexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.tar
dexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.tar.gz
dexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.tar.bz2
dexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.tar.lz
dexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.tar.xz
dexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.tar.zst
dexon-2e939de0678b7ef8da6a0306270e0ef126a8df01.zip
transaction: use all transaction gas to reduce attack intention (#180)
The ci test in /tests will use origin evm logic.
-rw-r--r--build/ci.go18
-rw-r--r--core/state_transition.go26
2 files changed, 43 insertions, 1 deletions
diff --git a/build/ci.go b/build/ci.go
index f49071295..810f7d69b 100644
--- a/build/ci.go
+++ b/build/ci.go
@@ -330,6 +330,15 @@ func doTest(cmdline []string) {
}
packages = build.ExpandPackagesNoVendor(packages)
+ packageForLegacyEvm := []string{}
+ for i := 0; i < len(packages); i++ {
+ if strings.HasSuffix(packages[i], "dexon/tests") {
+ packageForLegacyEvm = append(packageForLegacyEvm, packages[i])
+ packages = append(packages[:i], packages[i+1:]...)
+ i--
+ }
+ }
+
// Run the actual tests.
// Test a single package at a time. CI builders are slow
// and some tests run into timeouts under load.
@@ -341,6 +350,15 @@ func doTest(cmdline []string) {
gotest.Args = append(gotest.Args, packages...)
build.MustRun(gotest)
+
+ gotestForLegacyEvm := goTool("test", buildFlags(env)...)
+ gotestForLegacyEvm.Args = append(gotestForLegacyEvm.Args, "-p", "1", "-timeout", "5m")
+ if *coverage {
+ gotestForLegacyEvm.Args = append(gotestForLegacyEvm.Args, "-covermode=atomic", "-cover")
+ }
+ gotestForLegacyEvm.Args = append(gotestForLegacyEvm.Args, packageForLegacyEvm...)
+ gotestForLegacyEvm.Args = append(gotestForLegacyEvm.Args, "-legacy-evm=true")
+ build.MustRun(gotestForLegacyEvm)
}
// runs gometalinter on requested packages
diff --git a/core/state_transition.go b/core/state_transition.go
index 3b5ea6a2f..ce05e54a2 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -18,6 +18,7 @@ package core
import (
"errors"
+ "flag"
"math"
"math/big"
@@ -27,6 +28,8 @@ import (
"github.com/dexon-foundation/dexon/params"
)
+var legacyEvm = flag.Bool("legacy-evm", false, "make evm run origin logic")
+
var (
errInsufficientBalanceForGas = errors.New("insufficient balance to pay for gas")
)
@@ -221,12 +224,33 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
return nil, 0, false, vmerr
}
}
- st.refundGas()
+
+ if *legacyEvm {
+ st.refundGas()
+ } else {
+ st.dexonRefundGas()
+ }
st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice))
return ret, st.gasUsed(), vmerr != nil, err
}
+func (st *StateTransition) dexonRefundGas() {
+ // Apply refund counter, capped to half of the used gas.
+ refund := st.gasUsed() / 2
+ if refund > st.state.GetRefund() {
+ refund = st.state.GetRefund()
+ }
+
+ // Return ETH for remaining gas, exchanged at the original rate.
+ remaining := new(big.Int).Mul(new(big.Int).SetUint64(refund), st.gasPrice)
+ st.state.AddBalance(st.msg.From(), remaining)
+
+ // Also return remaining gas to the block gas counter so it is
+ // available for the next transaction.
+ st.gp.AddGas(refund)
+}
+
func (st *StateTransition) refundGas() {
// Apply refund counter, capped to half of the used gas.
refund := st.gasUsed() / 2