Skip to content

Commit

Permalink
fork branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Musiczombie committed Dec 5, 2019
1 parent a2e55ec commit 4050338
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 60 deletions.
28 changes: 14 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
)

const (
methodCurrentBlock = "ft_getCurrentBlock"
methodBlockByNumber = "ft_getBlockByNumber"
methodChainConfig = "ft_getChainConfig"
methodBlockAndResultByNumber = "ft_getBlockAndResultByNumber"
methodFeeResult = "fee_getObjectFeeResult"
methodSendRawTransaction = "ft_sendRawTransaction"
methodAssetInfoByName = "account_getAssetInfoByName"
methodAssetInfoByID = "account_getAssetInfoByID"
methodGetAccountByName = "account_getAccountByName"
methodGetCode = "account_getCode"
methodAccountByID = "account_getAccountByID"
methodDposCadidatesSize = "dpos_candidatesSize"
methodDposIrreversible = "dpos_irreversible"
methodCurrentBlock = "ft_getCurrentBlock"
methodBlockByNumber = "ft_getBlockByNumber"
methodChainConfig = "ft_getChainConfig"
methodBlockAndResultByNumberWithPayer = "ft_getBlockAndResultByNumberWithPayer"
methodFeeResult = "fee_getObjectFeeResult"
methodSendRawTransaction = "ft_sendRawTransaction"
methodAssetInfoByName = "account_getAssetInfoByName"
methodAssetInfoByID = "account_getAssetInfoByID"
methodGetAccountByName = "account_getAccountByName"
methodGetCode = "account_getCode"
methodAccountByID = "account_getAccountByID"
methodDposCadidatesSize = "dpos_candidatesSize"
methodDposIrreversible = "dpos_irreversible"
)

func GetAccountByID(id uint64) (string, error) {
Expand Down Expand Up @@ -107,7 +107,7 @@ func GetCandidatesCount() (uint64, error) {

func GetBlockAndResult(number int64) (*types.BlockAndResult, error) {
data := &types.BlockAndResult{}
err := GetData(methodBlockAndResultByNumber, &data, number)
err := GetData(methodBlockAndResultByNumberWithPayer, &data, number)
if err == ErrNull {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions db/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ type MysqlAction struct {
Payload []byte
PayloadSize int
InternalCount int
Payer string
PayerGasPrice *big.Int
}

func InsertAction(data *MysqlAction, dbTx *sql.Tx) error {
tName := GetTableNameHash("actions_hash", data.TxHash)
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)
"action_type,from_account,to_account,amount,gas_limit,gas_used,state,error_msg,remark,payload,payload_size,internal_action_count, payer, payer_gas_price) 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), 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,
data.ActionType, data.From, data.To, data.Amount.String(), data.GasLimit, data.GasUsed, state, data.ErrorMsg, data.Remark, data.Payload, data.PayloadSize, data.InternalCount)
data.ActionType, data.From, data.To, data.Amount.String(), data.GasLimit, data.GasUsed, state, data.ErrorMsg, data.Remark, data.Payload, data.PayloadSize, data.InternalCount, data.Payer, data.PayerGasPrice.String())
if err != nil {
ZapLog.Panic("InsertAction error", zap.Error(err), zap.String("txHash", data.TxHash))
}
Expand Down
2 changes: 1 addition & 1 deletion dispatch/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func scanning(fromHeight, toHeight int64, blockDataChan chan *types.BlockAndResu
case number := <-chParse:
block, err := client.GetBlockAndResult(number)
if err != nil {
ZapLog.Panic("getBlockByNumber failed", zap.Error(err))
ZapLog.Panic("GetBlockAndResult failed", zap.Error(err), zap.Int64("height", number))
}
index := (number - startNumber) % int64(rpcWorkers)

Expand Down
2 changes: 2 additions & 0 deletions mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ CREATE TABLE IF NOT EXISTS actions_hash (
amount VARCHAR(1000) NOT NULL,
gas_limit BIGINT NOT NULL,
gas_used BIGINT NOT NULL,
payer VARCHAR(100),
payer_gas_price VARCHAR(1000),
state ENUM('0', '1') NOT NULL,
error_msg longtext,
remark BLOB,
Expand Down
2 changes: 2 additions & 0 deletions task/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func (a *ActionTask) analysisAction(data *types.BlockAndResult, dbTx *sql.Tx) er
ErrorMsg: actionResult.Error,
Remark: []byte(fmt.Sprintf("%s", []byte(action.Remark))),
InternalCount: internalCount,
Payer: action.Payer.String(),
PayerGasPrice: action.PayerGasPrice,
}
parsedPayload, err := parsePayload(action)
if err != nil {
Expand Down
32 changes: 25 additions & 7 deletions task/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ func (b *BalanceTask) analysisBalance(data *types.BlockAndResult, dbTx *sql.Tx)
receipts := data.Receipts
detailTxs := data.DetailTxs
balanceChangedMap := make(map[string]map[uint64]*big.Int)
zeroBig := big.NewInt(0)
for i, tx := range txs {
receipt := receipts[i]
for j, at := range tx.RPCActions {
actionReceipt := receipt.ActionResults[j]
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(actionReceipt.GasUsed), big.NewInt(0).SetUint64(tx.GasPrice.Uint64()))
gasPrice := big.NewInt(0).Set(tx.GasPrice)
gasFrom := at.From.String()
if gasPrice.Cmp(zeroBig) == 0 {
if at.PayerGasPrice != nil {
gasPrice.Set(at.PayerGasPrice)
gasFrom = at.Payer.String()
}
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(actionReceipt.GasUsed), gasPrice)
if data.Block.Number.Uint64() > 0 {
if at.From.String() != "" {
changeBalance(balanceChangedMap, at.From.String(), tx.GasAssetID, fee, false)
if gasFrom != "" {
changeBalance(balanceChangedMap, gasFrom, tx.GasAssetID, fee, false)
}
changeBalance(balanceChangedMap, config.Chain.ChainFeeName, tx.GasAssetID, fee, true)
}
Expand Down Expand Up @@ -174,16 +183,25 @@ func (b *BalanceTask) rollback(data *types.BlockAndResult, dbTx *sql.Tx) error {
txs := data.Block.Txs
receipts := data.Receipts
detailTxs := data.DetailTxs
zeroBig := big.NewInt(0)
balanceChangedMap := make(map[string]map[uint64]*big.Int)
for i, tx := range txs {
receipt := receipts[i]
for j, at := range tx.RPCActions {
actionReceipt := receipt.ActionResults[j]
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(actionReceipt.GasUsed), big.NewInt(0).SetUint64(tx.GasPrice.Uint64()))
if at.From.String() != "" {
err := addBalance(at.From.String(), tx.GasAssetID, fee, data.Block.Number.Uint64(), data.Block.Time, dbTx)
gasPrice := big.NewInt(0).Set(tx.GasPrice)
gasFrom := at.From.String()
if gasPrice.Cmp(zeroBig) == 0 {
if at.PayerGasPrice != nil {
gasPrice.Set(at.PayerGasPrice)
gasFrom = at.Payer.String()
}
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(actionReceipt.GasUsed), gasPrice)
if gasFrom != "" {
err := addBalance(gasFrom, tx.GasAssetID, fee, data.Block.Number.Uint64(), data.Block.Time, dbTx)
if err != nil {
ZapLog.Error("add fee error: ", zap.Error(err), zap.String("fee from", at.From.String()))
ZapLog.Error("add fee error: ", zap.Error(err), zap.String("fee from", gasFrom))
return err
}
}
Expand Down
9 changes: 8 additions & 1 deletion task/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ func (b *BlockTask) analysisBlock(d *types.BlockAndResult, dbTx *sql.Tx) error {
height := d.Block.Number.Uint64()
blockFee := big.NewInt(0)
txs := d.Block.Txs
bigZero := big.NewInt(0)
for i := 0; i < len(txs); i++ {
receipt := d.Receipts[i]
err := db.InsertBlockTx(b.Tx, height, receipt.TxHash)
if err != nil {
ZapLog.Panic("InsertBlockTx error: ", zap.Error(err), zap.Uint64("height", height))
return err
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(receipt.TotalGasUsed), big.NewInt(0).SetUint64(txs[i].GasPrice.Uint64()))
gasPrice := big.NewInt(0).Set(txs[i].GasPrice)
if gasPrice.Cmp(bigZero) == 0 {
if txs[i].RPCActions[0].PayerGasPrice != nil {
gasPrice.Set(txs[i].RPCActions[0].PayerGasPrice)
}
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(receipt.TotalGasUsed), gasPrice)
blockFee = blockFee.Add(blockFee, fee)
}
err := db.InsertBlockChain(b.Tx, d.Block, d.Block.Hash, blockFee, len(txs))
Expand Down
16 changes: 14 additions & 2 deletions task/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func (b *TransactionTask) analysisTransaction(data *types.BlockAndResult, dbTx *
ZapLog.Error("InsertTransaction error: ", zap.Error(err), zap.Uint64("height", block.Number.Uint64()), zap.Int("txIndex", i))
return err
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(mTx.GasUsed), mTx.GasPrice)
gasPrice := big.NewInt(0).Set(tx.GasPrice)
if gasPrice.Cmp(big.NewInt(0)) == 0 {
if tx.RPCActions[0].PayerGasPrice != nil {
gasPrice.Set(tx.RPCActions[0].PayerGasPrice)
}
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(mTx.GasUsed), gasPrice)
b.FeeIncome = b.FeeIncome.Add(b.FeeIncome, fee)
}
b.TxCount += uint64(len(block.Txs))
Expand All @@ -67,7 +73,13 @@ func (b *TransactionTask) rollback(data *types.BlockAndResult, dbTx *sql.Tx) err
ZapLog.Error("DeleteTransactionByHash error: ", zap.Error(err), zap.Uint64("height", data.Block.Number.Uint64()), zap.String("txHash", tx.Hash.String()))
return err
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(receipt.TotalGasUsed), tx.GasPrice)
gasPrice := big.NewInt(0).Set(tx.GasPrice)
if gasPrice.Cmp(big.NewInt(0)) == 0 {
if tx.RPCActions[0].PayerGasPrice != nil {
gasPrice.Set(tx.RPCActions[0].PayerGasPrice)
}
}
fee := big.NewInt(0).Mul(big.NewInt(0).SetUint64(receipt.TotalGasUsed), gasPrice)
b.FeeIncome = b.FeeIncome.Sub(b.FeeIncome, fee)
}
b.TxCount -= uint64(len(data.Block.Txs))
Expand Down
7 changes: 7 additions & 0 deletions task/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func parsePayload(action *types.RPCAction) (interface{}, error) {
return nil, err
}
parsedPayload = arg
case types.UpdateCandidatePubKey:
arg := types.DposUpdateCandidatePubKey{}
if err := rlp.DecodeBytes(action.Payload, &arg); err != nil {
ZapLog.Error("DecodeBytes UpdateCandidatePubKey payload failed, error: ", zap.Error(err))
return nil, err
}
parsedPayload = arg
default:
return action.Payload, nil
}
Expand Down
3 changes: 1 addition & 2 deletions types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ type Account struct {
Suicide bool `json:"suicide"`
Destroy bool `json:"destroy"`
Description string `json:"description"`
}

}
67 changes: 36 additions & 31 deletions types/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const (
RefundCandidate
// VoteCandidate repesents voter vote candidate action.
VoteCandidate
// UpdateCandidatePubKey repesents update candidate action.
UpdateCandidatePubKey
)

const (
Expand All @@ -69,38 +71,41 @@ const (
)

var ActionTypeToString map[ActionType]string = map[ActionType]string{
CallContract: "CallContract",
CreateContract: "CreateContract",
CreateAccount: "CreateAccount",
UpdateAccount: "UpdateAccount",
IncreaseAsset: "IncreaseAsset",
IssueAsset: "IssueAsset",
DestroyAsset: "DestroyAsset",
SetAssetOwner: "SetAssetOwner",
Transfer: "Transfer",
UpdateAccountAuthor: "UpdateAccountAuthor",
UpdateCandidate: "UpdateCandidate",
UnregCandidate: "UnregCandidate",
RefundCandidate: "RefundCandidate",
VoteCandidate: "VoteCandidate",
WithdrawFee: "WithdrawFee",
UpdateAsset: "UpdateAsset",
RegCandidate: "RegCandidate",
KickedCandidate: "KickedCandidate",
ExitTakeOver: "ExitTakeOver",
UpdateAssetContract: "UpdateAssetContract",
CallContract: "CallContract",
CreateContract: "CreateContract",
CreateAccount: "CreateAccount",
UpdateAccount: "UpdateAccount",
IncreaseAsset: "IncreaseAsset",
IssueAsset: "IssueAsset",
DestroyAsset: "DestroyAsset",
SetAssetOwner: "SetAssetOwner",
Transfer: "Transfer",
UpdateAccountAuthor: "UpdateAccountAuthor",
UpdateCandidate: "UpdateCandidate",
UnregCandidate: "UnregCandidate",
RefundCandidate: "RefundCandidate",
VoteCandidate: "VoteCandidate",
WithdrawFee: "WithdrawFee",
UpdateAsset: "UpdateAsset",
RegCandidate: "RegCandidate",
KickedCandidate: "KickedCandidate",
ExitTakeOver: "ExitTakeOver",
UpdateAssetContract: "UpdateAssetContract",
UpdateCandidatePubKey: "UpdateCandidatePubKey",
}

type RPCAction struct {
Type ActionType `json:"type"`
Nonce uint64 `json:"nonce"`
From Name `json:"from"`
To Name `json:"to"`
AssetID uint64 `json:"assetID"`
GasLimit uint64 `json:"gas"`
Amount *big.Int `json:"value"`
Remark hexutil.Bytes `json:"remark"`
Payload hexutil.Bytes `json:"payload"`
ActionHash Hash `json:"actionHash"`
ActionIndex uint64 `json:"actionIndex"`
Type ActionType `json:"type"`
Nonce uint64 `json:"nonce"`
From Name `json:"from"`
To Name `json:"to"`
AssetID uint64 `json:"assetID"`
GasLimit uint64 `json:"gas"`
Amount *big.Int `json:"value"`
Remark hexutil.Bytes `json:"remark"`
Payload hexutil.Bytes `json:"payload"`
ActionHash Hash `json:"actionHash"`
ActionIndex uint64 `json:"actionIndex"`
Payer Name `json:"payer"`
PayerGasPrice *big.Int `json:"payerGasPrice"`
}
4 changes: 4 additions & 0 deletions types/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ type DposIrreversible struct {
ProposedIrreversible uint64 `json:"proposedIrreversible"`
BftIrreversible uint64 `json:"bftIrreversible"`
}

type DposUpdateCandidatePubKey struct {
PubKey PubKey
}

0 comments on commit 4050338

Please sign in to comment.