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

Commit

Permalink
[client/db] storage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Feb 17, 2019
1 parent 06c32e0 commit 7fb7053
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 49 deletions.
4 changes: 4 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func NewChain(config *clienttypes.ConfigClient) (*Chain, error) {
bestNumber := c.Blocks.BestNumber.Get(nil)
logGenesis := ""

if bestNumber == nil {
log.Fatal("[client/chain] bestNumber is nil")
}

if bestNumber.Cmp(big.NewInt(0)) != 0 {
logGenesis = fmt.Sprintf("(genesis %s)", u8util.ToHex(c.Genesis.Block.Hash[:], 48, true))
}
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) Start(config *clienttypes.ConfigClient) {
if err != nil {
log.Fatal(err)
}
c.P2P, err = p2p.New(context.Background(), nil, nil, config, c.Chain)
c.P2P, err = p2p.NewP2P(context.Background(), nil, nil, config, c.Chain)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions client/db/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ func createU8a(dbs db.BaseDB, fn types.StorageFunction) StorageMethodU8a {
return NewStorageMethodU8a(dbs, fn)
}

func createBn(dbs db.BaseDB, fn types.StorageFunction, n int) StorageMethodBn {
// TODO
return StorageMethodBn{}
func createBn(dbs db.BaseDB, createKey types.StorageFunction, bitLen int) StorageMethodBn {

return NewStorageMethodBn(dbs, createKey, bitLen)
}

// NewBlockDB ...
Expand Down
43 changes: 24 additions & 19 deletions client/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
clientchainloader "github.com/opennetsys/golkadot/client/chain/loader"
clientdbtypes "github.com/opennetsys/golkadot/client/db/types"
clienttypes "github.com/opennetsys/golkadot/client/types"
"github.com/opennetsys/golkadot/common/db"
"github.com/opennetsys/golkadot/common/bnutil"
db "github.com/opennetsys/golkadot/common/db"
diskdb "github.com/opennetsys/golkadot/common/diskdb"
"github.com/opennetsys/golkadot/common/triedb"
triedb "github.com/opennetsys/golkadot/common/triedb"
"github.com/opennetsys/golkadot/common/u8util"
types "github.com/opennetsys/golkadot/types"
)

// TODO: https://github.com/polkadot-js/client/blob/master/packages/client-db/src/index.ts

// Config ...
type Config struct {
Compact bool
Expand All @@ -44,14 +44,9 @@ type InterfaceChainDbs interface {
}

// StorageMethodU8a ...
// TODO
type StorageMethodU8a struct {
base *Base
createKey types.StorageFunction
//Del(params ...interface{})
//Get(params ...interface{}) []uint8
//Set(value []uint8, params ...interface{})
//OnUpdate(callback func(value []uint8))
}

// NewStorageMethodU8a ...
Expand Down Expand Up @@ -83,29 +78,39 @@ func (s *StorageMethodU8a) OnUpdate(callback func(value []uint8)) {
}

// StorageMethodBn ...
// TODO
type StorageMethodBn struct {
base *Base
createKey types.StorageFunction
bitLen int
}

// NewStorageMethodBn ...
func NewStorageMethodBn(dbs db.BaseDB, createKey types.StorageFunction, bitLen int) StorageMethodBn {
return StorageMethodBn{
base: NewBase(dbs),
createKey: createKey,
bitLen: bitLen,
}
}

// Del ...
func (s *StorageMethodBn) Del(params ...interface{}) {
// TODO
func (s *StorageMethodBn) Del(keyParams ...interface{}) {
s.base.Del(s.createKey(keyParams))
}

// Get ...
func (s *StorageMethodBn) Get(params ...interface{}) *big.Int {
// TODO
return nil
func (s *StorageMethodBn) Get(keyParams ...interface{}) *big.Int {
return u8util.ToBN(s.base.Get(s.createKey(keyParams)), true)
}

// Set ...
func (s *StorageMethodBn) Set(value *big.Int, params ...interface{}) {
// TODO
func (s *StorageMethodBn) Set(value *big.Int, keyParams ...interface{}) {
s.base.Set(s.createKey(keyParams), bnutil.ToUint8Slice(value, s.bitLen, true, false))
}

// OnUpdate ...
func (s *StorageMethodBn) OnUpdate(callback func(value *big.Int)) {
// TODO
func (s *StorageMethodBn) OnUpdate(callback func(value []uint8)) {
s.base.OnUpdate(callback)
}

// DB ...
Expand Down
7 changes: 5 additions & 2 deletions client/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ import (
// Ensure the struct implements the interface
var _ clienttypes.InterfaceP2P = (*P2P)(nil)

// New builds a new p2p service
func New(ctx context.Context, cancel context.CancelFunc, ch chan interface{}, cfg *clienttypes.ConfigClient, c clienttypes.InterfaceChains) (*P2P, error) {
// NewP2P builds a new p2p service
func NewP2P(ctx context.Context, cancel context.CancelFunc, ch chan interface{}, cfg *clienttypes.ConfigClient, c clienttypes.InterfaceChains) (*P2P, error) {
// 1. check inputs
if cfg == nil {
return nil, ErrNoConfig
}
if cfg.P2P == nil {
return nil, errors.New("nil p2p config")
}
if cfg.P2P.Pub == nil {
return nil, ErrNoPublicKey
}
if c == nil {
return nil, ErrNoChainService
}
Expand Down
2 changes: 2 additions & 0 deletions client/p2p/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var (
ErrNoChainService = errors.New("a chain service is required")
// ErrNoHost ...
ErrNoHost = errors.New("the p2p service has no host")
// ErrNoPublicKey ...
ErrNoPublicKey = errors.New("the p2p service has no public key associated with it")
)

// P2P implements the p2p interface
Expand Down
17 changes: 10 additions & 7 deletions cmd/node/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/rand"
"fmt"
"os"
Expand Down Expand Up @@ -75,6 +76,10 @@ func setup() {
Nodes: p2pNodes,
NoBootNodes: p2pNoBootnodes,
Port: p2pPort,
Syncer: nil,
Priv: nil,
Pub: nil,
Context: context.Background(),
},
Peer: &clienttypes.ConfigPeer{
BestHash: nil,
Expand All @@ -89,14 +94,12 @@ func setup() {
ID: pi,
},
RPC: &clienttypes.ConfigRPC{
/*
Host: nil
SystemService: nil
StateService: nil,
ChainService: nil,
Host: nil,
SystemService: nil,
StateService: nil,
ChainService: nil,
AuthorService: nil,
ID: nil,
*/
ID: nil,
},
Telemetry: &clienttypes.TelemetryConfig{},
Wasm: &clienttypes.WasmConfig{},
Expand Down
4 changes: 3 additions & 1 deletion common/db/transactiondb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import "errors"
import (
"errors"
)

// KV ...
type KV struct {
Expand Down
24 changes: 20 additions & 4 deletions common/fileflatdb/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func (c *Cache) GetCachedBranch(branchAt int64) []byte {
branch, found := c.lruBranch[branchAt]
if !found {
branch = make([]byte, branchSize)
fd := os.NewFile(uintptr(c.file.fd), "temp")
fd := os.NewFile(c.fd(), "temp")
_, err := fd.ReadAt(branch, branchAt)
if err != nil {
log.Fatal(err)
log.Fatalf("[fileflatdb/cache] get cached branch error: %v", err)
}

c.CacheBranch(branchAt, branch)
Expand All @@ -56,14 +56,30 @@ func (c *Cache) GetCachedData(dataAt int64, length int64) []byte {
data, found := c.lruData[dataAt]
if !found {
data = make([]byte, length)
fd := os.NewFile(uintptr(c.file.fd), "temp")
fd := os.NewFile(c.fd(), "temp")
_, err := fd.ReadAt(data, dataAt)
if err != nil {
log.Fatal(err)
log.Fatalf("[fileflatdb/cache] get cached data error: %v", err)
}

c.CacheData(dataAt, data)
}

return data
}

// fd ...
func (c *Cache) fd() uintptr {
filepath := c.file.path

if _, err := os.Stat(filepath); os.IsNotExist(err) {
log.Fatalf("[fileflatdb/cache] file %q does not exist", filepath)
}

file, err := os.OpenFile(filepath, os.O_RDWR, 0755)
if err != nil {
log.Fatalf("[fileflatdb/cache] error opening file: %v", err)
}

return file.Fd()
}
4 changes: 3 additions & 1 deletion common/fileflatdb/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ func NewFile(base, file string, options *db.BaseDBOptions) *File {
isCompressed = true
}

filepath := dirutil.NormalizePath(fmt.Sprintf("%s/%s", base, file))

f := &File{
serializer: NewSerializer(),
fd: 0,
fileSize: 0,
path: fmt.Sprintf("%s/%s", base, file),
path: filepath,
file: file,
}

Expand Down
33 changes: 24 additions & 9 deletions common/fileflatdb/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (i *Impl) RetrieveLeaf(doCreate bool, branch []byte, branchAt int, entryInd

if matchIndex != keySize {
if doCreate {
return i.WriteNewBranch(branch, int64(branchAt), int64(entryIndex), key, int64(keyAt.Uint64()), prevKey, int64(matchIndex), int64(matchIndex-keyIndex-1))
return i.WriteNewBranch(branch, int64(branchAt), int64(entryIndex), key, int64(keyAt.Uint64()), prevKey, uint64(matchIndex), int64(matchIndex-keyIndex-1))
}

return nil
Expand All @@ -97,7 +97,10 @@ func (i *Impl) RetrieveLeaf(doCreate bool, branch []byte, branchAt int, entryInd

// FindKey ...
func (i *Impl) FindKey(key *NibbleBuffer, doCreate bool, keyIndex, branchAt int64) *Key {
entryIndex := int(key.Nibbles[keyIndex]) * entrySize
var entryIndex int
if len(key.Nibbles) > 0 {
entryIndex = int(key.Nibbles[keyIndex]) * entrySize
}
branch := i.cache.GetCachedBranch(branchAt)
entryType := branch[entryIndex]
switch int(entryType) {
Expand Down Expand Up @@ -178,16 +181,23 @@ func (i *Impl) WriteNewKey(key *NibbleBuffer) *Key {
}

// WriteNewBranch ...
func (i *Impl) WriteNewBranch(branch []byte, branchAt int64, entryIndex int64, key *NibbleBuffer, prevAt int64, prevKey *NibbleBuffer, matchIndex int64, depth int64) *Key {
func (i *Impl) WriteNewBranch(branch []byte, branchAt int64, entryIndex int64, key *NibbleBuffer, prevAt int64, prevKey *NibbleBuffer, matchIndex uint64, depth int64) *Key {

newKey := i.WriteNewKey(key)

if matchIndex >= int64(len(key.Nibbles)) {
matchIndex = int64(len(key.Nibbles) - 1)
if matchIndex >= uint64(len(key.Nibbles)) && len(key.Nibbles) > 0 {
matchIndex = uint64(len(key.Nibbles) - 1)
}

var keyIndex int
if len(key.Nibbles) > 0 {
keyIndex = int(key.Nibbles[matchIndex]) * entrySize
}

keyIndex := int(key.Nibbles[matchIndex]) * entrySize
prevIndex := int(prevKey.Nibbles[matchIndex]) * entrySize
var prevIndex int
if len(prevKey.Nibbles) > 0 {
prevIndex = int(prevKey.Nibbles[matchIndex]) * entrySize
}
var buffers [][]byte
newBranchAt := i.cache.file.fileSize
newBranch := make([]byte, branchSize)
Expand All @@ -202,7 +212,7 @@ func (i *Impl) WriteNewBranch(branch []byte, branchAt int64, entryIndex int64, k

var offset int64 = 1
for depth > 0 {
branchIndex := int64(key.Nibbles[matchIndex-offset]) * int64(entrySize)
branchIndex := int64(key.Nibbles[int64(matchIndex)-offset]) * int64(entrySize)

newBranch = make([]byte, branchSize)
newBranch[branchIndex] = byte(SlotBranch)
Expand Down Expand Up @@ -306,9 +316,14 @@ func (i *Impl) WriteNewBuffers(buffers [][]byte) int64 {
// fd ...
func (i *Impl) fd() uintptr {
filepath := i.cache.file.path

if _, err := os.Stat(filepath); os.IsNotExist(err) {
log.Fatalf("[fileflatdb] file %q does not exist", filepath)
}

file, err := os.OpenFile(filepath, os.O_RDWR, 0755)
if err != nil {
log.Fatal(err)
log.Fatalf("[fileflatdb] error opening file: %v", err)
}
return file.Fd()
}
5 changes: 4 additions & 1 deletion common/mathutil/mathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ func iushrn(value *big.Int, bits uint, hint int, extended bool) *big.Int {
// BitLen ...
func BitLen(value *big.Int) int {
bits := value.Bits()
w := bits[len(bits)-1]
var w big.Word
if len(bits) > 0 {
w = bits[len(bits)-1]
}
hi := CountBits(int(w))
return (len(bits)-1)*26 + hi
}
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ github.com/multiformats/go-multibase v0.3.0 h1:KWPXEW0HCkqUHO7XZsoo0jwephTxh9roP
github.com/multiformats/go-multibase v0.3.0/go.mod h1:RUrDbdRB1mQ1K/3PAh7h7+6NliRK10PA5joM8V0IYLI=
github.com/multiformats/go-multicodec v0.1.6 h1:4u6lcjbE4VVVoigU4QJSSVYsGVP4j2jtDkR8lPwOrLE=
github.com/multiformats/go-multicodec v0.1.6/go.mod h1:lliaRHbcG8q33yf4Ot9BGD7JqR/Za9HE7HTyVyKwrUQ=
github.com/multiformats/go-multihash v1.0.8 h1:pyowaBSivNxBr137ZjYkr0q4o41MKSJVPKuO7F7AAfY=
github.com/multiformats/go-multihash v1.0.8 h1:v/1HVWH2gq7IZGnFXrTsjoG2I5XIsU5gXiGH5SH915k=
github.com/multiformats/go-multihash v1.0.8/go.mod h1:sT17phG+xVgnrZc8ht/ZoCIV0sKRwvmZkXk46UfSxM4=
github.com/multiformats/go-multistream v0.3.9 h1:ZqVaUxtVzjRUCGaO3596vk/rj9UXheIGAdKXXo/VKUA=
github.com/multiformats/go-multistream v0.3.9/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg=
Expand Down

0 comments on commit 7fb7053

Please sign in to comment.