-
Notifications
You must be signed in to change notification settings - Fork 211
/
proposals.go
197 lines (174 loc) · 5.27 KB
/
proposals.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package proposals
import (
"fmt"
"io"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
)
// Get gets a proposal by a given ID.
func Get(db sql.Executor, id types.ProposalID) (proposal *types.Proposal, err error) {
if rows, err := db.Exec(`
select
ballots.pubkey,
ballots.ballot,
length(identities.proof),
proposals.id,
proposals.ballot_id,
proposals.tx_ids,
proposals.mesh_hash,
proposals.signature
from proposals
left join ballots on proposals.ballot_id = ballots.id
left join identities using(pubkey)
where proposals.id = ?1;`,
func(stmt *sql.Statement) {
stmt.BindBytes(1, id.Bytes())
}, func(stmt *sql.Statement) bool {
proposal, err = decodeProposal(stmt)
return true
}); err != nil {
return nil, fmt.Errorf("get %s: %w", id, err)
} else if rows == 0 {
return nil, fmt.Errorf("%w proposal ID %s", sql.ErrNotFound, id)
}
return proposal, err
}
// Has checks if a proposal exists by a given ID.
func Has(db sql.Executor, id types.ProposalID) (bool, error) {
rows, err := db.Exec("select 1 from proposals where id = ?1;",
func(stmt *sql.Statement) {
stmt.BindBytes(1, id.Bytes())
}, nil,
)
if err != nil {
return false, fmt.Errorf("exec id %v: %w", id, err)
}
return rows > 0, nil
}
// GetByLayer gets proposals by a given layer ID.
func GetByLayer(db sql.Executor, layerID types.LayerID) (proposals []*types.Proposal, err error) {
if rows, err := db.Exec(`
select
ballots.pubkey,
ballots.ballot,
length(identities.proof),
proposals.id,
proposals.ballot_id,
proposals.tx_ids,
proposals.mesh_hash,
proposals.signature
from proposals
left join ballots on proposals.ballot_id = ballots.id
left join identities using(pubkey)
where proposals.layer = ?1;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(layerID.Uint32()))
}, func(stmt *sql.Statement) bool {
proposal, decodeErr := decodeProposal(stmt)
if decodeErr != nil {
err = decodeErr
return true
}
proposals = append(proposals, proposal)
return true
}); err != nil {
return nil, fmt.Errorf("get %s: %w", layerID, err)
} else if rows == 0 {
return nil, fmt.Errorf("%w layer %s", sql.ErrNotFound, layerID)
}
return proposals, err
}
// GetBlob loads proposal as an encoded blob, ready to be sent over the wire.
func GetBlob(db sql.Executor, id []byte) (proposal []byte, err error) {
if rows, err := db.Exec(`select proposal from proposals where id = ?1;`,
func(stmt *sql.Statement) {
stmt.BindBytes(1, id)
}, func(stmt *sql.Statement) bool {
proposal = make([]byte, stmt.ColumnLen(0))
stmt.ColumnBytes(0, proposal)
return true
}); err != nil {
return nil, fmt.Errorf("exec %s: %w", types.BytesToHash(id), err)
} else if rows == 0 {
return nil, fmt.Errorf("%w proposal ID %s", sql.ErrNotFound, types.BytesToHash(id))
}
return proposal, err
}
// Add adds a proposal for a given ID.
func Add(db sql.Executor, proposal *types.Proposal) error {
txIDsBytes, err := codec.EncodeSlice(proposal.TxIDs)
if err != nil {
return fmt.Errorf("encode TX IDs: %w", err)
}
encodedProposal, err := codec.Encode(proposal)
if err != nil {
return fmt.Errorf("encode proposal: %w", err)
}
enc := func(stmt *sql.Statement) {
stmt.BindBytes(1, proposal.ID().Bytes())
stmt.BindBytes(2, proposal.Ballot.ID().Bytes())
stmt.BindInt64(3, int64(proposal.Layer.Uint32()))
stmt.BindBytes(4, txIDsBytes)
stmt.BindBytes(5, proposal.MeshHash.Bytes())
stmt.BindBytes(6, proposal.Signature.Bytes())
stmt.BindBytes(7, encodedProposal)
}
_, err = db.Exec(`
insert into proposals (id, ballot_id, layer, tx_ids, mesh_hash, signature, proposal)
values (?1, ?2, ?3, ?4, ?5, ?6, ?7);`, enc, nil)
if err != nil {
return fmt.Errorf("insert proposal ID %v: %w", proposal.ID(), err)
}
return nil
}
func decodeProposal(stmt *sql.Statement) (*types.Proposal, error) {
ballotID := types.BallotID{}
stmt.ColumnBytes(4, ballotID[:])
var nodeID types.NodeID
stmt.ColumnBytes(0, nodeID[:])
bodyBytes := make([]byte, stmt.ColumnLen(1))
stmt.ColumnBytes(1, bodyBytes[:])
ballot := types.Ballot{}
if err := codec.Decode(bodyBytes, &ballot); err != nil {
return nil, err
}
ballot.SetID(ballotID)
ballot.SmesherID = nodeID
if stmt.ColumnInt(2) > 0 {
ballot.SetMalicious()
}
proposalID := types.ProposalID{}
stmt.ColumnBytes(3, proposalID[:])
txIDsBytes := make([]byte, stmt.ColumnLen(5))
stmt.ColumnBytes(5, txIDsBytes)
meshBytes := make([]byte, stmt.ColumnLen(6))
stmt.ColumnBytes(6, meshBytes)
signature := types.EdSignature{}
stmt.ColumnBytes(7, signature[:])
txIDs, err := codec.DecodeSlice[types.TransactionID](txIDsBytes)
if err != nil {
if err != io.EOF {
return nil, fmt.Errorf("decode TX IDs: %w", err)
}
}
proposal := &types.Proposal{
InnerProposal: types.InnerProposal{
Ballot: ballot,
TxIDs: txIDs,
MeshHash: types.BytesToHash(meshBytes),
},
Signature: signature,
}
proposal.SetID(proposalID)
return proposal, nil
}
func DeleteBefore(db sql.Executor, lid types.LayerID) error {
if _, err := db.Exec(`delete from proposals where layer < ?1;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
}, nil); err != nil {
return fmt.Errorf("delete %s: %w", lid, err)
}
return nil
}