-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gas management, estimation, limitation #963
Merged
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
46f9445
Add gas limit / gas consumed to context
cwgoes 2699180
GasMeter & context updates
cwgoes efc7843
Changes to bank keeper for gas
cwgoes f0e4d24
Basic gas impl, quick testcase
cwgoes ddb3b36
Pass gas consumed back in result struct
cwgoes af379b6
Linter fixes
cwgoes 1f8ef62
Swap to panic/recover version
cwgoes 0951705
Catch out-of-gas panics
cwgoes ca4ef9a
Add baseapp test for out-of-gas handling
cwgoes c410ceb
GasKVStore WIP
cwgoes ef1923f
Add GasIterator
cwgoes 9dfccb1
Update iterator gas pricing model
cwgoes 1c4ed7b
Gas-wrap ctx.KVStore
cwgoes da5fe2e
Add baseapp.CheckFull
cwgoes 097646e
Correct semantics for simulateDeliver
cwgoes a240554
SimulateTx through Query
cwgoes 8c1c40b
New store query prefixes (ref #979)
cwgoes 2147203
Update changelog
cwgoes 702ffaf
Rebase
cwgoes 147cf9f
Move GasKVStore to /store
cwgoes ce38d8f
Minor fix, testcases
cwgoes a801874
PR comment: codec => cdc
cwgoes 38716d5
ConsumeGas for pubkey.VerifyBytes
cwgoes 4cfa99e
Move to new version in changelog
cwgoes d55ba2c
Add p2p filter functions & tests
cwgoes 396bf74
Update changelog
cwgoes 4775437
Unexport verifyCost
cwgoes 0cc1c52
Rebase changelog
cwgoes 4134bf9
Address PR comments
cwgoes 03e2207
Unexport RunTxMode (fix linter)
cwgoes 3d5b048
Remove txGasLimit, update tests
cwgoes 4bdcad5
remove gasLimit from NewContext
ebuchman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,17 @@ import ( | |
// and to avoid affecting the Merkle root. | ||
var dbHeaderKey = []byte("header") | ||
|
||
type RunTxMode uint8 | ||
|
||
const ( | ||
// Check a transaction | ||
RunTxModeCheck RunTxMode = iota | ||
// Simulate a transaction | ||
RunTxModeSimulate RunTxMode = iota | ||
// Deliver a transaction | ||
RunTxModeDeliver RunTxMode = iota | ||
) | ||
|
||
// The ABCI application | ||
type BaseApp struct { | ||
// initialized on creation | ||
|
@@ -309,13 +320,17 @@ func (app *BaseApp) FilterPeerByPubKey(info string) abci.ResponseQuery { | |
// Implements ABCI. | ||
// Delegates to CommitMultiStore if it implements Queryable | ||
func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { | ||
path := req.Path | ||
path := strings.Split(req.Path, "/") | ||
// first element is empty string | ||
if len(path) > 0 && path[0] == "" { | ||
path = path[1:] | ||
} | ||
fmt.Sprintf("Path: %v\n", path) | ||
// "/app" prefix for special application queries | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we ran There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, updated. |
||
if strings.HasPrefix(path, "/app") { | ||
query := path[4:] | ||
if len(path) >= 2 && path[0] == "app" { | ||
var result sdk.Result | ||
switch query { | ||
case "/simulate": | ||
switch path[1] { | ||
case "simulate": | ||
txBytes := req.Data | ||
tx, err := app.txDecoder(txBytes) | ||
if err != nil { | ||
|
@@ -333,27 +348,23 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { | |
} | ||
} | ||
// "/store" prefix for store queries | ||
if strings.HasPrefix(path, "/store") { | ||
if len(path) >= 1 && path[0] == "store" { | ||
queryable, ok := app.cms.(sdk.Queryable) | ||
if !ok { | ||
msg := "multistore doesn't support queries" | ||
return sdk.ErrUnknownRequest(msg).QueryResult() | ||
} | ||
req.Path = req.Path[6:] // slice off "/store" | ||
req.Path = "/" + strings.Join(path[1:], "/") | ||
return queryable.Query(req) | ||
} | ||
// "/p2p" prefix for p2p queries | ||
if strings.HasPrefix(path, "/p2p") { | ||
path = path[4:] | ||
if strings.HasPrefix(path, "/filter") { | ||
path = path[7:] | ||
if strings.HasPrefix(path, "/addr") { | ||
path = path[6:] | ||
return app.FilterPeerByAddrPort(path) | ||
if len(path) >= 4 && path[0] == "p2p" { | ||
if path[1] == "filter" { | ||
if path[2] == "addr" { | ||
return app.FilterPeerByAddrPort(path[3]) | ||
} | ||
if strings.HasPrefix(path, "/pubkey") { | ||
path = path[8:] | ||
return app.FilterPeerByPubKey(path) | ||
if path[2] == "pubkey" { | ||
return app.FilterPeerByPubKey(path[3]) | ||
} | ||
} | ||
} | ||
|
@@ -385,7 +396,7 @@ func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { | |
if err != nil { | ||
result = err.Result() | ||
} else { | ||
result = app.runTx(true, false, txBytes, tx) | ||
result = app.runTx(RunTxModeCheck, txBytes, tx) | ||
} | ||
|
||
return abci.ResponseCheckTx{ | ||
|
@@ -410,7 +421,7 @@ func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { | |
if err != nil { | ||
result = err.Result() | ||
} else { | ||
result = app.runTx(false, false, txBytes, tx) | ||
result = app.runTx(RunTxModeDeliver, txBytes, tx) | ||
} | ||
|
||
// After-handler hooks. | ||
|
@@ -434,22 +445,22 @@ func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { | |
|
||
// nolint - Mostly for testing | ||
func (app *BaseApp) Check(tx sdk.Tx) (result sdk.Result) { | ||
return app.runTx(true, false, nil, tx) | ||
return app.runTx(RunTxModeCheck, nil, tx) | ||
} | ||
|
||
// nolint - full tx execution | ||
func (app *BaseApp) Simulate(tx sdk.Tx) (result sdk.Result) { | ||
return app.runTx(true, true, nil, tx) | ||
return app.runTx(RunTxModeSimulate, nil, tx) | ||
} | ||
|
||
// nolint | ||
func (app *BaseApp) Deliver(tx sdk.Tx) (result sdk.Result) { | ||
return app.runTx(false, false, nil, tx) | ||
return app.runTx(RunTxModeDeliver, nil, tx) | ||
} | ||
|
||
// txBytes may be nil in some cases, eg. in tests. | ||
// Also, in the future we may support "internal" transactions. | ||
func (app *BaseApp) runTx(isCheckTx bool, simulateDeliver bool, txBytes []byte, tx sdk.Tx) (result sdk.Result) { | ||
func (app *BaseApp) runTx(mode RunTxMode, txBytes []byte, tx sdk.Tx) (result sdk.Result) { | ||
// Handle any panics. | ||
defer func() { | ||
if r := recover(); r != nil { | ||
|
@@ -479,17 +490,14 @@ func (app *BaseApp) runTx(isCheckTx bool, simulateDeliver bool, txBytes []byte, | |
|
||
// Get the context | ||
var ctx sdk.Context | ||
if isCheckTx { | ||
if mode == RunTxModeCheck || mode == RunTxModeSimulate { | ||
ctx = app.checkState.ctx.WithTxBytes(txBytes) | ||
} else { | ||
ctx = app.deliverState.ctx.WithTxBytes(txBytes) | ||
} | ||
|
||
// Create a new zeroed gas meter | ||
ctx = ctx.WithGasMeter(sdk.NewGasMeter(app.txGasLimit)) | ||
|
||
// Simulate a DeliverTx for gas calculation | ||
if isCheckTx && simulateDeliver { | ||
if mode == RunTxModeSimulate { | ||
ctx = ctx.WithIsCheckTx(false) | ||
} | ||
|
||
|
@@ -513,7 +521,7 @@ func (app *BaseApp) runTx(isCheckTx bool, simulateDeliver bool, txBytes []byte, | |
|
||
// Get the correct cache | ||
var msCache sdk.CacheMultiStore | ||
if isCheckTx { | ||
if mode == RunTxModeCheck || mode == RunTxModeSimulate { | ||
// CacheWrap app.checkState.ms in case it fails. | ||
msCache = app.checkState.CacheMultiStore() | ||
ctx = ctx.WithMultiStore(msCache) | ||
|
@@ -529,7 +537,7 @@ func (app *BaseApp) runTx(isCheckTx bool, simulateDeliver bool, txBytes []byte, | |
result.GasUsed = ctx.GasMeter().GasConsumed() | ||
|
||
// If not a simulated run and result was successful, write to app.checkState.ms or app.deliverState.ms | ||
if !simulateDeliver && result.IsOK() { | ||
if mode != RunTxModeSimulate && result.IsOK() { | ||
msCache.Write() | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove