Skip to content
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

feat: E2E test for fraudproof prototype #261

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,8 @@ func (app *BaseApp) generateFraudProof(storeKeyToSubstoreTraceBuf map[string]*by
}
for key := range keys {
bKey := []byte(key)
has, err := deepSubstoreSMT.Has(bKey)
if err != nil {
return FraudProof{}, err
}
has, _ := deepSubstoreSMT.Has(bKey)
// The error returned here is sometimes `invalid key` but the boolean is false
if has {
value, err := deepSubstoreSMT.Get([]byte(key))
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,99 @@ func executeBlock(t *testing.T, app *BaseApp, txs []txTest, blockHeight int64) {
app.EndBlock(abci.RequestEndBlock{Height: blockHeight})
}

func TestEndToEndFraudProof(t *testing.T) {
routerOpt := func(bapp *BaseApp) {
bapp.Router().AddRoute(sdk.NewRoute(routeMsgKeyValue, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
kv := msg.(*msgKeyValue)
bapp.cms.GetKVStore(capKey2).Set(kv.Key, kv.Value)
return &sdk.Result{}, nil
}))
}

// BaseApp, B1 with no Tracing
appB1 := setupBaseApp(t,
AppOptionFunc(routerOpt),
)

// B1 <- S0
appB1.InitChain(abci.RequestInitChain{})

numTransactions := 1
// B1 <- S1
executeBlockWithArbitraryTxs(t, appB1, numTransactions, 1)
appB1.Commit()
storeHashB1 := appB1.cms.(*multi.Store).GetSubstoreSMT(capKey2.Name()).Root()

// B1 <- S2
executeBlockWithArbitraryTxs(t, appB1, numTransactions, 2)

// Do not commit here in order to preserve saved previous versions

// the only store key we'd like to enable tracing for
storeKeys := []types.StoreKey{capKey2}
routerOpts := make(map[string]AppOptionFunc)
routerOpts[capKey2.Name()] = func(bapp *BaseApp) {
bapp.Router().AddRoute(sdk.NewRoute(routeMsgKeyValue, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
kv := msg.(*msgKeyValue)
cms := bapp.cms.(*multi.Store)
// There is only storeKey in the test for now
storeKey := cms.GetStoreKeys()[len(storeKeys)-1]
bapp.cms.GetKVStore(storeKey).Set(kv.Key, kv.Value)
return &sdk.Result{}, nil
}))
}
appB2, storeKeyToSubstoreTraceBuf, err := appB1.enableFraudProofGenerationMode(storeKeys, routerOpts)
require.Nil(t, err)

cmsB2 := appB2.cms.(*multi.Store)
for _, storeKey := range storeKeys {
storeHashB2 := cmsB2.GetSubstoreSMT(storeKey.Name()).Root()
require.Equal(t, storeHashB1, storeHashB2)
}

// Execute fraudulent block
// Note: should be one block but not able to revert state for a fresh baseapp because it needs a commit to revert
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue documented here: #259

// so we have two blocks where we commit one
fraudTxs := executeBlockWithArbitraryTxs(t, appB2, numTransactions, 1)
commitHashB2 := appB2.Commit()
appHashB2 := commitHashB2.GetData()
executeBlockWithArbitraryTxs(t, appB2, numTransactions, 2)

fraudProof, err := appB2.generateFraudProof(storeKeyToSubstoreTraceBuf, appB2.LastBlockHeight())
require.Nil(t, err)

// Light Client
fraudProofVerified, err := fraudProof.verifyFraudProof(appHashB2)
require.Nil(t, err)
require.True(t, fraudProofVerified)

// Check if fraudproof only contains keys from that fraudulent block or not, if yes, pass
for _, moduleName := range fraudProof.getModules() {
witnessKeys, witnessVals := make([]string, 0), make([]string, 0)
stateWitness := fraudProof.stateWitness[moduleName]
for _, witnessData := range stateWitness.WitnessData {
key, val := string(witnessData.Key), string(witnessData.Value)
witnessKeys = append(witnessKeys, key)
witnessVals = append(witnessVals, val)
}
txKeys, txVals := make([]string, 0), make([]string, 0)
for _, tx := range fraudTxs {
for _, msg := range tx.GetMsgs() {
msgKV := msg.(msgKeyValue)
txKeys = append(txKeys, (string(msgKV.Key)))
txVals = append(txVals, (string(msgKV.Value)))
}
}
sort.Strings(witnessKeys)
sort.Strings(txKeys)
require.Equal(t, witnessKeys, txKeys)

sort.Strings(witnessVals)
sort.Strings(txVals)
require.Equal(t, witnessVals, txVals)
}
}

func TestFraudProofGenerationMode(t *testing.T) {
/*
Tests fraudproof generation mode. Steps:
Expand Down