Skip to content

Commit

Permalink
Update transaction target serialization (#126)
Browse files Browse the repository at this point in the history
Update transaction target serialization
  • Loading branch information
ZhmakAS authored Dec 19, 2024
1 parent 6ab70e8 commit 4296921
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 71 deletions.
2 changes: 1 addition & 1 deletion tests/data/rpc_response/query_global_state_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
},
"merkle_proof": "01000000108e08c43f144a13c915cf3681cc97bcd98c6a81d7b5da5164dc066318ec1c80a70c010000000200000001000000a5cf5917505ef60a6f0df395dd19e86a0f075d00f2e6ce49f5aa0e18f6e26f5d00000000000000000002000000008e4c0000000000c6f1b9ca884c35c3326f3d17cd3947de0481fb65b49740b344f1ae42e739eab90100774e41e1b426ed159fb3f2c28f9da9cf6c02fbb3a3892ca9abe5bf8dfe1e97f60300b966a0acb632ef7be2c3f72a854fa002ef3d106e2786dc95f044e21e05f9c0690700e7bd117780d831bad6659884e2fede9481d5e017182bfe45d861fdc706a4634b0c002b0443ed2d501c615226e97389e3131abc95ab47efa13bb5397e942739424bae0d01c4fe0149516b296fa27396e2437df1e56a90881588f02b457a2958856b1045da0e00b607c92dbeacd588d09b052b27df065efaf4c22bcc375ec14d1c4d0a39aba9d11200d099ff451321429ddf5191d9d990fc4deadab71b5367f91ba46236eed63666ca1600e3147b25c0f6828fc491419b6324c811f363d78c93821f3d2359f5ebc5bcefaf1700e2ec7344576f036136854be95a86a01e33ad14857d4485846b844b793c4595fb1900821a9ffe26974f853292ba1db87866ef0eef09a362183749910b2b8ff5eac2781a007dbc45026bb32d8e94ccf3c61b086ab527c8260c42e39eb1509f5e2d848322342000a3b0a3230c905ca68ef8be2c8cef6966e553f40afeeb4de329169474cda8f76b2300e477825f1d399f2ab13aab656a1b07ceb27230f2966be67c571834c125a51397260008b0575ce185f76ad15c9bed036fe93abc529e72749eade6247bcb63813aef3d2b0060c5e051c3ed621cbbe70552b8a41e64cdf7ad2a2fe47d64a48c366d6d307c692c017f79f9ad0b1b2bcce42f10b9323c8bd3cd858233701e837fb6f6cdc965b7d8862f000e8f81b4ac8bc3bb538cf40640ba3179958ec5fc9efdd35b9d7842dc754f2bc83200d7b01d7d8336b597e9f633fdf4dfdf014e9228de0ee728f3ffe2d7cc21d72fba35000c0153f39054e4da3c22686f1251ab3ff4e312c3942236c6aa3712a2acd47ebd36003ae82e35741833a317fbdbd2ef311b623a007e0be8530e4198202ccf484d58e43900d4a2b5745bc4007ce5719207638ee8619d5afdae312b22f75e066711c154df033d0097b349d9e2698fc472c7069bed1a3144f3f851e44301b2dafa07eac08a29dd36410048bdbc914f67b165b27184dac9dcc5859b283a5fde253aff265b6aa88229a4114a00c19636043ca5aca8877330597f36deb46b0ff068f735fee5e6b44e9df886d4964b00e4c4bdff0bd2d01e161dc09c2d43f98ce8710e7703ed29397d1cda90753d62e8520004f0dbd1b4997e616418c3e312d7444b2af44fa4db65334d28189b2dd6c3c80d54000722b9ba9b708a7549e5c14f8fd3f39c340ac0d09897f2c3013e49f2358ce92d5700be4f10bd1a1d7ae6bd10147f4f9ad8ce5ef45815cf70e63d4655f66acf414a8b580009e8bc02cab67a51cc70348d5533150c3dac78376d6049c2e9ac1e94e5f9b74c5a00580ad3cf2a9c4244517e4f279853247ba410c896b849cd662a98014bd29662c05c008fe23b757defdfb411505e026fbb82a221c6d8bf6e0b87e6eb27e87332fafd0161001518fb461fe158ead01b5ab7029f529eeefc2893b16aed9b08f9ce6ad205f4736200a1b4de3bd2586d51554f38a51c32bc1c6a5dce26b3302e49c41815b40d96bff5630056ddc17c50f24bedae39d40eee8eaf0491c8dd9db290d88b4190546f5c79a4d56501da68a246394955babd0d5c32ee90b637bbb331114829cf2d8f669752f897f8cc6f001bce4b2c8eb7b76e692911485d815e1bd56296b302d00fef449f60ec28db63c17400c641fedc9c3787e773cbe2852bd0d800b2d3f83fea8223959e6713b1294ab8ba7b008ebc297d8ddc318b98ca7fc531d8a154e5da2c023b12b1423d1a42bbf018e43c86009eb367f18e7874245c67411174980e949f199db863b2d7ca2e272462f1c1ae91900008d1213c949a5ef250e6860f76e65d1f19923d83d61e084293524aff86ca39529700c483ab0652e3655bb41d76181b3a82eccd808c238e292d67ead2d701b207d18a980071df1f5b6f8d385f7ee3b53227ffc00fe378a156f0192265ee59ff593a4af51c990035902928a51aec7c193c9d46397268cf8a6fbf2869081d1a9f8fe3987ccda3d19e017687466da7a31fb22afdd4256cbbb3af20dcbb5b444d3d597cdf9e921f9b1de49f00916dea0a9fcea5057882e93cb384e5c37fb5ed23a12b11b0436f017f945246f7a80069b1450b7b0a4711d2078bee4541f80f27787020d69831e6b81264027eecb83fa9003900a4c49b3e135e7e393a4310326f13f726eb968936af097f8ea490179f6da8ae01b363bc91f88d6171da6f9fc9bd39f8439f6023e86889e7b977dce9d9bf99f1a5b100ca84d074d3880da1feb663e61feb528e1feab85c591c2655a9928267091230aeb200974a3215119d5efdcf8f7deb1c51fdbe5233a1f7d697d1fe28d6fc4166a3f064b700efa00f9b7a0e27152fe243600e92ddaa106759d67bab8e54b6e94c6ffb586818ba00f786f408a82e634b9e7d760c1b787590942723cb4f97513ad54af356dab1fa29bc007863057d60a44f4c758e74cb123c62f3f95396c67ebfe94e4b8e968f2fad5059bd009df58fd05e6fc940efa0399b19cb1a865e7c1740c8fb82b50c2f1120b44bc733c000891a4140189337efde21c296b3feb9b303771a74ea38ccc984720d192104bcc8c100cd523a7097d4d15eff78334192b332e4efbc3449c79a0feb868cd1aadc1ba753c600f6605c8cf03765578231df2198baf4b37a94393fc606db5a14077f5b3d1a9620c700121ce42239b1df3f79cec58359035ece958f43ddd19a37d13c4656c00277c571cd00da11d5fee206969cc8bfc50c63ab6520d631730a878bc45a1d419587283ef684cf00b557524c6d1dda28d269f76315a8951a739746f57df940daa3431a72b0a2a0bed100f33fda91a746be3955675bb6a4d5fdbbefed2613a2c51599d0d449a46887def9d2000581762135c895af0908fa1f42d84c54cb562d486d97931f788d28ba25e502ecd700a3a558e617f0a34a352877e83101140ded84349c9563d1f0eb4f2aa18a0c9003d800b4d8d94e1300c6be8d6b8f950e88d23495433d9cb1c12e502c6499e2391b43bcda000921cd1e32f7fd85d75a1336cd4d0260aabb6c35db8c00685ac8b8ace9e1c378de0026302f3b115160c544c753e031ebcf073c8049c5e1464ebaa00d6f6466aadf43e301a668c4b5922c114acf5d35782746ef1b43a9f6948bcb82f9b88fb9adeedc5610e9006c4e66734f97a3435744bc7dd4b99ec782fc669caf8dc8733bb787ce5d984311ed00312ec19f0765f39974dba780c9b1790c69490511abd5e2218b9819f404e1cb0eef01dbc3765bcd0099ba66ce81948f48c4abbd0c52fad1f40d6af4535dc2cb36d876f4002ffd5f178bd2b916b12578f0865bb12284aa3e964dda6c82984e573e147ece99f600e984b94fab98142dfb1974f9805b7299788f438584eed13d9be933fe26dfb062fa00a573449ba54e24b065c017b7b004e0983b59bb8469d4e8d402989372fd6c8737fb00ef19c8edfb37c0e0327fa9eb41de351fca6a78349790c663dc0662ec2c0d2d3afd0095fc72cdbbd1424fafe3f9f2269a69c7c11674147beb341d031b82b46edd33f00010100000000001f3bf20a63548bc9a9d4dc43c3e6939222ef64f305905d32545a334be05a5bf3102014e6dfb996b829283050c84f04797970c7637419fc07a1b2db1aa780e82317d98060155089e4b39eaba1b8e5259ae150b2aa0a9a0da0a757803c0ee57520a194178cf090106f8f679d0cf2a59896501b999fbffe04a643391483a530974586248c7f5eadb0a00a88a2ec26e14b9984f2f7588fc27dcbde754144ac8991fcdcb1555e05d5a3c720b0068c1ef6e0fd9083cf80b028d1eaef64e2d6b8f7875d18bf67d8fb8d997d89bd70d009f8aacc0ef174fce35805337a386e3651bc5460955b034a1056aa806305c59810e0043d39741a6e7b395eace3b1fbbbd5bbf288f7a788e80745a2e2bb0f2173a8b4f0f013e0bbc0cb4947a47dd6cdd447bd2d5ef64928eea5453e4ffc06fa2886c3ec498110107e372c43ea33f4681bf87bb1064bf55c72bf1c0b6ec86989c574b088854fbff1201f7f3aadccd1c9690c80c2951ffac31cc422bb79af88ae21c92f1aba1366311ad13014a957c2567b348d50f47437361689b8eabbf38a9c3026afb972354df5bb37df8140157b785ba1107cbfb473e1f8ed37fbc861ac94387fc404b1013d2ec220107e2b61501726a256369aa5572f95b45eba8e7951e6c77f6a049bc378637532c888081dea61601df3764330f12ce0a96cf32d6efd641d64d1b5612b73deae82518ad5a18f9a22617016f094feaf99dfd69a891215d3bb347e47def925b703b579713ef5b5ed6ecd30c"
}
}
}
2 changes: 1 addition & 1 deletion types/key/entity_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
SystemKind EntityKind = iota
// AccountKind `EntityKind::Account` variant.
AccountKind
// SmartContractKind `EntityKind::SmartContract` variant.
// SmartContractKind `EntityKind::Package` variant.
SmartContractKind
)

Expand Down
1 change: 1 addition & 0 deletions types/stored_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ type StoredValue struct {
NamedKey *NamedKeyValue `json:"NamedKey,omitempty"`
Prepayment *PrepaymentKind `json:"Prepayment,omitempty"`
EntryPoint *EntryPointValue `json:"EntryPoint,omitempty"`
RawBytes *string `json:"RawBytes,omitempty"`
}
26 changes: 0 additions & 26 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,32 +173,6 @@ type TransactionV1 struct {
Approvals []Approval `json:"approvals"`
}

type TransactionV1Header struct {
// `Hash` of the body part of this `Deploy`.
BodyHash key.Hash `json:"body_hash"`
ChainName string `json:"chain_name"`
// `Timestamp` formatted as per RFC 3339
Timestamp Timestamp `json:"timestamp"`
// Duration of the `Deploy` in milliseconds (from timestamp).
TTL Duration `json:"ttl"`
// The address of the initiator of a TransactionV1.
InitiatorAddr InitiatorAddr `json:"initiator_addr"`
// Pricing mode of a Transaction.
PricingMode PricingMode `json:"pricing_mode"`
}

type TransactionV1Body struct {
Args *Args `json:"args,omitempty"`
// Execution target of a Transaction.
Target TransactionTarget `json:"target"`
// Entry point of a Transaction.
TransactionEntryPoint TransactionEntryPoint `json:"entry_point"`
// Scheduling mode of a Transaction.
TransactionScheduling TransactionScheduling `json:"scheduling"`
// Transaction category
TransactionCategory uint8 `json:"transaction_category"`
}

// TransactionHash A versioned wrapper for a transaction hash or deploy hash
type TransactionHash struct {
Deploy *key.Hash `json:"Deploy,omitempty"`
Expand Down
24 changes: 23 additions & 1 deletion types/transaction_runtime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package types

// TransactionRuntime SmartContract transaction types.
import (
"github.com/make-software/casper-go-sdk/v2/types/serialization"
"github.com/make-software/casper-go-sdk/v2/types/serialization/encoding"
)

// TransactionRuntime Package transaction types.
type TransactionRuntime string

const (
Expand All @@ -21,3 +26,20 @@ func (t TransactionRuntime) RuntimeTag() byte {
}
return 0
}

func (t *TransactionRuntime) Bytes() ([]byte, error) {
builder, err := serialization.NewCallTableSerializationEnvelopeBuilder(t.serializedFieldLengths())
if err != nil {
return nil, err
}
if err = builder.AddField(TagFieldIndex, []byte{t.RuntimeTag()}); err != nil {
return nil, err
}
return builder.BinaryPayloadBytes()
}

func (t TransactionRuntime) serializedFieldLengths() []int {
return []int{
encoding.U8SerializedLength,
}
}
53 changes: 11 additions & 42 deletions types/transaction_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ const (
SessionIsInstallIndex uint16 = iota + 1
SessionRuntimeIndex
SessionModuleBytesIndex
SessionTransferredValueIndex
SessionSeedIndex
)

const (
StoredIdIndex uint16 = iota + 1
StoredRuntimeIndex
StoredTransferredValueIndex
)

const (
Expand All @@ -46,17 +43,14 @@ func (t *TransactionTarget) SerializedLength() int {
}

type StoredTarget struct {
ID TransactionInvocationTarget `json:"id"`
Runtime TransactionRuntime `json:"runtime"`
TransferredValue uint64 `json:"transferred_value"`
ID TransactionInvocationTarget `json:"id"`
Runtime TransactionRuntime `json:"runtime"`
}

type SessionTarget struct {
ModuleBytes []byte `json:"module_bytes"`
Runtime TransactionRuntime `json:"runtime"`
TransferredValue uint64 `json:"transferred_value"`
IsInstallUpgrade bool `json:"is_install_upgrade"`
Seed *key.Hash `json:"seed,omitempty"`
}

func (t *TransactionTarget) Bytes() ([]byte, error) {
Expand Down Expand Up @@ -84,11 +78,12 @@ func (t *TransactionTarget) Bytes() ([]byte, error) {
return nil, err
}

runtimeBytes, _ := encoding.NewStringToBytesEncoder(string(t.Stored.Runtime)).Bytes()
if err = builder.AddField(StoredRuntimeIndex, runtimeBytes); err != nil {
runtimeBytes, err := t.Stored.Runtime.Bytes()
if err != nil {
return nil, err
}
if err = builder.AddField(StoredTransferredValueIndex, []byte{byte(t.Stored.TransferredValue)}); err != nil {

if err = builder.AddField(StoredRuntimeIndex, runtimeBytes); err != nil {
return nil, err
}
case t.Session != nil:
Expand All @@ -101,30 +96,17 @@ func (t *TransactionTarget) Bytes() ([]byte, error) {
return nil, err
}

if err = builder.AddField(SessionRuntimeIndex, []byte{t.Session.Runtime.RuntimeTag()}); err != nil {
return nil, err
}

moduleBytes, _ := encoding.NewBytesToBytesEncoder(t.Session.ModuleBytes).Bytes()
if err = builder.AddField(SessionModuleBytesIndex, moduleBytes); err != nil {
runtimeBytes, err := t.Session.Runtime.Bytes()
if err != nil {
return nil, err
}

transferredValuesBytes, _ := encoding.NewU64ToBytesEncoder(t.Session.TransferredValue).Bytes()
if err = builder.AddField(SessionTransferredValueIndex, transferredValuesBytes); err != nil {
if err = builder.AddField(SessionRuntimeIndex, runtimeBytes); err != nil {
return nil, err
}

var seedBytes []byte
if t.Session.Seed != nil {
seedBytes = []byte{1} // Option Some tag
bytes, _ := encoding.NewStringToBytesEncoder(t.Session.Seed.String()).Bytes()
seedBytes = append(seedBytes, bytes...)
} else {
seedBytes = []byte{0} // Option none tag
}

if err = builder.AddField(SessionSeedIndex, seedBytes); err != nil {
moduleBytes, _ := encoding.NewBytesToBytesEncoder(t.Session.ModuleBytes).Bytes()
if err = builder.AddField(SessionModuleBytesIndex, moduleBytes); err != nil {
return nil, err
}
default:
Expand All @@ -148,18 +130,11 @@ func (t TransactionTarget) serializedFieldLengths() []int {
encoding.U64SerializedLength,
}
case t.Session != nil:
var seedSerializedLength int
if t.Session.Seed != nil {
seedSerializedLength = encoding.StringSerializedLength(t.Session.Seed.String())
}

return []int{
encoding.U8SerializedLength,
encoding.BoolSerializedLength,
encoding.U8SerializedLength,
encoding.BytesSerializedLength(t.Session.ModuleBytes),
encoding.U64SerializedLength,
encoding.U8SerializedLength + seedSerializedLength,
}
default:
return []int{}
Expand All @@ -171,9 +146,7 @@ func (t *TransactionTarget) UnmarshalJSON(data []byte) error {
Stored *StoredTarget `json:"Stored"`
Session *struct {
Runtime TransactionRuntime `json:"runtime"`
TransferredValue uint64 `json:"transferred_value"`
IsInstallUpgrade bool `json:"is_install_upgrade"`
Seed *key.Hash `json:"seed,omitempty"`
Module string `json:"module_bytes"`
} `json:"Session"`
}
Expand All @@ -188,9 +161,7 @@ func (t *TransactionTarget) UnmarshalJSON(data []byte) error {
Session: &SessionTarget{
ModuleBytes: decodedBytes,
Runtime: target.Session.Runtime,
TransferredValue: target.Session.TransferredValue,
IsInstallUpgrade: target.Session.IsInstallUpgrade,
Seed: target.Session.Seed,
},
}
}
Expand Down Expand Up @@ -241,9 +212,7 @@ func (t TransactionTarget) MarshalJSON() ([]byte, error) {
}{
Session: sessionTarget{
Runtime: t.Session.Runtime,
TransferredValue: t.Session.TransferredValue,
IsInstallUpgrade: t.Session.IsInstallUpgrade,
Seed: t.Session.Seed,
ModuleBytes: hex.EncodeToString(t.Session.ModuleBytes),
},
})
Expand Down

0 comments on commit 4296921

Please sign in to comment.