-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (97 loc) · 3.01 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
package main
import (
"encoding/hex"
"fmt"
"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() {
// for test net
address := "preprod-node.play.dev.cardano.org:3001"
networkMagic := uint32(1)
startBlockHash, _ := hex.DecodeString("4b7f0ff899395dade775c7eb2bc8e16fb9824d9091266c7d4c9c55ac143ae6c8")
startSlot := uint64(74683550)
addressesOfInterest := []string{
"addr_test1wr64gtafm8rpkndue4ck2nx95u4flhwf643l2qmg9emjajg2ww0nj",
}
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),
},
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():
}
}