Skip to content

Commit

Permalink
Merge branch 'master' into trieOpt4
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Mar 9, 2022
2 parents c73e56a + 12976b9 commit 18e5643
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 169 deletions.
5 changes: 0 additions & 5 deletions db/trie/mptrie/branchnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (b *branchNode) MarkAsRoot() {
}

func (b *branchNode) Children() []node {
trieMtc.WithLabelValues("branchNode", "children").Inc()
ret := make([]node, 0, len(b.children))
for _, idx := range b.indices.List() {
ret = append(ret, b.children[idx])
Expand All @@ -89,7 +88,6 @@ func (b *branchNode) Children() []node {
}

func (b *branchNode) Delete(key keyType, offset uint8) (node, error) {
trieMtc.WithLabelValues("branchNode", "delete").Inc()
offsetKey := key[offset]
child, err := b.child(offsetKey)
if err != nil {
Expand Down Expand Up @@ -143,7 +141,6 @@ func (b *branchNode) Delete(key keyType, offset uint8) (node, error) {
}

func (b *branchNode) Upsert(key keyType, offset uint8, value []byte) (node, error) {
trieMtc.WithLabelValues("branchNode", "upsert").Inc()
var newChild node
offsetKey := key[offset]
child, err := b.child(offsetKey)
Expand All @@ -161,7 +158,6 @@ func (b *branchNode) Upsert(key keyType, offset uint8, value []byte) (node, erro
}

func (b *branchNode) Search(key keyType, offset uint8) (node, error) {
trieMtc.WithLabelValues("branchNode", "search").Inc()
child, err := b.child(key[offset])
if err != nil {
return nil, err
Expand All @@ -170,7 +166,6 @@ func (b *branchNode) Search(key keyType, offset uint8) (node, error) {
}

func (b *branchNode) proto(flush bool) (proto.Message, error) {
trieMtc.WithLabelValues("branchNode", "serialize").Inc()
nodes := []*triepb.BranchNodePb{}
for _, idx := range b.indices.List() {
c := b.children[idx]
Expand Down
5 changes: 0 additions & 5 deletions db/trie/mptrie/extensionnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func newExtensionNodeFromProtoPb(pb *triepb.ExtendPb, mpt *merklePatriciaTrie, h
}

func (e *extensionNode) Delete(key keyType, offset uint8) (node, error) {
trieMtc.WithLabelValues("extensionNode", "delete").Inc()
matched := e.commonPrefixLength(key[offset:])
if matched != uint8(len(e.path)) {
return nil, trie.ErrNotExist
Expand Down Expand Up @@ -87,7 +86,6 @@ func (e *extensionNode) Delete(key keyType, offset uint8) (node, error) {
}

func (e *extensionNode) Upsert(key keyType, offset uint8, value []byte) (node, error) {
trieMtc.WithLabelValues("extensionNode", "upsert").Inc()
matched := e.commonPrefixLength(key[offset:])
if matched == uint8(len(e.path)) {
newChild, err := e.child.Upsert(key, offset+matched, value)
Expand Down Expand Up @@ -122,7 +120,6 @@ func (e *extensionNode) Upsert(key keyType, offset uint8, value []byte) (node, e
}

func (e *extensionNode) Search(key keyType, offset uint8) (node, error) {
trieMtc.WithLabelValues("extensionNode", "search").Inc()
matched := e.commonPrefixLength(key[offset:])
if matched != uint8(len(e.path)) {
return nil, trie.ErrNotExist
Expand All @@ -132,7 +129,6 @@ func (e *extensionNode) Search(key keyType, offset uint8) (node, error) {
}

func (e *extensionNode) proto(flush bool) (proto.Message, error) {
trieMtc.WithLabelValues("extensionNode", "proto").Inc()
if flush {
if sn, ok := e.child.(serializable); ok {
_, err := sn.store()
Expand All @@ -156,7 +152,6 @@ func (e *extensionNode) proto(flush bool) (proto.Message, error) {
}

func (e *extensionNode) Child() node {
trieMtc.WithLabelValues("extensionNode", "child").Inc()
return e.child
}

Expand Down
3 changes: 0 additions & 3 deletions db/trie/mptrie/leafnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func (l *leafNode) Delete(key keyType, offset uint8) (node, error) {
}

func (l *leafNode) Upsert(key keyType, offset uint8, value []byte) (node, error) {
trieMtc.WithLabelValues("leafNode", "upsert").Inc()
matched := commonPrefixLength(l.key[offset:], key[offset:])
if offset+matched == uint8(len(key)) {
return l.updateValue(value)
Expand Down Expand Up @@ -108,7 +107,6 @@ func (l *leafNode) Upsert(key keyType, offset uint8, value []byte) (node, error)
}

func (l *leafNode) Search(key keyType, offset uint8) (node, error) {
trieMtc.WithLabelValues("leafNode", "search").Inc()
if !bytes.Equal(l.key[offset:], key[offset:]) {
return nil, trie.ErrNotExist
}
Expand All @@ -117,7 +115,6 @@ func (l *leafNode) Search(key keyType, offset uint8) (node, error) {
}

func (l *leafNode) proto(_ bool) (proto.Message, error) {
trieMtc.WithLabelValues("leafNode", "proto").Inc()
return &triepb.NodePb{
Node: &triepb.NodePb_Leaf{
Leaf: &triepb.LeafPb{
Expand Down
18 changes: 0 additions & 18 deletions db/trie/mptrie/merklepatriciatrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,12 @@ import (

"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/db/trie"
"github.com/iotexproject/iotex-core/db/trie/triepb"
)

var (
trieMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_trie",
Help: "IoTeX Trie",
},
[]string{"node", "type"},
)
)

func init() {
prometheus.MustRegister(trieMtc)
}

type (
// HashFunc defines a function to generate the hash which will be used as key in db
HashFunc func([]byte) []byte
Expand Down Expand Up @@ -176,7 +161,6 @@ func (mpt *merklePatriciaTrie) Get(key []byte) ([]byte, error) {
mpt.mutex.RLock()
defer mpt.mutex.RUnlock()

trieMtc.WithLabelValues("root", "Get").Inc()
kt, err := mpt.checkKeyType(key)
if err != nil {
return nil, err
Expand All @@ -196,7 +180,6 @@ func (mpt *merklePatriciaTrie) Delete(key []byte) error {
mpt.mutex.Lock()
defer mpt.mutex.Unlock()

trieMtc.WithLabelValues("root", "Delete").Inc()
kt, err := mpt.checkKeyType(key)
if err != nil {
return err
Expand All @@ -217,7 +200,6 @@ func (mpt *merklePatriciaTrie) Upsert(key []byte, value []byte) error {
mpt.mutex.Lock()
defer mpt.mutex.Unlock()

trieMtc.WithLabelValues("root", "Upsert").Inc()
kt, err := mpt.checkKeyType(key)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions e2etest/rewarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,9 @@ func TestBlockEpochReward(t *testing.T) {
svrs[i] = svr
}

// Create a probe server
probeSvr := probe.New(7788)

// Start mini-cluster
for i := 0; i < numNodes; i++ {
go itx.StartServer(context.Background(), svrs[i], probeSvr, configs[i])
go itx.StartServer(context.Background(), svrs[i], probe.New(7788+i), configs[i])
}

// target address for grpc connection. Default is "127.0.0.1:14014"
Expand Down
43 changes: 31 additions & 12 deletions ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ type (
DecryptPrivateKey(string, string) (*ecdsa.PrivateKey, error)
// AliasMap returns the alias map: accountAddr-aliasName
AliasMap() map[string]string
// doing
WriteConfig(config.Config) error
// SetAlias write alias and account address to the default config file
SetAlias(string, string) error
// DeleteAlias delete alias from the default config file
DeleteAlias(string) error
// PrintInfo print the command result or the question query
PrintInfo(string)
// IsCryptoSm2 return true if use sm2 cryptographic algorithm, false if not use
Expand All @@ -71,9 +73,10 @@ type (
}

client struct {
cfg config.Config
conn *grpc.ClientConn
cryptoSm2 bool
cfg config.Config
conn *grpc.ClientConn
cryptoSm2 bool
configFilePath string
}

// Option sets client construction parameter
Expand All @@ -94,9 +97,10 @@ func EnableCryptoSm2() Option {
}

// NewClient creates a new ioctl client
func NewClient(cfg config.Config, opts ...Option) Client {
func NewClient(cfg config.Config, configFilePath string, opts ...Option) Client {
c := &client{
cfg: cfg,
cfg: cfg,
configFilePath: configFilePath,
}
for _, opt := range opts {
opt(c)
Expand Down Expand Up @@ -235,13 +239,28 @@ func (c *client) AliasMap() map[string]string {
return aliases
}

func (c *client) WriteConfig(cfg config.Config) error {
out, err := yaml.Marshal(&cfg)
func (c *client) SetAlias(alias string, addr string) error {
aliases := c.AliasMap()
for aliases[addr] != "" {
delete(c.cfg.Aliases, aliases[addr])
aliases = c.AliasMap()
}
c.cfg.Aliases[alias] = addr
return c.writeAlias()
}

func (c *client) DeleteAlias(alias string) error {
delete(c.cfg.Aliases, alias)
return c.writeAlias()
}

func (c *client) writeAlias() error {
out, err := yaml.Marshal(&c.cfg)
if err != nil {
return errors.Wrap(err, "failed to marshal config")
return errors.Wrapf(err, "failed to marshal config to config file %s", c.configFilePath)
}
if err := os.WriteFile(config.DefaultConfigFile, out, 0600); err != nil {
return errors.Wrapf(err, "failed to write to config file %s", config.DefaultConfigFile)
if err = os.WriteFile(c.configFilePath, out, 0600); err != nil {
return errors.Wrapf(err, "failed to write to config file %s", c.configFilePath)
}
return nil
}
Expand Down
Loading

0 comments on commit 18e5643

Please sign in to comment.