Skip to content

Commit

Permalink
Extend transforms parsing (#107)
Browse files Browse the repository at this point in the history
Extend transfoms parsing
  • Loading branch information
ZhmakAS authored Jul 25, 2024
1 parent 477d7e0 commit fdac8c4
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 56 deletions.
2 changes: 1 addition & 1 deletion casper/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type (
NamedKey = types.NamedKey
TransformKey = types.TransformKey
Transform = types.Transform
TransformV2 = types.TransformV2
Argument = types.Argument
Account = types.Account
PublicKeyAndBid = types.PublicKeyAndBid
Reward = types.EraReward
WriteTransfer = types.WriteTransfer
UnbondingPurse = types.UnbondingPurse
Expand Down
28 changes: 28 additions & 0 deletions tests/data/transform/addressable_entity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"key": "entity-account-a3a8e3087123170fccbaef0cd14dae6127dbb4a959172836ba2d105dcc8179d8",
"kind": {
"Write": {
"AddressableEntity": {
"protocol_version": "2.0.1",
"entity_kind": {
"Account": "account-hash-a3a8e3087123170fccbaef0cd14dae6127dbb4a959172836ba2d105dcc8179d8"
},
"package_hash": "package-0527a12331d6ad8236946760e9d21081454372e808672068467c74f6afef6d87",
"byte_code_hash": "byte-code-0000000000000000000000000000000000000000000000000000000000000000",
"main_purse": "uref-9ad469145198891e38290940b28cde71aeefd7bd5a482139bbc6b23316819587-007",
"associated_keys": [
{
"account_hash": "account-hash-a3a8e3087123170fccbaef0cd14dae6127dbb4a959172836ba2d105dcc8179d8",
"weight": 1
}
],
"action_thresholds": {
"deployment": 1,
"upgrade_management": 1,
"key_management": 1
},
"message_topics": []
}
}
}
}
14 changes: 14 additions & 0 deletions tests/data/transform/bid_kind.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"key": "bid-addr-04da3cd8cc4c8f34e7731583e67ddc211ff9b5c3f2c52640582415c2cce9315b2aea00000000000000",
"kind": {
"Write": {
"BidKind": {
"Credit": {
"validator_public_key": "01032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c",
"era_id": 234,
"amount": "100000000"
}
}
}
}
}
10 changes: 10 additions & 0 deletions tests/data/transform/cl_value_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"key": "balance-b06a1ab0cfb52b5d4f9a08b68a5dbe78e999de0b0484c03e64f5c03897cf637b",
"transform": {
"WriteCLValue": {
"cl_type": "U512",
"bytes": "08f09a09bdd338c18a",
"parsed": "9998335129799990000"
}
}
}
12 changes: 12 additions & 0 deletions tests/data/transform/cl_value_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"key": "balance-86b4925adbefbfa5cbecc2994010e1291ac87a197c98c7b751ffbb860ea2a7ed",
"kind": {
"Write": {
"CLValue": {
"cl_type": "U512",
"bytes": "08f09a09bdd338c18a",
"parsed": "9998335129799990000"
}
}
}
}
8 changes: 8 additions & 0 deletions tests/data/transform/message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"key": "message-entity-contract-a9987538f3cceb823d627d6e28174fd7b50022c847db44b96c36077818e322ed-5721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e1-0",
"kind": {
"Write": {
"Message": "message-checksum-4fa4135e65967751f007064a7cbf6c17d27f7189cc97b7b0f484445c72ecbd6f"
}
}
}
11 changes: 11 additions & 0 deletions tests/data/transform/message_topic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"key": "message-topic-entity-contract-a9987538f3cceb823d627d6e28174fd7b50022c847db44b96c36077818e322ed-5721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e1",
"kind": {
"Write": {
"MessageTopic": {
"message_count": 1,
"blocktime": 1719847489116
}
}
}
}
19 changes: 19 additions & 0 deletions tests/data/transform/named_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"key": "named-key-entity-account-2eebf033107ad08de5e776ef98a0222452a13bb310c9070bae50c5b05a35c27b-dde6f264c89fe385a5b07c26d77284d6fddabe79653c5ca25cec39a6363e6ec7",
"kind": {
"Write": {
"NamedKey": {
"named_key": {
"cl_type": "Key",
"bytes": "023100736f594c5c112e8e2f15c1f758ac37209864959d68eac14fa67645f0d29a07",
"parsed": "uref-3100736f594c5c112e8e2f15c1f758ac37209864959d68eac14fa67645f0d29a-007"
},
"name": {
"cl_type": "String",
"bytes": "0b0000006d792d6b65792d6e616d65",
"parsed": "my-key-name"
}
}
}
}
}
21 changes: 21 additions & 0 deletions tests/data/transform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"key": "package-0527a12331d6ad8236946760e9d21081454372e808672068467c74f6afef6d87",
"kind": {
"Write": {
"Package": {
"versions": [
{
"entity_version_key": {
"protocol_version_major": 2,
"entity_version": 1
},
"addressable_entity_hash": "addressable-entity-a3a8e3087123170fccbaef0cd14dae6127dbb4a959172836ba2d105dcc8179d8"
}
],
"disabled_versions": [],
"groups": [],
"lock_status": "Locked"
}
}
}
}
137 changes: 137 additions & 0 deletions tests/types/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,143 @@ func Test_Transform_AddUInt512(t *testing.T) {
assert.EqualValues(t, 100000000, val.Value().Int64())
}

func Test_Transform_CLValue(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/cl_value_v1.json")
require.NoError(t, err)
var transform types.TransformKey

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

val, err := transform.Transform.ParseAsWriteCLValue()
require.NoError(t, err)
clValue, err := val.Value()
require.NoError(t, err)

assert.True(t, transform.Transform.IsWriteCLValue())
assert.EqualValues(t, "9998335129799990000", clValue.UI512.Value().String())
}

func Test_Transform_CLValue_V2(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/cl_value_v2.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

val, err := transform.Kind.ParseAsWriteCLValue()
require.NoError(t, err)
clValue, err := val.Value()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWriteCLValue())
assert.EqualValues(t, "9998335129799990000", clValue.UI512.Value().String())
}

func Test_Transform_Package(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/package.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

packageRes, err := transform.Kind.ParseAsWritePackage()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWritePackage())
assert.NotEmpty(t, packageRes.LockStatus)
assert.NotEmpty(t, packageRes.Versions)
}

func Test_Transform_AddressableEntity(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/addressable_entity.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

addressableEntity, err := transform.Kind.ParseAsWriteAddressableEntity()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWriteAddressableEntity())
assert.NotEmpty(t, addressableEntity.EntityKind)
assert.True(t, addressableEntity.EntityKind.Account != nil)
assert.NotEmpty(t, addressableEntity.MainPurse)
assert.NotEmpty(t, addressableEntity.AssociatedKeys)
assert.NotEmpty(t, addressableEntity.PackageHash)
}

func Test_Transform_BidKind(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/bid_kind.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

bidKind, err := transform.Kind.ParseAsWriteBidKind()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWriteBidKind())
assert.True(t, bidKind.Credit != nil)
assert.Equal(t, bidKind.Credit.Amount.String(), "100000000")
}

func Test_Transform_NamedKey(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/named_key.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

namedKey, err := transform.Kind.ParseAsWriteNamedKey()
require.NoError(t, err)

nameCLValue, err := namedKey.Name.Value()
require.NoError(t, err)

nameKeyCLValue, err := namedKey.NamedKey.Value()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWriteNamedKey())
assert.Equal(t, nameCLValue.StringVal.String(), "my-key-name")
assert.True(t, nameKeyCLValue.Key != nil)
}

func Test_Transform_Message(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/message.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

message, err := transform.Kind.ParseAsWriteMessage()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWriteMessage())
assert.Equal(t, string(*message), "message-checksum-4fa4135e65967751f007064a7cbf6c17d27f7189cc97b7b0f484445c72ecbd6f")
}

func Test_Transform_MessageTopic(t *testing.T) {
fixture, err := os.ReadFile("../data/transform/message_topic.json")
require.NoError(t, err)
var transform types.Transform

err = json.Unmarshal(fixture, &transform)
require.NoError(t, err)

messageTopic, err := transform.Kind.ParseAsWriteMessageTopic()
require.NoError(t, err)

assert.True(t, transform.Kind.IsWriteMessageTopic())
assert.Equal(t, messageTopic.MessageCount, uint32(1))
}

func Test_Transform_WriteDeployInfo(t *testing.T) {
fixute, err := os.ReadFile("../data/transform/WriteDeployInfo.json")
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions types/execution_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ExecutionResult struct {
Payment json.RawMessage `json:"payment"`
Transfers []Transfer `json:"transfers"`
SizeEstimate uint64 `json:"size_estimate"`
Effects []TransformV2 `json:"effects"`
Effects []Transform `json:"effects"`

originExecutionResultV1 *ExecutionResultV1
originExecutionResultV2 *ExecutionResultV2
Expand Down Expand Up @@ -89,10 +89,10 @@ func (v *ExecutionResult) UnmarshalJSON(data []byte) error {
}

func NewExecutionResultFromV1(v1 ExecutionResultV1) ExecutionResult {
transforms := make([]TransformV2, 0)
transforms := make([]Transform, 0)
if v1.Success != nil {
for _, transform := range v1.Success.Effect.Transforms {
transforms = append(transforms, TransformV2{
transforms = append(transforms, Transform{
Key: transform.Key,
Kind: TransformKind(transform.Transform),
})
Expand Down Expand Up @@ -143,7 +143,7 @@ func NewExecutionResultFromV1(v1 ExecutionResultV1) ExecutionResult {

if v1.Failure != nil {
for _, transform := range v1.Failure.Effect.Transforms {
transforms = append(transforms, TransformV2{
transforms = append(transforms, Transform{
Key: transform.Key,
// TODO: we should convert old Transform to new format
Kind: TransformKind(transform.Transform),
Expand All @@ -169,7 +169,7 @@ type ExecutionResultV2 struct {
Payment json.RawMessage `json:"payment"`
Transfers []Transfer `json:"transfers"`
SizeEstimate uint64 `json:"size_estimate"`
Effects []TransformV2 `json:"effects"`
Effects []Transform `json:"effects"`
}

type ExecutionResultV1 struct {
Expand Down Expand Up @@ -217,7 +217,7 @@ type ExecutionResultStatusData struct {
}

// Effects A log of all transforms produced during execution, used only in 2.0+ Network
type Effects []TransformV2
type Effects []Transform

type Effect struct {
Operations []Operation `json:"operations"`
Expand Down
Loading

0 comments on commit fdac8c4

Please sign in to comment.