Skip to content
Merged
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
12 changes: 10 additions & 2 deletions cbor/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ func TestRatJsonUnmarshalNumDenom(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
if testData.TestRat.Cmp(expectedRat) != 0 {
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
t.Errorf(
"did not get expected value: got %s, wanted %s",
testData.TestRat.String(),
expectedRat.String(),
)
}
}

Expand All @@ -136,6 +140,10 @@ func TestRatJsonUnmarshalFloat(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
if testData.TestRat.Cmp(expectedRat) != 0 {
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
t.Errorf(
"did not get expected value: got %s, wanted %s",
testData.TestRat.String(),
expectedRat.String(),
)
}
}
4 changes: 3 additions & 1 deletion ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ type BabbageTransactionOutputDatumOption struct {
data *common.Datum
}

func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(cborData []byte) error {
func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(
cborData []byte,
) error {
datumOptionType, err := cbor.DecodeIdFromList(cborData)
if err != nil {
return err
Expand Down
40 changes: 31 additions & 9 deletions ledger/common/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@ import (
)

func TestDatumHash(t *testing.T) {
testDatumBytes, _ := hex.DecodeString("d8799fd8799fd8799f581cb255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5cffd8799fd8799fd8799f581c07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349ffffffffd8799f581cf5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c582073e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abceff1b0000000148f3f3579fd8799fd8799f4040ff1a094f78d8ffd8799fd8799f581cf13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b527546535452494b45ff1af7c5c601ffffff")
testDatumBytes, _ := hex.DecodeString(
"d8799fd8799fd8799f581cb255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5cffd8799fd8799fd8799f581c07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349ffffffffd8799f581cf5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c582073e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abceff1b0000000148f3f3579fd8799fd8799f4040ff1a094f78d8ffd8799fd8799f581cf13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b527546535452494b45ff1af7c5c601ffffff",
)
expectedHash := "4dfec91f63f946d7c91af0041e5d92a45531790a4a104637dd8691f46fdce842"
var tmpDatum common.Datum
if _, err := cbor.Decode(testDatumBytes, &tmpDatum); err != nil {
t.Fatalf("unexpected error: %s", err)
}
datumHash := tmpDatum.Hash()
if datumHash.String() != expectedHash {
t.Fatalf("did not get expected datum hash: got %s, wanted %s", datumHash.String(), expectedHash)
t.Fatalf(
"did not get expected datum hash: got %s, wanted %s",
datumHash.String(),
expectedHash,
)
}
}

func TestDatumDecode(t *testing.T) {
testDatumBytes, _ := hex.DecodeString("d8799fd8799fd8799f581cb255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5cffd8799fd8799fd8799f581c07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349ffffffffd8799f581cf5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c582073e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abceff1b0000000148f3f3579fd8799fd8799f4040ff1a094f78d8ffd8799fd8799f581cf13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b527546535452494b45ff1af7c5c601ffffff")
testDatumBytes, _ := hex.DecodeString(
"d8799fd8799fd8799f581cb255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5cffd8799fd8799fd8799f581c07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349ffffffffd8799f581cf5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c582073e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abceff1b0000000148f3f3579fd8799fd8799f4040ff1a094f78d8ffd8799fd8799f581cf13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b527546535452494b45ff1af7c5c601ffffff",
)
expectedDatum := common.Datum{
Data: data.NewConstr(
0,
Expand All @@ -49,7 +57,9 @@ func TestDatumDecode(t *testing.T) {
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("b255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5c"),
test.DecodeHexString(
"b255e2283f9b495dd663b841090c42bc5a5103283fc2aef5c6cd2f5c",
),
),
),
data.NewConstr(
Expand All @@ -59,7 +69,9 @@ func TestDatumDecode(t *testing.T) {
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349"),
test.DecodeHexString(
"07d8b4b15e9609e76a38b25637900d60cdf13a6abce984757bbc1349",
),
),
),
),
Expand All @@ -68,10 +80,14 @@ func TestDatumDecode(t *testing.T) {
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("f5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c"),
test.DecodeHexString(
"f5808c2c990d86da54bfc97d89cee6efa20cd8461616359478d96b4c",
),
),
data.NewByteString(
test.DecodeHexString("73e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abce"),
test.DecodeHexString(
"73e1518e92f367fd5820ac2da1d40ab24fbca1d6cb2c28121ad92f57aff8abce",
),
),
),
data.NewInteger(big.NewInt(5518914391)),
Expand All @@ -90,7 +106,9 @@ func TestDatumDecode(t *testing.T) {
data.NewConstr(
0,
data.NewByteString(
test.DecodeHexString("f13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b5275"),
test.DecodeHexString(
"f13ac4d66b3ee19a6aa0f2a22298737bd907cc95121662fc971b5275",
),
),
data.NewByteString(
test.DecodeHexString("535452494b45"),
Expand All @@ -106,6 +124,10 @@ func TestDatumDecode(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(tmpDatum.Data, expectedDatum.Data) {
t.Fatalf("did not get expected datum\n got: %#v\n wanted: %#v", tmpDatum.Data, expectedDatum.Data)
t.Fatalf(
"did not get expected datum\n got: %#v\n wanted: %#v",
tmpDatum.Data,
expectedDatum.Data,
)
}
}
47 changes: 36 additions & 11 deletions ledger/common/script/scriptcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func (s ScriptContextV3) ToPlutusData() data.PlutusData {
)
}

func NewScriptContextV3(txInfo TxInfo, redeemer Redeemer, purpose ScriptInfo) ScriptContext {
func NewScriptContextV3(
txInfo TxInfo,
redeemer Redeemer,
purpose ScriptInfo,
) ScriptContext {
return ScriptContextV3{
TxInfo: txInfo,
Redeemer: redeemer,
Expand Down Expand Up @@ -162,7 +166,10 @@ func (t TxInfoV3) ToPlutusData() data.PlutusData {
)
}

func NewTxInfoV3FromTransaction(tx lcommon.Transaction, resolvedInputs []lcommon.Utxo) TxInfoV3 {
func NewTxInfoV3FromTransaction(
tx lcommon.Transaction,
resolvedInputs []lcommon.Utxo,
) TxInfoV3 {
assetMint := tx.AssetMint()
if assetMint == nil {
assetMint = &lcommon.MultiAsset[lcommon.MultiAssetTypeMint]{}
Expand All @@ -180,11 +187,14 @@ func NewTxInfoV3FromTransaction(tx lcommon.Transaction, resolvedInputs []lcommon
),
)
ret := TxInfoV3{
Inputs: expandInputs(inputs, resolvedInputs),
ReferenceInputs: expandInputs(sortInputs(tx.ReferenceInputs()), resolvedInputs),
Outputs: collapseOutputs(tx.Produced()),
Fee: tx.Fee(),
Mint: *assetMint,
Inputs: expandInputs(inputs, resolvedInputs),
ReferenceInputs: expandInputs(
sortInputs(tx.ReferenceInputs()),
resolvedInputs,
),
Outputs: collapseOutputs(tx.Produced()),
Fee: tx.Fee(),
Mint: *assetMint,
ValidRange: TimeRange{
tx.TTL(),
tx.ValidityIntervalStart(),
Expand Down Expand Up @@ -350,7 +360,10 @@ func sortInputs(inputs []lcommon.TransactionInput) []lcommon.TransactionInput {
return ret
}

func expandInputs(inputs []lcommon.TransactionInput, resolvedInputs []lcommon.Utxo) []ResolvedInput {
func expandInputs(
inputs []lcommon.TransactionInput,
resolvedInputs []lcommon.Utxo,
) []ResolvedInput {
ret := make([]ResolvedInput, len(inputs))
for i, input := range inputs {
for _, resolvedInput := range resolvedInputs {
Expand All @@ -371,8 +384,17 @@ func collapseOutputs(outputs []lcommon.Utxo) []lcommon.TransactionOutput {
return ret
}

func sortedRedeemerKeys(redeemers lcommon.TransactionWitnessRedeemers) []lcommon.RedeemerKey {
tags := []lcommon.RedeemerTag{lcommon.RedeemerTagSpend, lcommon.RedeemerTagMint, lcommon.RedeemerTagCert, lcommon.RedeemerTagReward, lcommon.RedeemerTagVoting, lcommon.RedeemerTagProposing}
func sortedRedeemerKeys(
redeemers lcommon.TransactionWitnessRedeemers,
) []lcommon.RedeemerKey {
tags := []lcommon.RedeemerTag{
lcommon.RedeemerTagSpend,
lcommon.RedeemerTagMint,
lcommon.RedeemerTagCert,
lcommon.RedeemerTagReward,
lcommon.RedeemerTagVoting,
lcommon.RedeemerTagProposing,
}
ret := make([]lcommon.RedeemerKey, 0)
for _, tag := range tags {
idxs := redeemers.Indexes(tag)
Expand All @@ -391,7 +413,10 @@ func sortedRedeemerKeys(redeemers lcommon.TransactionWitnessRedeemers) []lcommon
return ret
}

func redeemersInfo(witnessSet lcommon.TransactionWitnessSet, toScriptPurpose toScriptPurposeFunc) KeyValuePairs[ScriptInfo, Redeemer] {
func redeemersInfo(
witnessSet lcommon.TransactionWitnessSet,
toScriptPurpose toScriptPurposeFunc,
) KeyValuePairs[ScriptInfo, Redeemer] {
var ret KeyValuePairs[ScriptInfo, Redeemer]
redeemers := witnessSet.Redeemers()
redeemerKeys := sortedRedeemerKeys(redeemers)
Expand Down
16 changes: 13 additions & 3 deletions ledger/common/script/scriptcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func formatPlutusData(pd data.PlutusData) string {
return ret
}

func buildTxInfoV3(txHex string, inputsHex string, inputOutputsHex string) (TxInfo, error) {
func buildTxInfoV3(
txHex string,
inputsHex string,
inputOutputsHex string,
) (TxInfo, error) {
// Transaction
txBytes, err := hex.DecodeString(txHex)
if err != nil {
Expand Down Expand Up @@ -134,13 +138,19 @@ func TestScriptContextV3SimpleSend(t *testing.T) {
// Build script context
sc := NewScriptContextV3(txInfo, redeemer, purpose)
// Read expected structure from file
expectedBytes, err := os.ReadFile(`testdata/simple_send_expected_structure.txt`)
expectedBytes, err := os.ReadFile(
`testdata/simple_send_expected_structure.txt`,
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
expected := strings.TrimSpace(string(expectedBytes))
scPd := strings.TrimSpace(formatPlutusData(sc.ToPlutusData()))
if scPd != expected {
t.Fatalf("did not get expected structure\n\n got:\n\n%s\n\n wanted:\n\n%s", scPd, expected)
t.Fatalf(
"did not get expected structure\n\n got:\n\n%s\n\n wanted:\n\n%s",
scPd,
expected,
)
}
}
20 changes: 16 additions & 4 deletions ledger/common/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,36 @@ func TestScriptRefDecode(t *testing.T) {
}

func TestNativeScriptHash(t *testing.T) {
testScriptBytes, _ := hex.DecodeString("820181830301838200581c058a5ab0c66647dcce82d7244f80bfea41ba76c7c9ccaf86a41b00fe8200581c45cbc234959cb619ef54e36c16e7719318592e627cdf1a39bd3d64398200581c85fd53e110449649b709ef0fa93e86d99535bdce5db306ce0e7418fc")
testScriptBytes, _ := hex.DecodeString(
"820181830301838200581c058a5ab0c66647dcce82d7244f80bfea41ba76c7c9ccaf86a41b00fe8200581c45cbc234959cb619ef54e36c16e7719318592e627cdf1a39bd3d64398200581c85fd53e110449649b709ef0fa93e86d99535bdce5db306ce0e7418fc",
)
expectedScriptHash := "1c0053ec18e2c0f7bd4d007fe14243ca220563f9c124381f75c43704"
var testScript common.NativeScript
if err := testScript.UnmarshalCBOR(testScriptBytes); err != nil {
t.Fatalf("unexpected error decoding native script: %s", err)
}
tmpHash := testScript.Hash()
if tmpHash.String() != expectedScriptHash {
t.Errorf("did not get expected script hash, got %s, wanted %s", tmpHash.String(), expectedScriptHash)
t.Errorf(
"did not get expected script hash, got %s, wanted %s",
tmpHash.String(),
expectedScriptHash,
)
}
}

func TestPlutusV3ScriptHash(t *testing.T) {
testScriptBytes, _ := hex.DecodeString("587f01010032323232323225333002323232323253330073370e900118041baa0011323232533300a3370e900018059baa00513232533300f301100214a22c6eb8c03c004c030dd50028b18069807001180600098049baa00116300a300b0023009001300900230070013004375400229309b2b2b9a5573aaae7955cfaba157441")
testScriptBytes, _ := hex.DecodeString(
"587f01010032323232323225333002323232323253330073370e900118041baa0011323232533300a3370e900018059baa00513232533300f301100214a22c6eb8c03c004c030dd50028b18069807001180600098049baa00116300a300b0023009001300900230070013004375400229309b2b2b9a5573aaae7955cfaba157441",
)
testScript := common.PlutusV3Script(testScriptBytes)
expectedScriptHash := "2909c3d0441e76cd6ae1fc09664bb209868902e191c2b8c30b82d331"
tmpHash := testScript.Hash()
if tmpHash.String() != expectedScriptHash {
t.Errorf("did not get expected script hash, got %s, wanted %s", tmpHash.String(), expectedScriptHash)
t.Errorf(
"did not get expected script hash, got %s, wanted %s",
tmpHash.String(),
expectedScriptHash,
)
}
}