X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=api%2Fblock_retrieve.go;h=10b9fb0f33440059d24f085a2c443590b816f8e1;hp=ed043695696bcc5b2ce14107453e4e38aad214d5;hb=31f8f7cf1ffbec5365ab6ebf217537809cf714e5;hpb=db158dcf09436b003defd333f1a665e7e051d820 diff --git a/api/block_retrieve.go b/api/block_retrieve.go index ed043695..10b9fb0f 100644 --- a/api/block_retrieve.go +++ b/api/block_retrieve.go @@ -1,14 +1,10 @@ package api import ( - "math/big" - - "gopkg.in/fatih/set.v0" + set "gopkg.in/fatih/set.v0" "github.com/vapor/blockchain/query" - "github.com/vapor/consensus/difficulty" chainjson "github.com/vapor/encoding/json" - "github.com/vapor/errors" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" ) @@ -45,18 +41,17 @@ type BlockReq struct { // GetBlockResp is the resp for getBlock api type GetBlockResp struct { - Hash *bc.Hash `json:"hash"` - Size uint64 `json:"size"` - Version uint64 `json:"version"` - Height uint64 `json:"height"` - PreviousBlockHash *bc.Hash `json:"previous_block_hash"` - Timestamp uint64 `json:"timestamp"` - Nonce uint64 `json:"nonce"` - Bits uint64 `json:"bits"` - Difficulty string `json:"difficulty"` - TransactionsMerkleRoot *bc.Hash `json:"transaction_merkle_root"` - TransactionStatusHash *bc.Hash `json:"transaction_status_hash"` - Transactions []*BlockTx `json:"transactions"` + Hash *bc.Hash `json:"hash"` + Size uint64 `json:"size"` + Version uint64 `json:"version"` + Height uint64 `json:"height"` + PreviousBlockHash *bc.Hash `json:"previous_block_hash"` + Timestamp uint64 `json:"timestamp"` + Witness []chainjson.HexBytes `json:"witness"` + Blocker string `json:"blocker"` + TransactionsMerkleRoot *bc.Hash `json:"transaction_merkle_root"` + TransactionStatusHash *bc.Hash `json:"transaction_status_hash"` + Transactions []*BlockTx `json:"transactions"` } // return block by hash/height @@ -73,6 +68,17 @@ func (a *API) getBlock(ins BlockReq) Response { return NewErrorResponse(err) } + witness := make([]chainjson.HexBytes, len(block.Witness)) + for i, w := range block.Witness { + witness[i] = w + } + var blocker string + if block.Height > 0 { + if blocker, err = a.chain.GetBlocker(&block.PreviousBlockHash, block.Timestamp); err != nil { + return NewErrorResponse(err) + } + } + resp := &GetBlockResp{ Hash: &blockHash, Size: uint64(len(rawBlock)), @@ -80,9 +86,8 @@ func (a *API) getBlock(ins BlockReq) Response { Height: block.Height, PreviousBlockHash: &block.PreviousBlockHash, Timestamp: block.Timestamp, - Nonce: block.Nonce, - Bits: block.Bits, - Difficulty: difficulty.CalcWork(block.Bits).String(), + Witness: witness, + Blocker: blocker, TransactionsMerkleRoot: &block.TransactionsMerkleRoot, TransactionStatusHash: &block.TransactionStatusHash, Transactions: []*BlockTx{}, @@ -103,10 +108,14 @@ func (a *API) getBlock(ins BlockReq) Response { } resOutID := orig.ResultIds[0] - resOut, ok := orig.Entries[*resOutID].(*bc.Output) - if ok { + switch resOut := orig.Entries[*resOutID].(type) { + case *bc.IntraChainOutput: + tx.MuxID = *resOut.Source.Ref + case *bc.CrossChainOutput: tx.MuxID = *resOut.Source.Ref - } else { + case *bc.VoteOutput: + tx.MuxID = *resOut.Source.Ref + case *bc.Retirement: resRetire, _ := orig.Entries[*resOutID].(*bc.Retirement) tx.MuxID = *resRetire.Source.Ref } @@ -161,7 +170,7 @@ func (a *API) getBlockHeader(ins BlockReq) Response { resp := &GetBlockHeaderResp{ BlockHeader: &block.BlockHeader, - Reward: block.Transactions[0].Outputs[0].Amount, + Reward: block.Transactions[0].Outputs[0].AssetAmount().Amount, } return NewSuccessResponse(resp) } @@ -181,72 +190,6 @@ func hexBytesToHash(hexBytes chainjson.HexBytes) bc.Hash { return bc.NewHash(b32) } -// GetDifficultyResp is resp struct for getDifficulty API -type GetDifficultyResp struct { - BlockHash *bc.Hash `json:"hash"` - BlockHeight uint64 `json:"height"` - Bits uint64 `json:"bits"` - Difficulty string `json:"difficulty"` -} - -func (a *API) getDifficulty(ins BlockReq) Response { - block, err := a.getBlockHelper(ins) - if err != nil { - return NewErrorResponse(err) - } - - blockHash := block.Hash() - resp := &GetDifficultyResp{ - BlockHash: &blockHash, - BlockHeight: block.Height, - Bits: block.Bits, - Difficulty: difficulty.CalcWork(block.Bits).String(), - } - return NewSuccessResponse(resp) -} - -// getHashRateResp is resp struct for getHashRate API -type getHashRateResp struct { - BlockHash *bc.Hash `json:"hash"` - BlockHeight uint64 `json:"height"` - HashRate uint64 `json:"hash_rate"` -} - -func (a *API) getHashRate(ins BlockReq) Response { - if len(ins.BlockHash) != 32 && len(ins.BlockHash) != 0 { - err := errors.New("Block hash format error.") - return NewErrorResponse(err) - } - if ins.BlockHeight == 0 { - ins.BlockHeight = a.chain.BestBlockHeight() - } - - block, err := a.getBlockHelper(ins) - if err != nil { - return NewErrorResponse(err) - } - - preBlock, err := a.chain.GetBlockByHash(&block.PreviousBlockHash) - if err != nil { - return NewErrorResponse(err) - } - - diffTime := block.Timestamp - preBlock.Timestamp - if preBlock.Timestamp >= block.Timestamp { - diffTime = 1 - } - hashCount := difficulty.CalcWork(block.Bits) - hashRate := new(big.Int).Div(hashCount, big.NewInt(int64(diffTime))) - - blockHash := block.Hash() - resp := &getHashRateResp{ - BlockHash: &blockHash, - BlockHeight: block.Height, - HashRate: hashRate.Uint64(), - } - return NewSuccessResponse(resp) -} - // MerkleBlockReq is used to handle getTxOutProof req type MerkleBlockReq struct { TxIDs []chainjson.HexBytes `json:"tx_ids"`