-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathblock.go
More file actions
291 lines (254 loc) · 10.3 KB
/
block.go
File metadata and controls
291 lines (254 loc) · 10.3 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package entries
import (
"context"
"encoding/hex"
"reflect"
"time"
"github.com/deso-protocol/core/lib"
"github.com/deso-protocol/state-consumer/consumer"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
type BlockEntry struct {
BlockHash string `pg:",pk,use_zero"`
PrevBlockHash string
TxnMerkleRoot string
Timestamp time.Time
Height uint64
Nonce uint64
ExtraNonce uint64
BlockVersion uint32
ProposerVotingPublicKey string `pg:",use_zero"`
ProposerRandomSeedSignature string `pg:",use_zero"`
ProposedInView uint64
ProposerVotePartialSignature string `pg:",use_zero"`
// TODO: Quorum Certificates. Separate entry.
BadgerKey []byte `pg:",use_zero"`
}
type PGBlockEntry struct {
bun.BaseModel `bun:"table:block"`
BlockEntry
}
type BlockSigner struct {
BlockHash string
SignerIndex uint64
}
type PGBlockSigner struct {
bun.BaseModel `bun:"table:block_signer"`
BlockSigner
}
// Convert the UserAssociation DeSo encoder to the PG struct used by bun.
func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte, params *lib.DeSoParams) (*PGBlockEntry, []*PGBlockSigner) {
blockHash, _ := block.Hash()
blockHashHex := hex.EncodeToString(blockHash[:])
qc := block.Header.GetQC()
blockSigners := []*PGBlockSigner{}
if !isInterfaceNil(qc) {
aggSig := qc.GetAggregatedSignature()
if !isInterfaceNil(aggSig) {
signersList := aggSig.GetSignersList()
for ii := 0; ii < signersList.Size(); ii++ {
// Skip signers that didn't sign.
if !signersList.Get(ii) {
continue
}
blockSigners = append(blockSigners, &PGBlockSigner{
BlockSigner: BlockSigner{
BlockHash: blockHashHex,
SignerIndex: uint64(ii),
},
})
}
}
}
return &PGBlockEntry{
BlockEntry: BlockEntry{
BlockHash: blockHashHex,
PrevBlockHash: hex.EncodeToString(block.Header.PrevBlockHash[:]),
TxnMerkleRoot: hex.EncodeToString(block.Header.TransactionMerkleRoot[:]),
Timestamp: consumer.UnixNanoToTime(uint64(block.Header.TstampNanoSecs)),
Height: block.Header.Height,
Nonce: block.Header.Nonce,
ExtraNonce: block.Header.ExtraNonce,
BlockVersion: block.Header.Version,
ProposerVotingPublicKey: block.Header.ProposerVotingPublicKey.ToString(),
ProposerRandomSeedSignature: block.Header.ProposerRandomSeedSignature.ToString(),
ProposedInView: block.Header.ProposedInView,
ProposerVotePartialSignature: block.Header.ProposerVotePartialSignature.ToString(),
BadgerKey: keyBytes,
},
}, blockSigners
}
// PostBatchOperation is the entry point for processing a batch of post entries. It determines the appropriate handler
// based on the operation type and executes it.
func BlockBatchOperation(entries []*lib.StateChangeEntry, db bun.IDB, params *lib.DeSoParams) error {
// We check before we call this function that there is at least one operation type.
// We also ensure before this that all entries have the same operation type.
operationType := entries[0].OperationType
var err error
if operationType == lib.DbOperationTypeDelete {
err = bulkDeleteBlockEntry(entries, db, operationType)
} else {
err = bulkInsertBlockEntry(entries, db, operationType, params)
}
if err != nil {
return errors.Wrapf(err, "entries.PostBatchOperation: Problem with operation type %v", operationType)
}
return nil
}
// bulkInsertUtxoOperationsEntry inserts a batch of user_association entries into the database.
func bulkInsertBlockEntry(entries []*lib.StateChangeEntry, db bun.IDB, operationType lib.StateSyncerOperationType, params *lib.DeSoParams) error {
// If this block is a part of the initial sync, skip it - it will be handled by the utxo operations.
if operationType == lib.DbOperationTypeInsert {
return nil
}
// Track the unique entries we've inserted so we don't insert the same entry twice.
uniqueBlocks := consumer.UniqueEntries(entries)
// Create a new array to hold the bun struct.
pgBlockEntrySlice := make([]*PGBlockEntry, 0)
pgTransactionEntrySlice := make([]*PGTransactionEntry, 0)
pgBlockSignersEntrySlice := make([]*PGBlockSigner, 0)
for _, entry := range uniqueBlocks {
block := entry.Encoder.(*lib.MsgDeSoBlock)
blockEntry, blockSigners := BlockEncoderToPGStruct(block, entry.KeyBytes, params)
pgBlockEntrySlice = append(pgBlockEntrySlice, blockEntry)
pgBlockSignersEntrySlice = append(pgBlockSignersEntrySlice, blockSigners...)
for jj, transaction := range block.Txns {
indexInBlock := uint64(jj)
pgTransactionEntry, err := TransactionEncoderToPGStruct(
transaction,
&indexInBlock,
blockEntry.BlockHash,
blockEntry.Height,
blockEntry.Timestamp,
nil,
nil,
params,
)
if err != nil {
return errors.Wrapf(err, "entries.bulkInsertBlockEntry: Problem converting transaction to PG struct")
}
pgTransactionEntrySlice = append(pgTransactionEntrySlice, pgTransactionEntry)
if transaction.TxnMeta.GetTxnType() != lib.TxnTypeAtomicTxnsWrapper {
continue
}
innerTxns, err := parseInnerTxnsFromAtomicTxn(pgTransactionEntry, params)
if err != nil {
return errors.Wrapf(err, "entries.bulkInsertBlockEntry: Problem parsing inner txns from atomic txn")
}
pgTransactionEntrySlice = append(pgTransactionEntrySlice, innerTxns...)
}
}
blockQuery := db.NewInsert().Model(&pgBlockEntrySlice)
if operationType == lib.DbOperationTypeUpsert {
blockQuery = blockQuery.On("CONFLICT (block_hash) DO UPDATE")
}
if _, err := blockQuery.Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkInsertBlock: Error inserting entries")
}
if err := bulkInsertTransactionEntry(pgTransactionEntrySlice, db, operationType); err != nil {
return errors.Wrapf(err, "entries.bulkInsertBlock: Error inserting transaction entries")
}
if len(pgBlockSignersEntrySlice) > 0 {
// Execute the insert query.
query := db.NewInsert().Model(&pgBlockSignersEntrySlice)
if operationType == lib.DbOperationTypeUpsert {
query = query.On("CONFLICT (block_hash, signer_index) DO UPDATE")
}
if _, err := query.Returning("").Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkInsertBlockEntry: Error inserting block signers")
}
}
return nil
}
// bulkDeleteBlockEntry deletes a batch of block entries from the database.
func bulkDeleteBlockEntry(entries []*lib.StateChangeEntry, db bun.IDB, operationType lib.StateSyncerOperationType) error {
// Track the unique entries we've inserted so we don't insert the same entry twice.
uniqueEntries := consumer.UniqueEntries(entries)
// Transform the entries into a list of keys to delete.
keysToDelete := consumer.KeysToDelete(uniqueEntries)
return bulkDeleteBlockEntriesFromKeysToDelete(db, keysToDelete)
}
// bulkDeleteBlockEntriesFromKeysToDelete deletes a batch of block entries from the database.
// It also deletes any transactions and utxo operations associated with the block.
func bulkDeleteBlockEntriesFromKeysToDelete(db bun.IDB, keysToDelete [][]byte) error {
// Execute the delete query on the blocks table.
if _, err := db.NewDelete().
Model(&PGBlockEntry{}).
Where("badger_key IN (?)", bun.In(keysToDelete)).
Returning("").
Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkDeleteBlockEntry: Error deleting entries")
}
// Get block hashes from keys to delete.
blockHashHexesToDelete := make([]string, len(keysToDelete))
for ii, keyToDelete := range keysToDelete {
blockHashHexesToDelete[ii] = hex.EncodeToString(consumer.GetBlockHashBytesFromKey(keyToDelete))
}
// Delete any transactions associated with the block.
if _, err := db.NewDelete().
Model(&PGTransactionEntry{}).
Where("block_hash IN (?)", bun.In(blockHashHexesToDelete)).
Returning("").
Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkDeleteBlockEntry: Error deleting transaction entries")
}
// Delete any utxo operations associated with the block.
if _, err := db.NewDelete().
Model(&PGUtxoOperationEntry{}).
Where("block_hash IN (?)", bun.In(blockHashHexesToDelete)).
Returning("").
Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkDeleteBlockEntry: Error deleting utxo operation entries")
}
// Delete any signers associated with the block.
if _, err := db.NewDelete().
Model(&PGBlockSigner{}).
Where("block_hash IN (?)", bun.In(blockHashHexesToDelete)).
Returning("").
Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkDeleteBlockEntry: Error deleting block signers")
}
// Delete any stake rewards associated with the block.
if _, err := db.NewDelete().
Model(&PGStakeReward{}).
Where("block_hash IN (?)", bun.In(blockHashHexesToDelete)).
Returning("").
Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkDeleteBlockEntry: Error deleting stake rewards")
}
return nil
}
// golang interface types are stored as a tuple of (type, value). A single i==nil check is not enough to
// determine if a pointer that implements an interface is nil. This function checks if the interface is nil
// by checking if the pointer itself is nil.
func isInterfaceNil(i interface{}) bool {
if i == nil {
return true
}
value := reflect.ValueOf(i)
return value.Kind() == reflect.Ptr && value.IsNil()
}
func BlockNodeOperation(entries []*lib.StateChangeEntry, db bun.IDB, params *lib.DeSoParams) error {
operationType := entries[0].OperationType
if operationType == lib.DbOperationTypeDelete {
// This should NEVER happen.
return errors.New("BlockNodeOperation: Delete operation not supported")
}
uniqueBlockNodes := consumer.UniqueEntries(entries)
blockHashesToDelete := []*lib.BlockHash{}
for _, entry := range uniqueBlockNodes {
blockNode := entry.Encoder.(*lib.BlockNode)
if !blockNode.IsCommitted() {
blockHashesToDelete = append(blockHashesToDelete, blockNode.Hash)
}
}
if len(blockHashesToDelete) == 0 {
return nil
}
blockKeysToDelete := make([][]byte, len(blockHashesToDelete))
for ii, blockHashToDelete := range blockHashesToDelete {
blockKeysToDelete[ii] = lib.BlockHashToBlockKey(blockHashToDelete)
}
return bulkDeleteBlockEntriesFromKeysToDelete(db, blockKeysToDelete)
}