Skip to content

Commit

Permalink
fix map sorting in plutusData
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvionied committed Apr 25, 2024
1 parent df0e5f2 commit e5979f8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
76 changes: 76 additions & 0 deletions ApolloBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,5 +1155,81 @@ func TestMapPDDecodeEncode(t *testing.T) {
fmt.Println(val, hex.EncodeToString(recoded))
t.Error("Tx is not correct")
}
}

func TestMapPdRecode(t *testing.T) {
for i := 0; i < 100; i++ {

pdCbor := "d8799fa200a140a1401a01312d0001a09fd8799f4040ffd8799f581c95a427e384527065f2f8946f5e86320d0117839a5e98ea2c0b55fb004448554e54ffffd8799f581c500cebfc153990c15c001e3441a0e0774634cfe1864d4f2e07e17474472f426b424b4f41ffa3457072696365d87b9fd8799f1b0001b57b632d33951b00071afd498d0000ffff47656e6444617465d905009f1b000003bb2cc3d418ff49737461727444617465d905009f1b00000092f3973818ffa0ff"
pd := PlutusData.PlutusData{}
decodedCbor, _ := hex.DecodeString(pdCbor)
err := cbor.Unmarshal(decodedCbor, &pd)
if err != nil {
t.Error(err)
}
recoded, err := cbor.Marshal(pd)
if err != nil {
t.Error(err)
}
if hex.EncodeToString(recoded) != pdCbor {
fmt.Println(pdCbor, hex.EncodeToString(recoded))
t.Error("Tx is not correct after", i)
break
}
}
}

func TestRecodeTx(t *testing.T) {
for i := 0; i < 100; i++ {
txCbor := "84ac0083825820cda3926787c51f124e530d255fe8841aac9eed3be2fe518c8a44c801e75b204e0182582010453108b22eb9ea4c43e2aef257f269b6624f39dce0175fff44513bcc2f297200825820cda3926787c51f124e530d255fe8841aac9eed3be2fe518c8a44c801e75b204e040185a30058391155ff0e63efa0694e8065122c552e80c7b51768b7f20917af25752a7cf68864a338ae8ed81f61114d857cb6a215c8e685aa5c43bc1f879cce01821a004c4b40a2581c420000029ad9527271b1b1e3c27ee065c18df70a4a4cfc3093a41a44a14341584f1a45632e08581cc6b287b7b14d19db292f203824ac676b128eeebe8d09c3612755a09ba14754514e634d504101028201d81858d9d8799fa200a1581c420000029ad9527271b1b1e3c27ee065c18df70a4a4cfc3093a41a44a14341584f1a45632e0801a09fd8799f581c420000029ad9527271b1b1e3c27ee065c18df70a4a4cfc3093a41a444341584fffd8799f4040ffffd8799f581cc6b287b7b14d19db292f203824ac676b128eeebe8d09c3612755a09b4754514e634d5041ffa3457072696365d87b9fd8799f1b00029e05316a59991b000005af3107a400ffff47656e6444617465d905009f1b000003bb2cc3d418ff49737461727444617465d905009f1b00000092f3973818ffa0ff8258390137dce7298152979f0d0ff71fb2d0c759b298ac6fa7bc56b928ffc1bcf68864a338ae8ed81f61114d857cb6a215c8e685aa5c43bc1f879cce821a001e8480a1581cc6b287b7b14d19db292f203824ac676b128eeebe8d09c3612755a09ba14754514e634d5041018258390137dce7298152979f0d0ff71fb2d0c759b298ac6fa7bc56b928ffc1bcf68864a338ae8ed81f61114d857cb6a215c8e685aa5c43bc1f879cce821a001e8480a1581cf0fc4932134ad45a42ef9e5591a16dc88fff230bc4487eb0f1774f8ca1475250364146484101825839018b0fac6777891b925d646af1727d1ef288338f7966455a93ce31dfbc3120b7074a8521bcc9ea2f05e38cf8924ffb7bc871ebfaa35ae98bf21a000f42408258390137dce7298152979f0d0ff71fb2d0c759b298ac6fa7bc56b928ffc1bcf68864a338ae8ed81f61114d857cb6a215c8e685aa5c43bc1f879cce1a1deeb464021a0004c1b2031a074cf233075820b83a67b95c0136a1490445e62f9878de1ed0a5f3fd0e7d143741a923d88f820e081a074ceeaf09a1581cc6b287b7b14d19db292f203824ac676b128eeebe8d09c3612755a09ba14754514e634d5041020b58208a0898f7e654d715598e9bd1bd6d9467bd35cd41e112edfb0b704895592949040d81825820b65b15369b13ee118915606e86f28f0a84df22f599deafbd11aa9b7671623655000e82581c37dce7298152979f0d0ff71fb2d0c759b298ac6fa7bc56b928ffc1bc581cf68864a338ae8ed81f61114d857cb6a215c8e685aa5c43bc1f879cce108258390137dce7298152979f0d0ff71fb2d0c759b298ac6fa7bc56b928ffc1bcf68864a338ae8ed81f61114d857cb6a215c8e685aa5c43bc1f879cce1a00181f91111a004c4b40a30380068159022659022301000032323232323232323232323232223232323232533300c3370e9000001099999199111980711299980a80089128008a99980919baf301630180010041300530180011300230170010012322230020033756602c0026ea4004dd7180900099199180511998011bab001232223002003374c002244a002464a66601e6ae8c0044894004488c00800ccc02894ccc03ccdd78009ba8480004894004488c00800c004004dd598091918091809180900098088021299980699807180780099111801001a4008264649319999806111299980a00089128008a9998089801180b800899111801001980b800899801801180b0009199119baf374e60300046e9cc06000530012bd8799fd8799f5820cda3926787c51f124e530d255fe8841aac9eed3be2fe518c8a44c801e75b204eff01ff00001001200116332300c22533301300114bd700998079801980b0009801180a8009180a180a8009bac30130051533300d3300e300f0013222300200332337029000000a40082930b0b0b180980118070009baa300f300e002300f300e001300e001223300422533300b00110051323330053011300f00223300933760601c602000600200420026004601a00200297adef6c602323002233002002001230022330020020015740ae6888cc0088cc0088cdc38010008a5013300124a0294494ccc00800448940044c94ccc00c0044c888c00800cdd69804180300109128009802000aab9f5573aae895d0918011baa0015573d0581840100d87980821a0001778c1a0225e159f5a21902a2a1636d7367816f44657868756e7465722054726164651902d1a178386336623238376237623134643139646232393266323033383234616336373662313238656565626538643039633336313237353561303962a16754514e634d5041a964616c676fa4646c696e6b82784068747470733a2f2f6170702e61786f2e74726164652f636f6d706f7365722f61326634383739302d393432362d343361372d383134372d61656135313761386363663761646e616d656c536d617274204d61726b6574647574786f82784036393762386437356463323736613835383734653834363438313738626635653538363862646335356461623161323338613136643834343835613765626165a2696e756d657261746f728200806b64656e6f6d696e61746f72820181016b6465736372697074696f6e6064696e6974a263414441a2696e756d657261746f728200806b64656e6f6d696e61746f72820181016341584fa2696e756d657261746f728201811a45632e086b64656e6f6d696e61746f7282018101646e616d6577536d617274204d61726b6574203c41584f2c204144413e65696d6167657835697066733a2f2f516d517a4a64464e7a39564e42384e53764c6242546a4e7a6f74794e59353656387376664141557a5259626e3738657374726174a3646c696e6b782d68747470733a2f2f6170702e61786f2e74726164652f737472617465676965732f766965772f54514e634d5041646e616d65606b6465736372697074696f6e60666173736574738281783e3432303030303032396164393532373237316231623165336332376565303635633138646637306134613463666333303933613431613434343135383466816066706172616d73a4657072696365a2696e756d657261746f728201821ad77c1fd1016b64656e6f6d696e61746f728201811a0400000066616d6f756e74a2696e756d657261746f728201811a45632e086b64656e6f6d696e61746f728201810167656e644461746574323039392d31322d33315432333a35393a35395a6973746172744461746574313938392d31322d33315432333a35393a35395a67776562736974657168747470733a2f2f61786f2e7472616465696d656469615479706569696d6167652f706e67"
tx := Transaction.Transaction{}
decodedCbor, _ := hex.DecodeString(txCbor)
err := cbor.Unmarshal(decodedCbor, &tx)
if err != nil {
t.Error(err)

}
recoded, err := cbor.Marshal(tx)
if err != nil {
t.Error(err)

}
if hex.EncodeToString(recoded) != txCbor {
fmt.Println(txCbor, hex.EncodeToString(recoded))
t.Error("Tx is not correct")
break
}
}
}

func TestMapWithCbor(t *testing.T) {
for i := 0; i < 100; i++ {
data := PlutusData.PlutusData{
TagNr: 121,
PlutusDataType: PlutusData.PlutusMap,
Value: map[serialization.CustomBytes]PlutusData.PlutusData{

serialization.NewCustomBytes("endDate"): PlutusData.PlutusData{
PlutusDataType: PlutusData.PlutusInt,
Value: 100,
},
serialization.NewCustomBytes("startDate"): PlutusData.PlutusData{
PlutusDataType: PlutusData.PlutusInt,
Value: 100,
}, serialization.NewCustomBytes("price"): PlutusData.PlutusData{
PlutusDataType: PlutusData.PlutusInt,
Value: 100,
},
},
}
encoded, err := cbor.Marshal(data)
if err != nil {
t.Error(err)
}
if hex.EncodeToString(encoded) != "d879a3457072696365186447656e64446174651864497374617274446174651864" {
t.Error(hex.EncodeToString(encoded))
break
}
}

}
27 changes: 22 additions & 5 deletions serialization/PlutusData/PlutusData.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ func (pia *PlutusIndefArray) Clone() PlutusIndefArray {
func (pia PlutusIndefArray) MarshalCBOR() ([]uint8, error) {
res := make([]byte, 0)
res = append(res, 0x9f)

for _, el := range pia {
bytes, err := cbor.Marshal(el)
if err != nil {
Expand Down Expand Up @@ -910,12 +909,30 @@ func (pd *PlutusData) Clone() PlutusData {
error: An error, if any, during ecoding.
*/
func (pd *PlutusData) MarshalCBOR() ([]uint8, error) {
enc, _ := cbor.CanonicalEncOptions().EncMode()
if pd.TagNr == 0 {
return enc.Marshal(pd.Value)
//enc, _ := cbor.CanonicalEncOptions().EncMode()
if pd.PlutusDataType == PlutusMap {
customEnc, _ := cbor.EncOptions{Sort: cbor.SortBytewiseLexical}.EncMode()
if pd.TagNr != 0 {
return customEnc.Marshal(cbor.Tag{Number: pd.TagNr, Content: pd.Value})
} else {
return customEnc.Marshal(pd.Value)
}
} else if pd.PlutusDataType == PlutusIntMap {
canonicalenc, _ := cbor.CanonicalEncOptions().EncMode()
if pd.TagNr != 0 {
return canonicalenc.Marshal(cbor.Tag{Number: pd.TagNr, Content: pd.Value})
} else {
return canonicalenc.Marshal(pd.Value)
}
} else {
return enc.Marshal(cbor.Tag{Number: pd.TagNr, Content: pd.Value})
//enc, _ := cbor.EncOptions{Sort: cbor.SortCTAP2}.EncMode()
if pd.TagNr == 0 {
return cbor.Marshal(pd.Value)
} else {
return cbor.Marshal(cbor.Tag{Number: pd.TagNr, Content: pd.Value})
}
}

}

/*
Expand Down

0 comments on commit e5979f8

Please sign in to comment.