Skip to content

Commit

Permalink
chore(pkg/scale): scale integration into genesis (ChainSafe#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj authored and timwu20 committed Dec 6, 2021
1 parent 024eaea commit 31458fd
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 250 deletions.
20 changes: 13 additions & 7 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -302,13 +302,19 @@ func setupSystemModule(t *testing.T) *SystemModule {
Nonce: 3,
//RefCount: 0,
Data: struct {
Free common.Uint128
Reserved common.Uint128
MiscFrozen common.Uint128
FreeFrozen common.Uint128
}{},
Free *scale.Uint128
Reserved *scale.Uint128
MiscFrozen *scale.Uint128
FreeFrozen *scale.Uint128
}{
Free: scale.MustNewUint128(big.NewInt(0)),
Reserved: scale.MustNewUint128(big.NewInt(0)),
MiscFrozen: scale.MustNewUint128(big.NewInt(0)),
FreeFrozen: scale.MustNewUint128(big.NewInt(0)),
},
}
aliceAcctEncoded, err := scale.Encode(aliceAcctInfo)

aliceAcctEncoded, err := scale.Marshal(aliceAcctInfo)
require.NoError(t, err)
ts.Set(aliceAcctStoKey, aliceAcctEncoded)

Expand Down
10 changes: 5 additions & 5 deletions dot/types/account.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
)

// AccountInfo Information of an account.
Expand All @@ -12,9 +12,9 @@ type AccountInfo struct {
Producers uint32
// The additional data that belongs to this account. Used to store the balance(s) in a lot of chains.
Data struct {
Free common.Uint128
Reserved common.Uint128
MiscFrozen common.Uint128
FreeFrozen common.Uint128
Free *scale.Uint128
Reserved *scale.Uint128
MiscFrozen *scale.Uint128
FreeFrozen *scale.Uint128
}
}
77 changes: 49 additions & 28 deletions lib/genesis/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"math/big"
"path/filepath"
"reflect"
Expand All @@ -32,8 +33,8 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
)

// NewGenesisFromJSONRaw parses a JSON formatted genesis file
Expand Down Expand Up @@ -161,10 +162,13 @@ func buildRawMap(m map[string]map[string]interface{}) (map[string]string, error)
for k, v := range m {
kv := new(keyValue)
kv.key = append(kv.key, k)
buildRawMapInterface(v, kv)
err := buildRawMapInterface(v, kv)
if err != nil {
return nil, err
}

if reflect.DeepEqual([]string{"palletBalances", "balances"}, kv.key) {
err := buildBalances(kv, res)
err = buildBalances(kv, res)
if err != nil {
return nil, err
}
Expand All @@ -187,36 +191,46 @@ func buildRawMap(m map[string]map[string]interface{}) (map[string]string, error)
return res, nil
}

func buildRawMapInterface(m map[string]interface{}, kv *keyValue) {
func buildRawMapInterface(m map[string]interface{}, kv *keyValue) error {
for k, v := range m {
kv.key = append(kv.key, k)
switch v2 := v.(type) {
case []interface{}:
kv.valueLen = big.NewInt(int64(len(v2)))
buildRawArrayInterface(v2, kv)
err := buildRawArrayInterface(v2, kv)
if err != nil {
return err
}
case string:
kv.value = v2
}
}
return nil
}

func buildRawArrayInterface(a []interface{}, kv *keyValue) {
func buildRawArrayInterface(a []interface{}, kv *keyValue) error {
for _, v := range a {
switch v2 := v.(type) {
case []interface{}:
buildRawArrayInterface(v2, kv)
err := buildRawArrayInterface(v2, kv)
if err != nil {
return err
}
case string:
// todo check to confirm it's an address
tba := crypto.PublicAddressToByteArray(common.Address(v2))
kv.value = kv.value + fmt.Sprintf("%x", tba)
kv.iVal = append(kv.iVal, tba)
case float64:
// TODO: determine how to handle this error
encVal, _ := scale.Encode(uint64(v2))
encVal, err := scale.Marshal(uint64(v2))
if err != nil {
return err
}
kv.value = kv.value + fmt.Sprintf("%x", encVal)
kv.iVal = append(kv.iVal, big.NewInt(int64(v2)))
}
}
return nil
}

func formatKey(kv *keyValue) (string, error) {
Expand Down Expand Up @@ -247,7 +261,7 @@ func formatValue(kv *keyValue) (string, error) {
switch {
case reflect.DeepEqual([]string{"grandpa", "authorities"}, kv.key):
if kv.valueLen != nil {
lenEnc, err := scale.Encode(kv.valueLen)
lenEnc, err := scale.Marshal(kv.valueLen)
if err != nil {
return "", err
}
Expand All @@ -259,7 +273,7 @@ func formatValue(kv *keyValue) (string, error) {
return kv.value, nil
default:
if kv.valueLen != nil {
lenEnc, err := scale.Encode(kv.valueLen)
lenEnc, err := scale.Marshal(kv.valueLen)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -287,19 +301,19 @@ func buildBalances(kv *keyValue, res map[string]string) error {
Nonce: 0,
//RefCount: 0,
Data: struct {
Free common.Uint128
Reserved common.Uint128
MiscFrozen common.Uint128
FreeFrozen common.Uint128
Free *scale.Uint128
Reserved *scale.Uint128
MiscFrozen *scale.Uint128
FreeFrozen *scale.Uint128
}{
Free: *common.Uint128FromBigInt(kv.iVal[i+1].(*big.Int)),
Reserved: *common.Uint128FromBigInt(big.NewInt(0)),
MiscFrozen: *common.Uint128FromBigInt(big.NewInt(0)),
FreeFrozen: *common.Uint128FromBigInt(big.NewInt(0)),
Free: scale.MustNewUint128(kv.iVal[i+1].(*big.Int)),
Reserved: scale.MustNewUint128(big.NewInt(0)),
MiscFrozen: scale.MustNewUint128(big.NewInt(0)),
FreeFrozen: scale.MustNewUint128(big.NewInt(0)),
},
}

encBal, err := scale.Encode(accInfo)
encBal, err := scale.Marshal(accInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -360,34 +374,41 @@ func addAuthoritiesValues(k1, k2 string, kt crypto.KeyType, value []byte, gen *G

// decode authorities values into []interface that will be decoded into json array
ava := [][]interface{}{}
buf := &bytes.Buffer{}
sd := scale.Decoder{Reader: buf}
_, err := buf.Write(value)
reader := new(bytes.Buffer)
_, err := reader.Write(value)
if err != nil {
return err
}

alen, err := sd.DecodeInteger()
var alen int
err = scale.Unmarshal(value, &alen)
if err != nil {
return err
}
for i := 0; i < int(alen); i++ {
for i := 0; i < alen; i++ {
auth := []interface{}{}
buf := make([]byte, 32)
if _, err = sd.Reader.Read(buf); err == nil {
if _, err = reader.Read(buf); err == nil {
var arr = [32]byte{}
copy(arr[:], buf)
//nolint
pa, err := bytesToAddress(kt, arr[:])
if err != nil {
return err
}
auth = append(auth, pa)
}
iv, err := sd.DecodeFixedWidthInt(uint64(0))
b := make([]byte, 8)
if _, err = reader.Read(b); err != nil {
log.Fatal(err)
}
var iv uint64
err = scale.Unmarshal(b, &iv)

if err != nil {
return err
}
auth = append(auth, iv.(uint64))
auth = append(auth, iv)
ava = append(ava, auth)
}

Expand Down
Loading

0 comments on commit 31458fd

Please sign in to comment.