|
|
@ -9,6 +9,7 @@ import ( |
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
"github.com/harmony-one/harmony/core/types" |
|
|
|
"github.com/harmony-one/harmony/core/types" |
|
|
|
"github.com/harmony-one/harmony/db" |
|
|
|
"github.com/harmony-one/harmony/db" |
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/log" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Constants for storage.
|
|
|
|
// Constants for storage.
|
|
|
@ -20,6 +21,9 @@ const ( |
|
|
|
AddressPrefix = "ad" |
|
|
|
AddressPrefix = "ad" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Log is the temporary log for storage.
|
|
|
|
|
|
|
|
var Log = log.New() |
|
|
|
|
|
|
|
|
|
|
|
// GetBlockInfoKey ...
|
|
|
|
// GetBlockInfoKey ...
|
|
|
|
func GetBlockInfoKey(id int) string { |
|
|
|
func GetBlockInfoKey(id int) string { |
|
|
|
return fmt.Sprintf("%s_%d", BlockInfoPrefix, id) |
|
|
|
return fmt.Sprintf("%s_%d", BlockInfoPrefix, id) |
|
|
@ -64,12 +68,11 @@ func (storage *Storage) Init(ip, port string, remove bool) { |
|
|
|
if remove { |
|
|
|
if remove { |
|
|
|
var err = os.RemoveAll(dbFileName) |
|
|
|
var err = os.RemoveAll(dbFileName) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err.Error()) |
|
|
|
Log.Error(err.Error()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if storage.db, err = db.NewLDBDatabase(dbFileName, 0, 0); err != nil { |
|
|
|
if storage.db, err = db.NewLDBDatabase(dbFileName, 0, 0); err != nil { |
|
|
|
fmt.Println(err.Error()) |
|
|
|
Log.Error(err.Error()) |
|
|
|
os.Exit(1) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -103,16 +106,12 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { |
|
|
|
|
|
|
|
|
|
|
|
if data, err := rlp.EncodeToBytes(blockInfo); err == nil { |
|
|
|
if data, err := rlp.EncodeToBytes(blockInfo); err == nil { |
|
|
|
key := GetBlockInfoKey(int(height)) |
|
|
|
key := GetBlockInfoKey(int(height)) |
|
|
|
fmt.Println("store blockinfo with key ", key) |
|
|
|
|
|
|
|
fmt.Println("data to store ", data) |
|
|
|
|
|
|
|
storage.db.Put([]byte(key), data) |
|
|
|
storage.db.Put([]byte(key), data) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
fmt.Println("EncodeRLP blockInfo error") |
|
|
|
Log.Error("EncodeRLP blockInfo error") |
|
|
|
os.Exit(1) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Store txs
|
|
|
|
// Store txs
|
|
|
|
fmt.Println("# of txs ", len(block.Transactions())) |
|
|
|
|
|
|
|
for _, tx := range block.Transactions() { |
|
|
|
for _, tx := range block.Transactions() { |
|
|
|
if tx.To() == nil { |
|
|
|
if tx.To() == nil { |
|
|
|
continue |
|
|
|
continue |
|
|
@ -127,19 +126,18 @@ func (storage *Storage) Dump(accountBlock []byte, height uint32) { |
|
|
|
Bytes: strconv.Itoa(int(tx.Size())), |
|
|
|
Bytes: strconv.Itoa(int(tx.Size())), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
storage.UpdateTxStorage(explorerTransaction, tx) |
|
|
|
storage.UpdateTXStorage(explorerTransaction, tx) |
|
|
|
storage.UpdateAddressStorage(explorerTransaction, tx) |
|
|
|
storage.UpdateAddressStorage(explorerTransaction, tx) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// UpdateTxStorage ...
|
|
|
|
// UpdateTXStorage ...
|
|
|
|
func (storage *Storage) UpdateTxStorage(explorerTransaction Transaction, tx *types.Transaction) { |
|
|
|
func (storage *Storage) UpdateTXStorage(explorerTransaction Transaction, tx *types.Transaction) { |
|
|
|
if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { |
|
|
|
if data, err := rlp.EncodeToBytes(explorerTransaction); err == nil { |
|
|
|
key := GetTXKey(tx.Hash().Hex()) |
|
|
|
key := GetTXKey(tx.Hash().Hex()) |
|
|
|
storage.db.Put([]byte(key), data) |
|
|
|
storage.db.Put([]byte(key), data) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
fmt.Println("EncodeRLP transaction error") |
|
|
|
Log.Error("EncodeRLP transaction error") |
|
|
|
os.Exit(1) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -148,29 +146,23 @@ func (storage *Storage) UpdateAddressStorage(explorerTransaction Transaction, tx |
|
|
|
toAddress := tx.To().Hex() |
|
|
|
toAddress := tx.To().Hex() |
|
|
|
key := GetAddressKey(toAddress) |
|
|
|
key := GetAddressKey(toAddress) |
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("dumping address", toAddress, key) |
|
|
|
var address Address |
|
|
|
|
|
|
|
|
|
|
|
var addressAccount Address |
|
|
|
|
|
|
|
if data, err := storage.db.Get([]byte(key)); err == nil { |
|
|
|
if data, err := storage.db.Get([]byte(key)); err == nil { |
|
|
|
fmt.Println("the key existed") |
|
|
|
err = rlp.DecodeBytes(data, address) |
|
|
|
err = rlp.DecodeBytes(data, addressAccount) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
addressAccount.Balance.Add(addressAccount.Balance, tx.Value()) |
|
|
|
address.Balance.Add(address.Balance, tx.Value()) |
|
|
|
txCount, _ := strconv.Atoi(addressAccount.TXCount) |
|
|
|
txCount, _ := strconv.Atoi(address.TXCount) |
|
|
|
addressAccount.TXCount = strconv.Itoa(txCount + 1) |
|
|
|
address.TXCount = strconv.Itoa(txCount + 1) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
fmt.Println("the key not existed") |
|
|
|
address.Balance = tx.Value() |
|
|
|
addressAccount.Balance = tx.Value() |
|
|
|
address.TXCount = "1" |
|
|
|
addressAccount.TXCount = "1" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
addressAccount.ID = toAddress |
|
|
|
address.ID = toAddress |
|
|
|
addressAccount.TXs = append(addressAccount.TXs, explorerTransaction) |
|
|
|
address.TXs = append(address.TXs, explorerTransaction) |
|
|
|
fmt.Println("trying to encode it") |
|
|
|
if encoded, err := rlp.EncodeToBytes(address); err == nil { |
|
|
|
if encoded, err := rlp.EncodeToBytes(addressAccount); err == nil { |
|
|
|
|
|
|
|
fmt.Println("store addressAccount with length ", len(encoded)) |
|
|
|
|
|
|
|
storage.db.Put([]byte(key), encoded) |
|
|
|
storage.db.Put([]byte(key), encoded) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
fmt.Println("err when encoding ", err) |
|
|
|
Log.Error("Can not encode address account.") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|