From 2073561493d318ae0f0b53b16d399298f0c02c7a Mon Sep 17 00:00:00 2001 From: ZhmakAS Date: Mon, 2 Dec 2024 17:23:19 +0100 Subject: [PATCH] Implement DelegatorKind support fot Bid (#122) * Implement DelegatorKind support fot Bid --- tests/data/era/era_summary_v2.json | 126 +++++++++++++++-------------- tests/types/era_summary_test.go | 35 ++++++-- types/bid.go | 34 ++++++++ types/bid_kind.go | 3 + types/era_info.go | 54 ++++++++++++- 5 files changed, 181 insertions(+), 71 deletions(-) diff --git a/tests/data/era/era_summary_v2.json b/tests/data/era/era_summary_v2.json index 7a1b305..1e9f9c9 100644 --- a/tests/data/era/era_summary_v2.json +++ b/tests/data/era/era_summary_v2.json @@ -1,67 +1,69 @@ { - "jsonrpc": "2.0", - "id": "1", - "result": { - "api_version": "2.0.0", - "era_summary": { - "block_hash": "34f7c0246c6dd3b4ae7161a59c6834a9aaa12f74b8d2c4d16ec347a9b8a90407", - "era_id": 15383, - "stored_value": { - "EraInfo": { - "seigniorage_allocations": [ - { - "Validator": { - "validator_public_key": "01032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c", - "amount": "100542274855185" - } + "block_hash": "34f7c0246c6dd3b4ae7161a59c6834a9aaa12f74b8d2c4d16ec347a9b8a90407", + "era_id": 15383, + "stored_value": { + "EraInfo": { + "seigniorage_allocations": [ + { + "Validator": { + "validator_public_key": "01032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c", + "amount": "100542274855185" + } + }, + { + "Validator": { + "validator_public_key": "01032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c", + "amount": "84485499521" + } + }, + { + "Validator": { + "validator_public_key": "0126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf", + "amount": "88379190136305" + } + }, + { + "Validator": { + "validator_public_key": "0126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf", + "amount": "84493160372" + } + }, + { + "Validator": { + "validator_public_key": "0140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c", + "amount": "99527758907914" + } + }, + { + "Validator": { + "validator_public_key": "0140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c", + "amount": "84483913675" + } + }, + { + "Validator": { + "validator_public_key": "017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b", + "amount": "83817182779548" + } + }, + { + "Validator": { + "validator_public_key": "017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b", + "amount": "84494615864" + } + }, + { + "Delegator": { + "delegator_kind": { + "PublicKey": "0202bb58b5feca505c74edc000d8282fc556e51a1024fc8e7d7e56c6f887c5c8d5f2" }, - { - "Validator": { - "validator_public_key": "01032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c", - "amount": "84485499521" - } - }, - { - "Validator": { - "validator_public_key": "0126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf", - "amount": "88379190136305" - } - }, - { - "Validator": { - "validator_public_key": "0126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf", - "amount": "84493160372" - } - }, - { - "Validator": { - "validator_public_key": "0140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c", - "amount": "99527758907914" - } - }, - { - "Validator": { - "validator_public_key": "0140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c", - "amount": "84483913675" - } - }, - { - "Validator": { - "validator_public_key": "017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b", - "amount": "83817182779548" - } - }, - { - "Validator": { - "validator_public_key": "017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b", - "amount": "84494615864" - } - } - ] + "validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61", + "amount": "1000000000" + } } - }, - "state_root_hash": "6e8ed1a812f9518ee39277f77f707955a3f73b2c775df7424b2da175625b8f2e", - "merkle_proof": "010000000b000000000000000000000000000000000000000000000000000000000000000007080000000001032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c0611e19352715b0001032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c05816abaab13000126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf06f1cd3b626150000126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf05b44f2fac13000140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c060a0ab71c855a000140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c05cb37a2ab1300017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b069cd867353b4c00017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b05388545ac1301000000000b140000000001510e5f331b1a84b14252fe3913d3afb152f287462714c108b2cd03c29164207801018eb48c820fb106611a9dc65b5516c76267c7a546c8b0f7f87ccad67dc1afa1f50201d39dc1d39d36b8c59e9355cdd55adf4d769cc6d63db915fe69ea94a24e8454840301a86c2de6d91155a59adda00169c1372f409b8e71aca5ecc95888e75ab4df366e0401744d6727bd6c08d70a1d49fa3fcb0397230595e628ed0c52f5a78d3d59c6cdbb06014bcefe3ed7eb0d929ffb1b06975df2caf7476f9beb3b0828be78fd766730668d0801732d8be21ba8d9d166007384999a8f4887b01534151e5d22a1f388be99412fc409018b587e1127177e34a3e470c3bc536da9f89345f0bcfdeb6b548be732d984d82c0a00aa359968ebc13aeb0ec170d86c230e35d478037aa4cc1441b186501323f5d9c90d00d133641314a03c3da85597bc23aaa10b56ecc43efc1035c69d76463cecb633a00e0043d39741a6e7b395eace3b1fbbbd5bbf288f7a788e80745a2e2bb0f2173a8b4f0f018e1ee0db6abadb25182adb757ee0fb68645c62892c43bdb5f66f04ddd49c26db1001d515393ea4ba786b0a887b03406e563b8bb6eec7130c2b74af11c301edb19242110177049afcfe3f07f8fb1ff429073573fd537ec93a14c0aaad71141afaac088093120176706b834e9776d305d9d5face0a7c273a646827ffc02c4f0fdf92192767ca3b1301b86fb4cf28dec81b269a278ed437302f10921d86fe16a4de0e0240f735ee8f5d1401e127033e641533f0ee99f0612c9e20c86e9c1974aada7e03af57ae81ef58c09b1501d789c8bba61eab50b62479a596b99f4268f8ae0a4c770e71ab75ed870b6ecda1160109ee69158b030e49dd6f79b65ea0dea62cca0df0163c7b08dc7379ac2e5265a91701ccc91d447ba4fa1d39e26b2e4c45cee12291790d0e54d95ce0c71ce798cc02de" + ] } - } + }, + "state_root_hash": "6e8ed1a812f9518ee39277f77f707955a3f73b2c775df7424b2da175625b8f2e", + "merkle_proof": "010000000b000000000000000000000000000000000000000000000000000000000000000007080000000001032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c0611e19352715b0001032146b0b9de01e26aaec7b0d1769920de94681dbd432c3530bfe591752ded6c05816abaab13000126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf06f1cd3b626150000126d4637eb0c0769274f03a696df1112383fa621c9f73f57af4c5c0fbadafa8cf05b44f2fac13000140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c060a0ab71c855a000140afe8f752e5ff100e0189c080bc207e8805b3e5e82f792ec608de2f11f39f6c05cb37a2ab1300017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b069cd867353b4c00017536433a73f7562526f3e9fcb8d720428ae2d28788a9909f3c6f637a9d848a4b05388545ac1301000000000b140000000001510e5f331b1a84b14252fe3913d3afb152f287462714c108b2cd03c29164207801018eb48c820fb106611a9dc65b5516c76267c7a546c8b0f7f87ccad67dc1afa1f50201d39dc1d39d36b8c59e9355cdd55adf4d769cc6d63db915fe69ea94a24e8454840301a86c2de6d91155a59adda00169c1372f409b8e71aca5ecc95888e75ab4df366e0401744d6727bd6c08d70a1d49fa3fcb0397230595e628ed0c52f5a78d3d59c6cdbb06014bcefe3ed7eb0d929ffb1b06975df2caf7476f9beb3b0828be78fd766730668d0801732d8be21ba8d9d166007384999a8f4887b01534151e5d22a1f388be99412fc409018b587e1127177e34a3e470c3bc536da9f89345f0bcfdeb6b548be732d984d82c0a00aa359968ebc13aeb0ec170d86c230e35d478037aa4cc1441b186501323f5d9c90d00d133641314a03c3da85597bc23aaa10b56ecc43efc1035c69d76463cecb633a00e0043d39741a6e7b395eace3b1fbbbd5bbf288f7a788e80745a2e2bb0f2173a8b4f0f018e1ee0db6abadb25182adb757ee0fb68645c62892c43bdb5f66f04ddd49c26db1001d515393ea4ba786b0a887b03406e563b8bb6eec7130c2b74af11c301edb19242110177049afcfe3f07f8fb1ff429073573fd537ec93a14c0aaad71141afaac088093120176706b834e9776d305d9d5face0a7c273a646827ffc02c4f0fdf92192767ca3b1301b86fb4cf28dec81b269a278ed437302f10921d86fe16a4de0e0240f735ee8f5d1401e127033e641533f0ee99f0612c9e20c86e9c1974aada7e03af57ae81ef58c09b1501d789c8bba61eab50b62479a596b99f4268f8ae0a4c770e71ab75ed870b6ecda1160109ee69158b030e49dd6f79b65ea0dea62cca0df0163c7b08dc7379ac2e5265a91701ccc91d447ba4fa1d39e26b2e4c45cee12291790d0e54d95ce0c71ce798cc02de" } diff --git a/tests/types/era_summary_test.go b/tests/types/era_summary_test.go index 375acff..e6c6ff8 100644 --- a/tests/types/era_summary_test.go +++ b/tests/types/era_summary_test.go @@ -11,14 +11,33 @@ import ( ) func Test_EraSummary_MarshalUnmarshal_ShouldReturnSameResult(t *testing.T) { - fixture, err := os.ReadFile("../data/era/era_summary_example.json") - assert.NoError(t, err) + tests := []struct { + name string + fixturePath string + }{ + { + "V1 EraSummary", + "../data/era/era_summary_example.json", + }, + { + "V2 EraSummary", + "../data/era/era_summary_v2.json", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + fixture, err := os.ReadFile(test.fixturePath) + assert.NoError(t, err) - var era types.EraSummary - err = json.Unmarshal(fixture, &era) - assert.NoError(t, err) + var era types.EraSummary + err = json.Unmarshal(fixture, &era) + assert.NoError(t, err) - result, err := json.Marshal(era) - assert.NoError(t, err) - assert.JSONEq(t, string(fixture), string(result)) + for _, summary := range era.StoredValue.EraInfo.SeigniorageAllocations { + if summary.Delegator != nil { + assert.NotNil(t, summary.Delegator.DelegatorKind.PublicKey) + } + } + }) + } } diff --git a/types/bid.go b/types/bid.go index 2987a53..cf61184 100644 --- a/types/bid.go +++ b/types/bid.go @@ -145,3 +145,37 @@ type VestingSchedule struct { InitialReleaseTimestampMillis uint64 `json:"initial_release_timestamp_millis"` LockedAmounts []clvalue.UInt512 `json:"locked_amounts"` } + +// Reservation represents a validator reserving a slot for specific delegator +type Reservation struct { + // Individual delegation rate. + DelegationRate uint8 `json:"delegation_rate"` + // Validator's public key. + ValidatorPublicKey keypair.PublicKey `json:"validator_public_key"` + // Delegator kind. + DelegatorKind DelegatorKind `json:"delegator_kind"` +} + +type Unbond struct { + // Validator's public key. + ValidatorPublicKey keypair.PublicKey `json:"validator_public_key"` + // Unbond kind + UnbondKind UnbondKind `json:"unbond_kind"` + // List of Unbond eras + Eras []UnbondEra `json:"eras"` +} + +type UnbondKind struct { + Validator keypair.PublicKey `json:"Validator"` + DelegatedPublicKey keypair.PublicKey `json:"DelegatedPublicKey"` + DelegatedPurse key.URef `json:"DelegatedPurse"` +} + +type UnbondEra struct { + // Unbond amount + Amount clvalue.UInt512 `json:"amount"` + // Unbound era + EraOfCreation uint64 `json:"era_of_creation"` + // The purse was used for bonding + BondingPurse key.URef `json:"bonding_purse"` +} diff --git a/types/bid_kind.go b/types/bid_kind.go index 77ace6e..be1b9ee 100644 --- a/types/bid_kind.go +++ b/types/bid_kind.go @@ -12,4 +12,7 @@ type BidKind struct { Bridge *Bridge `json:"Bridge,omitempty"` // New validator public key associated with the bid. Credit *Credit `json:"Credit,omitempty"` + // Represents a validator reserving a slot for specific delegator + Reservation *Reservation `json:"Reservation,omitempty"` + Unbond *Unbond `json:"Unbond,omitempty"` } diff --git a/types/era_info.go b/types/era_info.go index 2057a42..89faaeb 100644 --- a/types/era_info.go +++ b/types/era_info.go @@ -1,7 +1,11 @@ package types import ( + "encoding/json" + "errors" + "github.com/make-software/casper-go-sdk/v2/types/clvalue" + "github.com/make-software/casper-go-sdk/v2/types/key" "github.com/make-software/casper-go-sdk/v2/types/keypair" ) @@ -26,9 +30,57 @@ type ValidatorAllocation struct { type DelegatorAllocation struct { // Public key of the delegator - DelegatorPublicKey keypair.PublicKey `json:"delegator_public_key"` + DelegatorKind DelegatorKind `json:"delegator_kind"` // Public key of the validator ValidatorPublicKey keypair.PublicKey `json:"validator_public_key"` // Amount allocated as a reward. Amount clvalue.UInt512 `json:"amount"` } + +func (t *DelegatorAllocation) UnmarshalJSON(data []byte) error { + if t == nil { + return errors.New("json.RawMessage: UnmarshalJSON on nil pointer") + } + temp := struct { + // Public key of the delegator + DelegatorKind *DelegatorKind `json:"delegator_kind"` + // Public key of the validator + DelegatorPublicKey *keypair.PublicKey `json:"delegator_public_key"` + // Public key of the validator + ValidatorPublicKey keypair.PublicKey `json:"validator_public_key"` + // Amount allocated as a reward. + Amount clvalue.UInt512 `json:"amount"` + }{} + + if err := json.Unmarshal(data, &temp); err != nil { + return err + } + + if temp.DelegatorKind != nil { + *t = DelegatorAllocation{ + DelegatorKind: *temp.DelegatorKind, + ValidatorPublicKey: temp.ValidatorPublicKey, + Amount: temp.Amount, + } + } else if temp.DelegatorPublicKey != nil { + *t = DelegatorAllocation{ + DelegatorKind: DelegatorKind{ + PublicKey: temp.DelegatorPublicKey, + }, + ValidatorPublicKey: temp.ValidatorPublicKey, + Amount: temp.Amount, + } + } else { + return errors.New("unexpected DelegatorAllocation format") + } + + return nil +} + +// DelegatorKind Auction bid variants. Kinds of delegation bids. +type DelegatorKind struct { + // Delegation from public key. + PublicKey *keypair.PublicKey `json:"PublicKey,omitempty"` + // Delegation from purse. + Purse *key.URef `json:"Purse,omitempty"` +}