Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
siovanus committed Jun 23, 2020
1 parent 3fd1223 commit bbd7eb1
Show file tree
Hide file tree
Showing 10 changed files with 652 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea/*
idea/*
temp/*
build/
*.patch
*.log
*.rej
vendor/*
*.iml
ontology-tool
wallet.dat
main
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"JsonRpcAddress":"http://localhost:20336",
"WalletPath": "wallet.dat",
"PeerPublicKey": "02d500543eaa4110b8d5f8df4fabe31a8caabaa58cb8512baa8af15a303606efe4",
"InitPos":100000,
"GasPrice":0,
"GasLimit":20000
}
88 changes: 88 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

//common use fot ontology-tool
package config

import (
"encoding/json"
"fmt"
log4 "github.com/alecthomas/log4go"
"io/ioutil"
"os"
)

//Default config instance
var DefConfig = NewConfig()

//Config object used by ontology-instance
type Config struct {
//JsonRpcAddress of ontology
JsonRpcAddress string
WalletPath string
PeerPublicKey string
InitPos uint32
//Gas Price of transaction
GasPrice uint64
//Gas Limit of invoke transaction
GasLimit uint64
}

//NewConfig retuen a Config instance
func NewConfig() *Config {
return &Config{}
}

//Init Config with a config file
func (this *Config) Init(fileName string) error {
err := this.loadConfig(fileName)
if err != nil {
return fmt.Errorf("loadConfig error:%s", err)
}
return nil
}

func (this *Config) loadConfig(fileName string) error {
data, err := this.readFile(fileName)
if err != nil {
return err
}
err = json.Unmarshal(data, this)
if err != nil {
return fmt.Errorf("json.Unmarshal TestConfig:%s error:%s", data, err)
}
return nil
}

func (this *Config) readFile(fileName string) ([]byte, error) {
file, err := os.OpenFile(fileName, os.O_RDONLY, 0666)
if err != nil {
return nil, fmt.Errorf("OpenFile %s error %s", fileName, err)
}
defer func() {
err := file.Close()
if err != nil {
log4.Error("File %s close error %s", fileName, err)
}
}()
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadAll %s error %s", fileName, err)
}
return data, nil
}
156 changes: 156 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package core

import (
log4 "github.com/alecthomas/log4go"
sdk "github.com/ontio/ontology-go-sdk"
"github.com/ontio/triones-node-tool/config"
)

var OntTool = NewOntologyTool()

type Method func(sdk *sdk.OntologySdk) bool

type OntologyTool struct {
//Map name to method
methodsMap map[string]Method
//Map method result
methodsRes map[string]bool
}

func NewOntologyTool() *OntologyTool {
return &OntologyTool{
methodsMap: make(map[string]Method, 0),
methodsRes: make(map[string]bool, 0),
}
}

func (this *OntologyTool) RegMethod(name string, method Method) {
this.methodsMap[name] = method
}

//Start run
func (this *OntologyTool) Start(methodsList []string) {
if len(methodsList) > 0 {
this.runMethodList(methodsList)
return
}
log4.Info("No method to run")
return
}

func (this *OntologyTool) runMethodList(methodsList []string) {
this.onStart()
defer this.onFinish(methodsList)
ontSdk := sdk.NewOntologySdk()
ontSdk.NewRpcClient().SetAddress(config.DefConfig.JsonRpcAddress)
for i, method := range methodsList {
this.runMethod(i+1, ontSdk, method)
}
}

func (this *OntologyTool) runMethod(index int, sdk *sdk.OntologySdk, methodName string) {
this.onBeforeMethodStart(index, methodName)
method := this.getMethodByName(methodName)
if method != nil {
ok := method(sdk)
this.onAfterMethodFinish(index, methodName, ok)
this.methodsRes[methodName] = ok
}
}

func (this *OntologyTool) onStart() {
log4.Info("===============================================================")
log4.Info("-------Ontology Tool Start-------")
log4.Info("===============================================================")
log4.Info("")
}

func (this *OntologyTool) onFinish(methodsList []string) {
failedList := make([]string, 0)
successList := make([]string, 0)
for methodName, ok := range this.methodsRes {
if ok {
successList = append(successList, methodName)
} else {
failedList = append(failedList, methodName)
}
}

skipList := make([]string, 0)
for _, method := range methodsList {
_, ok := this.methodsRes[method]
if !ok {
skipList = append(skipList, method)
}
}

succCount := len(successList)
failedCount := len(failedList)

log4.Info("===============================================================")
log4.Info("Ontology Tool Finish Total:%v Success:%v Failed:%v Skip:%v",
len(methodsList),
succCount,
failedCount,
len(methodsList)-succCount-failedCount)
if succCount > 0 {
log4.Info("---------------------------------------------------------------")
log4.Info("Success list:")
for i, succ := range successList {
log4.Info("%d.\t%s", i+1, succ)
}
}
if failedCount > 0 {
log4.Info("---------------------------------------------------------------")
log4.Info("Fail list:")
for i, fail := range failedList {
log4.Info("%d.\t%s", i+1, fail)
}
}
if len(skipList) > 0 {
log4.Info("---------------------------------------------------------------")
log4.Info("Skip list:")
for i, skip := range skipList {
log4.Info("%d.\t%s", i+1, skip)
}
}
log4.Info("===============================================================")
}

func (this *OntologyTool) onBeforeMethodStart(index int, methodName string) {
log4.Info("===============================================================")
log4.Info("%d. Start Method:%s", index, methodName)
log4.Info("---------------------------------------------------------------")
}

func (this *OntologyTool) onAfterMethodFinish(index int, methodName string, res bool) {
if res {
log4.Info("Run Method:%s success.", methodName)
} else {
log4.Info("Run Method:%s failed.", methodName)
}
log4.Info("---------------------------------------------------------------")
log4.Info("")
}

func (this *OntologyTool) getMethodByName(name string) Method {
return this.methodsMap[name]
}
31 changes: 31 additions & 0 deletions log4go.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<logging>
<filter enabled="true">
<tag>stdout</tag>
<type>console</type>
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
<level>DEBUG</level>
<property name="format">[%D %T] [%L] %M</property>
</filter>
<filter enabled="true">
<tag>file</tag>
<type>file</type>
<level>DEBUG</level>
<property name="filename">./log/test.log</property>
<!--
%T - Time (15:04:05 MST)
%t - Time (15:04)
%D - Date (2006/01/02)
%d - Date (01/02/06)
%L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT)
%S - Source
%M - Message
It ignores unknown format strings (and removes them)
Recommended: "[%D %T] [%L] (%S) %M"
-->
<property name="format">[%D %T] [%L] %M</property>
<property name="rotate">false</property> <!-- true enables log rotation, otherwise append -->
<property name="maxsize">100M</property> <!-- \d+[KMG]? Suffixes are in terms of 2**10 -->
<property name="maxlines">0K</property> <!-- \d+[KMG]? Suffixes are in terms of thousands -->
<property name="daily">true</property> <!-- Automatically rotates when a log message is written after midnight -->
</filter>
</logging>
62 changes: 62 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package main

import (
"flag"
"math/rand"
"strings"
"time"

log4 "github.com/alecthomas/log4go"
"github.com/ontio/triones-node-tool/config"
"github.com/ontio/triones-node-tool/core"
_ "github.com/ontio/triones-node-tool/methods"
)

var (
Config string //config file
LogConfig string //Log config file
Methods string //Methods list in cmdline
)

func init() {
flag.StringVar(&Config, "cfg", "./config.json", "Config of ontology-tool")
flag.StringVar(&LogConfig, "lfg", "./log4go.xml", "Log config of ontology-tool")
flag.StringVar(&Methods, "t", "", "methods to run. use ',' to split methods")
flag.Parse()
}

func main() {
rand.Seed(time.Now().UnixNano())
log4.LoadConfiguration(LogConfig)
defer time.Sleep(time.Second)

err := config.DefConfig.Init(Config)
if err != nil {
log4.Error("DefConfig.Init error:%s", err)
return
}

methods := make([]string, 0)
if Methods != "" {
methods = strings.Split(Methods, ",")
}

core.OntTool.Start(methods)
}
26 changes: 26 additions & 0 deletions methods/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package methods

import (
"github.com/ontio/triones-node-tool/methods/triones"
)

func init() {
triones.InitTriones()
}
Loading

0 comments on commit bbd7eb1

Please sign in to comment.