Skip to content

Commit

Permalink
modify client
Browse files Browse the repository at this point in the history
  • Loading branch information
Musiczombie committed Oct 24, 2019
1 parent 97a53e0 commit c68fffe
Show file tree
Hide file tree
Showing 32 changed files with 374 additions and 489 deletions.
373 changes: 80 additions & 293 deletions client/client.go

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/browser/types"
"testing"
)

Expand All @@ -20,3 +21,11 @@ func TestSendRawTransaction(t *testing.T) {
}
t.Log(hash)
}

func TestGetCurrentBlockInfo(t *testing.T) {
data := &types.RpcBlock{}
err := GetData(methodCurrentBlock, data, false)
if err != nil {
t.Failed()
}
}
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ browser:
port: 3306
database: ft
node:
rpcHost: http://192.168.1.148:3090
rpcHost: http://192.168.1.148:9090
log:
level: info #error info debug
file:
Expand All @@ -15,7 +15,7 @@ browser:
rotationTime: 1 #time.Duration 1:day 2:hour
maxAge: 15 #day
console: true
syncBlockShowNumber: 1000
syncBlockShowNumber: 1
tasks:
- block
- txs
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ type ChainConfig struct {
CandidateScheduleSize uint64
BlockFrequency uint64
ChainId uint64
StartTime int64
StartTime uint64
}
4 changes: 2 additions & 2 deletions db/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type MysqlAccount struct {
Permissions string
ContractCode string
CodeHash string
Created uint
ContractCreated uint
Created uint64
ContractCreated uint64
Suicide bool
Destroy bool
Description string
Expand Down
9 changes: 5 additions & 4 deletions db/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MysqlAction struct {
ActionIndex int
Nonce uint64
Height uint64
Created uint
Created uint64
GasAssetId uint64
TransferAssetId uint64
ActionType uint64
Expand All @@ -36,11 +36,12 @@ type MysqlAction struct {

func InsertAction(data *MysqlAction, dbTx *sql.Tx) error {
tName := GetTableNameHash("actions_hash", data.TxHash)
stmt, err := dbTx.Prepare(fmt.Sprintf("insert into %s (tx_hash, action_hash, action_index,nonce,height,created,gas_asset_id,transfer_asset_id,"+
"action_type,from_account,to_account,amount,gas_limit,gas_used,state,error_msg,remark,payload,payload_size,internal_action_count) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", tName))
insertSql := fmt.Sprintf("insert into %s (tx_hash, action_hash, action_index,nonce,height,created,gas_asset_id,transfer_asset_id,"+
"action_type,from_account,to_account,amount,gas_limit,gas_used,state,error_msg,remark,payload,payload_size,internal_action_count) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", tName)
stmt, err := dbTx.Prepare(insertSql)
defer stmt.Close()
if err != nil {
ZapLog.Panic("InsertAction error", zap.Error(err), zap.String("txHash", data.TxHash))
ZapLog.Panic("InsertAction error", zap.Error(err), zap.String("txHash", data.TxHash), zap.String("sql", insertSql))
}
state := strconv.FormatUint(data.State, 10)
_, err = stmt.Exec(data.TxHash, data.ActionHash, data.ActionIndex, data.Nonce, data.Height, data.Created, data.GasAssetId, data.TransferAssetId,
Expand Down
4 changes: 2 additions & 2 deletions db/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetAccountBalance(name string, assetId uint64, dbTx *sql.Tx) (*big.Int, err
return data, nil
}

func UpdateAccountBalance(name string, amount *big.Int, assetId uint64, updateHeight uint64, updateTime uint, dbTx *sql.Tx) error {
func UpdateAccountBalance(name string, amount *big.Int, assetId uint64, updateHeight uint64, updateTime uint64, dbTx *sql.Tx) error {
tName := GetTableNameHash("balance_hash", name)
updateSql := fmt.Sprintf("update %s set amount = '%s', update_height = %d, update_time = %d where account_name = '%s' and asset_id = %d",
tName, amount.String(), updateHeight, updateTime, name, assetId)
Expand All @@ -48,7 +48,7 @@ func UpdateAccountBalance(name string, amount *big.Int, assetId uint64, updateHe
return nil
}

func InsertAccountBalance(name string, amount *big.Int, assetId uint64, height uint64, updateTime uint, dbTx *sql.Tx) error {
func InsertAccountBalance(name string, amount *big.Int, assetId uint64, height uint64, updateTime uint64, dbTx *sql.Tx) error {
tName := GetTableNameHash("balance_hash", name)
insertSql := fmt.Sprintf("insert %s set account_name = '%s', asset_id = %d, amount = '%s', update_height = %d, update_time = %d",
tName, name, assetId, amount.String(), height, updateTime)
Expand Down
4 changes: 2 additions & 2 deletions db/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"go.uber.org/zap"
)

func InsertBlockChain(tx *sql.Tx, hd *types.Header, bhash types.Hash, fee *big.Int, txCount int) error {
func InsertBlockChain(tx *sql.Tx, hd *types.RpcBlock, bhash types.Hash, fee *big.Int, txCount int) error {
tName := GetTableNameID1("block_id", hd.Number.Uint64())
blocksql := fmt.Sprintf("INSERT INTO %s(hash, parent_hash, height, created, gas_limit, gas_used, producer, tx_count ,fee) VALUES('%s','%s',%d,%d,%d,%d,'%s',%d,'%s');",
tName, bhash.String(), hd.ParentHash.String(), hd.Number.Int64(), hd.Time, hd.GasLimit, hd.GasUsed, hd.Coinbase.String(), txCount, fee.String())
tName, bhash.String(), hd.ParentHash.String(), hd.Number.Int64(), hd.Time, hd.GasLimit, hd.GasUsed, hd.CoinBase.String(), txCount, fee.String())
_, err := tx.Exec(blocksql)
if err != nil {
ZapLog.Error("insert block failed", zap.String("sql", blocksql), zap.Error(err))
Expand Down
2 changes: 1 addition & 1 deletion db/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MysqlFee struct {
ActionIndex int
FeeIndex int
Height uint64
Created uint
Created uint64
AssetId uint64
From string
To string
Expand Down
2 changes: 1 addition & 1 deletion db/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MysqlInternal struct {
ActionIndex int
InternalIndex int
Height uint64
Created uint
Created uint64
AssetId uint64
ActionType uint64
From string
Expand Down
4 changes: 2 additions & 2 deletions db/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ type Token struct {
ContractName string
Description string
CreateUser string
CreateTime uint
CreateTime uint64
AssetOwner string
Founder string
UpperLimit *big.Int
Liquidity *big.Int
CumulativeIssue *big.Int
CumulativeDestruction *big.Int
UpdateTime uint
UpdateTime uint64
}

func AddToken(tx *sql.Tx, token *Token) {
Expand Down
24 changes: 12 additions & 12 deletions dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ func (d *Dispatch) sendBlockToTask() {
for {
block := <-d.blockDataChan

if block.Block.Head.Number.Uint64() > d.batchTo { //the inverse calculation block
if block.Block.Number.Uint64() > d.batchTo { //the inverse calculation block
if d.currentBlock == nil {
d.currentBlock = block
} else {
if d.currentBlock.Hash.String() != block.Block.Head.ParentHash.String() {
if d.currentBlock.Block.Hash.String() != block.Block.ParentHash.String() {
d.currentBlock = block
d.rollback()
ZapLog.Info("rollback success", zap.Uint64("height", block.Block.Head.Number.Uint64()-1))
ZapLog.Info("rollback success", zap.Uint64("height", block.Block.Number.Uint64()-1))
continue
}
d.currentBlock = block
Expand All @@ -179,8 +179,8 @@ func (d *Dispatch) sendBlockToTask() {
taskDataChan <- taskData
}
d.checkTaskResult()
if block.Block.Head.Number.Int64()%config.Log.SyncBlockShowNumber == 0 {
ZapLog.Info("commit success", zap.Uint64("height", block.Block.Head.Number.Uint64()))
if block.Block.Number.Int64()%config.Log.SyncBlockShowNumber == 0 {
ZapLog.Info("commit success", zap.Uint64("height", block.Block.Number.Uint64()))
}
}
}()
Expand Down Expand Up @@ -215,15 +215,15 @@ func (d *Dispatch) rollback() {
break
}
}
endHeight := d.currentBlock.Block.Head.Number.Uint64() - 1
endHeight := d.currentBlock.Block.Number.Uint64() - 1

for ; ; endHeight-- {
dbBlock := db.Mysql.GetBlockOriginalByHeight(endHeight)
chainBlock, err := client.GetBlockAndResult(int64(endHeight))
if err != nil {
ZapLog.Panic("rpc get block error", zap.Error(err))
}
if dbBlock.BlockHash != chainBlock.Hash.String() {
if dbBlock.BlockHash != chainBlock.Block.Hash.String() {
rollbackData := &task.TaskChanData{
Block: BlobToBlock(dbBlock),
Tx: nil,
Expand All @@ -244,13 +244,13 @@ func (d *Dispatch) rollback() {
}

func (d *Dispatch) cacheBlock(blockData *task.TaskChanData) {
height := blockData.Block.Block.Head.Number.Uint64()
height := blockData.Block.Block.Number.Uint64()
if height > d.batchTo {
irreversible, err := client.GetDposIrreversible()
if err != nil {
ZapLog.Panic("cache block data error", zap.Error(err))
}
if blockData.Block.Block.Head.Number.Uint64() > irreversible.BftIrreversible {
if blockData.Block.Block.Number.Uint64() > irreversible.BftIrreversible {
db.AddReversibleBlockCache(BlockToBlob(blockData.Block))
}
}
Expand All @@ -272,9 +272,9 @@ func BlockToBlob(block *types.BlockAndResult) *db.BlockOriginal {
}
result := &db.BlockOriginal{
BlockData: data,
Height: block.Block.Head.Number.Uint64(),
BlockHash: block.Hash.String(),
ParentHash: block.Block.Head.ParentHash.String(),
Height: block.Block.Number.Uint64(),
BlockHash: block.Block.Hash.String(),
ParentHash: block.Block.ParentHash.String(),
}
return result
}
Expand Down
2 changes: 1 addition & 1 deletion dispatch/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func scanning(fromHeight, toHeight int64, blockDataChan chan *types.BlockAndResu
case <-ctx.Done():
return
case block := <-chSave:
currentHeight := block.Block.Head.Number.Int64()
currentHeight := block.Block.Number.Int64()
ZapLog.Debug("get block data finished", zap.Int64("height", currentHeight))
if currentHeight%config.Log.SyncBlockShowNumber == 0 {
ZapLog.Info("get block data finished", zap.Int64("height", currentHeight))
Expand Down
3 changes: 2 additions & 1 deletion init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ func initChainConfig() {
ZapLog.Error("GetChainConfig error: ", zap.Error(err))
panic(err)
}
sTime, err := client.GetBlockTimeByNumber(1)
block, err := client.GetBlockByNumber(1)
if err != nil {
ZapLog.Error("GetBlockTimeByNumber error: ", zap.Error(err))
panic(err)
}
sTime := block.Time / 1000000000
config.Chain = &config.ChainConfig{
FeeAssetId: uint64(0),
ChainName: chainConfig.ChainName,
Expand Down
30 changes: 12 additions & 18 deletions task/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/browser/db"
. "github.com/browser/log"
"github.com/browser/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"go.uber.org/zap"
"strings"
)
Expand All @@ -18,7 +17,7 @@ type AccountTask struct {
*Base
}

func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, block *types.Block, oldAccounts map[string]struct{}) error {
func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, block *types.RpcBlock, oldAccounts map[string]struct{}) error {
aType := action.Type
payload, err := parsePayload(action)
if err != nil {
Expand Down Expand Up @@ -69,7 +68,7 @@ func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, blo
Threshold: 1,
UpdateAuthorThreshold: 1,
Permissions: string(authorText),
Created: block.Head.Time,
Created: block.Time,
ContractCreated: 0,
Description: arg.Description,
}
Expand All @@ -81,8 +80,8 @@ func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, blo
mAcct.AuthorVersion = authorVersion.String()
mAcct.CodeHash = crypto.Keccak256Hash(nil).String()

if block.Head.Number.Uint64() == 0 {
mAcct.ContractCreated = block.Head.Time
if block.Number.Uint64() == 0 {
mAcct.ContractCreated = block.Time
}
err = db.InsertAccount(mAcct, dbTx)
if err != nil {
Expand All @@ -103,7 +102,7 @@ func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, blo
}
mOldAccount := &db.MysqlAccountRollback{
Account: account,
Height: block.Head.Number.Uint64() - 1,
Height: block.Number.Uint64() - 1,
}
err = db.InsertAccountRollback(mOldAccount, dbTx)
if err != nil {
Expand Down Expand Up @@ -211,7 +210,7 @@ func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, blo
}
mOldAccount := &db.MysqlAccountRollback{
Account: account,
Height: block.Head.Number.Uint64() - 1,
Height: block.Number.Uint64() - 1,
}
err = db.InsertAccountRollback(mOldAccount, dbTx)
if err != nil {
Expand All @@ -229,19 +228,14 @@ func (a *AccountTask) ActionToAccount(action *types.RPCAction, dbTx *sql.Tx, blo
if len(action.Payload) != 0 {
values := map[string]interface{}{
"contract_code": hex.EncodeToString(action.Payload),
"contract_created": block.Head.Time,
"contract_created": block.Time,
}
code, err := client.GetCode(action.To.String())
if err != nil {
ZapLog.Error("GetCode failed", zap.String("name", action.To.String()), zap.Error(err))
return err
}
bitCode, err := hexutil.Decode(code)
if err != nil {
ZapLog.Error("Decode failed", zap.String("name", action.To.String()), zap.Error(err))
return err
}
values["code_hash"] = crypto.Keccak256Hash(bitCode).String()
values["code_hash"] = crypto.Keccak256Hash(code.Code).String()
err = db.UpdateAccount(action.To.String(), values, dbTx)
if err != nil {
ZapLog.Error("CreateContract failed", zap.Error(err))
Expand Down Expand Up @@ -397,11 +391,11 @@ func (a *AccountTask) Start(data chan *TaskChanData, rollbackData chan *TaskChan
for {
select {
case d := <-data:
if d.Block.Block.Head.Number.Uint64() >= a.startHeight {
if d.Block.Block.Number.Uint64() >= a.startHeight {
a.init()
err := a.analysisAccount(d.Block, a.Tx)
if err != nil {
ZapLog.Error("AccountTask analysisAccount error: ", zap.Error(err), zap.Uint64("height", d.Block.Block.Head.Number.Uint64()))
ZapLog.Error("AccountTask analysisAccount error: ", zap.Error(err), zap.Uint64("height", d.Block.Block.Number.Uint64()))
panic(err)
}
a.startHeight++
Expand All @@ -410,11 +404,11 @@ func (a *AccountTask) Start(data chan *TaskChanData, rollbackData chan *TaskChan
result <- true
case rd := <-rollbackData:
a.startHeight--
if a.startHeight == rd.Block.Block.Head.Number.Uint64() {
if a.startHeight == rd.Block.Block.Number.Uint64() {
a.init()
err := a.rollback(rd.Block, a.Tx)
if err != nil {
ZapLog.Error("AccountTask rollback error: ", zap.Error(err), zap.Uint64("height", rd.Block.Block.Head.Number.Uint64()))
ZapLog.Error("AccountTask rollback error: ", zap.Error(err), zap.Uint64("height", rd.Block.Block.Number.Uint64()))
panic(err)
}
a.commit()
Expand Down
Loading

0 comments on commit c68fffe

Please sign in to comment.