Add pagination for trace_filter rpc method (#6312)

* Optional offset and count added on client level

* Rpc interface extended

* Tests corrected

* Filtering code refactored a little bit

* Offset type was changed to usize

* Handling of block hash in offset added

* Offset reworked to number

* Trailing comma removed

* Import for Quantity type added

* Offset and count moved into filter object

* Lost comma removed

* Return empty vec instead of None in case of incorrect range
This commit is contained in:
Anton Gavrilov 2017-10-03 10:03:46 +02:00 committed by Gav Wood
parent 41c3b05a20
commit 0a5c9b0465
4 changed files with 43 additions and 7 deletions

View File

@ -1692,14 +1692,22 @@ impl BlockChainClient for Client {
match (start, end) { match (start, end) {
(Some(s), Some(e)) => { (Some(s), Some(e)) => {
let filter = trace::Filter { let db_filter = trace::Filter {
range: s as usize..e as usize, range: s as usize..e as usize,
from_address: From::from(filter.from_address), from_address: From::from(filter.from_address),
to_address: From::from(filter.to_address), to_address: From::from(filter.to_address),
}; };
let traces = self.tracedb.read().filter(&filter); let traces = self.tracedb.read().filter(&db_filter);
Some(traces) if traces.is_empty() {
return Some(vec![]);
}
let traces_iter = traces.into_iter().skip(filter.after.unwrap_or(0));
Some(match filter.count {
Some(count) => traces_iter.take(count).collect(),
None => traces_iter.collect(),
})
}, },
_ => None, _ => None,
} }

View File

@ -28,4 +28,8 @@ pub struct Filter {
pub from_address: Vec<Address>, pub from_address: Vec<Address>,
/// To address. /// To address.
pub to_address: Vec<Address>, pub to_address: Vec<Address>,
/// Output offset
pub after: Option<usize>,
/// Output amount
pub count: Option<usize>,
} }

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { Address, BlockNumber, Data, Hash, CallRequest } from '../types'; import { Address, BlockNumber, Data, Hash, CallRequest, Quantity } from '../types';
import { withPreamble, Dummy, fromDecimal } from '../helpers'; import { withPreamble, Dummy, fromDecimal } from '../helpers';
const SECTION_FILTERING = 'Transaction-Trace Filtering'; const SECTION_FILTERING = 'Transaction-Trace Filtering';
@ -134,12 +134,24 @@ then it should look something like:
type: Address, type: Address,
desc: 'Sent to these addresses.', desc: 'Sent to these addresses.',
optional: true optional: true
},
after: {
type: Quantity,
desc: 'The offset trace number',
optional: true
},
count: {
type: Quantity,
desc: 'Integer number of traces to display in a batch.',
optional: true
} }
}, },
example: { example: {
fromBlock: fromDecimal(3068100), fromBlock: fromDecimal(3068100),
toBlock: fromDecimal(3068200), toBlock: fromDecimal(3068200),
toAddress: ['0x8bbB73BCB5d553B5A556358d27625323Fd781D37'] toAddress: ['0x8bbB73BCB5d553B5A556358d27625323Fd781D37'],
after: 1000,
count: 100
} }
} }
], ],

View File

@ -36,6 +36,10 @@ pub struct TraceFilter {
/// To address /// To address
#[serde(rename="toAddress")] #[serde(rename="toAddress")]
pub to_address: Option<Vec<H160>>, pub to_address: Option<Vec<H160>>,
/// Output offset
pub after: Option<usize>,
/// Output amount
pub count: Option<usize>,
} }
impl Into<client::TraceFilter> for TraceFilter { impl Into<client::TraceFilter> for TraceFilter {
@ -46,6 +50,8 @@ impl Into<client::TraceFilter> for TraceFilter {
range: start..end, range: start..end,
from_address: self.from_address.map_or_else(Vec::new, |x| x.into_iter().map(Into::into).collect()), from_address: self.from_address.map_or_else(Vec::new, |x| x.into_iter().map(Into::into).collect()),
to_address: self.to_address.map_or_else(Vec::new, |x| x.into_iter().map(Into::into).collect()), to_address: self.to_address.map_or_else(Vec::new, |x| x.into_iter().map(Into::into).collect()),
after: self.after,
count: self.count,
} }
} }
} }
@ -64,7 +70,9 @@ mod tests {
from_block: None, from_block: None,
to_block: None, to_block: None,
from_address: None, from_address: None,
to_address: None to_address: None,
after: None,
count: None,
}); });
} }
@ -74,7 +82,9 @@ mod tests {
"fromBlock": "latest", "fromBlock": "latest",
"toBlock": "latest", "toBlock": "latest",
"fromAddress": ["0x0000000000000000000000000000000000000003"], "fromAddress": ["0x0000000000000000000000000000000000000003"],
"toAddress": ["0x0000000000000000000000000000000000000005"] "toAddress": ["0x0000000000000000000000000000000000000005"],
"after": 50,
"count": 100
}"#; }"#;
let deserialized: TraceFilter = serde_json::from_str(s).unwrap(); let deserialized: TraceFilter = serde_json::from_str(s).unwrap();
assert_eq!(deserialized, TraceFilter { assert_eq!(deserialized, TraceFilter {
@ -82,6 +92,8 @@ mod tests {
to_block: Some(BlockNumber::Latest), to_block: Some(BlockNumber::Latest),
from_address: Some(vec![Address::from(3).into()]), from_address: Some(vec![Address::from(3).into()]),
to_address: Some(vec![Address::from(5).into()]), to_address: Some(vec![Address::from(5).into()]),
after: 50.into(),
count: 100.into(),
}); });
} }
} }