-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create foundation for fraudulent block production (#1992)
## Overview creates a malicious package that allows for minimal changes to the rest of the application while also keeping the malicious portion of the code as separate as possible as to not accidentally trigger the malicious logic. part of #1953 ## Checklist - [x] New and updated code has appropriate documentation - [x] New and updated code has new and/or updated testing - [x] Required CI checks are passing - [x] Visual proof for any user facing features like CLI or documentation updates - [ ] Linked issues closed with keywords --------- Co-authored-by: Rootul P <[email protected]>
- Loading branch information
1 parent
9c1d220
commit 90a611b
Showing
10 changed files
with
548 additions
and
27 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package malicious | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/celestiaorg/celestia-app/app" | ||
"github.com/celestiaorg/celestia-app/app/encoding" | ||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
const ( | ||
// PrepareProposalHandlerKey is the key used to retrieve the PrepareProposal handler from the | ||
// app options. | ||
PrepareProposalHandlerKey = "prepare_proposal_handler" | ||
) | ||
|
||
type App struct { | ||
*app.App | ||
preparePropsoalHandler func(req abci.RequestPrepareProposal) abci.ResponsePrepareProposal | ||
} | ||
|
||
func New( | ||
logger log.Logger, | ||
db dbm.DB, | ||
traceStore io.Writer, | ||
loadLatest bool, | ||
skipUpgradeHeights map[int64]bool, | ||
homePath string, | ||
invCheckPeriod uint, | ||
encodingConfig encoding.Config, | ||
appOpts servertypes.AppOptions, | ||
baseAppOptions ...func(*baseapp.BaseApp), | ||
) *App { | ||
goodApp := app.New(logger, db, traceStore, loadLatest, skipUpgradeHeights, homePath, invCheckPeriod, encodingConfig, appOpts, baseAppOptions...) | ||
badApp := &App{App: goodApp} | ||
|
||
// default to using the good app's handlers | ||
badApp.SetPrepareProposalHandler(goodApp.PrepareProposal) | ||
|
||
// override the handler if it is set in the app options | ||
if prepareHander := appOpts.Get(PrepareProposalHandlerKey); prepareHander != nil { | ||
badApp.SetPrepareProposalHandler(prepareHander.(func(req abci.RequestPrepareProposal) abci.ResponsePrepareProposal)) | ||
} | ||
|
||
return badApp | ||
} | ||
|
||
func (app *App) PrepareProposal(req abci.RequestPrepareProposal) abci.ResponsePrepareProposal { | ||
return app.preparePropsoalHandler(req) | ||
} | ||
|
||
// SetPrepareProposalHandler sets the PrepareProposal handler. | ||
func (app *App) SetPrepareProposalHandler(handler func(req abci.RequestPrepareProposal) abci.ResponsePrepareProposal) { | ||
app.preparePropsoalHandler = handler | ||
} | ||
|
||
// ProcessProposal overwrites the default app's method to auto accept any | ||
// proposal. | ||
func (app *App) ProcessProposal(_ abci.RequestProcessProposal) (resp abci.ResponseProcessProposal) { | ||
return abci.ResponseProcessProposal{ | ||
Result: abci.ResponseProcessProposal_ACCEPT, | ||
} | ||
} |
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,66 @@ | ||
package malicious | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/pkg/wrapper" | ||
"github.com/celestiaorg/celestia-app/test/util/testfactory" | ||
"github.com/celestiaorg/celestia-app/test/util/testnode" | ||
"github.com/stretchr/testify/require" | ||
tmrand "github.com/tendermint/tendermint/libs/rand" | ||
) | ||
|
||
// TestOutOfOrderNMT tests that the malicious NMT implementation is able to | ||
// generate the same root as the ordered NMT implementation when the leaves are | ||
// added in the same order and can generate roots when leaves are out of | ||
// order. | ||
func TestOutOfOrderNMT(t *testing.T) { | ||
squareSize := uint64(64) | ||
c := NewConstructor(squareSize) | ||
goodConstructor := wrapper.NewConstructor(squareSize) | ||
|
||
orderedTree := goodConstructor(0, 0) | ||
maliciousOrderedTree := c(0, 0) | ||
maliciousUnorderedTree := c(0, 0) | ||
data := testfactory.GenerateRandNamespacedRawData(64) | ||
|
||
// compare the roots generated by pushing ordered data | ||
for _, d := range data { | ||
err := orderedTree.Push(d) | ||
require.NoError(t, err) | ||
err = maliciousOrderedTree.Push(d) | ||
require.NoError(t, err) | ||
} | ||
|
||
goodOrderedRoot, err := orderedTree.Root() | ||
require.NoError(t, err) | ||
malOrderedRoot, err := maliciousOrderedTree.Root() | ||
require.NoError(t, err) | ||
require.Equal(t, goodOrderedRoot, malOrderedRoot) | ||
|
||
// test the new tree with unordered data | ||
for i := range data { | ||
j := tmrand.Intn(len(data)) | ||
data[i], data[j] = data[j], data[i] | ||
} | ||
|
||
for _, d := range data { | ||
err := maliciousUnorderedTree.Push(d) | ||
require.NoError(t, err) | ||
} | ||
|
||
root, err := maliciousUnorderedTree.Root() | ||
require.NoError(t, err) | ||
require.Len(t, root, 90) // two namespaces + 32 bytes of hash = 90 bytes | ||
require.NotEqual(t, goodOrderedRoot, root) // quick sanity check to ensure the roots are different | ||
} | ||
|
||
func TestMaliciousNodeTestNode(t *testing.T) { | ||
// TODO: flesh out this test further | ||
cfg := testnode.DefaultConfig(). | ||
WithAppCreator(NewAppServer) | ||
|
||
cctx, _, _ := testnode.NewNetwork(t, cfg) | ||
|
||
require.NoError(t, cctx.WaitForNextBlock()) | ||
} |
Oops, something went wrong.