Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
dylenfu committed May 9, 2020
0 parents commit 030005b
Show file tree
Hide file tree
Showing 92 changed files with 10,672 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
log
wallet.dat
*.dht-tool
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
GOFMT=gofmt
GC=go build

VERSION := $(shell git describe --always --tags --long)
BUILD_NODE_PAR = -ldflags "-X main.Version=1.0.0"

ARCH=$(shell uname -m)
SRC_FILES = $(shell git ls-files | grep -e .go$ | grep -v _test.go)

ont-tool: $(SRC_FILES)
$(GC) $(BUILD_NODE_PAR) -o dht-tool main.go

dht-tool-cross: dht-tool-windows dht-tool-linux dht-tool-darwin

dht-tool-windows:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-windows-amd64.exe main.go

dht-tool-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-linux-amd64 main.go

dht-tool-darwin:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-darwin-amd64 main.go

tools-cross: tools-windows tools-linux tools-darwin

format:
$(GOFMT) -w main.go

clean:
rm -rf *.8 *.o *.out *.6 *exe
rm -rf dht-tool dht-tool-*

restart:
make clean && make dht-tool && ./dht-tool
129 changes: 129 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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 common

import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ontio/ontology-tool/config"
"time"

log4 "github.com/alecthomas/log4go"
"github.com/ontio/ontology-crypto/keypair"
gosdk "github.com/ontio/ontology-go-sdk"
scommon "github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/password"
"github.com/ontio/ontology/consensus/vbft"
"github.com/ontio/ontology/consensus/vbft/config"
"github.com/ontio/ontology/core/types"
)

var sdk = gosdk.NewOntologySdk()

func Initialize() {
sdk.NewRpcClient().SetAddress(config.DefConfig.JsonRpcAddress)
}

func GetAccountByPassword(path string) (*gosdk.Account, bool) {
wallet, err := sdk.OpenWallet(path)
if err != nil {
log4.Error("open wallet error:", err)
return nil, false
}
pwd, err := password.GetPassword()
if err != nil {
log4.Error("getPassword error:", err)
return nil, false
}
user, err := wallet.GetDefaultAccount(pwd)
if err != nil {
log4.Error("getDefaultAccount error:", err)
return nil, false
}
return user, true
}

func InvokeNativeContractWithMultiSign(
gasPrice,
gasLimit uint64,
pubKeys []keypair.PublicKey,
singers []*gosdk.Account,
cversion byte,
contractAddress scommon.Address,
method string,
params []interface{},
) (scommon.Uint256, error) {
tx, err := sdk.Native.NewNativeInvokeTransaction(gasPrice, gasLimit, cversion, contractAddress, method, params)
if err != nil {
return scommon.UINT256_EMPTY, err
}
for _, singer := range singers {
err = sdk.MultiSignToTransaction(tx, uint16((5*len(pubKeys)+6)/7), pubKeys, singer)
if err != nil {
return scommon.UINT256_EMPTY, err
}
}
return sdk.SendTransaction(tx)
}

func WaitForBlock(sdk *gosdk.OntologySdk) bool {
_, err := sdk.WaitForGenerateBlock(30*time.Second, 1)
if err != nil {
log4.Error("WaitForGenerateBlock error:", err)
return false
}
return true
}

func ConcatKey(args ...[]byte) []byte {
temp := []byte{}
for _, arg := range args {
temp = append(temp, arg...)
}
return temp
}

func InitVbftBlock(block *types.Block) (*vbft.Block, error) {
if block == nil {
return nil, fmt.Errorf("nil block in initVbftBlock")
}

blkInfo := &vconfig.VbftBlockInfo{}
if err := json.Unmarshal(block.Header.ConsensusPayload, blkInfo); err != nil {
return nil, fmt.Errorf("unmarshal blockInfo: %s", err)
}

return &vbft.Block{
Block: block,
Info: blkInfo,
}, nil
}

func GetAddressByHexString(hexString string) (scommon.Address, error) {
contractByte, err := hex.DecodeString(hexString)
if err != nil {
return scommon.Address{}, fmt.Errorf("hex.DecodeString failed %v", err)
}
contractAddress, err := scommon.AddressParseFromBytes(scommon.ToArrayReverse(contractByte))
if err != nil {
return scommon.Address{}, fmt.Errorf("common.AddressParseFromBytes failed %v", err)
}
return contractAddress, nil
}
43 changes: 43 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"Net":{
"ReservedPeersOnly":false,
"ReservedCfg":{
"reserved":[
"1.2.3.4",
"1.2.3.5"
],
"mask":[
"2.3.4.5"
]
},
"NetworkMagic":0,
"NetworkId":0,
"NetworkName":"",
"NodePort":0,
"IsTLS":false
},
"Sdk":{
"JsonRpcAddress":"http://localhost:20336",
"GasPrice":0,
"GasLimit":20000
},
"Sync":[
"172.168.3.151:20338",
"172.168.3.152:20338",
"172.168.3.153:20338",
"172.168.3.154:20338",
"172.168.3.155:20338"
],
"Seed":[
"172.168.3.156:20338",
"172.168.3.157:20338",
"172.168.3.158:20338",
"172.168.3.159:20338",
"172.168.3.160:20338",
"172.168.3.161:20338",
"172.168.3.162:20338",
"172.168.3.163:20338",
"172.168.3.164:20338",
"172.168.3.165:20338"
]
}
92 changes: 92 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 config

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

var DefConfig = NewDHTConfig()

type DHTConfig struct {
Seed []string
Sync []string
Net *config.P2PNodeConfig
Sdk *SDKConfig
}

type SDKConfig struct {
JsonRpcAddress string
RestfulAddress string
WebSocketAddress string

//Gas Price of transaction
GasPrice uint64
//Gas Limit of invoke transaction
GasLimit uint64
//Gas Limit of deploy transaction
GasDeployLimit uint64
}

func NewDHTConfig() *DHTConfig {
return &DHTConfig{}
}

func (c *DHTConfig) Init(fileName string) error {
err := c.loadConfig(fileName)
if err != nil {
return fmt.Errorf("loadConfig error:%s", err)
}
return nil
}

func (this *DHTConfig) 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 *DHTConfig) 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
}
32 changes: 32 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 config

import "testing"

// go test -count=1 -v github.com/ontio/ontology-tool/config -run TestLoadDHTConfig
func TestLoadDHTConfig(t *testing.T) {
if err := DefConfig.Init("../dht_config.json"); err != nil {
t.Fatal(err)
}

t.Log(DefConfig.Seed)
t.Log(DefConfig.Sync)
t.Log(DefConfig.Net)
}
Loading

0 comments on commit 030005b

Please sign in to comment.