Skip to content

Commit 6f1a638

Browse files
committed
Add new fields to block and transaction that are introduced w/ pos
1 parent 02e614c commit 6f1a638

7 files changed

Lines changed: 151 additions & 24 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

entries/block.go

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build relic
2+
13
package entries
24

35
import (
@@ -12,14 +14,23 @@ import (
1214
)
1315

1416
type BlockEntry struct {
15-
BlockHash string `pg:",pk,use_zero"`
16-
PrevBlockHash string
17-
TxnMerkleRoot string
18-
Timestamp time.Time
19-
Height uint64
20-
Nonce uint64
21-
ExtraNonce uint64
22-
BadgerKey []byte `pg:",use_zero"`
17+
BlockHash string `pg:",pk,use_zero"`
18+
PrevBlockHash string
19+
TxnMerkleRoot string
20+
Timestamp time.Time
21+
Height uint64
22+
Nonce uint64
23+
ExtraNonce uint64
24+
BlockVersion uint32
25+
TxnConnectStatusByIndexHash string `pg:",use_zero"`
26+
ProposerPublicKey string `pg:",use_zero"`
27+
ProposerVotingPublicKey string `pg:",use_zero"`
28+
ProposerRandomSeedSignature string `pg:",use_zero"`
29+
ProposedInView uint64
30+
ProposerVotePartialSignature string `pg:",use_zero"`
31+
// TODO: Quorum Certificates. Separate entry.
32+
33+
BadgerKey []byte `pg:",use_zero"`
2334
}
2435

2536
type PGBlockEntry struct {
@@ -28,18 +39,34 @@ type PGBlockEntry struct {
2839
}
2940

3041
// Convert the UserAssociation DeSo encoder to the PG struct used by bun.
31-
func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte) *PGBlockEntry {
42+
func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte, params *lib.DeSoParams) *PGBlockEntry {
3243
blockHash, _ := block.Hash()
44+
var txnConnectStatusByIndexHash string
45+
if block.Header.TxnConnectStatusByIndexHash != nil {
46+
txnConnectStatusByIndexHash = hex.EncodeToString(block.Header.TxnConnectStatusByIndexHash.ToBytes())
47+
}
48+
var proposerPublicKey string
49+
if block.Header.ProposerPublicKey != nil {
50+
proposerPublicKey = consumer.PublicKeyBytesToBase58Check(
51+
block.Header.ProposerPublicKey.ToBytes(), params)
52+
}
3353
return &PGBlockEntry{
3454
BlockEntry: BlockEntry{
35-
BlockHash: hex.EncodeToString(blockHash[:]),
36-
PrevBlockHash: hex.EncodeToString(block.Header.PrevBlockHash[:]),
37-
TxnMerkleRoot: hex.EncodeToString(block.Header.TransactionMerkleRoot[:]),
38-
Timestamp: consumer.UnixNanoToTime(uint64(block.Header.TstampNanoSecs)),
39-
Height: block.Header.Height,
40-
Nonce: block.Header.Nonce,
41-
ExtraNonce: block.Header.ExtraNonce,
42-
BadgerKey: keyBytes,
55+
BlockHash: hex.EncodeToString(blockHash[:]),
56+
PrevBlockHash: hex.EncodeToString(block.Header.PrevBlockHash[:]),
57+
TxnMerkleRoot: hex.EncodeToString(block.Header.TransactionMerkleRoot[:]),
58+
Timestamp: consumer.UnixNanoToTime(uint64(block.Header.TstampNanoSecs)),
59+
Height: block.Header.Height,
60+
Nonce: block.Header.Nonce,
61+
ExtraNonce: block.Header.ExtraNonce,
62+
BlockVersion: block.Header.Version,
63+
TxnConnectStatusByIndexHash: txnConnectStatusByIndexHash,
64+
ProposerPublicKey: proposerPublicKey,
65+
ProposerVotingPublicKey: block.Header.ProposerVotingPublicKey.ToString(),
66+
ProposerRandomSeedSignature: block.Header.ProposerRandomSeedSignature.ToString(),
67+
ProposedInView: block.Header.ProposedInView,
68+
ProposerVotePartialSignature: block.Header.ProposerVotePartialSignature.ToString(),
69+
BadgerKey: keyBytes,
4370
},
4471
}
4572
}
@@ -77,10 +104,16 @@ func bulkInsertBlockEntry(entries []*lib.StateChangeEntry, db *bun.DB, operation
77104

78105
for _, entry := range uniqueBlocks {
79106
block := entry.Encoder.(*lib.MsgDeSoBlock)
80-
blockEntry := BlockEncoderToPGStruct(block, entry.KeyBytes)
107+
blockEntry := BlockEncoderToPGStruct(block, entry.KeyBytes, params)
81108
pgBlockEntrySlice = append(pgBlockEntrySlice, blockEntry)
82109
for jj, transaction := range block.Txns {
83-
pgTransactionEntry, err := TransactionEncoderToPGStruct(transaction, uint64(jj), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, params)
110+
// Check if the transaction connects or not.
111+
txnConnects := blockEntry.Height < uint64(params.ForkHeights.ProofOfStake2ConsensusCutoverBlockHeight) ||
112+
jj == 0 || block.TxnConnectStatusByIndex.Get(jj-1)
113+
pgTransactionEntry, err := TransactionEncoderToPGStruct(
114+
transaction, uint64(jj), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, txnConnects,
115+
params,
116+
)
84117
if err != nil {
85118
return errors.Wrapf(err, "entries.bulkInsertBlockEntry: Problem converting transaction to PG struct")
86119
}

entries/transaction.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,24 @@ type TransactionEntry struct {
3333
IndexInBlock uint64
3434
BlockHeight uint64
3535
Timestamp time.Time `pg:",use_zero"`
36-
BadgerKey []byte `pg:",use_zero"`
36+
Connects bool
37+
BadgerKey []byte `pg:",use_zero"`
3738
}
3839

3940
type PGTransactionEntry struct {
4041
bun.BaseModel `bun:"table:transaction_partitioned"`
4142
TransactionEntry
4243
}
4344

44-
func TransactionEncoderToPGStruct(transaction *lib.MsgDeSoTxn, blockIndex uint64, blockHash string, blockHeight uint64, timestamp time.Time, params *lib.DeSoParams) (*PGTransactionEntry, error) {
45+
func TransactionEncoderToPGStruct(
46+
transaction *lib.MsgDeSoTxn,
47+
blockIndex uint64,
48+
blockHash string,
49+
blockHeight uint64,
50+
timestamp time.Time,
51+
connects bool,
52+
params *lib.DeSoParams,
53+
) (*PGTransactionEntry, error) {
4554

4655
var txInputs []map[string]string
4756
for _, input := range transaction.TxInputs {
@@ -86,6 +95,7 @@ func TransactionEncoderToPGStruct(transaction *lib.MsgDeSoTxn, blockIndex uint64
8695
IndexInBlock: blockIndex,
8796
BlockHeight: blockHeight,
8897
Timestamp: timestamp,
98+
Connects: connects,
8999
BadgerKey: transaction.Hash()[:],
90100
},
91101
}
@@ -127,7 +137,9 @@ func transformTransactionEntry(entries []*lib.StateChangeEntry, params *lib.DeSo
127137

128138
for _, entry := range uniqueTransactions {
129139
transaction := entry.Encoder.(*lib.MsgDeSoTxn)
130-
transactionEntry, err := TransactionEncoderToPGStruct(transaction, 0, "", 0, time.Now(), params)
140+
// Assuming transactions connect when using this function. We can only
141+
// tell if a transaction connects or not if we have the block.
142+
transactionEntry, err := TransactionEncoderToPGStruct(transaction, 0, "", 0, time.Now(), true, params)
131143
if err != nil {
132144
return nil, errors.Wrapf(err, "entries.transformAndBulkInsertTransactionEntry: Problem converting transaction to PG struct")
133145
}

entries/utxo_operation.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,14 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
117117
if entry.Block != nil {
118118
insertTransactions = true
119119
block := entry.Block
120-
blockEntry := BlockEncoderToPGStruct(block, entry.KeyBytes)
120+
blockEntry := BlockEncoderToPGStruct(block, entry.KeyBytes, params)
121121
blockEntries = append(blockEntries, blockEntry)
122122
for ii, txn := range block.Txns {
123-
pgTxn, err := TransactionEncoderToPGStruct(txn, uint64(ii), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, params)
123+
// Check if the transaction connects or not.
124+
txnConnects := blockEntry.Height < uint64(params.ForkHeights.ProofOfStake2ConsensusCutoverBlockHeight) ||
125+
ii == 0 || block.TxnConnectStatusByIndex.Get(ii-1)
126+
pgTxn, err := TransactionEncoderToPGStruct(
127+
txn, uint64(ii), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, txnConnects, params)
124128
if err != nil {
125129
return errors.Wrapf(err, "entries.bulkInsertUtxoOperationsEntry: Problem converting transaction to PG struct")
126130
}
@@ -216,6 +220,11 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
216220
affectedPublicKeys = append(affectedPublicKeys, affectedPublicKeyEntry)
217221
}
218222
transactionUpdates = append(transactionUpdates, transactions[jj])
223+
} else if jj == len(transactions) {
224+
// TODO: parse utxo operations for the block level index.
225+
// Examples: deletion of expired nonces, staking rewards (restaked
226+
// + payed to balance), validator jailing, updating validator's
227+
// last active at epoch.
219228
}
220229
}
221230
// Print how long it took to insert the entries.

migrations/.DS_Store

6 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package initial_migrations
2+
3+
import (
4+
"context"
5+
"github.com/uptrace/bun"
6+
)
7+
8+
// TODO: Not nullable fields
9+
func updateBlockTableWithPoSFields(db *bun.DB, tableName string) error {
10+
_, err := db.Exec(`
11+
ALTER TABLE block
12+
ADD COLUMN block_version BIGINT,
13+
ADD COLUMN txn_connect_status_by_index_hash VARCHAR,
14+
ADD COLUMN proposer_public_key VARCHAR,
15+
ADD COLUMN proposer_voting_public_key VARCHAR,
16+
ADD COLUMN proposer_random_seed_signature VARCHAR,
17+
ADD COLUMN proposed_in_view BIGINT,
18+
ADD COLUMN proposer_vote_partial_signature VARCHAR;
19+
`)
20+
// TODO: What other fields do we need indexed?
21+
return err
22+
}
23+
24+
func init() {
25+
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
26+
return updateBlockTableWithPoSFields(db, "block")
27+
}, func(ctx context.Context, db *bun.DB) error {
28+
_, err := db.Exec(`
29+
ALTER TABLE block
30+
DROP COLUMN block_version,
31+
DROP COLUMN txn_connect_status_by_index_hash,
32+
DROP COLUMN proposer_public_key,
33+
DROP COLUMN proposer_voting_public_key,
34+
DROP COLUMN proposer_random_seed_signature,
35+
DROP COLUMN proposed_in_view,
36+
DROP COLUMN proposer_vote_partial_signature;
37+
`)
38+
if err != nil {
39+
return err
40+
}
41+
return nil
42+
})
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package initial_migrations
2+
3+
import (
4+
"context"
5+
"github.com/uptrace/bun"
6+
)
7+
8+
func updateTransactionTableWithPoSFields(db *bun.DB) error {
9+
_, err := db.Exec(`
10+
ALTER TABLE transaction_partitioned
11+
ADD COLUMN connects BOOLEAN DEFAULT TRUE NOT NULL;
12+
`)
13+
// TODO: What other fields do we need indexed?
14+
return err
15+
}
16+
17+
func init() {
18+
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
19+
return updateTransactionTableWithPoSFields(db)
20+
}, func(ctx context.Context, db *bun.DB) error {
21+
_, err := db.Exec(`
22+
ALTER TABLE transaction_partitioned
23+
DROP COLUMN connects;
24+
`)
25+
if err != nil {
26+
return err
27+
}
28+
return nil
29+
})
30+
}

0 commit comments

Comments
 (0)