-
Notifications
You must be signed in to change notification settings - Fork 312
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
Implement PreprocessTxs #21
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4815e2a
implement PayForMessage
evan-forbes a829d80
implement PreprocessTxs
evan-forbes 9afafdb
integrate PayForMessage into PreprocessTxs
evan-forbes 04cbae1
review feedback: add and use constants, better wording for some docum…
evan-forbes a41e189
review feedback: follow the spec and pad shares when generating the s…
evan-forbes 1203b18
add a handler for SignedTransactionDataPayForMessage
evan-forbes e0e7a86
change PreprocessTxs so that it does not run the transaction
evan-forbes 6abdee4
add a test for Preprocessing and remove the old test for runTx
evan-forbes 7295885
refactor message padding
evan-forbes 3578791
remove unused comment
evan-forbes 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 |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package app | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"sort" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/lazyledger/lazyledger-app/x/lazyledgerapp/types" | ||
abci "github.com/lazyledger/lazyledger-core/abci/types" | ||
core "github.com/lazyledger/lazyledger-core/proto/tendermint/types" | ||
) | ||
|
||
// This file should contain all of the altered ABCI methods | ||
|
||
// PreprocessTxs fullfills the lazyledger-core version of the ACBI interface, by | ||
// performing basic validation for the incoming txs, and by cleanly separating | ||
// share messages from transactions | ||
func (app *App) PreprocessTxs(txs abci.RequestPreprocessTxs) abci.ResponsePreprocessTxs { | ||
squareSize := app.SquareSize() | ||
shareCounter := uint64(0) | ||
var shareMsgs []*core.Message | ||
var processedTxs [][]byte | ||
for _, rawTx := range txs.Txs { | ||
// decode the Tx | ||
tx, err := app.txConfig.TxDecoder()(rawTx) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// don't process the tx if the transaction doesn't contain a | ||
// PayForMessage sdk.Msg | ||
if !hasWirePayForMessage(tx) { | ||
processedTxs = append(processedTxs, rawTx) | ||
continue | ||
} | ||
|
||
// only support transactions that contain a single sdk.Msg | ||
if len(tx.GetMsgs()) != 1 { | ||
continue | ||
} | ||
|
||
msg := tx.GetMsgs()[0] | ||
|
||
// run basic validation on the transaction | ||
err = tx.ValidateBasic() | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// process the message | ||
coreMsg, signedTx, err := app.processMsg(msg) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// increment the share counter by the number of shares taken by the message | ||
sharesTaken := uint64(len(coreMsg.Data) / types.ShareSize) | ||
shareCounter += sharesTaken | ||
|
||
// if there are too many shares stop processing and return the transactions | ||
if shareCounter > squareSize*squareSize { | ||
break | ||
} | ||
|
||
// encode the processed tx | ||
rawProcessedTx, err := app.appCodec.MarshalBinaryBare(signedTx) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
// add the message and tx to the output | ||
shareMsgs = append(shareMsgs, &coreMsg) | ||
processedTxs = append(processedTxs, rawProcessedTx) | ||
} | ||
|
||
// sort messages lexigraphically | ||
sort.Slice(shareMsgs, func(i, j int) bool { | ||
return bytes.Compare(shareMsgs[i].NamespaceId, shareMsgs[j].NamespaceId) < 0 | ||
}) | ||
|
||
return abci.ResponsePreprocessTxs{ | ||
Txs: processedTxs, | ||
Messages: &core.Messages{MessagesList: shareMsgs}, | ||
} | ||
} | ||
|
||
func hasWirePayForMessage(tx sdk.Tx) bool { | ||
for _, msg := range tx.GetMsgs() { | ||
if msg.Type() == types.TypeMsgPayforMessage { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// processMsgs will perform the processing required by PreProcessTxs for a set | ||
// of sdk.Msg's from a single sdk.Tx | ||
func (app *App) processMsg(msg sdk.Msg) (core.Message, *types.TxSignedTransactionDataPayForMessage, error) { | ||
squareSize := app.SquareSize() | ||
// reject all msgs in tx if a single included msg is not correct type | ||
wireMsg, ok := msg.(*types.MsgWirePayForMessage) | ||
if !ok { | ||
return core.Message{}, | ||
nil, | ||
errors.New("transaction contained a message type other than types.MsgWirePayForMessage") | ||
} | ||
|
||
// make sure that a ShareCommitAndSignature of the correct size is | ||
// included in the message | ||
var shareCommit types.ShareCommitAndSignature | ||
for _, commit := range wireMsg.MessageShareCommitment { | ||
if commit.K == squareSize { | ||
shareCommit = commit | ||
} | ||
} | ||
// K == 0 means there was no share commit with the desired current square size | ||
if shareCommit.K == 0 { | ||
return core.Message{}, | ||
nil, | ||
fmt.Errorf("No share commit for correct square size. Current square size: %d", squareSize) | ||
} | ||
|
||
// add the message to the list of core message to be returned to ll-core | ||
coreMsg := core.Message{ | ||
NamespaceId: wireMsg.GetMessageNameSpaceId(), | ||
Data: wireMsg.GetMessage(), | ||
} | ||
|
||
// wrap the signed transaction data | ||
sTxData, err := wireMsg.SignedTransactionDataPayForMessage(squareSize) | ||
if err != nil { | ||
return core.Message{}, nil, err | ||
} | ||
|
||
signedData := &types.TxSignedTransactionDataPayForMessage{ | ||
Message: sTxData, | ||
Signature: shareCommit.Signature, | ||
PublicKey: wireMsg.PublicKey, | ||
} | ||
|
||
return coreMsg, signedData, nil | ||
} |
Oops, something went wrong.
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.
Note that in PreProcess the app also needs to track the (original / mempool) Tx that were processed, s.t. on re-check it can tell ll-core to remove them from the mempool.
This has the caveat that currently only the proposer will call preprocess: the app needs to know if a proposed block didn't make it through consensus (e.g. because of timeouts) and other nodes to be able to evict their mempools too...
@adlerjohn you and I need to think about, too.
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.
Not necessarily? Conflicting transactions (which includes duplicates in the mempool) between the block and the mempool are removed with CheckTx, so PreProcess doesn't technically need to do that?
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.
Looks like I'm confused about this again, then 🤔
How does the app know which (mempool) Tx made it into the block if the app doesn't track this?
Sure, it "sees" the Tx during each deliver Tx but only the already processed which are different from the mempool Tx potenially.
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.
Leaving this here for reference (quoting @adlerjohn):
So my concern was the mapping between mempool tx <-> block tx but as mentioned by John that mapping is essentially there via nonce and sender.