Skip to content

Commit

Permalink
abci logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Dec 12, 2024
1 parent 84eeacd commit 5e5ec93
Show file tree
Hide file tree
Showing 14 changed files with 558 additions and 65 deletions.
15 changes: 14 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ import (
gasestimatekeeper "github.com/ojo-network/ojo/x/gasestimate/keeper"
gasestimatetypes "github.com/ojo-network/ojo/x/gasestimate/types"

symbiotickeeper "github.com/ojo-network/ojo/x/symbiotic/keeper"
symbiotictypes "github.com/ojo-network/ojo/x/symbiotic/types"

"github.com/ojo-network/ojo/x/airdrop"
airdropkeeper "github.com/ojo-network/ojo/x/airdrop/keeper"
airdroptypes "github.com/ojo-network/ojo/x/airdrop/types"
Expand Down Expand Up @@ -150,6 +153,7 @@ var (
oracletypes.ModuleName: {authtypes.Minter},
gmptypes.ModuleName: {authtypes.Minter},
gasestimatetypes.ModuleName: {authtypes.Burner},
symbiotictypes.ModuleName: {authtypes.Minter},
airdroptypes.ModuleName: {authtypes.Minter},
}
)
Expand Down Expand Up @@ -208,6 +212,7 @@ type App struct {
GmpKeeper gmpkeeper.Keeper
GasEstimateKeeper gasestimatekeeper.Keeper
AirdropKeeper airdropkeeper.Keeper
SymbioticKeeper symbiotickeeper.Keeper
ConsensusParamsKeeper consensusparamkeeper.Keeper

// make scoped keepers public for test purposes
Expand Down Expand Up @@ -270,7 +275,7 @@ func New(
govtypes.StoreKey, paramstypes.StoreKey, ibcexported.StoreKey, upgradetypes.StoreKey,
feegrant.StoreKey, evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey,
consensusparamtypes.StoreKey, group.StoreKey, oracletypes.StoreKey, gmptypes.StoreKey,
gasestimatetypes.ModuleName, airdroptypes.StoreKey,
gasestimatetypes.ModuleName, airdroptypes.StoreKey, symbiotictypes.StoreKey,
)
tkeys := storetypes.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := storetypes.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -421,6 +426,13 @@ func New(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.SymbioticKeeper = symbiotickeeper.NewKeeper(
appCodec,
keys[symbiotictypes.ModuleName],
app.StakingKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.OracleKeeper = oraclekeeper.NewKeeper(
appCodec,
keys[oracletypes.ModuleName],
Expand All @@ -430,6 +442,7 @@ func New(
app.DistrKeeper,
app.StakingKeeper,
app.GasEstimateKeeper,
app.SymbioticKeeper,
distrtypes.ModuleName,
cast.ToBool(appOpts.Get("telemetry.enabled")),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Expand Down
8 changes: 8 additions & 0 deletions app/preblocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ojo-network/ojo/x/oracle/types"

gasestimatetypes "github.com/ojo-network/ojo/x/gasestimate/types"
symbiotictypes "github.com/ojo-network/ojo/x/symbiotic/types"
)

// PreBlocker is run before finalize block to update the aggregrate exchange rate votes on the oracle module
Expand Down Expand Up @@ -67,6 +68,13 @@ func (app *App) PreBlocker(ctx sdk.Context, req *cometabci.RequestFinalizeBlock)
})
}
app.Logger().Info("gas estimates updated", "gasestimates", injectedVoteExtTx.GasEstimateMedians)

currentBlockHash := app.OracleKeeper.SymbioticKeeper.TallyBlockHashVotes(ctx, injectedVoteExtTx.BlockHashVotes)
cachedBlockHash := symbiotictypes.CachedBlockHash{
BlockHash: currentBlockHash,
Height: req.Height,
}
app.OracleKeeper.SymbioticKeeper.SetCachedBlockHash(ctx, cachedBlockHash)
}

app.Logger().Info(
Expand Down
10 changes: 10 additions & 0 deletions proto/ojo/oracle/v1/abci.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ message OracleVoteExtension {
(gogoproto.nullable) = false
];
repeated GasEstimate gas_estimates = 3 [(gogoproto.nullable) = false];
string block_hash = 4;
}

// InjectedVoteExtensionTx defines the vote extension tx injected by the prepare
Expand All @@ -28,10 +29,19 @@ message InjectedVoteExtensionTx {
];
bytes extended_commit_info = 2;
repeated GasEstimate gas_estimate_medians = 3 [(gogoproto.nullable) = false];
repeated BlockHashVote block_hash_votes = 4 [(gogoproto.nullable) = false];
}

// GasEstimate defines a gas estimate for a given network.
message GasEstimate {
int64 gas_estimation = 1;
string network = 2;
}

// Struct for voting on the block hash to store.
message BlockHashVote {
// Block hash of cached block on chain.
string block_hash = 1;
// Validator address proposing block hash.
string voter = 2;
}
87 changes: 87 additions & 0 deletions x/oracle/abci/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ func (h *ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
if err != nil {
return &cometabci.ResponsePrepareProposal{Txs: make([][]byte, 0)}, err
}
blockHashVotes, err := h.generateBlockHashVotes(ctx, req.LocalLastCommit)
if err != nil {
return &cometabci.ResponsePrepareProposal{Txs: make([][]byte, 0)}, err
}
injectedVoteExtTx := oracletypes.InjectedVoteExtensionTx{
ExchangeRateVotes: exchangeRateVotes,
ExtendedCommitInfo: extendedCommitInfoBz,
GasEstimateMedians: medianGasEstimates,
BlockHashVotes: blockHashVotes,
}

bz, err := injectedVoteExtTx.Marshal()
Expand Down Expand Up @@ -184,6 +189,14 @@ func (h *ProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHandler {
if err := h.verifyMedianGasEstimations(injectedVoteExtTx.GasEstimateMedians, gasEstimateMedians); err != nil {
return &cometabci.ResponseProcessProposal{Status: cometabci.ResponseProcessProposal_REJECT}, err
}
// Verify proposer's block hash votes by computing the same ballot.
blockHashVotes, err := h.generateBlockHashVotes(ctx, extendedCommitInfo)
if err != nil {
return &cometabci.ResponseProcessProposal{Status: cometabci.ResponseProcessProposal_REJECT}, err
}
if err := h.verifyBlockHashVotes(injectedVoteExtTx.BlockHashVotes, blockHashVotes); err != nil {
return &cometabci.ResponseProcessProposal{Status: cometabci.ResponseProcessProposal_REJECT}, err
}
}

h.logger.Info(
Expand Down Expand Up @@ -360,3 +373,77 @@ func (h *ProposalHandler) verifyMedianGasEstimations(

return nil
}

func (h *ProposalHandler) generateBlockHashVotes(
ctx sdk.Context,
ci cometabci.ExtendedCommitInfo,
) (blockHashVotes []oracletypes.BlockHashVote, err error) {
for _, vote := range ci.Votes {
if vote.BlockIdFlag != cmtproto.BlockIDFlagCommit {
continue
}

var voteExt oracletypes.OracleVoteExtension
if err := voteExt.Unmarshal(vote.VoteExtension); err != nil {
h.logger.Error(
"failed to decode vote extension",
"err", err,
)
return nil, err
}

var valConsAddr sdk.ConsAddress
if err := valConsAddr.Unmarshal(vote.Validator.Address); err != nil {
h.logger.Error(
"failed to unmarshal validator consensus address",
"err", err,
)
return nil, err
}
val, err := h.stakingKeeper.GetValidatorByConsAddr(ctx, valConsAddr)
if err != nil {
h.logger.Error(
"failed to get consensus validator from staking keeper",
"err", err,
)
return nil, err
}
valAddr, err := sdk.ValAddressFromBech32(val.OperatorAddress)
if err != nil {
return nil, err
}

blockHashVote := oracletypes.BlockHashVote{
BlockHash: voteExt.BlockHash,
Voter: valAddr.String(),
}
blockHashVotes = append(blockHashVotes, blockHashVote)
}

// sort votes so they are verified in the same order in ProcessProposalHandler
sort.Slice(blockHashVotes, func(i, j int) bool {
return blockHashVotes[i].Voter < blockHashVotes[j].Voter
})

return blockHashVotes, nil
}

func (h *ProposalHandler) verifyBlockHashVotes(
injectedVotes []oracletypes.BlockHashVote,
generatedVotes []oracletypes.BlockHashVote,
) error {
if len(injectedVotes) != len(generatedVotes) {
return oracletypes.ErrNonEqualInjVotesLen
}

for i := range injectedVotes {
injectedVote := injectedVotes[i]
generatedVote := generatedVotes[i]

if injectedVote.Voter != generatedVote.Voter || injectedVote.BlockHash != generatedVote.BlockHash {
return oracletypes.ErrNonEqualInjVotesBlockHash
}
}

return nil
}
12 changes: 12 additions & 0 deletions x/oracle/abci/voteextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,22 @@ func (h *VoteExtensionHandler) ExtendVoteHandler() sdk.ExtendVoteHandler {
})
}

var blockHash string
symbioticParams := h.oracleKeeper.SymbioticKeeper.GetParams(ctx)
if req.Height%symbioticParams.SymbioticSyncPeriod == 0 {
blockHash, err = h.oracleKeeper.SymbioticKeeper.GetFinalizedBlockHash(ctx)

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This definition of blockHash is never used.
h.logger.Error(
"height", req.Height,
err.Error(),
)
return &cometabci.ResponseExtendVote{VoteExtension: []byte{}}, err
}

voteExt := types.OracleVoteExtension{
Height: req.Height,
ExchangeRates: filteredDecCoins,
GasEstimates: gasEstimates,
BlockHash: blockHash,
}

bz, err := voteExt.Marshal()
Expand Down
3 changes: 3 additions & 0 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Keeper struct {
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
GasEstimateKeeper types.GasEstimateKeeper
SymbioticKeeper types.SymbioticKeeper

PriceFeeder *pricefeeder.PriceFeeder

Expand All @@ -50,6 +51,7 @@ func NewKeeper(
distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper,
gasEstimateKeeper types.GasEstimateKeeper,
symbioticKeeper types.SymbioticKeeper,
distrName string,
telemetryEnabled bool,
authority string,
Expand All @@ -73,6 +75,7 @@ func NewKeeper(
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
GasEstimateKeeper: gasEstimateKeeper,
SymbioticKeeper: symbioticKeeper,
PriceFeeder: &pricefeeder.PriceFeeder{},
distrName: distrName,
telemetryEnabled: telemetryEnabled,
Expand Down
Loading

0 comments on commit 5e5ec93

Please sign in to comment.