diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 7f6901e14..fba316c05 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a96b85ea0..2bced486f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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" @@ -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 diff --git a/go.mod b/go.mod index 4e4a5cf95..5117101dc 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 2de5d5254..4341b831a 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/modules/gov/handle_block.go b/modules/gov/handle_block.go index e16e34164..55b3b9de3 100644 --- a/modules/gov/handle_block.go +++ b/modules/gov/handle_block.go @@ -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") @@ -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 { @@ -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 +}