forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 30
/
abci.go
84 lines (66 loc) · 2.32 KB
/
abci.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
79
80
81
82
83
84
package simapp
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type (
// VoteExtensionHandler defines a dummy vote extension handler for SimApp.
//
// NOTE: This implementation is solely used for testing purposes. DO NOT use
// in a production application!
VoteExtensionHandler struct{}
// VoteExtension defines the structure used to create a dummy vote extension.
VoteExtension struct {
Hash []byte
Height int64
Data []byte
}
)
func NewVoteExtensionHandler() *VoteExtensionHandler {
return &VoteExtensionHandler{}
}
func (h *VoteExtensionHandler) SetHandlers(bApp *baseapp.BaseApp) {
bApp.SetExtendVoteHandler(h.ExtendVote())
bApp.SetVerifyVoteExtensionHandler(h.VerifyVoteExtension())
}
func (h *VoteExtensionHandler) ExtendVote() sdk.ExtendVoteHandler {
return func(_ sdk.Context, req *abci.ExtendVoteRequest) (*abci.ExtendVoteResponse, error) {
buf := make([]byte, 1024)
_, err := rand.Read(buf)
if err != nil {
return nil, fmt.Errorf("failed to generate random vote extension data: %w", err)
}
ve := VoteExtension{
Hash: req.Hash,
Height: req.Height,
Data: buf,
}
bz, err := json.Marshal(ve)
if err != nil {
return nil, fmt.Errorf("failed to encode vote extension: %w", err)
}
return &abci.ExtendVoteResponse{VoteExtension: bz}, nil
}
}
func (h *VoteExtensionHandler) VerifyVoteExtension() sdk.VerifyVoteExtensionHandler {
return func(ctx sdk.Context, req *abci.VerifyVoteExtensionRequest) (*abci.VerifyVoteExtensionResponse, error) {
var ve VoteExtension
if err := json.Unmarshal(req.VoteExtension, &ve); err != nil {
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_REJECT}, nil
}
switch {
case req.Height != ve.Height:
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_REJECT}, nil
case !bytes.Equal(req.Hash, ve.Hash):
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_REJECT}, nil
case len(ve.Data) != 1024:
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_REJECT}, nil
}
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_ACCEPT}, nil
}
}