Skip to content

Commit

Permalink
Fix redeemer processing in Conway
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg committed Sep 23, 2024
1 parent a33dab0 commit 0c5ee62
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class NewmChainService : NewmChainGrpcKt.NewmChainCoroutineImplBase() {
val protocolParams = stateQueryClient.protocolParameters().result
val calculateTxExecutionUnits: suspend (ByteArray) -> EvaluateTxResult = { cborBytes ->
// FIXME: We should use PlutoK, Aiken, or some other way to calculate the execution units without relying on Ogmios.
// log.warn { "EvaluateTx: ${cborBytes.toHexString()}" }
txSubmitClient.evaluate(cborBytes.toHexString()).result
}
val calculateReferenceScriptBytes: suspend (Set<Utxo>) -> Long = { utxos ->
Expand Down Expand Up @@ -436,7 +437,7 @@ class NewmChainService : NewmChainGrpcKt.NewmChainCoroutineImplBase() {
if (updatedRequest.hasEra()) {
CardanoEra.valueOf(updatedRequest.era.name)
} else {
CardanoEra.BABBAGE
CardanoEra.CONWAY
}

val (txId, cborBytes) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow

/**
* A builder for cardano babbage transactions.
* reference: https://github.com/input-output-hk/cardano-ledger/blob/master/eras/babbage/test-suite/cddl-files/babbage.cddl
* A builder for cardano conway transactions.
* reference: https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/conway.cddl
*/
class TransactionBuilder(
private val protocolParameters: ProtocolParametersResult,
private val cardanoEra: CardanoEra = CardanoEra.BABBAGE,
private val cardanoEra: CardanoEra = CardanoEra.CONWAY,
private val calculateTxExecutionUnits: (suspend (ByteArray) -> EvaluateTxResult)? = null,
private val calculateReferenceScriptBytes: (suspend (Set<Utxo>) -> Long)? = null,
) {
// private val log by lazy { LoggerFactory.getLogger("TransactionBuilder") }
private val secureRandom by lazy { SecureRandom() }

private val txFeeFixed by lazy {
Expand Down Expand Up @@ -382,6 +383,10 @@ class TransactionBuilder(
ByteArray(1) { 0xa0.toByte() }
}
scriptDataHash = Blake2b.hash256(redeemerBytes + datumBytes + languageViewMap)
// log.warn("redeeemerBytes: ${redeemerBytes.toHexString()}")
// log.warn("datumBytes: ${datumBytes.toHexString()}")
// log.warn("languageViewMap: ${languageViewMap.toHexString()}")
// log.warn("scriptDataHash: ${scriptDataHash!!.toHexString()}")
}
}

Expand Down Expand Up @@ -872,7 +877,7 @@ class TransactionBuilder(
companion object {
suspend fun transactionBuilder(
protocolParameters: ProtocolParametersResult,
cardanoEra: CardanoEra = CardanoEra.BABBAGE,
cardanoEra: CardanoEra = CardanoEra.CONWAY,
calculateTxExecutionUnits: (suspend (ByteArray) -> EvaluateTxResult)? = null,
calculateReferenceScriptBytes: (suspend (Set<Utxo>) -> Long)? = null,
block: TransactionBuilder.() -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,41 @@ fun TransactionBuilderResponse.extractFields(): TxFields {
}

override val redeemers: List<Redeemer> by lazy {
(witnessSet[KEY_REDEEMERS] as CborArray).map {
redeemer {
val redeemerArray = it as CborArray
tagValue = (redeemerArray.elementAt(0) as CborInteger).intValueExact()
index = (redeemerArray.elementAt(1) as CborInteger).longValue()
data = redeemerArray.elementAt(2).toPlutusData()
exUnits = exUnits {
val exUnitsArray = redeemerArray.elementAt(3) as CborArray
mem = (exUnitsArray.elementAt(0) as CborInteger).longValue()
steps = (exUnitsArray.elementAt(1) as CborInteger).longValue()
when (witnessSet[KEY_REDEEMERS]) {
is CborArray -> {
(witnessSet[KEY_REDEEMERS] as CborArray).map {
redeemer {
val redeemerArray = it as CborArray
tagValue = (redeemerArray.elementAt(0) as CborInteger).intValueExact()
index = (redeemerArray.elementAt(1) as CborInteger).longValue()
data = redeemerArray.elementAt(2).toPlutusData()
exUnits = exUnits {
val exUnitsArray = redeemerArray.elementAt(3) as CborArray
mem = (exUnitsArray.elementAt(0) as CborInteger).longValue()
steps = (exUnitsArray.elementAt(1) as CborInteger).longValue()
}
}
}
}

is CborMap -> {
(witnessSet[KEY_REDEEMERS] as CborMap).entrySet().map { (key, value) ->
redeemer {
val keyArray = key as CborArray
val redeemerArray = value as CborArray
tagValue = (keyArray.elementAt(0) as CborInteger).intValueExact()
index = (keyArray.elementAt(1) as CborInteger).longValue()
data = redeemerArray.elementAt(0).toPlutusData()
exUnits = exUnits {
val exUnitsArray = redeemerArray.elementAt(1) as CborArray
mem = (exUnitsArray.elementAt(0) as CborInteger).longValue()
steps = (exUnitsArray.elementAt(1) as CborInteger).longValue()
}
}
}
}

else -> throw IllegalStateException("Expected redeemers to be a CborArray or CborMap")
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions newm-tx-builder/src/main/kotlin/io/newm/txbuilder/ktx/UtxoKtx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ fun Set<Utxo>.toCborObject(cardanoEra: CardanoEra): CborObject? {
listOf(
CborByteString.create(sourceUtxo.hash.hexToByteArray()),
CborInteger.create(sourceUtxo.ix),
),
when (cardanoEra) {
CardanoEra.CONWAY -> 258
else -> CborTag.UNTAGGED
}
)
)
},
when (cardanoEra) {
CardanoEra.CONWAY -> 258
else -> CborTag.UNTAGGED
}
)
}
Expand Down Expand Up @@ -106,7 +106,12 @@ fun OutputUtxo.toCborObject(): CborObject =
CborArray.create(
listOf(
TransactionBuilder.DATUM_KEY_INLINE,
CborByteString.create(datumBytes, 0, datumBytes.size, TransactionBuilder.INLINE_DATUM_TAG),
CborByteString.create(
datumBytes,
0,
datumBytes.size,
TransactionBuilder.INLINE_DATUM_TAG
),
)
)
} else {
Expand Down

0 comments on commit 0c5ee62

Please sign in to comment.