Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
lengzhao committed Jul 31, 2020
1 parent 386d4dc commit 1e4a524
Show file tree
Hide file tree
Showing 20 changed files with 1,389 additions and 322 deletions.
63 changes: 62 additions & 1 deletion api/default_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func init() {
// identifying code,before new transaction,user need input it.
func identifyBeforeTransaction(msg ...interface{}) error {
c := conf.GetConf()
if !c.IdentifyingCode && !c.SafeEnvironment {
if c.SafeEnvironment {
return nil
}
if !c.IdentifyingCode {
return fmt.Errorf("not support, IdentifyingCode closed")
}
//clean inputString if exist
Expand Down Expand Up @@ -1157,6 +1160,43 @@ func DataPost(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

// DataExist return 200OK when exist the key
func DataExist(w http.ResponseWriter, r *http.Request) {
info := DataInfo{}
vars := mux.Vars(r)
chainStr := vars["chain"]
r.ParseForm()
info.AppName = r.Form.Get("app_name")
info.StructName = r.Form.Get("struct_name")
info.Key = r.Form.Get("key")
if r.Form.Get("is_db_data") == "true" {
info.IsDBData = true
}
if info.AppName == "" {
info.AppName = "ff0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
}

chain, err := strconv.ParseUint(chainStr, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("error chain"))
return
}

key, err := hex.DecodeString(info.Key)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "fail to Decode preKey,", info.Key, err)
return
}
exist := runtime.KeyExist(chain, info.IsDBData, info.AppName, info.StructName, key)
if exist {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}

// DataNextKey the next key of data
type DataNextKey struct {
AppName string `json:"app_name,omitempty"`
Expand Down Expand Up @@ -1721,3 +1761,24 @@ func VoteRewardGet(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
enc.Encode(out)
}

// ChainsGet get chains list
func ChainsGet(w http.ResponseWriter, r *http.Request) {
out := make([]uint64, 0, 100)
visit := make([]uint64, 1, 100)
visit[0] = 1
for len(visit) > 0 {
chain := visit[0]
visit = visit[1:]
id := core.GetLastBlockIndex(chain)
if id == 0 {
continue
}
visit = append(visit, chain*2, chain*2+1)
out = append(out, chain)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.Encode(out)
}
13 changes: 13 additions & 0 deletions api/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ var routes = Routes{
DataNextKeyGet,
},

Route{
"DataExist",
strings.ToUpper("Get"),
"/api/v1/{chain}/data/exist",
DataExist,
},

Route{
"DataPost",
strings.ToUpper("Post"),
Expand Down Expand Up @@ -292,6 +299,12 @@ var routes = Routes{
"/api/v1/time",
TimeGet,
},
Route{
"ChainsGet",
strings.ToUpper("Get"),
"/api/v1/chains",
ChainsGet,
},
}

var wsRoutes = WSRoutes{
Expand Down
2 changes: 1 addition & 1 deletion conf/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type TConfig struct {
var (
conf TConfig
// Version software version
Version string = "v0.5.7"
Version string = "v0.5.8"
// BuildTime build time
BuildTime string
// GitHead git head
Expand Down
18 changes: 18 additions & 0 deletions core/core.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zff0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f

import (
"encoding/hex"
"encoding/json"
"os"

Expand Down Expand Up @@ -212,6 +213,11 @@ func (h *Hash) UnmarshalJSON(b []byte) error {
return nil
}

// ToHexString encode to hex string
func (h Hash) ToHexString() string{
return hex.EncodeToString(h[:])
}

// Empty Check where Address is empty
func (a Address) Empty() bool {
return a == (Address{})
Expand All @@ -236,6 +242,18 @@ func (a *Address) UnmarshalJSON(b []byte) error {
return nil
}

// Decode decode hex string
func (a *Address) Decode(hexStr string) {
d, err := hex.DecodeString(hexStr)
assert(err == nil)
Decode(0, d, a)
}

// ToHexString encode to hex string
func (a Address) ToHexString() string{
return hex.EncodeToString(a[:])
}

func assert(cond bool) {
if !cond {
panic("error")
Expand Down
10 changes: 9 additions & 1 deletion counter/energy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ var mu sync.Mutex
var energy uint64 = defaultEnergy
var used uint64

// ResetEnergy reset energy
func ResetEnergy() {
log.Printf("energy. used:%d,energy:%d\n", used, energy)
used = 0
energy = defaultEnergy
}

// SetEnergy set energy
func SetEnergy(n uint64) {
mu.Lock()
Expand All @@ -26,7 +33,7 @@ func SetEnergy(n uint64) {
}

// ConsumeEnergy consume energy
func ConsumeEnergy(n uint64) {
func ConsumeEnergy(n uint64) uint64 {
if n == 0 {
n = 10
}
Expand All @@ -38,4 +45,5 @@ func ConsumeEnergy(n uint64) {
log.Printf("energy.hope:%d,have:%d\n", used, energy)
panic("not enough energy")
}
return energy - used
}
8 changes: 0 additions & 8 deletions messages/internal_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ type Mine struct {
Index uint64
}

// Rollback Rollback
// type Rollback struct {
// BaseMsg
// Chain uint64
// Index uint64
// Key []byte
// }

// ChainEvent ChainEvent
type ChainEvent struct {
BaseMsg
Expand Down
20 changes: 17 additions & 3 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package runtime
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strings"

"github.com/govm-net/govm/counter"
db "github.com/govm-net/govm/database"
"github.com/govm-net/govm/wallet"
"github.com/lengzhao/database/client"
"log"
"reflect"
"strings"
)

// TRuntime 执行机的结构体定义
Expand Down Expand Up @@ -332,6 +333,19 @@ func GetValue(chain uint64, isDb bool, appName, structName string, key []byte) (
return data[:n-8], life
}

// KeyExist return true if exist
func KeyExist(chain uint64, isDb bool, appName, structName string, key []byte) bool {
var tbName string
if isDb {
tbName = string(startOfDB)
} else {
tbName = string(startOfLog)
}
tbName += appName + "." + structName
// log.Printf("GetNextKey,tbName:%s\n", string(tbName))
return db.GetClient().Exist(chain, []byte(tbName), key)
}

// Recover 校验签名信息
func (r *TRuntime) Recover(address, sign, msg []byte) bool {
return wallet.Recover(address, sign, msg)
Expand Down
10 changes: 8 additions & 2 deletions static/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ <h3 class="panel-title" data-localize="vote_info">Vote Info</h3>
$("#chain").html(gChainID);
function updateAccount(chain, address) {
if (address === undefined) {
address = "";
return;
}
$("#address").val("");
$("#balance").val("");
Expand Down Expand Up @@ -173,7 +173,7 @@ <h3 class="panel-title" data-localize="vote_info">Vote Info</h3>
var myDate = new Date()
myDate.setTime(data.start_day*24*3600*1000)
// $("#v_address").val(data.address);
$("#votes").html(data.votes);
$("#votes").html(data.votes|0);
$("#admin").html(data.admin);
$("#time").html(myDate.toString());
}
Expand Down Expand Up @@ -240,6 +240,12 @@ <h3 class="panel-title" data-localize="vote_info">Vote Info</h3>
// console.log("start getAllAccount");
getNext(result, chainID, nextKey);
}
var chain = getUrlParam('chain');
var key = getUrlParam('key');
if (chain != "" && key != "") {
$("#chain").val(chain);
updateAccount(chain, key);
}
</script>

</body>
Expand Down
83 changes: 83 additions & 0 deletions static/api.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>

<head>
<title>GOVM</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/govm.css" rel="stylesheet">
<link rel="icon" type="image/x-ico" href="logo.ico" />
<link rel="shortcut icon" href="logo.ico">
</head>

<body ng-app="">
<div class="container">
<div id="navbar"></div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title" data-localize="openapi">
OpenAPI
</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item">open swagger
<a href="https://editor.swagger.io/">https://editor.swagger.io/</a></li>
<li class="list-group-item">download api documentation <a href="/openapi.yaml">openapi.yaml</a>
</li>
<li class="list-group-item">"Import file" in Swagger Editor</li>
<li class="list-group-item">view the api</li>
<li class="list-group-item">"Generate Client" by Swagger Editor</li>
</ul>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title" data-localize="precautions">
Precautions
</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item">By default, "POST" is not supported.</li>
<li class="list-group-item">How to support</li>
<li class="list-group-item">1. open conf/conf.json</li>
<li class="list-group-item">2. set the value of "http_address" to "127.0.0.1"</li>
<li class="list-group-item">3. set the value of "safe_environment" to "true"</li>
<li class="list-group-item">4. set the value of "identifying_code" to "false"</li>
<li class="list-group-item">5. restart govm.exe</li>
</ul>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title" data-localize="example">
Examples
</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item"><a href="https://github.com/govm-net/govm">https://github.com/govm-net/govm</a> ,"static/index.html"</li>
<li class="list-group-item"><a href="https://github.com/govm-net/wallet">https://github.com/govm-net/wallet</a> ,"bin/gui/screens"</li>
<li class="list-group-item"><a href="https://github.com/govm-net/wallet">https://github.com/govm-net/wallet</a> ,"bin/html"</li>
</ul>
</div>
</div>
</div>
</div>

<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
<script src="js/jquery-3.3.1.min.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.localize.min.js"></script>
<script src="js/load.js?v=3"></script>
</body>

</html>
3 changes: 2 additions & 1 deletion static/block.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/govm.css" rel="stylesheet">
<link rel="icon" type="image/x-ico" href="logo.ico" />
<link rel="shortcut icon" href="logo.ico">

Expand Down Expand Up @@ -52,7 +53,7 @@ <h3 class="panel-title" data-localize="block_info">
Block Info
</h3>
</div>
<div class="panel-body">
<div class="panel-body govm_fixed">
<div class="input-group">
<span class="input-group-addon" data-localize="chain">Chain:</span>
<span class="form-control" id="chain"></span>
Expand Down
6 changes: 6 additions & 0 deletions static/css/govm.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

.govm_fixed {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
Loading

0 comments on commit 1e4a524

Please sign in to comment.