Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bid parsing for V1 #110

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions tests/data/bid/auction_bid_example_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"bonding_purse": "uref-b3c03358245a0d9514064b6d0c3dd90023d29a0fe137507d430a26990f5ce8e3-007",
"staked_amount": "900000000000",
"delegation_rate": 10,
"delegators": [
{
"public_key": "02038f1f65c0297281a9eb5c83a525121715a8c71f1cdddabe70ae795dcd31b1ba92",
"staked_amount": "555000000000",
"bonding_purse": "uref-41059a3aa5c5759c7f5e3c826cb47e0b21257ac57ee63a4c00dc305133e55d64-007",
"delegatee": "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070"
}
],
"inactive": false
}
20 changes: 20 additions & 0 deletions tests/data/bid/auction_bid_example_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61",
"bonding_purse": "uref-fafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafa-007",
"staked_amount": "900000000000",
"delegation_rate": 0,
"vesting_schedule": null,
"delegators": [
{
"delegator_public_key": "014508a07aa941707f3eb2db94c8897a80b2c1197476b6de213ac273df7d86c4ff",
"delegator": {
"delegator_public_key": "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070",
"staked_amount": "10",
"bonding_purse": "uref-fbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb-007",
"validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61",
"vesting_schedule": null
}
}
],
"inactive": false
}
41 changes: 41 additions & 0 deletions tests/types/bid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package types

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/v2/types"
)

func Test_Bid_MarshalUnmarshal(t *testing.T) {
tests := []struct {
name string
fixturePath string
}{
{
"Auction Bid V1",
"../data/bid/auction_bid_example_v1.json",
},
{
"Auction Bid V1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v2?

"../data/bid/auction_bid_example_v2.json",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
data, err := os.ReadFile(test.fixturePath)
require.NoError(t, err)

var bid types.Bid
err = json.Unmarshal(data, &bid)
require.NoError(t, err)

require.Equal(t, bid.StakedAmount.String(), "900000000000")
require.Equal(t, 1, len(bid.Delegators))
require.Equal(t, bid.Delegators[0].DelegatorPublicKey.ToHex(), "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070")
})
}
}
40 changes: 35 additions & 5 deletions types/bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ func (d *Delegators) UnmarshalJSON(data []byte) error {
}

publicKeyAndDelegators := make([]struct {
DelegatorPublicKey keypair.PublicKey `json:"delegator_public_key"`
Delegator Delegator `json:"delegator"`
DelegatorPublicKey *keypair.PublicKey `json:"delegator_public_key"`
Delegator Delegator `json:"delegator"`
}, 0)

if err := json.Unmarshal(data, &publicKeyAndDelegators); err == nil && len(publicKeyAndDelegators) > 0 {
err := json.Unmarshal(data, &publicKeyAndDelegators)
if err == nil && len(publicKeyAndDelegators) > 0 && publicKeyAndDelegators[0].DelegatorPublicKey != nil {
delegators := make(Delegators, 0, len(publicKeyAndDelegators))
for _, item := range publicKeyAndDelegators {
delegators = append(delegators, item.Delegator)
Expand All @@ -68,11 +69,16 @@ func (d *Delegators) UnmarshalJSON(data []byte) error {
return nil
}

delegators := make([]Delegator, 0)
if err := json.Unmarshal(data, &publicKeyAndDelegators); err != nil {
delegatorsV1 := make([]DelegatorV1, 0)
if err := json.Unmarshal(data, &delegatorsV1); err != nil {
return err
}

delegators := make(Delegators, 0, len(delegatorsV1))
for _, item := range delegatorsV1 {
delegators = append(delegators, NewDelegatorFromDelegatorV1(item))
}

*d = delegators
return nil
}
Expand All @@ -91,6 +97,30 @@ type Delegator struct {
VestingSchedule *VestingSchedule `json:"vesting_schedule"`
}

// DelegatorV1 of version 1 which is associated with the given validator.
type DelegatorV1 struct {
// The purse that was used for delegating.
BondingPurse key.URef `json:"bonding_purse"`
// Amount of Casper token (in motes) delegated
StakedAmount clvalue.UInt512 `json:"staked_amount"`
// Public Key of the delegator
Delegatee keypair.PublicKey `json:"delegatee"`
// Public key of the validator
ValidatorPublicKey keypair.PublicKey `json:"validator_public_key"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the schema please. I think the fields here are:

  • delegatee. the validator public key
  • public_key. the delegator public key.

// Vesting schedule for a genesis validator. `None` if non-genesis validator.
VestingSchedule *VestingSchedule `json:"vesting_schedule"`
}

func NewDelegatorFromDelegatorV1(v1 DelegatorV1) Delegator {
return Delegator{
BondingPurse: v1.BondingPurse,
StakedAmount: v1.StakedAmount,
DelegatorPublicKey: v1.Delegatee,
ValidatorPublicKey: v1.ValidatorPublicKey,
VestingSchedule: v1.VestingSchedule,
}
}

// Credit is a bridge record pointing to a new `ValidatorBid` after the public key was changed.
type Credit struct {
// The era id the credit was created.
Expand Down
Loading