Skip to content

Commit

Permalink
Merge pull request #432 from liuji85/rpc_wallet_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
erickyan86 committed Aug 15, 2019
2 parents d23c0bd + fbfd10c commit 0f5f39e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ type IEngine interface {

GetVoterStake(state *state.StateDB, epoch uint64, voter string, candidate string) (stake *big.Int, err error)

CalcBFTIrreversible() uint64

IAPI

IValidator
Expand Down
29 changes: 19 additions & 10 deletions ftservice/apibackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ func (b *APIBackend) GetBlockDetailLog(ctx context.Context, blockNr rpc.BlockNum
}
}

func (b *APIBackend) GetTxsByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) []common.Hash {
var lastnum int64
if lookbackNum > blockNr {
lastnum = 0
} else {
lastnum = int64(blockNr - lookbackNum)
func (b *APIBackend) GetTxsByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookforwardNum uint64) *types.AccountTxs {
if lookforwardNum > 128 {
lookforwardNum = 128
}
txHashs := make([]common.Hash, 0)
for ublocknum := int64(blockNr); ublocknum >= lastnum; ublocknum-- {

lastnum := int64(blockNr + lookforwardNum)
txhhpairs := make([]*types.TxHeightHashPair, 0)
for ublocknum := int64(blockNr); ublocknum <= lastnum; ublocknum++ {
hash := rawdb.ReadCanonicalHash(b.ftservice.chainDb, uint64(ublocknum))
if hash == (common.Hash{}) {
continue
Expand All @@ -139,14 +138,24 @@ func (b *APIBackend) GetTxsByFilter(ctx context.Context, filterFn func(common.Na
for _, tx := range batchTxs {
for _, act := range tx.GetActions() {
if filterFn(act.Sender()) || filterFn(act.Recipient()) {
txHashs = append(txHashs, tx.Hash())
hhpair := &types.TxHeightHashPair{
Hash: tx.Hash(),
Height: uint64(ublocknum),
}
txhhpairs = append(txhhpairs, hhpair)
break
}
}
}
}

return txHashs
accountTxs := &types.AccountTxs{
Txs: txhhpairs,
ReversibleBlockHeight: b.ftservice.engine.CalcBFTIrreversible(),
EndHeight: uint64(lastnum),
}

return accountTxs
}

func (b *APIBackend) GetDetailTxByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) []*types.DetailTx {
Expand Down
2 changes: 1 addition & 1 deletion rpcapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Backend interface {
GetTd(blockHash common.Hash) *big.Int
GetEVM(ctx context.Context, account *accountmanager.AccountManager, state *state.StateDB, from common.Name, to common.Name, assetID uint64, gasPrice *big.Int, header *types.Header, vmCfg vm.Config) (*vm.EVM, func() error, error)
GetDetailTxByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) []*types.DetailTx
GetTxsByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) []common.Hash
GetTxsByFilter(ctx context.Context, filterFn func(common.Name) bool, blockNr, lookbackNum uint64) *types.AccountTxs
GetBadBlocks(ctx context.Context) ([]*types.Block, error)
SetStatePruning(enable bool) (bool, uint64)

Expand Down
46 changes: 34 additions & 12 deletions rpcapi/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ func (s *PublicBlockChainAPI) GetTransactionByHash(ctx context.Context, hash com
return nil
}

func (s *PublicBlockChainAPI) GetTransBatch(ctx context.Context, hashes []common.Hash) []*types.RPCTransaction {
txs := make([]*types.RPCTransaction, 0)

for i, hash := range hashes {
if i > 2048 {
break
}

if tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash); tx != nil {
txs = append(txs, tx.NewRPCTransaction(blockHash, blockNumber, index))
}
}

return txs
}

// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (s *PublicBlockChainAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (*types.RPCReceipt, error) {
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
Expand Down Expand Up @@ -130,44 +146,42 @@ func (s *PublicBlockChainAPI) checkRangeInputArgs(blockNr, lookbackNum uint64) e
if blockNr > current_num {
return fmt.Errorf("blockNr range err")
}
if lookbackNum > 128 {
return fmt.Errorf("lookbackNum cant bigger than 128")
}
return nil
}

// GetTxsByAccount return all txs, sent from or received by a specific account
// the range is indicate by blockNr and lookbackNum,
// from blocks with number from blockNr-lookbackNum to blockNr
func (s *PublicBlockChainAPI) GetTxsByAccount(ctx context.Context, acctName common.Name, blockNr rpc.BlockNumber, lookbackNum uint64) ([]common.Hash, error) {
// the range is indicate by blockNr and lookforwardNum,
// from blocks with number from blockNr to blockNr+lookforwardNum
func (s *PublicBlockChainAPI) GetTxsByAccount(ctx context.Context, acctName common.Name, blockNr rpc.BlockNumber, lookforwardNum uint64) (*types.AccountTxs, error) {
// check input argments
ui64BlockNr := uint64(blockNr)
if err := s.checkRangeInputArgs(ui64BlockNr, lookbackNum); err != nil {
if err := s.checkRangeInputArgs(ui64BlockNr, lookforwardNum); err != nil {
return nil, err
}

filterFn := func(name common.Name) bool {
return name == acctName
}
return s.b.GetTxsByFilter(ctx, filterFn, ui64BlockNr, lookbackNum), nil

return s.b.GetTxsByFilter(ctx, filterFn, ui64BlockNr, lookforwardNum), nil
}

// GetTxsByBloom return all txs, filtered by a bloomByte
// bloomByte is constructed by some quantities of account names
// the range is indicate by blockNr and lookbackNum,
// from blocks with number from blockNr-lookbackNum to blockNr
func (s *PublicBlockChainAPI) GetTxsByBloom(ctx context.Context, bloomByte hexutil.Bytes, blockNr rpc.BlockNumber, lookbackNum uint64) ([]common.Hash, error) {
// from blocks with number from blockNr to blockNr+lookforwardNum
func (s *PublicBlockChainAPI) GetTxsByBloom(ctx context.Context, bloomByte hexutil.Bytes, blockNr rpc.BlockNumber, lookforwardNum uint64) (*types.AccountTxs, error) {
// check input argments
ui64BlockNr := uint64(blockNr)
if err := s.checkRangeInputArgs(ui64BlockNr, lookbackNum); err != nil {
if err := s.checkRangeInputArgs(ui64BlockNr, lookforwardNum); err != nil {
return nil, err
}

bloom := types.BytesToBloom(bloomByte)
filterFn := func(name common.Name) bool {
return bloom.TestBytes([]byte(name))
}
return s.b.GetTxsByFilter(ctx, filterFn, ui64BlockNr, lookbackNum), nil
return s.b.GetTxsByFilter(ctx, filterFn, ui64BlockNr, lookforwardNum), nil
}

// GetInternalTxByAccount return all logs of interal txs, sent from or received by a specific account
Expand All @@ -180,6 +194,10 @@ func (s *PublicBlockChainAPI) GetInternalTxByAccount(ctx context.Context, acctNa
return nil, err
}

if lookbackNum > 128 {
lookbackNum = 128
}

filterFn := func(name common.Name) bool {
return name == acctName
}
Expand All @@ -197,6 +215,10 @@ func (s *PublicBlockChainAPI) GetInternalTxByBloom(ctx context.Context, bloomByt
return nil, err
}

if lookbackNum > 128 {
lookbackNum = 128
}

bloom := types.BytesToBloom(bloomByte)
filterFn := func(name common.Name) bool {
return bloom.TestBytes([]byte(name))
Expand Down
11 changes: 11 additions & 0 deletions types/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ type BlockAndResult struct {
Receipts []*Receipt `json:"receipts"`
DetailTxs []*DetailTx `json:"detailTxs"`
}

type TxHeightHashPair struct {
Hash common.Hash `json:"hash"`
Height uint64 `json:"height"`
}

type AccountTxs struct {
Txs []*TxHeightHashPair `json:"txs"`
ReversibleBlockHeight uint64 `json:"reversibleBlockHeight"`
EndHeight uint64 `json:"endheight"`
}

0 comments on commit 0f5f39e

Please sign in to comment.