Skip to content

Commit

Permalink
feat(x/tally): fixed payout to committers when data request expires
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Jan 11, 2025
1 parent 60b8c2f commit 1e9f138
Show file tree
Hide file tree
Showing 15 changed files with 451 additions and 166 deletions.
9 changes: 7 additions & 2 deletions integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewIntegrationApp(
logger log.Logger,
keys map[string]*storetypes.KVStoreKey,
appCodec codec.Codec,
router *baseapp.MsgServiceRouter,
modules map[string]appmodule.AppModule,
) *IntegationApp {
db := dbm.NewMemDB()
Expand Down Expand Up @@ -78,8 +79,12 @@ func NewIntegrationApp(
return moduleManager.EndBlock(sdkCtx)
})

router := baseapp.NewMsgServiceRouter()
router.SetInterfaceRegistry(interfaceRegistry)
configurator := module.NewConfigurator(appCodec, router, bApp.GRPCQueryRouter())
err := moduleManager.RegisterServices(configurator)
if err != nil {
panic(err)
}
bApp.SetMsgServiceRouter(router)

if keys[consensusparamtypes.StoreKey] != nil {
Expand All @@ -104,7 +109,7 @@ func NewIntegrationApp(
}
}

_, err := bApp.Commit()
_, err = bApp.Commit()
if err != nil {
panic(err)
}
Expand Down
3 changes: 3 additions & 0 deletions proto/sedachain/tally/v1/tally.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ message Params {
// FilterGasCostMultiplierStdDev is the gas cost multiplier for a filter type
// stddev.
uint64 filter_gas_cost_multiplier_stddev = 4;
// GasCostCommitment is the gas cost for a commitment corresponding to an
// expired data request.
uint64 gas_cost_commitment = 5;
}
6 changes: 4 additions & 2 deletions x/batching/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -168,14 +169,15 @@ func initFixture(tb testing.TB) *fixture {
)

// x/wasm
router := baseapp.NewMsgServiceRouter()
wasmKeeper := wasmkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[wasmtypes.StoreKey]),
accountKeeper,
bankKeeper,
stakingKeeper,
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, router, nil,
tempDir,
wasmtypes.DefaultWasmConfig(),
wasmCapabilities,
Expand Down Expand Up @@ -238,7 +240,7 @@ func initFixture(tb testing.TB) *fixture {
pubKeyModule := pubkey.NewAppModule(cdc, pubKeyKeeper)
batchingModule := batching.NewAppModule(cdc, batchingKeeper)

integrationApp := integration.NewIntegrationApp(ctx, logger, keys, cdc, map[string]appmodule.AppModule{
integrationApp := integration.NewIntegrationApp(ctx, logger, keys, cdc, router, map[string]appmodule.AppModule{
authtypes.ModuleName: authModule,
banktypes.ModuleName: bankModule,
sdkstakingtypes.ModuleName: stakingModule,
Expand Down
25 changes: 11 additions & 14 deletions x/pubkey/keeper/endblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -48,7 +49,6 @@ import (
"github.com/sedaprotocol/seda-chain/x/pubkey/types"
"github.com/sedaprotocol/seda-chain/x/staking"
stakingkeeper "github.com/sedaprotocol/seda-chain/x/staking/keeper"
stakingtypes "github.com/sedaprotocol/seda-chain/x/staking/types"
"github.com/sedaprotocol/seda-chain/x/vesting"
)

Expand Down Expand Up @@ -157,19 +157,16 @@ func initFixture(tb testing.TB) *fixture {
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, pubKeyKeeper)
pubkeyModule := pubkey.NewAppModule(cdc, pubKeyKeeper)

integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{
authtypes.ModuleName: authModule,
banktypes.ModuleName: bankModule,
sdkstakingtypes.ModuleName: stakingModule,
types.ModuleName: pubkeyModule,
})

types.RegisterMsgServer(integrationApp.MsgServiceRouter(), keeper.NewMsgServerImpl(pubKeyKeeper))

sdkStakingMsgServer := sdkstakingkeeper.NewMsgServerImpl(sdkStakingKeeper)
stakingMsgServer := stakingkeeper.NewMsgServerImpl(sdkStakingMsgServer, stakingKeeper)
sdkstakingtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), stakingMsgServer)
stakingtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), stakingMsgServer)
integrationApp := integration.NewIntegrationApp(
newCtx, logger, keys, cdc,
baseapp.NewMsgServiceRouter(),
map[string]appmodule.AppModule{
authtypes.ModuleName: authModule,
banktypes.ModuleName: bankModule,
sdkstakingtypes.ModuleName: stakingModule,
types.ModuleName: pubkeyModule,
},
)

return &fixture{
IntegationApp: integrationApp,
Expand Down
54 changes: 24 additions & 30 deletions x/tally/keeper/endblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err

// Loop through the list to apply filter, execute tally, and post
// execution result.
processedReqs := make(map[string]types.DistributionMessages)
tallyResults := make([]TallyResult, len(tallyList))
sudoMsgs := make([]types.SudoRemoveDataRequest, len(tallyList))
dataResults := make([]batchingtypes.DataResult, len(tallyList))
for i, req := range tallyList {
dataResults[i] = batchingtypes.DataResult{
Expand All @@ -90,15 +90,27 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err
SedaPayload: req.SedaPayload,
}

var distMsgs types.DistributionMessages
var err error
switch {
case len(req.Commits) == 0 || len(req.Commits) < int(req.ReplicationFactor):
dataResults[i].Result = []byte(fmt.Sprintf("need %d commits; received %d", req.ReplicationFactor, len(req.Commits)))
dataResults[i].ExitCode = TallyExitCodeNotEnoughCommits
k.Logger(ctx).Info("data request's number of commits did not meet replication factor", "request_id", req.ID)

distMsgs, err = k.CalculateCommitterPayouts(ctx, req)
if err != nil {
return err
}
case len(req.Reveals) == 0 || len(req.Reveals) < int(req.ReplicationFactor):
dataResults[i].Result = []byte(fmt.Sprintf("need %d reveals; received %d", req.ReplicationFactor, len(req.Reveals)))
dataResults[i].ExitCode = TallyExitCodeNotEnoughReveals
k.Logger(ctx).Info("data request's number of reveals did not meet replication factor", "request_id", req.ID)

distMsgs, err = k.CalculateCommitterPayouts(ctx, req)
if err != nil {
return err
}
default:
tallyResults[i] = k.FilterAndTally(ctx, req)
dataResults[i].Result = tallyResults[i].result
Expand All @@ -109,31 +121,27 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err

k.Logger(ctx).Info("completed tally", "request_id", req.ID)
k.Logger(ctx).Debug("tally result", "request_id", req.ID, "tally_result", tallyResults[i])

// TODO
distMsgs = types.DistributionMessages{
Messages: []types.DistributionMessage{},
RefundType: types.DistributionTypeNoConsensus,
}
}

processedReqs[req.ID] = distMsgs
dataResults[i].Id, err = dataResults[i].TryHash()
if err != nil {
return err
}
sudoMsgs[i] = types.SudoRemoveDataRequest{ID: req.ID}
}

// Notify the Core Contract of tally completion.
msg, err := json.Marshal(struct {
SudoRemoveDataRequests struct {
Requests []types.SudoRemoveDataRequest `json:"requests"`
} `json:"remove_data_requests"`
}{
SudoRemoveDataRequests: struct {
Requests []types.SudoRemoveDataRequest `json:"requests"`
}{
Requests: sudoMsgs,
},
})
msg, err := types.MarshalSudoRemoveDataRequests(processedReqs)
if err != nil {
return err
}
postRes, err := k.wasmKeeper.Sudo(ctx, coreContract, msg)
_, err = k.wasmKeeper.Sudo(ctx, coreContract, msg)
if err != nil {
return err
}
Expand All @@ -144,14 +152,8 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err
if err != nil {
return err
}
}

for i := range sudoMsgs {
k.Logger(ctx).Info(
"tally flow completed",
"request_id", dataResults[i].DrId,
"post_result", postRes,
)
k.Logger(ctx).Info("tally flow completed", "request_id", dataResults[i].DrId)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeTallyCompletion,
Expand All @@ -167,6 +169,7 @@ func (k Keeper) ProcessTallies(ctx sdk.Context, coreContract sdk.AccAddress) err
),
)
}

return nil
}

Expand Down Expand Up @@ -243,12 +246,3 @@ func (k Keeper) logErrAndRet(ctx sdk.Context, baseErr, registeredErr error, req
k.Logger(ctx).Debug(baseErr.Error(), "request_id", req.ID, "error", registeredErr)
return registeredErr
}

// TODO: This will become more complex when we introduce incentives.
func calculateExecGasUsed(reveals []types.RevealBody) uint64 {
var execGasUsed uint64
for _, reveal := range reveals {
execGasUsed += reveal.GasUsed
}
return execGasUsed
}
98 changes: 88 additions & 10 deletions x/tally/keeper/endblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,103 @@ import (

"github.com/sedaprotocol/seda-wasm-vm/tallyvm/v2"

"github.com/sedaprotocol/seda-chain/x/tally/keeper"
"github.com/sedaprotocol/seda-chain/x/tally/keeper/testdata"
"github.com/sedaprotocol/seda-chain/x/tally/types"
)

func TestProcessTallies(t *testing.T) {
func TestEndBlock(t *testing.T) {
f := initFixture(t)

drID := f.commitRevealDataRequest(t, "c2VkYXByb3RvY29s")
tests := []struct {
name string
memo string
replicationFactor int
numCommits int
numReveals int
timeout bool
expExitCode uint32
}{
{
name: "full single commit-reveal",
memo: "YzJWamRYSmxaR0YwWVE9PQ==",
replicationFactor: 1,
numCommits: 1,
numReveals: 1,
timeout: false,
expExitCode: 0,
},
{
name: "full 5 commit-reveals",
memo: "ZnVsbCA1IGNvbW1pdC1yZXZlYWxz",
replicationFactor: 5,
numCommits: 5,
numReveals: 5,
timeout: false,
expExitCode: 0,
},
{
name: "commit timeout",
memo: "Y29tbWl0IHRpbWVvdXQ=",
replicationFactor: 2,
numCommits: 0,
numReveals: 0,
timeout: true,
expExitCode: keeper.TallyExitCodeNotEnoughCommits,
},
{
name: "commit timeout with 1 commit",
memo: "Y29tbWl0IHRpbWVvdXQgd2l0aCAxIGNvbW1pdA==",
replicationFactor: 2,
numCommits: 1,
numReveals: 0,
timeout: true,
expExitCode: keeper.TallyExitCodeNotEnoughCommits,
},
{
name: "commit timeout with 2 commits",
memo: "Y29tbWl0IHRpbWVvdXQgd2l0aCAyIGNvbW1pdHM=",
replicationFactor: 2,
numCommits: 1,
numReveals: 0,
timeout: true,
expExitCode: keeper.TallyExitCodeNotEnoughCommits,
},
{
name: "reveal timeout",
memo: "cmV2ZWFsIHRpbWVvdXQ=",
replicationFactor: 2,
numCommits: 2,
numReveals: 0,
timeout: true,
expExitCode: keeper.TallyExitCodeNotEnoughReveals,
},
{
name: "reveal timeout with 2 reveals",
memo: "cmV2ZWFsIHRpbWVvdXQgd2l0aCAyIHJldmVhbHM=",
replicationFactor: 3,
numCommits: 3,
numReveals: 2,
timeout: true,
expExitCode: keeper.TallyExitCodeNotEnoughReveals,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
drID := f.commitRevealDataRequest(t, tt.memo, tt.replicationFactor, tt.numCommits, tt.numReveals, tt.timeout)

err := f.tallyKeeper.ProcessTallies(f.Context(), f.coreContractAddr)
require.NoError(t, err)
err := f.tallyKeeper.EndBlock(f.Context())
require.NoError(t, err)

dataResult, err := f.batchingKeeper.GetLatestDataResult(f.Context(), drID)
require.NoError(t, err)
require.Equal(t, uint32(0), dataResult.ExitCode)
dataResult, err := f.batchingKeeper.GetLatestDataResult(f.Context(), drID)
require.NoError(t, err)
require.Equal(t, tt.expExitCode, dataResult.ExitCode)

dataResults, err := f.batchingKeeper.GetDataResults(f.Context(), false)
require.NoError(t, err)
require.Equal(t, *dataResult, dataResults[0])
dataResults, err := f.batchingKeeper.GetDataResults(f.Context(), false)
require.NoError(t, err)
require.Contains(t, dataResults, *dataResult)
})
}
}

// TestTallyVM tests tally VM using a sample tally wasm that performs
Expand Down
Loading

0 comments on commit 1e9f138

Please sign in to comment.