Skip to content

Commit a9dfe77

Browse files
committed
Add coinbase parsing
1 parent 06510cc commit a9dfe77

File tree

14 files changed

+238
-25
lines changed

14 files changed

+238
-25
lines changed

cmd/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,26 @@ func main() {
4141
}
4242

4343
// Initialize SQLite DB with GORM
44-
db, err := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
44+
// Enable WAL mode and optimize for concurrent reads
45+
dsn := fmt.Sprintf("%s?_journal_mode=WAL&_busy_timeout=5000&_synchronous=NORMAL&cache=shared", databasePath)
46+
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
4547
if err != nil {
4648
log.Fatalf("failed to connect to database [%s]: %v", databasePath, err)
4749
}
50+
51+
// Configure connection pool for SQLite
52+
sqlDB, err := db.DB()
53+
if err != nil {
54+
log.Fatalf("failed to get underlying SQL database: %v", err)
55+
}
56+
57+
// Set max open connections to 1 for SQLite (prevents database locked errors)
58+
// WAL mode allows multiple readers even with single writer
59+
sqlDB.SetMaxOpenConns(1)
60+
// Set max idle connections
61+
sqlDB.SetMaxIdleConns(1)
62+
// Set max lifetime of a connection
63+
sqlDB.SetConnMaxLifetime(time.Hour)
4864
// Auto-migrate all schemas
4965
err = model.MigrateAll(db)
5066
if err != nil {

pkg/model/bestblock.go renamed to pkg/model/best_block_request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"time"
55
)
66

7-
// BestBlockRequest represents a request for the best block information from a peer
7+
// BestBlockRequest represents a best block request message
88
type BestBlockRequest struct {
99
ID uint `gorm:"primaryKey"`
1010
Network string `gorm:"index;not null"`
1111
PeerID string `gorm:"index;not null"`
12-
ReceivedAt time.Time
12+
ReceivedAt time.Time `gorm:"index"`
1313
}

pkg/model/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Block struct {
1313
DataHubURL string `gorm:"type:text" json:"DataHubURL"`
1414
PeerID string `gorm:"index;not null" json:"PeerID"`
1515
Header string `gorm:"type:text" json:"Header"`
16-
ReceivedAt time.Time `json:"ReceivedAt"`
16+
ReceivedAt time.Time `gorm:"index" json:"ReceivedAt"`
1717
}
1818

1919
type BlockHeader struct {

pkg/model/handshake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ type Handshake struct {
1515
DataHubURL string `gorm:"type:text"`
1616
UserAgent string `gorm:"type:text"`
1717
Services uint64
18-
ReceivedAt time.Time
18+
ReceivedAt time.Time `gorm:"index"`
1919
}

pkg/model/message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ type Message struct {
99
Topic string `gorm:"index;not null"`
1010
Data string `gorm:"type:text;not null"`
1111
Peer string `gorm:"type:text"`
12-
ReceivedAt time.Time
12+
ReceivedAt time.Time `gorm:"index"`
1313
}

pkg/model/mining_on.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ type MiningOn struct {
1616
Miner string `gorm:"index"`
1717
SizeInBytes uint64
1818
TxCount uint64
19-
ReceivedAt time.Time
19+
ReceivedAt time.Time `gorm:"index"`
2020
}

pkg/model/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ func MigrateAll(db *gorm.DB) error {
88
&Message{}, // Keep existing generic message table
99
&Block{},
1010
&BlockHeader{}, // New table for parsed block headers
11+
&BestBlockRequest{},
1112
&MiningOn{},
1213
&Subtree{},
1314
&Handshake{},

pkg/model/rejected_tx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ type RejectedTx struct {
1111
TxID string `gorm:"index;not null"`
1212
Reason string `gorm:"type:text"`
1313
PeerID string `gorm:"index;not null"`
14-
ReceivedAt time.Time
14+
ReceivedAt time.Time `gorm:"index"`
1515
}

pkg/model/subtree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ type Subtree struct {
1111
Hash string `gorm:"index;not null"`
1212
DataHubURL string `gorm:"type:text"`
1313
PeerID string `gorm:"index;not null"`
14-
ReceivedAt time.Time
14+
ReceivedAt time.Time `gorm:"index"`
1515
}

pkg/parser/coinbase_parser.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package parser
22

33
import (
44
"encoding/hex"
5-
"fmt"
65
"strings"
76
"unicode"
87
)
@@ -37,10 +36,10 @@ func ParseCoinbaseScript(scriptHex string) string {
3736

3837
// Clean up the text
3938
result := strings.TrimSpace(text.String())
40-
39+
4140
// Remove excessive spaces
4241
result = strings.Join(strings.Fields(result), " ")
43-
42+
4443
return result
4544
}
4645

@@ -57,7 +56,7 @@ func ExtractMinerInfo(coinbaseText string) string {
5756
}
5857

5958
lowerText := strings.ToLower(coinbaseText)
60-
59+
6160
for poolName, patterns := range poolPatterns {
6261
for _, pattern := range patterns {
6362
if strings.Contains(lowerText, strings.ToLower(pattern)) {
@@ -87,18 +86,18 @@ func CalculateBlockReward(height uint32) uint64 {
8786
// BSV block reward schedule
8887
// Initial reward: 50 BSV = 5,000,000,000 satoshis
8988
// Halving every 210,000 blocks
90-
89+
9190
const initialReward = uint64(5000000000) // 50 BSV in satoshis
9291
const halvingInterval = uint32(210000)
93-
92+
9493
halvings := height / halvingInterval
95-
94+
9695
// Block reward halves every 210,000 blocks
9796
reward := initialReward
9897
for i := uint32(0); i < halvings; i++ {
9998
reward = reward / 2
10099
}
101-
100+
102101
return reward
103102
}
104103

@@ -157,10 +156,10 @@ func CleanCoinbaseText(text string) string {
157156
}
158157
return -1
159158
}, text)
160-
159+
161160
// Trim and remove excessive whitespace
162161
cleaned = strings.TrimSpace(cleaned)
163162
cleaned = strings.Join(strings.Fields(cleaned), " ")
164-
163+
165164
return cleaned
166-
}
165+
}

0 commit comments

Comments
 (0)