Skip to content

Commit 18c8c0d

Browse files
committed
Add new fields to block and transaction that are introduced w/ pos
1 parent 3257aa2 commit 18c8c0d

5 files changed

Lines changed: 143 additions & 24 deletions

File tree

entries/block.go

Lines changed: 44 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 (
@@ -11,14 +13,23 @@ import (
1113
)
1214

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

2435
type PGBlockEntry struct {
@@ -27,18 +38,26 @@ type PGBlockEntry struct {
2738
}
2839

2940
// Convert the UserAssociation DeSo encoder to the PG struct used by bun.
30-
func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte) *PGBlockEntry {
41+
func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte, params *lib.DeSoParams) *PGBlockEntry {
3142
blockHash, _ := block.Hash()
3243
return &PGBlockEntry{
3344
BlockEntry: BlockEntry{
34-
BlockHash: hex.EncodeToString(blockHash[:]),
35-
PrevBlockHash: hex.EncodeToString(block.Header.PrevBlockHash[:]),
36-
TxnMerkleRoot: hex.EncodeToString(block.Header.TransactionMerkleRoot[:]),
37-
Timestamp: consumer.UnixNanoToTime(block.Header.TstampSecs * 1e9),
38-
Height: block.Header.Height,
39-
Nonce: block.Header.Nonce,
40-
ExtraNonce: block.Header.ExtraNonce,
41-
BadgerKey: keyBytes,
45+
BlockHash: hex.EncodeToString(blockHash[:]),
46+
PrevBlockHash: hex.EncodeToString(block.Header.PrevBlockHash[:]),
47+
TxnMerkleRoot: hex.EncodeToString(block.Header.TransactionMerkleRoot[:]),
48+
Timestamp: consumer.UnixNanoToTime(uint64(block.Header.TstampNanoSecs)),
49+
Height: block.Header.Height,
50+
Nonce: block.Header.Nonce,
51+
ExtraNonce: block.Header.ExtraNonce,
52+
BlockVersion: block.Header.Version,
53+
TxnConnectStatusByIndexHash: hex.EncodeToString(block.Header.TxnConnectStatusByIndexHash.ToBytes()),
54+
ProposerPublicKey: consumer.PublicKeyBytesToBase58Check(
55+
block.Header.ProposerPublicKey.ToBytes(), params),
56+
ProposerVotingPublicKey: block.Header.ProposerVotingPublicKey.ToString(),
57+
ProposerRandomSeedSignature: block.Header.ProposerRandomSeedSignature.ToString(),
58+
ProposedInView: block.Header.ProposedInView,
59+
ProposerVotePartialSignature: block.Header.ProposerVotePartialSignature.ToString(),
60+
BadgerKey: keyBytes,
4261
},
4362
}
4463
}
@@ -76,10 +95,16 @@ func bulkInsertBlockEntry(entries []*lib.StateChangeEntry, db *bun.DB, operation
7695

7796
for _, entry := range uniqueBlocks {
7897
block := entry.Encoder.(*lib.MsgDeSoBlock)
79-
blockEntry := BlockEncoderToPGStruct(block, entry.KeyBytes)
98+
blockEntry := BlockEncoderToPGStruct(block, entry.KeyBytes, params)
8099
pgBlockEntrySlice = append(pgBlockEntrySlice, blockEntry)
81100
for jj, transaction := range block.Txns {
82-
pgTransactionEntry, err := TransactionEncoderToPGStruct(transaction, uint64(jj), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, params)
101+
// Check if the transaction connects or not.
102+
txnConnects := blockEntry.Height < uint64(params.ForkHeights.ProofOfStake2ConsensusCutoverBlockHeight) ||
103+
jj == 0 || block.TxnConnectStatusByIndex.Get(jj-1)
104+
pgTransactionEntry, err := TransactionEncoderToPGStruct(
105+
transaction, uint64(jj), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, txnConnects,
106+
params,
107+
)
83108
if err != nil {
84109
return errors.Wrapf(err, "entries.bulkInsertBlockEntry: Problem converting transaction to PG struct")
85110
}

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.
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)