-
Notifications
You must be signed in to change notification settings - Fork 390
/
vote_ext.go
78 lines (64 loc) · 1.93 KB
/
vote_ext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package abci
import (
"context"
"encoding/json"
"fmt"
"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
auctiontypes "github.com/cosmos/sdk-tutorials/tutorials/nameservice/base/x/auction"
"github.com/cosmos/sdk-tutorials/tutorials/nameservice/base/x/auction/mempool"
)
func NewVoteExtensionHandler(lg log.Logger, mp *mempool.ThresholdMempool, cdc codec.Codec) *VoteExtHandler {
return &VoteExtHandler{
logger: lg,
mempool: mp,
cdc: cdc,
}
}
func (h *VoteExtHandler) ExtendVoteHandler() sdk.ExtendVoteHandler {
return func(ctx sdk.Context, req *abci.RequestExtendVote) (*abci.ResponseExtendVote, error) {
h.logger.Info(fmt.Sprintf("Extending votes at block height : %v", req.Height))
voteExtBids := [][]byte{}
// Get mempool txs
itr := h.mempool.SelectPending(context.Background(), nil)
for itr != nil {
tmptx := itr.Tx()
sdkMsgs := tmptx.GetMsgs()
// Iterate through msgs, check for any bids
for _, msg := range sdkMsgs {
switch msg := msg.(type) {
case *auctiontypes.MsgBid:
// Marshal sdk bids to []byte
bz, err := h.cdc.Marshal(msg)
if err != nil {
h.logger.Error(fmt.Sprintf("Error marshaling VE Bid : %v", err))
break
}
voteExtBids = append(voteExtBids, bz)
default:
}
}
// Move tx to ready pool
err := h.mempool.Update(context.Background(), tmptx)
//// Remove tx from app side mempool
// err = h.mempool.Remove(tmptx)
if err != nil {
h.logger.Info(fmt.Sprintf("Unable to update mempool tx: %v", err))
}
itr = itr.Next()
}
// Create vote extension
voteExt := AppVoteExtension{
Height: req.Height,
Bids: voteExtBids,
}
// Encode Vote Extension
bz, err := json.Marshal(voteExt)
if err != nil {
return nil, fmt.Errorf("error marshaling VE: %w", err)
}
return &abci.ResponseExtendVote{VoteExtension: bz}, nil
}
}