Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New TVM features #41

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/org/tronscan/actions/ActionRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import javax.inject.Inject
import play.api.Logger
import play.api.cache.NamedCache
import play.api.cache.redis.CacheAsyncApi
import play.api.inject.ConfigurationProvider

import scala.concurrent.duration._

Expand All @@ -15,7 +16,8 @@ class ActionRunner @Inject()(
representativeListReader: RepresentativeListReader,
statsOverview: StatsOverview,
voteList: VoteList,
voteScraper: VoteScraper) extends Actor {
voteScraper: VoteScraper,
configurationProvider: ConfigurationProvider) extends Actor {

val decider: Supervision.Decider = { exc =>
Logger.error("CACHE WARMER ERROR", exc)
Expand Down Expand Up @@ -57,10 +59,12 @@ class ActionRunner @Inject()(
}

override def preStart(): Unit = {
startWitnessReader()
startVoteListWarmer()
startVoteScraper()
startStatsOverview()
if (configurationProvider.get.get[Boolean]("cache.warmer")) {
startWitnessReader()
startVoteListWarmer()
startVoteScraper()
startStatsOverview()
}
}

def receive = {
Expand Down
3 changes: 3 additions & 0 deletions app/org/tronscan/actions/StatsOverview.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import org.tron.common.repositories.StatsRepository

import scala.concurrent.ExecutionContext

/**
* Reads the total stats
*/
class StatsOverview @Inject()(
statsRepository: StatsRepository) {

Expand Down
97 changes: 30 additions & 67 deletions app/org/tronscan/api/AccountApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import scala.async.Async._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
import org.tronscan.Extensions._

@Api(
value = "Accounts",
Expand All @@ -55,6 +56,10 @@ class AccountApi @Inject()(

val key = configurationProvider.get.get[String]("play.http.secret.key")

/**
* Query accounts
* @return
*/
@ApiResponses(Array(
new ApiResponse(
code = 200,
Expand Down Expand Up @@ -112,6 +117,9 @@ class AccountApi @Inject()(
}
}

/**
* Find account by the given address
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
@ApiOperation(
value = "Find account by address",
response = classOf[AccountModel])
Expand Down Expand Up @@ -149,7 +157,7 @@ class AccountApi @Inject()(
"allowance" -> account.allowance,
"url" -> witness.map(_.url),
),
"name" -> new String(account.accountName.toByteArray).toString,
"name" -> account.accountName.decodeString,
"address" -> address,
"bandwidth" -> Json.obj(
"freeNetUsed" -> accountBandwidth.freeNetUsed,
Expand All @@ -166,6 +174,7 @@ class AccountApi @Inject()(
),
"balances" -> Json.toJson(balances),
"balance" -> account.balance,
"allowance" -> account.allowance,
"tokenBalances" -> Json.toJson(balances),
"frozen" -> Json.obj(
"total" -> account.frozen.map(_.frozenBalance).sum,
Expand All @@ -180,6 +189,9 @@ class AccountApi @Inject()(
}
}

/**
* Retrieves the balances for the given address
*/
@ApiOperation(
value = "",
hidden = true
Expand Down Expand Up @@ -218,6 +230,9 @@ class AccountApi @Inject()(
}
}

/**
* Votes cast by the given address
*/
@ApiOperation(
value = "",
hidden = true
Expand All @@ -226,7 +241,7 @@ class AccountApi @Inject()(
for {
wallet <- walletClient.full
account <- wallet.getAccount(Account(
address = ByteString.copyFrom(Base58.decode58Check(address))
address = address.decodeAddress,
))
} yield {

Expand All @@ -248,6 +263,11 @@ class AccountApi @Inject()(
}
}

/**
* Retrieve the Super Representative github link
* @param address address of the SR
* @return
*/
@ApiOperation(
value = "",
hidden = true
Expand All @@ -267,6 +287,9 @@ class AccountApi @Inject()(
}
}

/**
* Update the super representative pages
*/
@ApiOperation(
value = "",
hidden = true)
Expand Down Expand Up @@ -308,70 +331,9 @@ class AccountApi @Inject()(
}
}

@ApiOperation(
value = "",
hidden = true
)
def resync = Action.async { req =>

throw new Exception("DISABLED")

val decider: Supervision.Decider = {
case exc =>
println("SOLIDITY STREAM ERROR", exc, ExceptionUtils.getStackTrace(exc))
Supervision.Resume
}

implicit val materializer = ActorMaterializer(
ActorMaterializerSettings(system)
.withSupervisionStrategy(decider))(system)

async {

val accounts = await(repo.findAll).toList

val source = Source(accounts)
.mapAsync(8) { existingAccount =>
async {

val walletSolidity = await(walletClient.full)

val account = await(walletSolidity.getAccount(Account(
address = ByteString.copyFrom(Base58.decode58Check(existingAccount.address))
)))

if (account != null) {

val accountModel = AccountModel(
address = existingAccount.address,
name = new String(account.accountName.toByteArray),
balance = account.balance,
power = account.frozen.map(_.frozenBalance).sum,
tokenBalances = Json.toJson(account.asset),
dateUpdated = DateTime.now,
)

List(repo.buildInsertOrUpdate(accountModel)) ++ addressBalanceModelRepository.buildUpdateBalance(accountModel)
} else {
List.empty
}

}
}
.flatMapConcat(queries => Source(queries))
.groupedWithin(150, 10.seconds)
.mapAsync(1) { queries =>
blockModelRepository.executeQueries(queries)
}
.toMat(Sink.ignore)(Keep.right)
.run

await(source)

Ok("Done")
}
}

/**
* Synchronises the given account to the database
*/
@ApiOperation(
value = "",
hidden = true)
Expand Down Expand Up @@ -484,7 +446,8 @@ class AccountApi @Inject()(
}

/**
* Generates a new private key
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cube now nephew path grocery observe
panda pen sock luggage dismiss frost

* Generates a new account with private key
*
* @return
*/
def create = Action {
Expand Down
3 changes: 3 additions & 0 deletions app/org/tronscan/api/BaseApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ trait BaseApi extends InjectedController {
"count",
)

/**
* Strip query parameters which are related to paging and navigation of the results
*/
def stripNav(params: Map[String, String], sortParams: List[String] = List.empty) = {
val navs = navParams ++ sortParams
params.filterKeys(x => !navs.contains(x))
Expand Down
6 changes: 3 additions & 3 deletions app/org/tronscan/api/BlockApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class BlockApi @Inject() (
val queryParams = request.queryString.map(x => x._1.toLowerCase -> x._2.mkString)
val queryHash = queryParams.map(x => x._1 + "-" + x._2).mkString
val filterHash = stripNav(queryParams).map(x => x._1 + "-" + x._2).mkString
val includeCount = request.getQueryString("count").exists(x => true)
val includeCount = request.getQueryString("count").isDefined

def getBlocks = {

Expand Down Expand Up @@ -146,10 +146,10 @@ class BlockApi @Inject() (
size = block.toByteArray.length,
hash = block.hash,
timestamp = new DateTime(header.timestamp),
txTrieRoot = Base58.encode58Check(header.txTrieRoot.toByteArray),
txTrieRoot = ByteUtil.toHexString(header.txTrieRoot.toByteArray),
parentHash = ByteUtil.toHexString(header.parentHash.toByteArray),
witnessId = header.witnessId,
witnessAddress = Base58.encode58Check(header.witnessAddress.toByteArray),
witnessAddress = header.witnessAddress.encodeAddress,
nrOfTrx = block.transactions.size,
).asJson)
}
Expand Down
Loading