-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (109 loc) · 3.41 KB
/
main.go
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
package main
import (
"encoding/hex"
"fmt"
"math"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/igorcrevar/cardano-go-indexer/core"
"github.com/igorcrevar/cardano-go-indexer/db"
"github.com/hashicorp/go-hclog"
)
func main() {
networkMagic := uint32(42)
address := "localhost:3000" // "/tmp/cardano-133064331/node-spo1/node.sock"
startBlockHash := []byte(nil) // from genesis
startSlot := uint64(0)
startBlockNum := uint64(math.MaxUint64)
addressesOfInterest := []string{}
// for test net
address = "preprod-node.play.dev.cardano.org:3001"
networkMagic = 1
// for main net
address = "backbone.cardano-mainnet.iohk.io:3001"
networkMagic = uint32(764824073)
startBlockHash, _ = hex.DecodeString("5d9435abf2a829142aaae08720afa05980efaa6ad58e47ebd4cffadc2f3c45d8")
startSlot = uint64(76592549)
startBlockNum = 7999980
addressesOfInterest = []string{
"addr1v9kganeshgdqyhwnyn9stxxgl7r4y2ejfyqjn88n7ncapvs4sugsd",
}
logger, err := core.NewLogger(core.LoggerConfig{
LogLevel: hclog.Debug,
JSONLogFormat: false,
AppendFile: true,
LogFilePath: "logs/cardano_indexer.log",
})
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
dbs, err := db.NewDatabaseInit("", "burek.db")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
logger.Error("Open database failed", "err", err)
os.Exit(1)
}
defer dbs.Close()
confirmedBlockHandler := func(confirmedBlock *core.CardanoBlock, txs []*core.Tx) error {
logger.Info("Confirmed block",
"hash", hex.EncodeToString(confirmedBlock.Hash[:]), "slot", confirmedBlock.Slot,
"allTxs", len(confirmedBlock.Txs), "ourTxs", len(txs))
lastBlocks, err := dbs.GetLatestConfirmedBlocks(5)
if err != nil {
return err
}
lastBlocksInfo := make([]string, len(lastBlocks))
for i, x := range lastBlocks {
lastBlocksInfo[i] = fmt.Sprintf("(%d-%s)", x.Slot, hex.EncodeToString(x.Hash[:]))
}
logger.Debug("Last n blocks", "info", strings.Join(lastBlocksInfo, ", "))
unprocessedTxs, err := dbs.GetUnprocessedConfirmedTxs(0)
if err != nil {
return err
}
for _, tx := range unprocessedTxs {
logger.Info("Tx has been processed", "tx", tx)
}
return dbs.MarkConfirmedTxsProcessed(unprocessedTxs)
}
indexerConfig := &core.BlockIndexerConfig{
StartingBlockPoint: &core.BlockPoint{
BlockSlot: startSlot,
BlockHash: core.Hash(startBlockHash),
BlockNumber: startBlockNum,
},
AddressCheck: core.AddressCheckAll,
ConfirmationBlockCount: 10,
AddressesOfInterest: addressesOfInterest,
SoftDeleteUtxo: false,
KeepAllTxOutputsInDB: false,
KeepAllTxsHashesInBlock: false,
}
syncerConfig := &core.BlockSyncerConfig{
NetworkMagic: networkMagic,
NodeAddress: address,
RestartOnError: true,
RestartDelay: time.Second * 2,
KeepAlive: true,
}
indexer := core.NewBlockIndexer(indexerConfig, confirmedBlockHandler, dbs, logger.Named("block_indexer"))
syncer := core.NewBlockSyncer(syncerConfig, indexer, logger.Named("block_syncer"))
defer syncer.Close()
err = syncer.Sync()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
logger.Error("Start syncing failed", "err", err)
os.Exit(1)
}
signalChannel := make(chan os.Signal, 1)
// Notify the signalChannel when the interrupt signal is received (Ctrl+C)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
select {
case <-signalChannel:
case <-syncer.ErrorCh():
}
}