|
|
|
@ -6,6 +6,8 @@ import ( |
|
|
|
|
"path" |
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
lru "github.com/hashicorp/golang-lru" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
|
"github.com/harmony-one/harmony/core/types" |
|
|
|
|
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" |
|
|
|
@ -21,6 +23,7 @@ const ( |
|
|
|
|
AddressPrefix = "ad" |
|
|
|
|
CheckpointPrefix = "dc" |
|
|
|
|
PrefixLen = 3 |
|
|
|
|
addrIndexCacheSize = 1000 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// GetAddressKey ...
|
|
|
|
@ -40,6 +43,7 @@ var once sync.Once |
|
|
|
|
type Storage struct { |
|
|
|
|
db *leveldb.DB |
|
|
|
|
lock sync.Mutex |
|
|
|
|
addrIndexCache *lru.Cache |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetStorageInstance returns attack model by using singleton pattern.
|
|
|
|
@ -68,6 +72,7 @@ func (storage *Storage) Init(ip, port string) { |
|
|
|
|
if storage.db, err = leveldb.OpenFile(dbFileName, options); err != nil { |
|
|
|
|
utils.Logger().Error().Err(err).Msg("Failed to create new database") |
|
|
|
|
} |
|
|
|
|
storage.addrIndexCache, _ = lru.New(addrIndexCacheSize) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetDB returns the LDBDatabase of the storage.
|
|
|
|
@ -106,12 +111,17 @@ func (storage *Storage) Dump(block *types.Block, height uint64) { |
|
|
|
|
func (storage *Storage) UpdateTxAddressStorage(addr string, txRecords TxRecords, isStaking bool) { |
|
|
|
|
var address Address |
|
|
|
|
key := GetAddressKey(addr) |
|
|
|
|
|
|
|
|
|
if cached, ok := storage.addrIndexCache.Get(key); ok { |
|
|
|
|
address = cached.(Address) |
|
|
|
|
} else { |
|
|
|
|
if data, err := storage.GetDB().Get([]byte(key), nil); err == nil { |
|
|
|
|
if err = rlp.DecodeBytes(data, &address); err != nil { |
|
|
|
|
utils.Logger().Error(). |
|
|
|
|
Bool("isStaking", isStaking).Err(err).Msg("Failed due to error") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
address.ID = addr |
|
|
|
|
if isStaking { |
|
|
|
@ -122,6 +132,7 @@ func (storage *Storage) UpdateTxAddressStorage(addr string, txRecords TxRecords, |
|
|
|
|
encoded, err := rlp.EncodeToBytes(address) |
|
|
|
|
if err == nil { |
|
|
|
|
storage.GetDB().Put([]byte(key), encoded, nil) |
|
|
|
|
storage.addrIndexCache.Add(key, address) |
|
|
|
|
} else { |
|
|
|
|
utils.Logger().Error(). |
|
|
|
|
Bool("isStaking", isStaking).Err(err).Msg("cannot encode address") |
|
|
|
|