From 4e7e5907bc82d2c8d1dc9a6b3a0f97927930138c Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Wed, 3 Aug 2022 03:38:41 -0400 Subject: [PATCH] Fraudproof WIP --- baseapp/baseapp_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ baseapp/fraudproof.go | 8 ++++++ 2 files changed, 68 insertions(+) create mode 100644 baseapp/fraudproof.go diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 474e84e5046..12866b1570e 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -2153,3 +2153,63 @@ func TestBaseApp_Init(t *testing.T) { require.Equal(t, tc.expectedSnapshot.KeepRecent, tc.bapp.snapshotManager.GetKeepRecent()) } } + +func TestGenerateFraudProof(t *testing.T) { + + /* + 1. Create a fresh baseapp + 2. Create a 'block' and put transactions that set certain key/value pairs in it + 3. We should be able to `check block` IF block.hash not the same, trigger fraud: + - go through all transactions, run them, and keep track of which substores are being used + - export the SMTs inside those substores into a fraud proof data structure + + + // Question: What is a block? Right now abstract, need to make it more concrete + + */ + + codec := codec.NewLegacyAmino() + registerTestCodec(codec) + + 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 + })) + } + + app := setupBaseApp(t, + AppOptionFunc(routerOpt)) + + app.InitChain(abci.RequestInitChain{}) + + blocks := 1 + txsPerBlock := 2 + + r := rand.New(rand.NewSource(3920758213583)) + keyCounter := 0 + for height := int64(1); height <= int64(blocks); height++ { + app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: height}}) + for txNum := 0; txNum < txsPerBlock; txNum++ { + tx := txTest{Msgs: []sdk.Msg{}} + for msgNum := 0; msgNum < 3; msgNum++ { + key := []byte(fmt.Sprintf("%v", keyCounter)) + value := make([]byte, 10000) + _, err := r.Read(value) + require.NoError(t, err) + tx.Msgs = append(tx.Msgs, msgKeyValue{Key: key, Value: value}) + keyCounter++ + } + txBytes, err := codec.Marshal(tx) + require.NoError(t, err) + resp := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) + require.True(t, resp.IsOK(), "%v", resp.String()) + } + app.EndBlock(abci.RequestEndBlock{Height: height}) + commitResponse := app.Commit() + _ = commitResponse.GetData() + } + + return +} diff --git a/baseapp/fraudproof.go b/baseapp/fraudproof.go new file mode 100644 index 00000000000..32d78874a9c --- /dev/null +++ b/baseapp/fraudproof.go @@ -0,0 +1,8 @@ +package baseapp + +// FraudProof is a fraud proof. +type FraudProof struct { + blockHeight uint64 + + // TODO: stateWitness StateWitness +}