Skip to content

Commit

Permalink
Merge branch 'cosmos/v0.47.x' of https://github.com/forbole/bdjuno in…
Browse files Browse the repository at this point in the history
…to chains/kyve/mainnet
  • Loading branch information
MonikaCat committed Feb 5, 2024
2 parents faf8c9e + 4590e64 commit 6cbfae3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Go 🧰
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "1.20"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Go 🧰
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "1.20"

Expand All @@ -47,6 +47,6 @@ jobs:

- name: Upload coverage 📤
if: "env.GIT_DIFF != ''"
uses: codecov/codecov-action@v4-beta
uses: codecov/codecov-action@v4.0.1
with:
file: ./coverage.txt
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/prometheus/client_golang v1.18.0
github.com/proullon/ramsql v0.1.3
github.com/rs/zerolog v1.31.0
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.60.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,8 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo=
github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
76 changes: 49 additions & 27 deletions modules/gov/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (

// HandleBlock implements modules.BlockModule
func (m *Module) HandleBlock(
b *tmctypes.ResultBlock, blockResults *tmctypes.ResultBlockResults, _ []*juno.Tx, _ *tmctypes.ResultValidators,
b *tmctypes.ResultBlock, blockResults *tmctypes.ResultBlockResults, txs []*juno.Tx, _ *tmctypes.ResultValidators,
) error {

err := m.updateProposalsStatus(b.Block.Height, blockResults.EndBlockEvents)
txEvents := collectTxEvents(txs)
err := m.updateProposalsStatus(b.Block.Height, txEvents, blockResults.EndBlockEvents)
if err != nil {
log.Error().Str("module", "gov").Int64("height", b.Block.Height).
Err(err).Msg("error while updating proposals")
Expand All @@ -28,35 +28,23 @@ func (m *Module) HandleBlock(
return nil
}

// updateProposalsStatus updates the status of proposals if they have been included in the EndBlockEvents
func (m *Module) updateProposalsStatus(height int64, events []abci.Event) error {
if len(events) == 0 {
return nil
}

// updateProposalsStatus updates the status of proposals if they have been included in the EndBlockEvents or status
// was changed from deposit to voting
func (m *Module) updateProposalsStatus(height int64, txEvents, endBlockEvents []abci.Event) error {
var ids []uint64
// check if EndBlockEvents contains active_proposal event
eventsList := juno.FindEventsByType(events, govtypes.EventTypeActiveProposal)
if len(eventsList) == 0 {
return nil
endBlockIDs, err := findProposalIDsInEvents(endBlockEvents, govtypes.EventTypeActiveProposal, govtypes.AttributeKeyProposalID)
if err != nil {
return err
}
ids = append(ids, endBlockIDs...)

for _, event := range eventsList {
// find proposal ID
proposalID, err := juno.FindAttributeByKey(event, govtypes.AttributeKeyProposalID)
if err != nil {
return fmt.Errorf("error while getting proposal ID from block events: %s", err)
}

// parse proposal ID from []byte to unit64
id, err := strconv.ParseUint(proposalID.Value, 10, 64)
if err != nil {
return fmt.Errorf("error while parsing proposal id: %s", err)
}

// add proposal ID to ids array
ids = append(ids, id)
// the proposal changes state from the deposit to voting
txIDs, err := findProposalIDsInEvents(txEvents, govtypes.EventTypeProposalDeposit, govtypes.AttributeKeyVotingPeriodStart)
if err != nil {
return err
}
ids = append(ids, txIDs...)

// update status for proposals IDs stored in ids array
for _, id := range ids {
Expand All @@ -68,3 +56,37 @@ func (m *Module) updateProposalsStatus(height int64, events []abci.Event) error

return nil
}

func findProposalIDsInEvents(events []abci.Event, eventType, attrKey string) ([]uint64, error) {
ids := make([]uint64, 0)
for _, event := range events {
if event.Type != eventType {
continue
}
for _, attr := range event.Attributes {
if attr.Key != attrKey {
continue
}
// parse proposal ID from []byte to unit64
id, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return nil, fmt.Errorf("error while parsing proposal id: %s", err)
}
// add proposal ID to ids array
ids = append(ids, id)
}
}

return ids, nil
}

func collectTxEvents(txs []*juno.Tx) []abci.Event {
events := make([]abci.Event, 0)
for _, tx := range txs {
for _, ev := range tx.Events {
events = append(events, abci.Event{Type: ev.Type, Attributes: ev.Attributes})
}
}

return events
}

0 comments on commit 6cbfae3

Please sign in to comment.