Browse Source

Display hash properly

master
Aditya Kulkarni 5 years ago
parent
commit
e68b202928
  1. 4
      .gitignore
  2. 9
      common/cache.go
  3. 32
      common/common.go
  4. 1
      go.sum

4
.gitignore

@ -1,2 +1,4 @@
main main
grpcfrontend
cert.pem
key.pem

9
common/cache.go

@ -6,7 +6,6 @@ import (
"github.com/adityapk00/lightwalletd/walletrpc" "github.com/adityapk00/lightwalletd/walletrpc"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/pkg/errors"
) )
type BlockCacheEntry struct { type BlockCacheEntry struct {
@ -34,7 +33,7 @@ func NewBlockCache(maxEntries int) *BlockCache {
} }
} }
func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) error { func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) (error, bool) {
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()
@ -58,14 +57,14 @@ func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) error {
// Don't allow out-of-order blocks. This is more of a sanity check than anything // Don't allow out-of-order blocks. This is more of a sanity check than anything
// If there is a reorg, then the ingestor needs to handle it. // If there is a reorg, then the ingestor needs to handle it.
if c.m[height-1] != nil && !bytes.Equal(block.PrevHash, c.m[height-1].hash) { if c.m[height-1] != nil && !bytes.Equal(block.PrevHash, c.m[height-1].hash) {
return errors.New("Prev hash of the block didn't match") return nil, true
} }
// Add the entry and update the counters // Add the entry and update the counters
data, err := proto.Marshal(block) data, err := proto.Marshal(block)
if err != nil { if err != nil {
println("Error marshalling block!") println("Error marshalling block!")
return nil return err, false
} }
c.m[height] = &BlockCacheEntry{ c.m[height] = &BlockCacheEntry{
@ -83,7 +82,7 @@ func (c *BlockCache) Add(height int, block *walletrpc.CompactBlock) error {
} }
//println("Cache size is ", len(c.m)) //println("Cache size is ", len(c.m))
return nil return nil, false
} }
func (c *BlockCache) Get(height int) *walletrpc.CompactBlock { func (c *BlockCache) Get(height int) *walletrpc.CompactBlock {

32
common/common.go

@ -141,20 +141,22 @@ func BlockIngestor(rpcClient *rpcclient.Client, cache *BlockCache, log *logrus.E
} }
log.Info("Ingestor adding block to cache: ", height) log.Info("Ingestor adding block to cache: ", height)
err = cache.Add(height, block) err, reorg := cache.Add(height, block)
//check for reorgs once we have inital block hash from startup
if err != nil { if err != nil {
reorgCount++ log.Error("Error adding block to cache: ", err)
continue
}
hash := hex.EncodeToString(block.Hash) //check for reorgs once we have inital block hash from startup
phash := hex.EncodeToString(block.PrevHash) if reorg {
reorgCount++
log.WithFields(logrus.Fields{ log.WithFields(logrus.Fields{
"height": height, "height": height,
"hash(reversed)": hash, "hash": displayHash(block.Hash),
"phash(reversed)": phash, "phash": displayHash(block.PrevHash),
"reorg": reorgCount, "reorg": reorgCount,
}).Warn("REORG") }).Warn("REORG")
} else { } else {
reorgCount = 0 reorgCount = 0
@ -208,3 +210,15 @@ func GetBlockRange(rpcClient *rpcclient.Client, cache *BlockCache,
errOut <- nil errOut <- nil
} }
func displayHash(hash []byte) string {
rhash := make([]byte, len(hash))
copy(rhash, hash)
// Reverse byte order
for i := 0; i < len(rhash)/2; i++ {
j := len(rhash) - 1 - i
rhash[i], rhash[j] = rhash[j], rhash[i]
}
return hex.EncodeToString(rhash)
}

1
go.sum

@ -334,6 +334,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

Loading…
Cancel
Save