Skip to content

Commit

Permalink
Go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
zhugelianglongming committed Dec 29, 2022
1 parent 5f59ca7 commit 04603ac
Show file tree
Hide file tree
Showing 61 changed files with 159 additions and 144 deletions.
9 changes: 5 additions & 4 deletions bcs/consensus/pow/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

// PoWConfig pow需要解析的创始块解析格式
// 根据Bitcoin推荐
// AdjustHeightGap: 2016,
// MaxTarget: 0x1d00FFFF,
// DefaultTarget: 0x207FFFFF
//
// 根据Bitcoin推荐
// AdjustHeightGap: 2016,
// MaxTarget: 0x1d00FFFF,
// DefaultTarget: 0x207FFFFF
type PoWConfig struct {
DefaultTarget uint32 `json:"defaultTarget"`
AdjustHeightGap int32 `json:"adjustHeightGap"`
Expand Down
4 changes: 2 additions & 2 deletions bcs/consensus/pow/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (pow *PoWConsensus) ProcessBeforeMiner(height, timestamp int64) ([]byte, []
bits, err := pow.refreshDifficulty(preBlock.GetBlockid(), tipHeight+1)
if err != nil {
// TODO: deal with error
_ = pow.Stop()
_ = pow.Stop()
}
pow.targetBits = bits
store := &PoWStorage{
Expand Down Expand Up @@ -393,7 +393,7 @@ func (pow *PoWConsensus) refreshDifficulty(tipHash []byte, nextHeight int64) (ui
return newTargetBits, nil
}

//IsProofed check workload proof
// IsProofed check workload proof
func (pow *PoWConsensus) IsProofed(blockID []byte, targetBits uint32) bool {
hash := new(big.Int)
hash.SetBytes(blockID)
Expand Down
6 changes: 4 additions & 2 deletions bcs/consensus/tdpos/kernel_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ func (tp *tdposConsensus) runRevokeCandidate(contractCtx contract.KContext) (*co

// runVote 执行投票
// Args: candidate::候选人钱包地址
// amount::投票者票数
//
// amount::投票者票数
func (tp *tdposConsensus) runVote(contractCtx contract.KContext) (*contract.Response, error) {
// 1.1 验证合约参数是否正确
candidateName, err := tp.checkArgs(contractCtx.Args())
Expand Down Expand Up @@ -253,7 +254,8 @@ func (tp *tdposConsensus) runVote(contractCtx contract.KContext) (*contract.Resp
// runRevokeVote 执行选票撤销
// 重构后的候选人撤销
// Args: candidate::候选人钱包地址
// amount: 投票数
//
// amount: 投票数
func (tp *tdposConsensus) runRevokeVote(contractCtx contract.KContext) (*contract.Response, error) {
// 1.1 验证合约参数
candidateName, err := tp.checkArgs(contractCtx.Args())
Expand Down
2 changes: 1 addition & 1 deletion bcs/consensus/tdpos/tdpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck

bmock "github.com/xuperchain/xupercore/bcs/consensus/mock"
lpb "github.com/xuperchain/xupercore/bcs/ledger/xledger/xldgpb"
Expand Down
2 changes: 1 addition & 1 deletion bcs/consensus/xpoa/xpoa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck
bmock "github.com/xuperchain/xupercore/bcs/consensus/mock"
lpb "github.com/xuperchain/xupercore/bcs/ledger/xledger/xldgpb"
cctx "github.com/xuperchain/xupercore/kernel/consensus/context"
Expand Down
16 changes: 9 additions & 7 deletions bcs/ledger/xledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sync"
"time"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck

"github.com/xuperchain/xupercore/bcs/ledger/xledger/def"
pb "github.com/xuperchain/xupercore/bcs/ledger/xledger/xldgpb"
Expand Down Expand Up @@ -353,7 +353,7 @@ func (l *Ledger) formatBlock(txList []*pb.Transaction,
return block, nil
}

//保存一个区块(只包括区块头)
// 保存一个区块(只包括区块头)
// 注:只是打包到一个leveldb batch write对象中
func (l *Ledger) saveBlock(block *pb.InternalBlock, batchWrite kvdb.Batch) error {
header := *block
Expand Down Expand Up @@ -385,7 +385,7 @@ func (l *Ledger) fetchBlockForModify(blockid []byte) (*pb.InternalBlock, error)
return &blk, nil
}

//根据blockid获取一个Block, 只包含区块头
// 根据blockid获取一个Block, 只包含区块头
func (l *Ledger) fetchBlock(blockid []byte) (*pb.InternalBlock, error) {
blkInCache, cacheHit := l.blkHeaderCache.Get(string(blockid))
if cacheHit {
Expand All @@ -409,7 +409,7 @@ func (l *Ledger) fetchBlock(blockid []byte) (*pb.InternalBlock, error) {
return block, nil
}

//当发生主干切换后,确保最长路径上的block的tx的blockid指向它
// 当发生主干切换后,确保最长路径上的block的tx的blockid指向它
func (l *Ledger) correctTxsBlockid(blockID []byte, batchWrite kvdb.Batch) error {
block, err := l.queryBlock(blockID, true)
if err != nil {
Expand All @@ -432,10 +432,12 @@ func (l *Ledger) correctTxsBlockid(blockID []byte, batchWrite kvdb.Batch) error
return nil
}

//处理分叉
// 处理分叉
// P---->P---->P---->P (old tip)
// |
// +---->Q---->Q--->NewTip
//
// |
// +---->Q---->Q--->NewTip
//
// 处理完后,会返回分叉点的block
func (l *Ledger) handleFork(oldTip []byte, newTipPre []byte, nextHash []byte, batchWrite kvdb.Batch) (*pb.InternalBlock, error) {
p := oldTip
Expand Down
2 changes: 1 addition & 1 deletion bcs/ledger/xledger/ledger/ledger_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func MakeMerkleTree(txList []*pb.Transaction) [][]byte {
// wg.Wait()
// }

//序列化系统合约失败的Txs
// 序列化系统合约失败的Txs
func encodeFailedTxs(buf *bytes.Buffer, block *pb.InternalBlock) error {
txids := []string{}
for txid := range block.FailedTxs {
Expand Down
2 changes: 1 addition & 1 deletion bcs/ledger/xledger/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
//"os"
"testing"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck

"github.com/xuperchain/xupercore/bcs/ledger/xledger/state/utxo/txhash"
pb "github.com/xuperchain/xupercore/bcs/ledger/xledger/xldgpb"
Expand Down
2 changes: 1 addition & 1 deletion bcs/ledger/xledger/state/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (t *StateCtx) SetTimerTaskMG(timerTaskMgr timerTask.TimerManager) {
t.TimerTaskMgr = timerTaskMgr
}

//state各个func里尽量调一下判断
// state各个func里尽量调一下判断
func (t *StateCtx) IsInit() bool {
if t.AclMgr == nil || t.ContractMgr == nil || t.GovernTokenMgr == nil || t.ProposalMgr == nil ||
t.TimerTaskMgr == nil || t.Crypt == nil || t.Ledger == nil {
Expand Down
20 changes: 10 additions & 10 deletions bcs/ledger/xledger/state/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"sync"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck

rich "github.com/xuperchain/xupercore/bcs/ledger/xledger/batch"
rb "github.com/xuperchain/xupercore/bcs/ledger/xledger/batch"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/def"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/ledger"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/state/context"
Expand Down Expand Up @@ -133,7 +133,7 @@ func (t *Meta) UpdateNewAccountResourceAmount(newAccountResourceAmount int64, ba
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.NewAccountResourceAmountKey, newAccountResourceAmountBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.NewAccountResourceAmountKey, newAccountResourceAmountBuf)
if err == nil {
t.log.Info("Update newAccountResourceAmount succeed")
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func (t *Meta) UpdateMaxBlockSize(maxBlockSize int64, batch kvdb.Batch) error {
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.MaxBlockSizeKey, maxBlockSizeBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.MaxBlockSizeKey, maxBlockSizeBuf)
if err == nil {
t.log.Info("Update maxBlockSize succeed")
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func (t *Meta) LoadReservedContracts() ([]*protos.InvokeRequest, error) {
return nil, findErr
}

//when to register to kernel method
// when to register to kernel method
func (t *Meta) UpdateReservedContracts(params []*protos.InvokeRequest, batch kvdb.Batch) error {
if params == nil {
return fmt.Errorf("invalid reservered contract requests")
Expand All @@ -226,7 +226,7 @@ func (t *Meta) UpdateReservedContracts(params []*protos.InvokeRequest, batch kvd
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.ReservedContractsKey, paramsBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.ReservedContractsKey, paramsBuf)
if err == nil {
t.log.Info("Update reservered contract succeed")
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func (t *Meta) UpdateForbiddenContract(param *protos.InvokeRequest, batch kvdb.B
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.ForbiddenContractKey, paramBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.ForbiddenContractKey, paramBuf)
if err == nil {
t.log.Info("Update forbidden contract succeed")
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func (t *Meta) UpdateIrreversibleBlockHeight(nextIrreversibleBlockHeight int64,
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.IrreversibleBlockHeightKey, irreversibleBlockHeightBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.IrreversibleBlockHeightKey, irreversibleBlockHeightBuf)
if err != nil {
return err
}
Expand Down Expand Up @@ -427,7 +427,7 @@ func (t *Meta) UpdateIrreversibleSlideWindow(nextIrreversibleSlideWindow int64,
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.IrreversibleSlideWindowKey, irreversibleSlideWindowBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.IrreversibleSlideWindowKey, irreversibleSlideWindowBuf)
if err != nil {
return err
}
Expand Down Expand Up @@ -506,7 +506,7 @@ func (t *Meta) UpdateGasPrice(nextGasPrice *protos.GasPrice, batch kvdb.Batch) e
t.log.Warn("failed to marshal pb meta")
return pbErr
}
err := rich.NewRichBatch(batch).PutMeta(ledger.GasPriceKey, gasPriceBuf)
err := rb.NewRichBatch(batch).PutMeta(ledger.GasPriceKey, gasPriceBuf)
if err != nil {
return err
}
Expand Down
24 changes: 12 additions & 12 deletions bcs/ledger/xledger/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"strconv"
"time"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck

rich "github.com/xuperchain/xupercore/bcs/ledger/xledger/batch"
rb "github.com/xuperchain/xupercore/bcs/ledger/xledger/batch"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/def"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/ledger"
"github.com/xuperchain/xupercore/bcs/ledger/xledger/state/context"
Expand Down Expand Up @@ -432,7 +432,7 @@ func (t *State) PlayForMiner(blockid []byte) error {
return err
}
} else {
rich.NewRichBatch(batch).DeleteUnconfirmedTx([]byte(txid))
rb.NewRichBatch(batch).DeleteUnconfirmedTx([]byte(txid))
}
err = t.payFee(tx, batch, block)
if err != nil {
Expand Down Expand Up @@ -829,7 +829,7 @@ func (t *State) doTxSync(tx *pb.Transaction) error {
return err
}

rich.NewRichBatch(batch).PutUnconfirmedTx(tx.Txid, pbTxBuf)
rb.NewRichBatch(batch).PutUnconfirmedTx(tx.Txid, pbTxBuf)
t.log.Debug("print tx size when DoTx", "tx_size", batch.ValueSize(), "txid", utils.F(tx.Txid))
beginTime = time.Now()
writeErr := batch.Write()
Expand Down Expand Up @@ -860,7 +860,7 @@ func (t *State) doTxInternal(tx *pb.Transaction, batch kvdb.Batch, cacheFiller *
t.log.Warn("xmodel DoTx failed", "err", err)
return ErrRWSetInvalid
}
richBatch := rich.NewRichBatch(batch)
richBatch := rb.NewRichBatch(batch)
for _, txInput := range tx.TxInputs {
addr := txInput.FromAddr
txid := txInput.RefTxid
Expand Down Expand Up @@ -987,7 +987,7 @@ func (t *State) undoUnconfirmedTx(tx *pb.Transaction,
if undoErr != nil {
return undoErr
}
rich.NewRichBatch(batch).DeleteUnconfirmedTx(tx.Txid)
rb.NewRichBatch(batch).DeleteUnconfirmedTx(tx.Txid)

// 记录回滚交易,用于重放
if undoDone != nil {
Expand All @@ -1012,7 +1012,7 @@ func (t *State) undoTxInternal(tx *pb.Transaction, batch kvdb.Batch) error {
return ErrRWSetInvalid
}

richBatch := rich.NewRichBatch(batch)
richBatch := rb.NewRichBatch(batch)
for _, txInput := range tx.TxInputs {
addr := txInput.FromAddr
txid := txInput.RefTxid
Expand Down Expand Up @@ -1141,7 +1141,7 @@ func (t *State) updateLatestBlockid(newBlockid []byte, batch kvdb.Batch, reason
if err != nil {
return err
}
if err := rich.NewRichBatch(batch).PutMeta(utxo.LatestBlockKey, newBlockid); err != nil {
if err := rb.NewRichBatch(batch).PutMeta(utxo.LatestBlockKey, newBlockid); err != nil {
return err
}
writeErr := batch.Write()
Expand All @@ -1156,7 +1156,7 @@ func (t *State) updateLatestBlockid(newBlockid []byte, batch kvdb.Batch, reason
}

func (t *State) undoPayFee(tx *pb.Transaction, batch kvdb.Batch, block *pb.InternalBlock) error {
richBatch := rich.NewRichBatch(batch)
richBatch := rb.NewRichBatch(batch)
for offset, txOutput := range tx.TxOutputs {
addr := txOutput.ToAddr
if !bytes.Equal(addr, []byte(FeePlaceholder)) {
Expand Down Expand Up @@ -1269,7 +1269,7 @@ func (t *State) payFee(tx *pb.Transaction, batch kvdb.Batch, block *pb.InternalB
if uErr != nil {
return uErr
}
rich.NewRichBatch(batch).PutUtxoWithPrefix(utxoKey, uItemBinary)// 插入本交易产生的utxo
rb.NewRichBatch(batch).PutUtxoWithPrefix(utxoKey, uItemBinary) // 插入本交易产生的utxo
t.utxo.AddBalance(addr, uItem.Amount)
t.utxo.UtxoCache.Insert(string(addr), utxoKey, uItem)
t.log.Trace(" insert fee utxo key", "utxoKey", utxoKey, "amount", uItem.Amount.String())
Expand Down Expand Up @@ -1362,7 +1362,7 @@ func (t *State) processUnconfirmTxs(block *pb.InternalBlock, batch kvdb.Batch, n

txid := string(tx.GetTxid())
if t.tx.Mempool.HasTx(txid) {
rich.NewRichBatch(batch).DeleteUnconfirmedTx([]byte(txid))
rb.NewRichBatch(batch).DeleteUnconfirmedTx([]byte(txid))
t.log.Trace(" delete from unconfirmed", "txid", fmt.Sprintf("%x", tx.GetTxid()))
unconfirmToConfirm[txid] = true
}
Expand All @@ -1379,7 +1379,7 @@ func (t *State) processUnconfirmTxs(block *pb.InternalBlock, batch kvdb.Batch, n
if undoDone[string(undoTx.Txid)] {
continue
}
rich.NewRichBatch(batch).DeleteUnconfirmedTx(undoTx.Txid) // mempool 中删除后,db 的未确认交易中也要删除。
rb.NewRichBatch(batch).DeleteUnconfirmedTx(undoTx.Txid) // mempool 中删除后,db 的未确认交易中也要删除。
undoErr := t.undoUnconfirmedTx(undoTx, batch, undoDone, nil)
if undoErr != nil {
t.log.Warn("fail to undo tx", "undoErr", undoErr)
Expand Down
31 changes: 16 additions & 15 deletions bcs/ledger/xledger/state/tx_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ import (
"github.com/xuperchain/xupercore/lib/metrics"
"github.com/xuperchain/xupercore/protos"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/golang/protobuf/proto" //nolint:staticcheck
)

// ImmediateVerifyTx verify tx Immediately
// Transaction verification workflow:
// 1. verify transaction ID is the same with data hash
// 2. verify all signatures of initiator and auth requires
// 3. verify the utxo input, there are three kinds of input validation
// 1). PKI technology for transferring from address
// 2). Account ACL for transferring from account
// 3). Contract logic transferring from contract
// 4. verify the contract requests' permission
// 5. verify the permission of contract RWSet (WriteSet could including unauthorized data change)
// 6. run contract requests and verify if the RWSet result is the same with preExed RWSet (heavy
// operation, keep it at last)
// 1. verify transaction ID is the same with data hash
// 2. verify all signatures of initiator and auth requires
// 3. verify the utxo input, there are three kinds of input validation
// 1). PKI technology for transferring from address
// 2). Account ACL for transferring from account
// 3). Contract logic transferring from contract
// 4. verify the contract requests' permission
// 5. verify the permission of contract RWSet (WriteSet could including unauthorized data change)
// 6. run contract requests and verify if the RWSet result is the same with preExed RWSet (heavy
// operation, keep it at last)
func (t *State) ImmediateVerifyTx(tx *pb.Transaction, isRootTx bool) (bool, error) {
beginTime := time.Now()
code := "InvalidTx"
Expand Down Expand Up @@ -149,10 +149,10 @@ func (t *State) ImmediateVerifyTx(tx *pb.Transaction, isRootTx bool) (bool, erro

// ImmediateVerifyTx verify auto tx Immediately
// Transaction verification workflow:
// 0. 其实可以直接判断二者的txid,相同,则包括读写集在内的内容都相同
// 1. verify transaction ID is the same with data hash
// 2. run contract requests and verify if the RWSet result is the same with preExed RWSet (heavy
// operation, keep it at last)
// 0. 其实可以直接判断二者的txid,相同,则包括读写集在内的内容都相同
// 1. verify transaction ID is the same with data hash
// 2. run contract requests and verify if the RWSet result is the same with preExed RWSet (heavy
// operation, keep it at last)
func (t *State) ImmediateVerifyAutoTx(blockHeight int64, tx *pb.Transaction, isRootTx bool) (bool, error) {
// 获取该区块触发的定时交易
autoTx, genErr := t.GetTimerTx(blockHeight)
Expand Down Expand Up @@ -332,6 +332,7 @@ func (t *State) verifyXuperSign(tx *pb.Transaction, digestHash []byte) (bool, ma
}

// verify utxo inputs, there are three kinds of input validation
//
// 1). PKI technology for transferring from address
// 2). Account ACL for transferring from account
// 3). Contract logic transferring from contract
Expand Down
Loading

0 comments on commit 04603ac

Please sign in to comment.