Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into remove-db
Browse files Browse the repository at this point in the history
  • Loading branch information
sorawit authored Jul 3, 2020
2 parents e873bb7 + 9e1f156 commit 1f48e99
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- (impv) [\#2121](https://github.com/bandprotocol/bandchain/pull/2121) Add handle edit validator msg for emitter
- (impv/chore) [\#2124](https://github.com/bandprotocol/bandchain/pull/2124) Add genesis ds and os and move same test logic to testapp.
- (impv) [\#2113](https://github.com/bandprotocol/bandchain/pull/2113) Add tests in types and deactivate event for activate flow.
- (impv) [\#2118](https://github.com/bandprotocol/bandchain/pull/2118) Implement emitter handle MsgDelegate, MsgUndelegate and MsgBeginRedelegate.
- (impv) [\#2109](https://github.com/bandprotocol/bandchain/pull/2109) Add Validator table and handle create validator message from genesis file and tx for emitter and flusher.
- (impv) [\#2093](https://github.com/bandprotocol/bandchain/pull/2093) Add missing pieces on app.go + some refactor and comments.
- (feat) [\#2114](https://github.com/bandprotocol/bandchain/pull/2114) Add more unit test coverage and enhance code comments in pkg.
Expand Down
7 changes: 7 additions & 0 deletions chain/emitter/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ func (app *App) handleMsg(txHash []byte, msg sdk.Msg, log sdk.ABCIMessageLog, ex
app.handleMsgCreateValidator(msg)
case staking.MsgEditValidator:
app.handleMsgEditValidator(msg)
case staking.MsgDelegate:
app.handleMsgDelegate(msg)
case staking.MsgUndelegate:
app.handleMsgUndelegate(msg)
case staking.MsgBeginRedelegate:
app.handleMsgBeginRedelegate(msg)
case bank.MsgSend:
app.handleMsgSend(msg)
case bank.MsgMultiSend:
app.handleMsgMultiSend(msg)
case dist.MsgWithdrawDelegatorReward:
app.handleMsgWithdrawDelegatorReward(txHash, msg, evMap, extra)

}
}

Expand Down
53 changes: 53 additions & 0 deletions chain/emitter/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@ func (app *App) emitSetValidator(addr sdk.ValAddress) {
})
}

func (app *App) emitUpdateValidator(addr sdk.ValAddress) {
val, _ := app.StakingKeeper.GetValidator(app.DeliverContext, addr)
currentReward, currentRatio := app.getCurrentRewardAndCurrentRatio(addr)
app.Write("UPDATE_VALIDATOR", JsDict{
"operator_address": addr.String(),
"tokens": val.Tokens.Uint64(),
"delegator_shares": val.DelegatorShares.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
})
}

func (app *App) emitDelegation(operatorAddress sdk.ValAddress, delegatorAddress sdk.AccAddress) {
delegation, found := app.StakingKeeper.GetDelegation(app.DeliverContext, delegatorAddress, operatorAddress)
if found {
info := app.DistrKeeper.GetDelegatorStartingInfo(app.DeliverContext, operatorAddress, delegatorAddress)
latestReward := app.DistrKeeper.GetValidatorHistoricalRewards(app.DeliverContext, operatorAddress, info.PreviousPeriod)
app.Write("SET_DELEGATION", JsDict{
"delegator_address": delegatorAddress,
"operator_address": operatorAddress,
"shares": delegation.Shares.String(),
"last_ratio": latestReward.CumulativeRewardRatio[0].Amount.String(),
})
} else {
app.Write("REMOVE_DELEGATION", JsDict{
"delegator_address": delegatorAddress,
"operator_address": operatorAddress,
})
}
}

// handleMsgCreateValidator implements emitter handler for MsgCreateValidator.
func (app *App) handleMsgCreateValidator(msg staking.MsgCreateValidator) {
app.emitSetValidator(msg.ValidatorAddress)
Expand All @@ -37,3 +68,25 @@ func (app *App) handleMsgCreateValidator(msg staking.MsgCreateValidator) {
func (app *App) handleMsgEditValidator(msg staking.MsgEditValidator) {
app.emitSetValidator(msg.ValidatorAddress)
}

func (app *App) emitUpdateValidatorAndDelegation(operatorAddress sdk.ValAddress, delegatorAddress sdk.AccAddress) {
app.emitUpdateValidator(operatorAddress)
app.emitDelegation(operatorAddress, delegatorAddress)
}

// handleMsgDelegate implements emitter handler for MsgDelegate
func (app *App) handleMsgDelegate(msg staking.MsgDelegate) {
app.emitUpdateValidatorAndDelegation(msg.ValidatorAddress, msg.DelegatorAddress)
}

// handleMsgUndelegate implements emitter handler for MsgUndelegate
func (app *App) handleMsgUndelegate(msg staking.MsgUndelegate) {
app.emitUpdateValidatorAndDelegation(msg.ValidatorAddress, msg.DelegatorAddress)

}

// handleMsgBeginRedelegate implements emitter handler for MsgBeginRedelegate
func (app *App) handleMsgBeginRedelegate(msg staking.MsgBeginRedelegate) {
app.emitUpdateValidatorAndDelegation(msg.ValidatorSrcAddress, msg.DelegatorAddress)
app.emitUpdateValidatorAndDelegation(msg.ValidatorDstAddress, msg.DelegatorAddress)
}
14 changes: 14 additions & 0 deletions flusher/flusher/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,17 @@ def Column(*args, **kwargs):
Column("current_reward", sa.DECIMAL),
Column("current_ratio", sa.DECIMAL),
)

delegations = sa.Table(
"delegations",
metadata,
Column("delegator_address", sa.String, sa.ForeignKey("accounts.address"), primary_key=True),
Column(
"operator_address",
sa.String,
sa.ForeignKey("validators.operator_address"),
primary_key=True,
),
Column("shares", sa.DECIMAL),
Column("last_ratio", sa.DECIMAL),
)
14 changes: 14 additions & 0 deletions flusher/flusher/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
reports,
raw_reports,
validators,
delegations,
)


Expand Down Expand Up @@ -73,3 +74,16 @@ def handle_update_validator(self, msg):
for col in validators.primary_key.columns.values():
condition = (col == msg[col.name]) & condition
self.conn.execute(validators.update().where(condition).values(**msg))

def handle_set_delegation(self, msg):
self.conn.execute(
insert(delegations)
.values(**msg)
.on_conflict_do_update(constraint="delegations_pkey", set_=msg)
)

def handle_remove_delegation(self, msg):
condition = True
for col in delegations.primary_key.columns.values():
condition = (col == msg[col.name]) & condition
self.conn.execute(delegations.delete().where(condition))

0 comments on commit 1f48e99

Please sign in to comment.