-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[occ] Add scheduler logic for validation (#336)
## Describe your changes and provide context - This was copied from #332 which became unwieldy due to commit history (merges/rebases) - Adds scheduler logic for validation - In this initial version it completes all executions then performs validations (which feed retries) - Once we start benchmarking we can make performance improvements to this - Retries tasks that fail validation and have no dependencies ## Testing performed to validate your change - Scheduler Test verifies multi-worker with conflicts
- Loading branch information
1 parent
b34d61c
commit 0aebbc9
Showing
10 changed files
with
426 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package baseapp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func toInt(b []byte) int { | ||
r, _ := strconv.Atoi(string(b)) | ||
return r | ||
} | ||
|
||
func toByteArr(i int) []byte { | ||
return []byte(fmt.Sprintf("%d", i)) | ||
} | ||
|
||
func handlerKVStore(capKey sdk.StoreKey) sdk.Handler { | ||
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { | ||
ctx = ctx.WithEventManager(sdk.NewEventManager()) | ||
res := &sdk.Result{} | ||
|
||
// Extract the unique ID from the message (assuming you have added this) | ||
txIndex := ctx.TxIndex() | ||
|
||
// Use the unique ID to get a specific key for this transaction | ||
sharedKey := []byte(fmt.Sprintf("shared")) | ||
txKey := []byte(fmt.Sprintf("tx-%d", txIndex)) | ||
|
||
// Similar steps as before: Get the store, retrieve a value, increment it, store back, emit an event | ||
// Get the store | ||
store := ctx.KVStore(capKey) | ||
|
||
// increment per-tx key (no conflict) | ||
val := toInt(store.Get(txKey)) | ||
store.Set(txKey, toByteArr(val+1)) | ||
|
||
// increment shared key | ||
sharedVal := toInt(store.Get(sharedKey)) | ||
store.Set(sharedKey, toByteArr(sharedVal+1)) | ||
|
||
// Emit an event with the incremented value and the unique ID | ||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent(sdk.EventTypeMessage, | ||
sdk.NewAttribute("shared-val", fmt.Sprintf("%d", sharedVal+1)), | ||
sdk.NewAttribute("tx-val", fmt.Sprintf("%d", val+1)), | ||
sdk.NewAttribute("tx-id", fmt.Sprintf("%d", txIndex)), | ||
), | ||
) | ||
|
||
res.Events = ctx.EventManager().Events().ToABCIEvents() | ||
return res, nil | ||
} | ||
} | ||
|
||
func requireAttribute(t *testing.T, evts []abci.Event, name string, val string) { | ||
for _, evt := range evts { | ||
for _, att := range evt.Attributes { | ||
if string(att.Key) == name { | ||
require.Equal(t, val, string(att.Value)) | ||
return | ||
} | ||
} | ||
} | ||
require.Fail(t, fmt.Sprintf("attribute %s not found via value %s", name, val)) | ||
} | ||
|
||
func TestDeliverTxBatch(t *testing.T) { | ||
// test increments in the ante | ||
//anteKey := []byte("ante-key") | ||
anteOpt := func(bapp *BaseApp) {} | ||
|
||
// test increments in the handler | ||
routerOpt := func(bapp *BaseApp) { | ||
r := sdk.NewRoute(routeMsgCounter, handlerKVStore(capKey1)) | ||
bapp.Router().AddRoute(r) | ||
} | ||
|
||
app := setupBaseApp(t, anteOpt, routerOpt) | ||
app.InitChain(context.Background(), &abci.RequestInitChain{}) | ||
|
||
// Create same codec used in txDecoder | ||
codec := codec.NewLegacyAmino() | ||
registerTestCodec(codec) | ||
|
||
nBlocks := 3 | ||
txPerHeight := 5 | ||
|
||
for blockN := 0; blockN < nBlocks; blockN++ { | ||
header := tmproto.Header{Height: int64(blockN) + 1} | ||
app.setDeliverState(header) | ||
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter()) | ||
app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header}) | ||
|
||
var requests []*sdk.DeliverTxEntry | ||
for i := 0; i < txPerHeight; i++ { | ||
counter := int64(blockN*txPerHeight + i) | ||
tx := newTxCounter(counter, counter) | ||
|
||
txBytes, err := codec.Marshal(tx) | ||
require.NoError(t, err) | ||
requests = append(requests, &sdk.DeliverTxEntry{ | ||
Request: abci.RequestDeliverTx{Tx: txBytes}, | ||
}) | ||
} | ||
|
||
responses := app.DeliverTxBatch(app.deliverState.ctx, sdk.DeliverTxBatchRequest{TxEntries: requests}) | ||
require.Len(t, responses.Results, txPerHeight) | ||
|
||
for idx, deliverTxRes := range responses.Results { | ||
res := deliverTxRes.Response | ||
require.Equal(t, abci.CodeTypeOK, res.Code) | ||
requireAttribute(t, res.Events, "tx-id", fmt.Sprintf("%d", idx)) | ||
requireAttribute(t, res.Events, "tx-val", fmt.Sprintf("%d", blockN+1)) | ||
requireAttribute(t, res.Events, "shared-val", fmt.Sprintf("%d", blockN*txPerHeight+idx+1)) | ||
} | ||
|
||
app.EndBlock(app.deliverState.ctx, abci.RequestEndBlock{}) | ||
require.Empty(t, app.deliverState.ctx.MultiStore().GetEvents()) | ||
app.SetDeliverStateToCommit() | ||
app.Commit(context.Background()) | ||
} | ||
} |
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
Oops, something went wrong.