77 "fmt"
88 "github.com/deso-protocol/core/lib"
99 "github.com/deso-protocol/state-consumer/consumer"
10+ "github.com/golang/glog"
1011 "github.com/pkg/errors"
1112 "github.com/uptrace/bun"
1213 "time"
@@ -64,7 +65,7 @@ func ConvertUtxoOperationKeyToBlockHashHex(keyBytes []byte) string {
6465 return hex .EncodeToString (keyBytes [1 :])
6566}
6667
67- // PostBatchOperation is the entry point for processing a batch of post entries . It determines the appropriate handler
68+ // UtxoOperationBatchOperation is the entry point for processing a batch of utxo operations . It determines the appropriate handler
6869// based on the operation type and executes it.
6970func UtxoOperationBatchOperation (entries []* lib.StateChangeEntry , db * bun.DB , params * lib.DeSoParams ) error {
7071 // We check before we call this function that there is at least one operation type.
@@ -92,6 +93,7 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
9293 transactionUpdates := make ([]* PGTransactionEntry , 0 )
9394 affectedPublicKeys := make ([]* PGAffectedPublicKeyEntry , 0 )
9495 blockEntries := make ([]* PGBlockEntry , 0 )
96+ stakeRewardEntries := make ([]* PGStakeReward , 0 )
9597
9698 // Start timer to track how long it takes to insert the entries.
9799 start := time .Now ()
@@ -113,14 +115,18 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
113115 blockHash := ConvertUtxoOperationKeyToBlockHashHex (entry .KeyBytes )
114116
115117 // Check to see if the state change entry has an attached block.
116- // Note that this only happens during the iniltial sync, in order to speed up the sync process.
118+ // Note that this only happens during the initial sync, in order to speed up the sync process.
117119 if entry .Block != nil {
118120 insertTransactions = true
119121 block := entry .Block
120- blockEntry := BlockEncoderToPGStruct (block , entry .KeyBytes )
122+ blockEntry := BlockEncoderToPGStruct (block , entry .KeyBytes , params )
121123 blockEntries = append (blockEntries , blockEntry )
122124 for ii , txn := range block .Txns {
123- pgTxn , err := TransactionEncoderToPGStruct (txn , uint64 (ii ), blockEntry .BlockHash , blockEntry .Height , blockEntry .Timestamp , params )
125+ // Check if the transaction connects or not.
126+ txnConnects := blockEntry .Height < uint64 (params .ForkHeights .ProofOfStake2ConsensusCutoverBlockHeight ) ||
127+ ii == 0 || block .TxnConnectStatusByIndex .Get (ii - 1 )
128+ pgTxn , err := TransactionEncoderToPGStruct (
129+ txn , uint64 (ii ), blockEntry .BlockHash , blockEntry .Height , blockEntry .Timestamp , txnConnects , params )
124130 if err != nil {
125131 return errors .Wrapf (err , "entries.bulkInsertUtxoOperationsEntry: Problem converting transaction to PG struct" )
126132 }
@@ -169,7 +175,7 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
169175 if err != nil {
170176 return fmt .Errorf ("entries.bulkInsertUtxoOperationsEntry: Problem decoding transaction for entry %+v at block height %v" , entry , entry .BlockHeight )
171177 }
172- txIndexMetadata , err := consumer .ComputeTransactionMetadata (transaction , blockHash , & lib . DeSoMainnetParams , transaction .TxnFeeNanos , uint64 (jj ), utxoOps )
178+ txIndexMetadata , err := consumer .ComputeTransactionMetadata (transaction , blockHash , params , transaction .TxnFeeNanos , uint64 (jj ), utxoOps )
173179 if err != nil {
174180 return fmt .Errorf ("entries.bulkInsertUtxoOperationsEntry: Problem computing transaction metadata for entry %+v at block height %v" , entry , entry .BlockHeight )
175181 }
@@ -216,6 +222,27 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
216222 affectedPublicKeys = append (affectedPublicKeys , affectedPublicKeyEntry )
217223 }
218224 transactionUpdates = append (transactionUpdates , transactions [jj ])
225+ } else if jj == len (transactions ) {
226+ // TODO: parse utxo operations for the block level index.
227+ // Examples: deletion of expired nonces, staking rewards (restaked
228+ // + payed to balance), validator jailing, updating validator's
229+ // last active at epoch.
230+ for ii , utxoOp := range utxoOps {
231+ switch utxoOp .Type {
232+ case lib .OperationTypeStakeDistributionRestake , lib .OperationTypeStakeDistributionPayToBalance :
233+ stateChangeMetadata , ok := utxoOp .StateChangeMetadata .(* lib.StakeRewardStateChangeMetadata )
234+ if ! ok {
235+ glog .Error ("bulkInsertUtxoOperationsEntry: Problem with state change metadata for " +
236+ "stake rewards" )
237+ continue
238+ }
239+ stakeReward := PGStakeReward {
240+ StakeReward : StakeRewardEncoderToPGStruct (stateChangeMetadata , params , blockHash , uint64 (ii )),
241+ }
242+ stakeRewardEntries = append (stakeRewardEntries , & stakeReward )
243+ }
244+ }
245+
219246 }
220247 }
221248 // Print how long it took to insert the entries.
@@ -273,6 +300,18 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
273300 }
274301
275302 fmt .Printf ("entries.bulkInsertUtxoOperationsEntry: Inserted %v affected public keys in %v s\n " , len (affectedPublicKeys ), time .Since (start ))
303+
304+ start = time .Now ()
305+
306+ // Insert stake rewards into db
307+ if len (stakeRewardEntries ) > 0 {
308+ _ , err := db .NewInsert ().Model (& stakeRewardEntries ).On ("CONFLICT (block_hash, utxo_op_index) DO UPDATE" ).Exec (context .Background ())
309+ if err != nil {
310+ return errors .Wrapf (err , "InsertStakeRewards: Problem inserting stake rewards" )
311+ }
312+ }
313+ fmt .Printf ("entries.bulkInsertUtxoOperationsEntry: Inserted %v stake rewards in %v s\n " , len (stakeRewardEntries ), time .Since (start ))
314+
276315 return nil
277316}
278317
0 commit comments