From: muscle_boy Date: Wed, 1 Aug 2018 11:11:24 +0000 (+0800) Subject: Dev wallet sa (#1204) X-Git-Tag: v1.0.5~2^2~40 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=748df812d7cf4b9626971f1d0f89ef3a69b07acb;p=bytom%2Fbytom.git Dev wallet sa (#1204) * the transaction output amout prohibit set zero * add network access control api * format import code style * refactor * code refactor * bug fix * the struct node_info add json field * estimate gas support multi-sign * add testcase of estimate gas * add testcase * bug fix * add test case * test case refactor * list-tx,list-address,list-utxo support partition * list-addresses list-tx list-utxo support pagging * refactor pagging --- diff --git a/api/accounts.go b/api/accounts.go index cd00d211..3d8c84a1 100644 --- a/api/accounts.go +++ b/api/accounts.go @@ -98,6 +98,8 @@ func (a SortByIndex) Less(i, j int) bool { return a[i].KeyIndex < a[j].KeyIndex func (a *API) listAddresses(ctx context.Context, ins struct { AccountID string `json:"account_id"` AccountAlias string `json:"account_alias"` + From uint `json:"from"` + Count uint `json:"count"` }) Response { accountID := ins.AccountID var target *account.Account @@ -136,7 +138,8 @@ func (a *API) listAddresses(ctx context.Context, ins struct { // sort AddressResp by KeyIndex sort.Sort(SortByIndex(addresses)) - return NewSuccessResponse(addresses) + start, end := getPageRange(len(addresses), ins.From, ins.Count) + return NewSuccessResponse(addresses[start:end]) } type minigAddressResp struct { diff --git a/api/page_util.go b/api/page_util.go new file mode 100644 index 00000000..4d2ec833 --- /dev/null +++ b/api/page_util.go @@ -0,0 +1,14 @@ +package api + +// Get the start and end of the page. +func getPageRange(size int, from uint, count uint) (uint, uint) { + total := uint(size) + if from == 0 && count == 0 { + return 0, total + } + start := from + end := from + count + if start > total {start = total} + if end > total {end = total} + return start, end +} \ No newline at end of file diff --git a/api/query.go b/api/query.go index 939adbd1..2b37a1e5 100644 --- a/api/query.go +++ b/api/query.go @@ -96,6 +96,8 @@ func (a *API) listTransactions(ctx context.Context, filter struct { AccountID string `json:"account_id"` Detail bool `json:"detail"` Unconfirmed bool `json:"unconfirmed"` + From uint `json:"from"` + Count uint `json:"count"` }) Response { transactions := []*query.AnnotatedTx{} var err error @@ -127,9 +129,11 @@ func (a *API) listTransactions(ctx context.Context, filter struct { if filter.Detail == false { txSummary := a.wallet.GetTransactionsSummary(transactions) - return NewSuccessResponse(txSummary) + start, end := getPageRange(len(txSummary), filter.From, filter.Count) + return NewSuccessResponse(txSummary[start:end]) } - return NewSuccessResponse(transactions) + start, end := getPageRange(len(transactions), filter.From, filter.Count) + return NewSuccessResponse(transactions[start:end]) } // POST /get-unconfirmed-transaction @@ -241,6 +245,8 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct { ID string `json:"id"` Unconfirmed bool `json:"unconfirmed"` SmartContract bool `json:"smart_contract"` + From uint `json:"from"` + Count uint `json:"count"` }) Response { accountUTXOs := a.wallet.GetAccountUtxos(filter.ID, filter.Unconfirmed, filter.SmartContract) @@ -262,8 +268,8 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct { Change: utxo.Change, }}, UTXOs...) } - - return NewSuccessResponse(UTXOs) + start, end := getPageRange(len(UTXOs), filter.From, filter.Count) + return NewSuccessResponse(UTXOs[start:end]) } // return gasRate