Skip to content

Commit

Permalink
add blockstatics (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
elvis88 committed May 24, 2019
1 parent a26d7fc commit 581c5ec
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 25 deletions.
17 changes: 10 additions & 7 deletions sdk/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (

// TTX
type TTX struct {
Comment string `json:"comment,omitempty"`
Type string `json:"type,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Expand Down Expand Up @@ -210,10 +211,6 @@ func runTx(api *sdk.API, tx *TTX, indent int) error {
default:
err = fmt.Errorf("unsupport type %v", tx.Type)
}
if bytes.Compare(hash.Bytes(), common.Hash{}.Bytes()) == 0 {
log.Error(strings.Repeat("*", indent), "txpool err", err, "tx", tx)
return fmt.Errorf("txpool error %v", err)
}
if tx.Succeed != (err == nil) {
log.Error(strings.Repeat("*", indent), "succeed mismatch", err, "tx", tx)
return fmt.Errorf("succeed mismatch %v", err)
Expand All @@ -222,10 +219,16 @@ func runTx(api *sdk.API, tx *TTX, indent int) error {
log.Error(strings.Repeat("*", indent), "contain mismatch", err, "tx", tx)
return fmt.Errorf("contain mismatch %v", err)
}
log.Info(strings.Repeat("*", indent), "hash", hash.String())
if tx.Succeed && bytes.Compare(hash.Bytes(), common.Hash{}.Bytes()) == 0 {
log.Error(strings.Repeat("*", indent), "txpool err", err, "tx", tx)
return fmt.Errorf("txpool error %v", err)
}
log.Info(strings.Repeat("*", indent), "hash", hash.String(), "comment", tx.Comment)
indent++
for _, ctx := range tx.Childs {
return runTx(api, ctx, indent)
if err := runTx(api, ctx, indent); err != nil {
return err
}
}
return nil
}
Expand All @@ -250,7 +253,7 @@ func main() {
failed := 0
indent := 0
for index, tx := range txs {
log.Info(strings.Repeat("*", indent), "index", index)
log.Info(strings.Repeat("*", indent), "file", fi.Name(), "index", index)
err := runTx(api, tx, indent)

total++
Expand Down
83 changes: 65 additions & 18 deletions sdk/test/testcase/createaccount001.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[{
"comment": "fractal.admin create account ctest001 & transfer 100 ft",
"comment": "index0 fractal.admin create account ctest001 & transfer 100 ft",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -15,7 +15,7 @@
"succeed": true
},
{
"comment": "fractal.admin create account,alreay exist",
"comment": "index1 fractal.admin create account ctest001 & transfer 100 ft,alreay exist",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -31,7 +31,7 @@
"succeed": false
},
{
"comment": "fractal.admin create account, first char invalid",
"comment": "index2 fractal.admin create account 1ctest001, first char invalid",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -47,7 +47,7 @@
"succeed": false
},
{
"comment": "fractal.admin create account, too short",
"comment": "index3 fractal.admin create account ctest, too short",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -63,7 +63,7 @@
"succeed": false
},
{
"comment": "fractal.admin create account, too length",
"comment": "index4 fractal.admin create account ctest001ctest001ctest001, too length",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -79,7 +79,7 @@
"succeed": false
},
{
"comment": "fractal.admin create account, funder not exist",
"comment": "index5 fractal.admin create account ctest001c1, funder ctest001c2 not exist",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -95,7 +95,7 @@
"succeed": false
},
{
"comment": "fractal.admin create account, no funder",
"comment": "index6 fractal.admin create account ctest001c1, no funder",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -110,7 +110,7 @@
"succeed": true
},
{
"comment": "fractal.admin create account, no publickey",
"comment": "index7 fractal.admin create account ctest001c2, no publickey",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -124,7 +124,7 @@
"succeed": true
},
{
"comment": "fractal.admin create account, over descript",
"comment": "index8 fractal.admin create account ctest001c3, over descript",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand All @@ -133,25 +133,70 @@
"value": 100000000000000000000,
"payload": {
"accountName": "ctest001c3",
"description": "ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 account"
"description": "ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 account ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 accountctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 account ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 accountctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 account ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3 ctest001c3ctest001c3ctest001c3 ctest001c3 account"
},
"succeed": true
"succeed": false
},
{
"comment": "fractal.admin create account, no descript",
"comment": "index9 fractal.admin create account ctest001c3, no descript",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
"to": "fractal.account",
"gas": 30000000,
"value": 100000000000000000000,
"payload": {
"accountName": "ctest001c4"
"accountName": "ctest001c3"
},
"succeed": true
},
{
"comment": "fractal.admin create account ctest001c5 & tranfer 100 ft",
"comment": "index10 fractal.admin create account ctest001c4 & transfer 100 ft, wrong asset id",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
"to": "fractal.account",
"gas": 30000000,
"id":1,
"value": 100000000000000000000,
"payload": {
"accountName": "ctest001c4",
"founder": "ctest001c4",
"publicKey": "0x047db227d7094ce215c3a0f57e1bcc732551fe351f94249471934567e0f5dc1bf795962b8cccb87a2eb56b29fbe37d614e2f4c3c45b789ae4f1f51f4cb21972ffd",
"description": "ctest001c4 account"
},
"succeed": false
},
{
"comment": "index11 fractal.admin create account ctest001c4 & transfer 100 ft, wrong to",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
"to": "ctest001",
"gas": 30000000,
"value": 100000000000000000000,
"payload": {
"accountName": "ctest001c4",
"founder": "ctest001c4",
"publicKey": "0x047db227d7094ce215c3a0f57e1bcc732551fe351f94249471934567e0f5dc1bf795962b8cccb87a2eb56b29fbe37d614e2f4c3c45b789ae4f1f51f4cb21972ffd",
"description": "ctest001c4 account"
},
"succeed": false
},
{
"comment": "index12 fractal.admin create account, no payload",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
"to": "fractal.account",
"gas": 30000000,
"value": 100000000000000000000,
"payload": {
},
"succeed": false
},
{
"comment": "index13 fractal.admin create account ctest001c5 & tranfer 100 ft",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "fractal.admin",
Expand Down Expand Up @@ -182,6 +227,7 @@
"succeed": true
},
{
"comment": "ctest001c5 create account ctest001c7 & tranfer 1 ft & founder ctest001c5",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "ctest001c5",
Expand All @@ -197,17 +243,18 @@
"succeed": true
},
{
"comment": "ctest001c5 create account ctest001c8 & tranfer 100 ft, not enough",
"priv": "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032",
"type": "createaccount",
"from": "ctest001c5",
"to": "fractal.account",
"gas": 30000000,
"value": 1000000000000000000,
"value": 100000000000000000000,
"payload": {
"accountName": "ctest001c5.ctest01",
"founder": "ctest001c5.ctest01",
"accountName": "ctest001c8",
"founder": "ctest001c8",
"publicKey": "0x047db227d7094ce215c3a0f57e1bcc732551fe351f94249471934567e0f5dc1bf795962b8cccb87a2eb56b29fbe37d614e2f4c3c45b789ae4f1f51f4cb21972ffd",
"description": "ctest001c5.ctest01 account"
"description": "ctest001c8 account"
},
"succeed": false
}
Expand Down
162 changes: 162 additions & 0 deletions test/blockstatistic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"math"
"strconv"
"strings"
"time"

"github.com/fractalplatform/fractal/sdk"
)

type blockinfo struct {
Height uint64 `json:"number"`
Size int64 `json:"size"`
Miner string `json:"miner"`
Txs []string `json:"transactions"`
TimeStamp int64 `json:"timestamp"`
}

func getBlockInfo(api *sdk.API, height uint64) (info *blockinfo, err error) {
result := map[string]interface{}{}
if height == math.MaxUint64 {
result, err = api.GetCurrentBlock(false)
} else {
result, err = api.GetBlockByNumber(int64(height), false)
}
if err != nil {
return
}
cj, _ := json.Marshal(result)
info = &blockinfo{}
err = json.Unmarshal(cj, info)
return info, nil
}

//格式化数值 1,234,567,898.55
func NumberFormat(str string) string {
length := len(str)
if length < 4 {
return str
}
arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
length1 := len(arr[0])
if length1 < 4 {
return str
}
count := (length1 - 1) / 3
for i := 0; i < count; i++ {
arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
}
return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
}
func NumberFormatInt(str int64) string {
i := strconv.FormatInt(str, 10)
return NumberFormat(i)
}

func main() {
_rpchost := flag.String("u", "http://192.168.2.13:8090", "RPC host地址")
_start := flag.Uint64("s", 1, "统计的起始高度")
_end := flag.Uint64("e", 0, "统计的结束高度")
_detail := flag.Bool("d", true, "是否显示区块细节情况")
flag.Parse()

// init
api := sdk.NewAPI(*_rpchost)
chainCfg, err := api.GetChainConfig()
if err != nil {
panic(fmt.Sprintf("init err %v", err))
}
curblk, err := getBlockInfo(api, math.MaxUint64)
if err != nil {
panic(err)
}
if *_end == 0 || *_end > curblk.Height {
*_end = curblk.Height
}
blockInterval := chainCfg.DposCfg.BlockInterval * uint64(time.Millisecond)
mepchoInterval := blockInterval * chainCfg.DposCfg.BlockFrequency * chainCfg.DposCfg.CandidateScheduleSize
epchoInterval := chainCfg.DposCfg.EpchoInterval * uint64(time.Millisecond)
epchoFunc := func(timestamp uint64) uint64 {
return (timestamp-chainCfg.ReferenceTime)/epchoInterval + 1
}
offsetFunc := func(timestamp uint64) uint64 {
offset := uint64(timestamp) % epchoInterval % mepchoInterval
offset /= blockInterval * chainCfg.DposCfg.BlockFrequency
return offset
}

// done
var timeStart, timeEnd int64
var totalTxsCount, totalBlockSize int64
var prevTime int64
for i := *_start; i <= *_end; i++ {
blk, err := getBlockInfo(api, i)
if err != nil {
panic(err)
}
if i == *_start {
timeStart = blk.TimeStamp
prevTime = blk.TimeStamp
} else if i == *_end {
timeEnd = blk.TimeStamp
}
totalTxsCount += int64(len(blk.Txs))
totalBlockSize += blk.Size

printDetails := func(timestamp int64, miner string, txs uint64, height uint64) {
if prevTime == blk.TimeStamp || epchoFunc(uint64(prevTime)) != epchoFunc(uint64(timestamp)) {
epcho := epchoFunc(uint64(prevTime))
fmt.Println("\n==========================周期==========================")
if height > 0 {
vcandidates, err := api.DposNextValidCandidatesByNumber(blk.Height)
if err != nil {
panic(err)
}
fmt.Println(epcho, vcandidates["activatedCandidateSchedule"])
} else {
fmt.Println(epcho)
}
fmt.Println("==========================周期==========================")
}
offset := offsetFunc(uint64(timestamp))
if prevTime == blk.TimeStamp || offset != offsetFunc(uint64(prevTime)) {
fmt.Printf("\n%03d%s:", offset, miner)
}
fmt.Printf("%5d(%05d-%ds)", txs, height, timestamp/(int64(time.Second))%60)
}
if *_detail {
for blk.TimeStamp-prevTime >= 2*int64(blockInterval) {
timestamp := prevTime + int64(blockInterval)
printDetails(timestamp, "******", 0, 0)
prevTime = timestamp
}
printDetails(blk.TimeStamp, blk.Miner, uint64(len(blk.Txs)), blk.Height)
prevTime = blk.TimeStamp
}
}

if *_detail {
fmt.Printf("\n")
}
if timeEnd < timeStart {
panic("启动时间大于结束时间")
}
tStart := time.Unix(timeStart/int64(time.Second), timeStart%int64(time.Second))
tEnd := time.Unix(timeEnd/int64(time.Second), timeEnd%int64(time.Second))
timeElapse := (timeEnd - timeStart) / int64(time.Second)
if timeElapse == 0 {
timeElapse = 1
}
fmt.Println("===================Result=======================")
fmt.Println("Block Num:", *_start, "=>", *_end, "count", *_end-*_start)
fmt.Println("Time From:", tStart.Format("2006-01-02 15:04:05.99"))
fmt.Println("Time To :", tEnd.Format("2006-01-02 15:04:05.99"))
fmt.Println("Elapse :", timeElapse/(60*60), "H:", timeElapse%(60*60)/60, "M:", timeElapse%(60*60)%60, "S")
fmt.Println("Total Txs:", NumberFormatInt(totalTxsCount), "\t\tSpeed:", float64(totalTxsCount)/float64(timeElapse), "Tx/s")
fmt.Println("Total Len:", NumberFormatInt(totalBlockSize), "B", "\tSpeed:", float64(totalBlockSize)/float64(timeElapse), "B/s")
}

0 comments on commit 581c5ec

Please sign in to comment.