Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
merges develop
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Jan 23, 2019
2 parents c459eb1 + 1eea8a4 commit e446a66
Show file tree
Hide file tree
Showing 26 changed files with 1,300 additions and 77 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,26 @@ test:
test/wasm:
@go test -v wasm/*.go

.PHONY: test/rpc
test/rpc:
@go test -v rpc/*.go

.PHONY: test/p2p
test/p2p:
@go test -v p2p/*.go

.PHONY: test/types
test/types:
@go test -v types/*.go

.PHONY: test/runtime
test/runtime:
@go test -v runtime/*.go $(ARGS)

.PHONY: test/clientdb
test/clientdb:
@go test -v clientdb/*.go

.PHONY: test/common
test/common:
@go test -v $$(go list ./... | grep common/)
Expand Down
27 changes: 27 additions & 0 deletions clientdb/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package clientdb

import (
"github.com/c3systems/go-substrate/common/db"
)

func createU8a(dbs *db.BaseDB, fn func()) StorageMethodU8a {
// TODO
return StorageMethodU8a{}
}

func createBn(dbs *db.BaseDB, fn func(), n int) StorageMethodBn {
// TODO
return StorageMethodBn{}
}

// NewBlockDB ...
func NewBlockDB(dbs *db.BaseDB) *BlockDB {
return &BlockDB{
DB: *dbs,
BestHash: createU8a(dbs, KeyBestHash()),
BestNumber: createBn(dbs, KeyBestNumber(), 64),
BlockData: createU8a(dbs, KeyBlockByHash()),
Header: createU8a(dbs, KeyHeaderByHash()),
Hash: createU8a(dbs, KeyHashByNumber()),
}
}
4 changes: 4 additions & 0 deletions clientdb/clientdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package clientdb

// https://github.com/polkadot-js/client/tree/master/packages/client-db/src
// TODO
6 changes: 6 additions & 0 deletions clientdb/clientdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package clientdb

import "testing"

func TestClientDB(t *testing.T) {
}
121 changes: 121 additions & 0 deletions clientdb/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package clientdb

// StorageFunctionMetadata, StorageFunctionModifier, StorageFunctionType from @polkadot/types/Metadata/Modules
// createFunction from @polkadot/storage/utils/createFunction

// MetadataType ...
type MetadataType struct {
Key string
Value string
}

// SubstrateMetadata ...
type SubstrateMetadata struct {
Documentation []string
Type MetadataType
}

// CreateItemOptions ...
type CreateItemOptions struct {
IsUnhashed bool
Key string
}

// StorageFunctionMetadata ...
type StorageFunctionMetadata struct {
//Documention *Vector
//Modifier *Modifier
//Type *StorageFunction
//ToJSON *
}

// CreateFunc ...
func CreateFunc(StorageFunctionMetadata, CreateItemOptions) func() {
// TODO
return func() {
// TODO

}
}

// CreateMethod is small helper function to factorize code on this page
func CreateMethod(method string, key string, meta SubstrateMetadata) func() {
b := 1
if meta.Type.Value == "" {
b = 0
}
_ = b

// TODO
return CreateFunc(
//NewText("Block"), // @polkadot/types/Text
//NewText(method),
StorageFunctionMetadata{
/*
Documentation: NewVector(Text, meta.Documention), // @polkadot/types/codec/Vector
Modifier: NewStorageFunctionModifier(0),
Type: NewStorageFunction(meta.Type, b),
*/
//ToJSON: func() interface{} {
// return key
//},
},
CreateItemOptions{
IsUnhashed: false,
Key: key,
},
)
}

// KeyBestHash ...
func KeyBestHash() func() {
return CreateMethod("bestHash", "bst:hsh", SubstrateMetadata{
Documentation: []string{"Best hash"},
Type: MetadataType{
Key: "Hash",
},
})
}

// KeyBestNumber ...
func KeyBestNumber() func() {
return CreateMethod("bestNumber", "bst:num", SubstrateMetadata{
Documentation: []string{"Best block"},
Type: MetadataType{
Key: "BlockNumber",
},
})
}

// KeyBlockByHash ...
func KeyBlockByHash() func() {
return CreateMethod("blockByHash", "blk:hsh", SubstrateMetadata{
Documentation: []string{"Retrieve block by hash"},
Type: MetadataType{
Key: "Hash",
Value: "Bytes",
},
})
}

// KeyHashByNumber ...
func KeyHashByNumber() func() {
return CreateMethod("hashByNumber", "hsh:num", SubstrateMetadata{
Documentation: []string{"Retrieve hash by number"},
Type: MetadataType{
Key: "u256",
Value: "Hash",
},
})
}

// KeyHeaderByHash ...
func KeyHeaderByHash() func() {
return CreateMethod("headerByHash", "hdr:hsh", SubstrateMetadata{
Documentation: []string{"Retrieve header by hash"},
Type: MetadataType{
Key: "Hash",
Value: "Bytes",
},
})
}
1 change: 1 addition & 0 deletions clientdb/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package clientdb
66 changes: 66 additions & 0 deletions clientdb/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package clientdb

import (
"github.com/c3systems/go-substrate/common/db"
"github.com/c3systems/go-substrate/common/triedb"
)

// DefaultPath ...
var DefaultPath = "~/.go-substrate"

// DefaultType ...
var DefaultType = "disk"

// DBPathPrefix ...
var DBPathPrefix = "database"

// DBConfigType ...
var DBConfigType = "disk" // other option is "memory"

// DBConfig ...
type DBConfig struct {
Compact bool
IsTrieDb bool
Path string
Snapshot bool
Type string // DBConfigType
}

// BlockDB ...
type BlockDB struct {
DB db.BaseDB
BestHash StorageMethodU8a
BestNumber StorageMethodBn
BlockData StorageMethodU8a
Hash StorageMethodU8a
Header StorageMethodU8a
}

// StateDB ...
type StateDB struct {
db *triedb.TrieDB
code StorageMethodU8a
}

// ChainDbs ...
type ChainDbs interface {
Snapshot()
}

// StorageMethodU8a ...
// TODO
type StorageMethodU8a struct {
//Del(params ...interface{})
//Get(params ...interface{}) []uint8
//Set(value []uint8, params ...interface{})
//OnUpdate(callback func(value []uint8))
}

// StorageMethodBn ...
// TODO
type StorageMethodBn struct {
//Del(params ...interface{})
//Get(params ...interface{}) *big.Int
//Set(value *big.Int, params ...interface{})
//OnUpdate(callback func(value *big.Int))
}
55 changes: 20 additions & 35 deletions common/bnutil/bnutil.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package bnutil

import (
"encoding/hex"
"math"
"math/big"

"github.com/c3systems/go-substrate/common/hexutil"
"github.com/c3systems/go-substrate/common/mathutil"
"github.com/c3systems/go-substrate/common/u8util"
)

// FromHex creates a math/big big number from a hex string.
func FromHex(hexStr string) (*big.Int, error) {
return hexutil.ToBN(hexStr, false)
return hexutil.ToBN(hexStr, false, false)
}

// ToBN creates a BN value from a number input
Expand Down Expand Up @@ -46,10 +49,8 @@ func ToBN(ivalue interface{}, isLittleEndian bool) *big.Int {
case uint:
return big.NewInt(int64(v))
case []uint8:
i := new(big.Int)
i.SetBytes(v)
hx := ToHex(i, -1)
n, err := hexutil.ToBN(hx, isLittleEndian)
hx := hex.EncodeToString(v)
n, err := hexutil.ToBN(hx, isLittleEndian, false)
if err != nil {
panic(err)
}
Expand All @@ -60,48 +61,32 @@ func ToBN(ivalue interface{}, isLittleEndian bool) *big.Int {
}

// ToHex creates a hex value from a math/big big number. 0 inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. With `bitLength` set, it fixes the number to the specified length.
func ToHex(value *big.Int, bitLength int) string {
return hexutil.HexFixLength(value.Text(16), bitLength, true)
func ToHex(value *big.Int, bitLength int, isNegative bool) string {
slice := ToUint8Slice(value, bitLength, false, false)
return u8util.ToHex(slice, bitLength, true)
}

// ToUint8Slice creates a uint8 array from a big number. Empty input returns an empty uint8 array result. Convert using little-endian format if `isLittleEndian` is set.
func ToUint8Slice(value *big.Int, bitLength int, isLittleEndian bool) []uint8 {
bufLength := int(math.Ceil(float64(bitLength) / float64(8)))
func ToUint8Slice(value *big.Int, bitLength int, isLittleEndian bool, isNegative bool) []uint8 {
var byteLength int
if bitLength == -1 {
byteLength = int(math.Ceil(float64(mathutil.BitLen(value)) / float64(8)))
} else {
byteLength = int(math.Ceil(float64(bitLength) / float64(8)))
}

if value == nil {
if bitLength == -1 {
return []uint8{}
}
return make([]uint8, bufLength)
return make([]uint8, byteLength)
}

if bitLength == -1 {
output, err := hexutil.ToUint8Slice(
ToHex(value, bitLength),
-1,
)
if err != nil {
panic(err)
}
return output
}

output := make([]uint8, bufLength)
b := value.Bytes()

for index := 0; index < bufLength; index++ {
if isLittleEndian {
if index < len(b) {
output[len(b)-index-1] = uint8(b[index])
}
} else {
if index < len(b) && len(b)-index > 0 {
output[len(output)-index-1] = uint8(b[len(b)-index-1])
}
}
if isNegative {
value = mathutil.ToTwos(value, byteLength*8)
}

return output
return mathutil.ToUint8Slice(value, isLittleEndian, byteLength)
}

// Uint8Slice ...
Expand Down
Loading

0 comments on commit e446a66

Please sign in to comment.