From 50f7e91eadbc58750693d81533cab1f7524874e5 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Wed, 7 Sep 2022 14:16:27 -0500 Subject: [PATCH] Infer chain (mainnet, testnet3, regtest) based on DBStateValue. Correct typo DDVersion -> DBVersion. Misc logging improvements. --- db/db.go | 2 +- db/prefixes/prefixes.go | 8 ++++---- main.go | 8 +++++--- server/server.go | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/db/db.go b/db/db.go index 27e703b..76ae41f 100644 --- a/db/db.go +++ b/db/db.go @@ -550,7 +550,7 @@ func GetDBColumnFamilies(name string, secondayPath string, cfNames []string) (*R var handlesMap = make(map[string]*grocksdb.ColumnFamilyHandle) for i, handle := range handles { - log.Printf("%d: %+v\n", i, handle) + log.Printf("handle %d(%s): %+v\n", i, cfNames[i], handle) handlesMap[cfNames[i]] = handle } diff --git a/db/prefixes/prefixes.go b/db/prefixes/prefixes.go index 92d8f54..e4b628a 100644 --- a/db/prefixes/prefixes.go +++ b/db/prefixes/prefixes.go @@ -194,7 +194,7 @@ type DBStateValue struct { UtxoFlushCount uint32 WallTime uint32 FirstSync bool - DDVersion uint8 + DBVersion uint8 HistFlushCount int32 CompFlushCount int32 CompCursor int32 @@ -210,7 +210,7 @@ func NewDBStateValue() *DBStateValue { UtxoFlushCount: 0, WallTime: 0, FirstSync: true, - DDVersion: 0, + DBVersion: 0, HistFlushCount: 0, CompFlushCount: -1, CompCursor: -1, @@ -248,7 +248,7 @@ func (v *DBStateValue) PackValue() []byte { bitSetVar = 1 } value[32+4+4+32+4+4] = bitSetVar - value[32+4+4+32+4+4+1] = v.DDVersion + value[32+4+4+32+4+4+1] = v.DBVersion binary.BigEndian.PutUint32(value[32+4+4+32+4+4+1+1:], uint32(v.HistFlushCount)) binary.BigEndian.PutUint32(value[32+4+4+32+4+4+1+1+4:], uint32(v.CompFlushCount)) @@ -290,7 +290,7 @@ func DBStateValueUnpack(value []byte) *DBStateValue { UtxoFlushCount: binary.BigEndian.Uint32(value[32+4+4+32:]), WallTime: binary.BigEndian.Uint32(value[32+4+4+32+4:]), FirstSync: value[32+4+4+32+4+4] == 1, - DDVersion: value[32+4+4+32+4+4+1], + DBVersion: value[32+4+4+32+4+4+1], HistFlushCount: int32(binary.BigEndian.Uint32(value[32+4+4+32+4+4+1+1:])), CompFlushCount: int32(binary.BigEndian.Uint32(value[32+4+4+32+4+4+1+1+4:])), CompCursor: int32(binary.BigEndian.Uint32(value[32+4+4+32+4+4+1+1+4+4:])), diff --git a/main.go b/main.go index 8dc1053..0be9f4d 100644 --- a/main.go +++ b/main.go @@ -39,11 +39,13 @@ func main() { defer func() { log.Println("Shutting down server...") - if !s.Args.DisableEs { + if s.EsClient != nil { s.EsClient.Stop() } - s.GrpcServer.GracefulStop() - if !s.Args.DisableResolve { + if s.GrpcServer != nil { + s.GrpcServer.GracefulStop() + } + if s.DB != nil { s.DB.Shutdown() } diff --git a/server/server.go b/server/server.go index 880f533..adf002a 100644 --- a/server/server.go +++ b/server/server.go @@ -37,9 +37,9 @@ type Server struct { MultiSpaceRe *regexp.Regexp WeirdCharsRe *regexp.Regexp DB *db.ReadOnlyDBColumnFamily + Chain *chaincfg.Params EsClient *elastic.Client QueryCache *ttlcache.Cache - Coin *chaincfg.Params S256 *hash.Hash LastRefreshCheck time.Time RefreshDelta time.Duration @@ -169,27 +169,48 @@ func LoadDatabase(args *Args) (*db.ReadOnlyDBColumnFamily, error) { log.Fatalln(err) } + if myDB.LastState != nil { + logrus.Infof("DB version: %v", myDB.LastState.DBVersion) + logrus.Infof("height: %v", myDB.LastState.Height) + logrus.Infof("tip: %v", myDB.LastState.Tip.String()) + logrus.Infof("tx count: %v", myDB.LastState.TxCount) + } + blockingChannelHashes := make([][]byte, 0, 10) + blockingIds := make([]string, 0, 10) filteringChannelHashes := make([][]byte, 0, 10) + filteringIds := make([]string, 0, 10) for _, id := range args.BlockingChannelIds { hash, err := hex.DecodeString(id) if err != nil { logrus.Warn("Invalid channel id: ", id) + continue } blockingChannelHashes = append(blockingChannelHashes, hash) + blockingIds = append(blockingIds, id) } for _, id := range args.FilteringChannelIds { hash, err := hex.DecodeString(id) if err != nil { logrus.Warn("Invalid channel id: ", id) + continue } filteringChannelHashes = append(filteringChannelHashes, hash) + filteringIds = append(filteringIds, id) } myDB.BlockingChannelHashes = blockingChannelHashes myDB.FilteringChannelHashes = filteringChannelHashes + + if len(filteringIds) > 0 { + logrus.Infof("filtering claims reposted by channels: %+s", filteringIds) + } + if len(blockingIds) > 0 { + logrus.Infof("blocking claims reposted by channels: %+s", blockingIds) + } + return myDB, nil } @@ -253,16 +274,30 @@ func MakeHubServer(ctx context.Context, args *Args) *Server { } } + chain := chaincfg.MainNetParams + if myDB != nil && myDB.LastState != nil && myDB.LastState.Genesis != nil { + // The chain params can be inferred from DBStateValue. + switch *myDB.LastState.Genesis { + case *chaincfg.MainNetParams.GenesisHash: + chain = chaincfg.MainNetParams + case *chaincfg.TestNet3Params.GenesisHash: + chain = chaincfg.TestNet3Params + case *chaincfg.RegressionNetParams.GenesisHash: + chain = chaincfg.RegressionNetParams + } + } + logrus.Infof("network: %v", chain.Name) + s := &Server{ GrpcServer: grpcServer, Args: args, MultiSpaceRe: multiSpaceRe, WeirdCharsRe: weirdCharsRe, DB: myDB, + Chain: &chain, EsClient: client, QueryCache: cache, S256: &s256, - Coin: &chaincfg.MainNetParams, LastRefreshCheck: time.Now(), RefreshDelta: refreshDelta, NumESRefreshes: 0,