Skip to content

Commit

Permalink
Implement uint128 field encoding for genesis. (#1732)
Browse files Browse the repository at this point in the history
* Implement uint128 field encoding for genesis.

* Address comments and fix failing tests.

* Fix failing tests.

* Remove unused buffer.
  • Loading branch information
arijitAD authored Aug 17, 2021
1 parent 8207704 commit 9ffde69
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions chain/dev/genesis.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions chain/gssmr/genesis.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/genesis/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func generateStorageValue(i interface{}, idx int) ([]byte, error) {
)

switch t := reflect.Indirect(val).Field(idx).Interface().(type) {
case int64, uint64, uint32:
case int64, uint64, uint32, *scale.Uint128:
encode, err = scale.Marshal(t)
if err != nil {
return nil, err
Expand Down
7 changes: 3 additions & 4 deletions lib/genesis/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ func TestNewGenesisFromJSON(t *testing.T) {
expRaw["top"]["0x1cb6f36e027abb2091cfb5110ab5087f5e0621c4869aa60c02be9adcc98a0d1d"] = "0x08d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0100000000000000" // raw babe authorities
expRaw["top"]["0x26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"] = "0x0000000000000000000000007aeb9049000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" // raw system account
expRaw["top"][common.BytesToHex(common.UpgradedToDualRefKey)] = "0x01"
expRaw["top"]["0x426e15054d267946093858132eb537f1a47a9ff5cd5bf4d848a80a0b1a947dc3"] = "0x0000000000000000" // Society
expRaw["top"]["0x426e15054d267946093858132eb537f1a47a9ff5cd5bf4d848a80a0b1a947dc3"] = "0x00000000000000000000000000000000" // Society
expRaw["top"]["0x426e15054d267946093858132eb537f1ba7fb8745735dc3be2a2c61a72c39e78"] = "0x0101d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" // Society
expRaw["top"]["0x426e15054d267946093858132eb537f1d0b4a3f7631f0c0e761898fe198211de"] = "0xe7030000" // Society
expRaw["top"]["0x426e15054d267946093858132eb537f1d0b4a3f7631f0c0e761898fe198211de"] = "0xe7030000" // Staking
expRaw["top"]["0x426e15054d267946093858132eb537f1d0b4a3f7631f0c0e761898fe198211de"] = "0xe7030000" // Society // Staking
expRaw["top"]["0x5f3e4907f716ac89b6347d15ececedca138e71612491192d68deab7e6f563fe1"] = "0x02000000" // Staking
expRaw["top"]["0x5f3e4907f716ac89b6347d15ececedca5579297f4dfb9609e7e4c2ebab9ce40a"] = "0x80be5ddb1579b72e84524fc29e78609e3caf42e85aa118ebfe0b0ad404b5bdd25f" // Staking
expRaw["top"]["0x5f3e4907f716ac89b6347d15ececedcaac0a2cbf8e355f5ea6cb2de8727bfb0c"] = "0x54000000" // Staking
Expand All @@ -90,7 +89,7 @@ func TestNewGenesisFromJSON(t *testing.T) {
expRaw["top"]["0xcec5070d609dd3497f72bde07fc96ba0726380404683fc89e8233450c8aa1950ed43a85541921049696d6f6e80d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"] = "0x" // Session
expRaw["top"]["0xcec5070d609dd3497f72bde07fc96ba0726380404683fc89e8233450c8aa1950f5537bdb2a1f626b6772616e8088dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee"] = "0x" // Session
expRaw["top"]["0x11f3ba2e1cdd6d62f2ff9b5589e7ff81ba7fb8745735dc3be2a2c61a72c39e78"] = zeroByte // Instance1Collective
expRaw["top"]["0x5f3e4907f716ac89b6347d15ececedca28dccb559b95c40168a1b2696581b5a7"] = "0x0000000000000000" // Instance1Collective
expRaw["top"]["0x5f3e4907f716ac89b6347d15ececedca28dccb559b95c40168a1b2696581b5a7"] = "0x00000000000000000000000000000000" // Staking.CanceledSlashPayout
expRaw["top"]["0x8985776095addd4789fccbce8ca77b23ba7fb8745735dc3be2a2c61a72c39e78"] = "0x08d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" // Instance2Collective
expRaw["top"]["0x492a52699edf49c972c21db794cfcf57ba7fb8745735dc3be2a2c61a72c39e78"] = zeroByte // Instance1Membership
// Contract
Expand Down
24 changes: 13 additions & 11 deletions lib/genesis/pallet.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package genesis

import "github.com/ChainSafe/gossamer/pkg/scale"

type contracts struct {
CurrentSchedule struct {
Version uint32 `json:"version"`
Expand Down Expand Up @@ -120,20 +122,20 @@ type contracts struct {
}

type society struct {
Pot uint64 `json:"Pot"` //TODO: figure out the type
MaxMembers uint32 `json:"MaxMembers"` //TODO: figure out the type
Members []string `json:"Members"`
Pot *scale.Uint128 `json:"Pot"`
MaxMembers uint32 `json:"MaxMembers"`
// TODO: figure out the correct encoding format of members field
Members []string `json:"Members"`
}

type staking struct {
HistoryDepth uint32 `json:"HistoryDepth"`
ValidatorCount uint32 `json:"ValidatorCount"`
MinimumValidatorCount uint32 `json:"MinimumValidatorCount"`
Invulnerables []string `json:"Invulnerables"`
ForceEra string `json:"ForceEra"`
SlashRewardFraction uint32 `json:"SlashRewardFraction"`
// TODO: Implement custom decoding for this field.
CanceledSlashPayout int64 `json:"CanceledSlashPayout"`
HistoryDepth uint32 `json:"HistoryDepth"`
ValidatorCount uint32 `json:"ValidatorCount"`
MinimumValidatorCount uint32 `json:"MinimumValidatorCount"`
Invulnerables []string `json:"Invulnerables"`
ForceEra string `json:"ForceEra"`
SlashRewardFraction uint32 `json:"SlashRewardFraction"`
CanceledSlashPayout *scale.Uint128 `json:"CanceledSlashPayout"`
// TODO: figure out below fields storage key.
// Stakers [][]interface{} `json:"Stakers"`
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/scale/uint128.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,19 @@ func (u *Uint128) trimBytes(b []byte, order binary.ByteOrder) []byte {
}
return b
}

// UnmarshalJSON converts data to Uint128.
func (u *Uint128) UnmarshalJSON(data []byte) error {
intVal, ok := big.NewInt(0).SetString(string(data), 10)
if !ok {
return fmt.Errorf("failed to unmarshal Uint128")
}

dec, err := NewUint128(intVal)
if err != nil {
return err
}
u.Upper = dec.Upper
u.Lower = dec.Lower
return nil
}

0 comments on commit 9ffde69

Please sign in to comment.