Giter Club home page Giter Club logo

Comments (3)

tdroxler avatar tdroxler commented on September 17, 2024 1

👍 all good to close it

from explorer-backend.

tdroxler avatar tdroxler commented on September 17, 2024

We do use inputs and outputs for example to get the balance of an address:

private val getBalanceQuery = Compiled { address: Rep[Address] =>
outputsTable
.filter(output => output.mainChain && output.address === address)
.map(output => (output.key, output.amount, output.lockTime))
.joinLeft(inputsTable.filter(_.mainChain))
.on(_._1 === _.outputRefKey)
.filter(_._2.isEmpty)
.map { case ((_, amount, lockTime), _) => (amount, lockTime) }
}
def getBalanceAction(address: Address): DBActionR[(U256, U256)] = {
getBalanceQuery(address).result.map { outputs =>
val now = TimeStamp.now()
outputs.foldLeft((U256.Zero, U256.Zero)) {
case ((total, locked), (amount, lockTime)) =>
val newTotal = total.addUnsafe(amount)
val newLocked = if (lockTime.map(_.isBefore(now)).getOrElse(true)) {
locked
} else {
locked.addUnsafe(amount)
}
(newTotal, newLocked)
}
}
}
no tx involved here.

While writing this I also realize we need to have those two tables for example to be able to find the spent output when searching the inputs:

private val getInputsQuery = Compiled { (txHash: Rep[Transaction.Hash]) =>
mainInputs
.filter(_.txHash === txHash)
.join(mainOutputs)
.on(_.outputRefKey === _.key)
.sortBy(_._1.order)
.map {
case (input, output) =>
(input.hint,
input.outputRefKey,
input.unlockScript,
output.txHash,
output.address,
output.amount)
}
}

In the UTXO model, an input is always linked to an older output, but when we insert the transaction with the outputs, we don't know in advance which inputs of a future tx will use those outputs. So we have to do all those join between outputs/inputs` table.

So I guess we need to go for option 1

Your thoughts @polarker ?

from explorer-backend.

simerplaha avatar simerplaha commented on September 17, 2024

Hey @tdroxler, you are right about this on our Slack conversion. I got Slick confused with Hibernate. In Hibernate adding foreign-key-constraints automatically optimises queries to use joins but in Slick we have to do this manually. My excuse: haven't used ORMs in a while :(

And now since we are writing raw SQL, adding foreign-key-constraints to optimise for performance makes even less sense. So this issue is invalid.

Thank you for pointing this out. If you are ok I will close this task?

from explorer-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.