Skip to content

Commit 6590461

Browse files
committed
update storage
1 parent ea9b25a commit 6590461

91 files changed

Lines changed: 11170 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package blockchain
2+
3+
import (
4+
"fmt"
5+
// "log"
6+
// "time"
7+
8+
//"github.com/code/bottos/service/storage/controller"
9+
"github.com/code/bottos/service/storage/internal/service"
10+
// "github.com/code/bottos/service/storage/util"
11+
)
12+
13+
func tokenAging(timeout int64, stat service.StateRepository, c chan int) {
14+
fmt.Println("okkkk")
15+
stat.CallTokenAging(timeout)
16+
c <- 1
17+
}
18+
19+
//func LoopAging(stat service.SqliteRepository) {
20+
// channel := make(chan int, 1)
21+
// go tokenAging(util.DefualtAgingTime, stat, channel)
22+
23+
// // 周期
24+
// for {
25+
// select {
26+
// case <-channel:
27+
// log.Println("syncing task is completed.")
28+
// time.Sleep(5 * time.Second) // TODO: using event listen
29+
// tokenAging(util.DefualtAgingTime, stat, channel)
30+
// }
31+
// }
32+
//}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package blockchain
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestAging(t *testing.T) {
9+
c := make(chan int, 1)
10+
go Sync(0, 12, c)
11+
fmt.Println("sync")
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package blockchain
2+
3+
import (
4+
5+
"github.com/code/bottos/service/storage/controller"
6+
)
7+
8+
func GetLatestBlockNumber() uint64 {
9+
blockInfo,err := controller.GetInfo()
10+
if err != nil {
11+
return 0
12+
}
13+
latestBlock := blockInfo.HeadBlockNum
14+
return latestBlock
15+
}

service/storage/blockchain/sync.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package blockchain
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"strconv"
8+
"time"
9+
10+
"github.com/code/bottos/service/storage/controller"
11+
"github.com/code/bottos/service/storage/internal/service"
12+
)
13+
14+
func InsertTransaction(tx interface{}) error {
15+
fmt.Println("insert transaction")
16+
return nil
17+
}
18+
func GetSyncedBlockCount(stat service.StateRepository) uint64 {
19+
20+
fmt.Println("find blocknumber")
21+
var syncedNumber uint64
22+
syncedNumber, err := stat.CallGetSyncBlockCount()
23+
if err != nil {
24+
fmt.Println("error")
25+
log.Fatal(err)
26+
return 0
27+
}
28+
return syncedNumber
29+
}
30+
31+
func Sync(syncedNumber uint64, latestBlock uint64, c chan int) {
32+
if syncedNumber == 0 {
33+
syncedNumber++
34+
}
35+
for i := syncedNumber; i <= latestBlock; i++ {
36+
num := strconv.FormatUint(i, 10)
37+
fmt.Println(num, latestBlock)
38+
mBlock, err := controller.GetBlock(num)
39+
if err != nil {
40+
log.Fatal(err)
41+
break
42+
}
43+
if err := InsertTransaction(mBlock); err != nil {
44+
log.Fatal(err)
45+
break
46+
}
47+
}
48+
49+
c <- 1
50+
}
51+
52+
func StartSync(stat service.StateRepository) {
53+
54+
sync := make(chan int, 1)
55+
go Sync(GetSyncedBlockCount(stat), GetLatestBlockNumber(), sync)
56+
57+
// 周期同步
58+
for {
59+
select {
60+
case <-sync:
61+
log.Println("syncing task is completed.")
62+
time.Sleep(7 * time.Second) // TODO: using event listen
63+
Sync(GetSyncedBlockCount(stat), GetLatestBlockNumber(), sync)
64+
}
65+
}
66+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package blockchain
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestSync(t *testing.T) {
9+
sync := make(chan int, 1)
10+
go Sync(0, 12, sync)
11+
fmt.Println("sync")
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package blockchain

service/storage/bottos.db

Whitespace-only changes.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package controller
2+
import (
3+
"encoding/json"
4+
"io/ioutil"
5+
"net/http"
6+
"strings"
7+
"github.com/code/bottos/service/storage/util"
8+
baseConfig "github.com/code/bottos/config"
9+
)
10+
11+
var (
12+
serverurl= baseConfig.BASE_CHAIN_URL
13+
)
14+
15+
//https://github.com/ethereum/wiki/wiki/JSON-RPC
16+
17+
func SetServer(newServer string) {
18+
serverurl = newServer
19+
}
20+
func GetInfo()(*util.Info,error){
21+
resp, err := http.Get("http://"+serverurl+"/v1/chain/get_info")
22+
if err != nil {
23+
return nil,err
24+
}
25+
defer resp.Body.Close()
26+
27+
body, err := ioutil.ReadAll(resp.Body)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
jResp := new(util.Info)
33+
34+
err = json.Unmarshal(body, jResp)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
return jResp,nil
40+
}
41+
func GetBlock(num_or_id string)(*util.Block,error){
42+
body := strings.NewReader(`{"block_num_or_id":`+num_or_id+`}`)
43+
req, err := http.NewRequest("POST", "http://"+serverurl+"/v1/chain/get_block", body)
44+
if err != nil {
45+
return nil,err
46+
}
47+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
48+
49+
resp, err := http.DefaultClient.Do(req)
50+
if err != nil {
51+
return nil,err
52+
}
53+
defer resp.Body.Close()
54+
55+
respBody, err := ioutil.ReadAll(resp.Body)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
block := new(util.Block)
61+
62+
err = json.Unmarshal(respBody, block)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
return block,nil
68+
}
69+
func GetAccountInfo()(*util.AccountInfo,error){
70+
71+
body := strings.NewReader(`{"account_name":"testa"}`)
72+
req, err := http.NewRequest("POST", "http://"+serverurl+"/v1/chain/get_account", body)
73+
if err != nil {
74+
// handle err
75+
}
76+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
77+
78+
resp, err := http.DefaultClient.Do(req)
79+
if err != nil {
80+
// handle err
81+
}
82+
defer resp.Body.Close()
83+
respBody, err := ioutil.ReadAll(resp.Body)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
account := new(util.AccountInfo)
89+
90+
err = json.Unmarshal(respBody, account)
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
return account,nil
96+
}
97+
func GetTxInfo()(*util.TxInfo,error){
98+
body := strings.NewReader(`{"transaction_id":"06ffce7503d82a4e19bd7cdfb9c507c5c3c40fda3bd316ee35f344d42807db6e"}`)
99+
req, err := http.NewRequest("POST", "http://"+serverurl+"/v1/account_history/get_transaction", body)
100+
if err != nil {
101+
// handle err
102+
}
103+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
104+
105+
resp, err := http.DefaultClient.Do(req)
106+
if err != nil {
107+
// handle err
108+
}
109+
defer resp.Body.Close()
110+
respBody, err := ioutil.ReadAll(resp.Body)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
tx := new(util.TxInfo)
116+
117+
err = json.Unmarshal(respBody, tx)
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
return tx,nil
123+
124+
}
125+
126+
127+
func GetCodeInfo()(string, error){
128+
body := strings.NewReader(`{"account_name":"currency"}`)
129+
req, err := http.NewRequest("POST", "http://"+serverurl+"/v1/chain/get_code", body)
130+
if err != nil {
131+
// handle err
132+
}
133+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
134+
135+
resp, err := http.DefaultClient.Do(req)
136+
if err != nil {
137+
// handle err
138+
}
139+
defer resp.Body.Close()
140+
return "",nil
141+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package controller
2+
3+
import (
4+
"testing"
5+
"fmt"
6+
"github.com/code/bottos/service/storage/util"
7+
)
8+
9+
10+
func TestGetInfo(t *testing.T) {
11+
var rsp *util.Info
12+
rsp, _= GetInfo()
13+
fmt.Println(rsp.HeadBlockNum)
14+
fmt.Println(rsp.ServerVersion)
15+
}
16+
func TestGetBlock(t *testing.T) {
17+
var rsp *util.Block
18+
rsp, _= GetBlock("0000000445a9f27898383fd7de32835d5d6a978cc14ce40d9f327b5329de796b")
19+
fmt.Println(rsp.Producer)
20+
fmt.Println(rsp.ID)
21+
}
22+
func TestGetBlockNum1(t *testing.T) {
23+
var rsp *util.Block
24+
rsp, _= GetBlock("1")
25+
fmt.Println(rsp.Producer)
26+
fmt.Println(rsp.ID)
27+
}
28+
func TestGetAccountInfo(t *testing.T) {
29+
var rsp *util.AccountInfo
30+
rsp, _= GetAccountInfo()
31+
fmt.Println(rsp.AccountName)
32+
}
33+
func TestGetTxInfo(t *testing.T) {
34+
var rsp *util.TxInfo
35+
rsp, _= GetTxInfo()
36+
fmt.Println(rsp.TransactionID)
37+
fmt.Println(rsp.Transaction.Expiration)
38+
}
39+
func TestGetCodeInfo(t *testing.T) {
40+
fmt.Println("ddd")
41+
}

0 commit comments

Comments
 (0)