Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao committed Sep 13, 2018
1 parent 22b6ca6 commit 889cf9f
Show file tree
Hide file tree
Showing 584 changed files with 113,294 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
*.log

# Test binary, build with `go test -c`
*.test
Expand Down
27 changes: 27 additions & 0 deletions Gopkg.lock

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

30 changes: 30 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/CityOfZion/neo-go"
version = "0.44.8"

[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"
40 changes: 40 additions & 0 deletions http/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package http

import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
)

//Request do a http request
func Request(method string, url string, reader io.Reader) ([]byte, error) {
var request *http.Request
if method == "POST" {
var err error
request, err = http.NewRequest("POST", url, reader)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
request.Header.Set("Content-Type", "application/json")
} else {
fmt.Println("not support http method")
return nil, errors.New("not support")
}

client := http.Client{}
resp, err := client.Do(request)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
return respBytes, nil
}
174 changes: 174 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package main

import (
"errors"
"fmt"
"log"
"nep5-balance-compare/nelapi"
"nep5-balance-compare/neoapi"
"nep5-balance-compare/utils"
"os"
"strings"
"time"

"github.com/urfave/cli"
)

/*
* 当前mainnet高度2569706, testnet高度1657028
* 测试1000个地址
*/
const (
BlocksPerGroup = 20
DefaultCaseCount = 1000
)

type testCase struct {
addr string
asset string
}

var neoSrv1 string
var neoSrv2 string

var addressAssets = make(map[string]testCase)
var testCaseCnt int
var caseCount int
var blockCount int
var unequalCases = []testCase{}

func handleNep5TransferResp(resp nelapi.Nep5TransferResp) {
for _, tx := range resp.Result {
addAddress(tx)
}
}
func addAddress(tx nelapi.Transaction) {
key := tx.To + tx.Asset
if _, ok := addressAssets[key]; !ok {
addressAssets[key] = testCase{
addr: tx.To,
asset: tx.Asset,
}
caseCount++
log.Printf("get case %d: address: %s, assetid: %s\n", caseCount, tx.To, tx.Asset)
}
}

func getAddressAndAssetID() {
i := 0
for i < blockCount {
params := []int{}
for j := 0; j < BlocksPerGroup; j++ {
params = append(params, blockCount-j-10*i)
}
resp, err := nelapi.GetNep5TransferByBlockIndex(params)
if err != nil {
fmt.Println(err.Error())
continue
}
handleNep5TransferResp(resp)
i += 10
if testCaseCnt < caseCount {
return
}
}
}

func main() {
app := cli.NewApp()
app.Name = "nep5-compare"
app.Version = "1.0.0"
app.Usage = "compare nep5 balance between two version-different neo-cli"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "base, b",
Usage: "The base server, usually 2.7.6",
Value: "http://47.254.44.88:10332",
Destination: &neoSrv2,
},
cli.StringFlag{
Name: "compare, c",
Usage: "The compare server, usually new version neo-cli. no default, must input",
Value: "",
Destination: &neoSrv2,
},
cli.IntFlag{
Name: "number, n",
Usage: "How many address-assetID pairs to test",
Value: DefaultCaseCount,
Destination: &testCaseCnt,
},
}
app.Action = func(c *cli.Context) error {
if neoSrv2 == "" {
return errors.New("no compare server, please input the new to-compare server")
}
if testCaseCnt <= 0 {
return errors.New("please input a valid number")
}
blockCount = nelapi.GetBlockCount()
if blockCount < 0 {
return errors.New("cannt get current block height")
}
log.Printf("current block count: %d\n", blockCount)
log.Printf("start to get at least %d cases.\n", testCaseCnt)
tStart := time.Now()
getAddressAndAssetID()
tGetaddrs := time.Now()
log.Printf("got %+v cases, start to compare...\n", caseCount)
for _, c := range addressAssets {
addr := c.addr
asset := c.asset
ch1 := make(chan neoapi.Nep5BalanceResp)
ch2 := make(chan neoapi.Nep5BalanceResp)

go func() {
resp, err := neoapi.GetAddressNep5Balance(neoSrv1, addr, asset)
if err != nil {
fmt.Println(err)
close(ch1)
return
}
ch1 <- resp
}()
go func() {
resp, err := neoapi.GetAddressNep5Balance(neoSrv2, addr, asset)
if err != nil {
fmt.Println(err)
close(ch2)
return
}
ch2 <- resp
}()
resp1, ok1 := <-ch1
resp2, ok2 := <-ch2
if ok1 && ok2 && !strings.Contains(resp1.Result.State, "FAULT") && !strings.Contains(resp2.Result.State, "FAULT") {
value1 := resp1.Result.Stack[0].Value
value2 := resp2.Result.Stack[0].Value
var result string
if value1 == value2 {
result = "Equal"
} else {
result = "Unequal"
unequalCases = append(unequalCases, testCase{addr: addr, asset: asset})
}
log.Printf("[%s] addr: %s, asset: %s, neo3.0: %s neo2.7.6: %s\n", result, addr, asset, value1, value2)
}

}
tFinish := time.Now()
log.Printf("total test: %d, unequal: %d, get cases cost: %v, compare cost: %v\n", caseCount, len(unequalCases), tGetaddrs.Sub(tStart), tFinish.Sub(tGetaddrs))
for _, value := range unequalCases {
hash160, err := utils.ToHash160(value.addr)
if err != nil {
continue
}
log.Printf("[unequal]%+v, address-hash160: %s\n", value, hash160)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
66 changes: 66 additions & 0 deletions nelapi/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package nelapi

import (
"bytes"
"encoding/json"
"fmt"
"nep5-balance-compare/http"
"strconv"
"strings"
)

//GetBlockCount get block count
func GetBlockCount() int {
reader := strings.NewReader(`{
"jsonrpc": "2.0",
"method": "getblockcount",
"params": [],
"id": "1"
}`)

respBytes, err := http.Request("POST", "https://api.nel.group/api/mainnet", reader)
if err != nil {
fmt.Print(err.Error())
return -1
}
var resp BlockCountResp
err = json.Unmarshal(respBytes, &resp)
if err != nil {
fmt.Println(err)
return -1
}
result, err := strconv.Atoi(resp.Result[0].Blockcount)
if err != nil {
return -1
}
return result
}

//GetNep5TransferByBlockIndex get nep5 transfers by block index
func GetNep5TransferByBlockIndex(blocks []int) (Nep5TransferResp, error) {
body := nep5TransferReq{
Jsonrpc: "2.0",
Method: "getnep5transferbyblockindex",
Params: blocks,
ID: 1,
}
data, err := json.Marshal(body)
if err != nil {
return Nep5TransferResp{}, err
}
reader := bytes.NewReader(data)

respBytes, err := http.Request("POST", "https://api.nel.group/api/mainnet", reader)

if err != nil {
fmt.Println(err.Error())
return Nep5TransferResp{}, err
}
var resp Nep5TransferResp
err = json.Unmarshal(respBytes, &resp)
if err != nil {
fmt.Println(err.Error())
return Nep5TransferResp{}, err
}
return resp, nil
}
33 changes: 33 additions & 0 deletions nelapi/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nelapi

//get block count
type BlockCountValue struct {
Blockcount string
}
type BlockCountResp struct {
Jsonrpc string
Id int
Result []BlockCountValue
}

//Nep5TransferReq get block nep5 transactions
type nep5TransferReq struct {
Jsonrpc string
Method string
Params []int
ID int
}
type Transaction struct {
Blockindex int
Txid string
N int
Asset string
From string
To string
Value string
}
type Nep5TransferResp struct {
Jsonrpc string
Id int
Result []Transaction
}
Loading

0 comments on commit 889cf9f

Please sign in to comment.