2020-02-09 10:31:52 +00:00
|
|
|
package apis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
2020-03-24 02:12:55 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-02-09 10:31:52 +00:00
|
|
|
)
|
|
|
|
|
2020-03-20 10:06:14 +00:00
|
|
|
func RegisterAccountAPI(router *gin.RouterGroup, e *Env) error {
|
|
|
|
router.GET(":accountID", e.GetAccount)
|
2020-02-09 10:31:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-20 10:06:14 +00:00
|
|
|
func (e *Env) GetAccount(c *gin.Context) {
|
2020-07-21 08:19:04 +00:00
|
|
|
account, err := findAccountByID(c.Param("accountID"), e.KV)
|
2020-03-20 10:06:14 +00:00
|
|
|
if err == ErrEntityNotFound {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"message": "account not found"})
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err) //nolint:errcheck
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, jsonifyAccount(account))
|
|
|
|
}
|
|
|
|
|
2020-02-09 10:31:52 +00:00
|
|
|
func jsonifyAccount(account *accounts.Account) map[string]interface{} {
|
|
|
|
result := map[string]interface{}{
|
|
|
|
"nonce": account.Nonce,
|
2020-05-26 16:53:50 +00:00
|
|
|
"balance": account.Balance.ToBig().String(),
|
2020-02-09 10:31:52 +00:00
|
|
|
"root_hash": account.Root.Hex(),
|
|
|
|
"code_hash": account.CodeHash.Hex(),
|
|
|
|
"implementation": map[string]interface{}{
|
|
|
|
"incarnation": account.Incarnation,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-03-24 02:12:55 +00:00
|
|
|
func findAccountByID(accountID string, remoteDB ethdb.KV) (*accounts.Account, error) {
|
2020-02-09 10:31:52 +00:00
|
|
|
|
|
|
|
possibleKeys := getPossibleKeys(accountID)
|
|
|
|
|
|
|
|
var account *accounts.Account
|
|
|
|
|
2020-03-24 02:12:55 +00:00
|
|
|
err := remoteDB.View(context.TODO(), func(tx ethdb.Tx) error {
|
2020-08-14 06:41:18 +00:00
|
|
|
c := tx.Cursor(dbutils.CurrentStateBucket)
|
2020-02-09 10:31:52 +00:00
|
|
|
|
|
|
|
for _, key := range possibleKeys {
|
2020-11-16 12:08:28 +00:00
|
|
|
_, accountRlp, err := c.SeekExact(key)
|
2020-02-09 10:31:52 +00:00
|
|
|
if len(accountRlp) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account = &accounts.Account{}
|
|
|
|
return account.DecodeForStorage(accountRlp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if account == nil {
|
|
|
|
return nil, ErrEntityNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPossibleKeys(accountID string) [][]byte {
|
|
|
|
address := common.FromHex(accountID)
|
|
|
|
addressHash, _ := common.HashData(address[:])
|
|
|
|
return [][]byte{
|
|
|
|
addressHash[:],
|
|
|
|
address,
|
|
|
|
}
|
|
|
|
}
|