Skip to content

Commit

Permalink
Allow voting with notes that are spent *after* the snapshot height
Browse files Browse the repository at this point in the history
  • Loading branch information
hhanh00 committed Jul 11, 2024
1 parent 790c045 commit d07d07d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 45 deletions.
29 changes: 17 additions & 12 deletions lib/pages/more/vote.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'dart:convert';

import 'package:YWallet/pages/utils.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:http/http.dart' as http;
import 'package:go_router/go_router.dart';
import 'package:settings_ui/settings_ui.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:warp_api/data_fb_generated.dart';
import 'package:warp_api/warp_api.dart';

import '../../pages/utils.dart';
import '../../accounts.dart';
import '../../generated/intl/messages.dart';
import '../../store2.dart';
Expand Down Expand Up @@ -109,16 +111,16 @@ class VoteState extends State<VotePage> with WithLoadingAnimation {
final electionJson = jsonDecode(electionString);
final election = Election.fromJson(electionJson);
setState(() {
vote = Vote(election: election, ids: []);
vote = Vote(election: election, notes: []);
});
}

_next() async {
final e = vote?.election;
if (e != null) {
WarpApi.populateVoteNotes(aa.coin, aa.id, e.start_height, e.end_height);
final ids = WarpApi.listVoteNotes(aa.coin, aa.id, e.end_height);
final vote = Vote(election: e, ids: ids);
final notes = WarpApi.listVoteNotes(aa.coin, aa.id, e.end_height);
final vote = Vote(election: e, notes: notes);
GoRouter.of(context).push('/more/vote/notes', extra: vote);
} else {
final electionURL = urlController.text;
Expand Down Expand Up @@ -152,11 +154,7 @@ class VoteNotesState extends State<VoteNotesPage> {
void initState() {
super.initState();
Future(() async {
final ids = widget.vote.ids;
final allNotes = await WarpApi.getNotes(aa.coin, aa.id);
for (var id in ids) {
final sn = allNotes.where((n) => n.id == id).firstOrNull;
if (sn != null)
for (var sn in widget.vote.notes) {
notes.add(Note.fromShieldedNote(sn));
};
setState(() {});
Expand All @@ -180,8 +178,15 @@ class VoteNotesState extends State<VoteNotesPage> {
}

_next() {
final ids = notes.where((n) => n.selected).map((n) => n.id).toList();
final vote = Vote(election: widget.vote.election, ids: ids);
List<ShieldedNoteT> selectedNotes = [];
for (final pairs in
IterableZip([notes, widget.vote.notes])) {
final n1 = pairs[0] as Note;
final n2 = pairs[1] as ShieldedNoteT;
if (n1.selected)
selectedNotes.add(n2);
}
final vote = Vote(election: widget.vote.election, notes: selectedNotes);
GoRouter.of(context).push('/more/vote/candidate', extra: vote);
}
}
Expand Down Expand Up @@ -267,7 +272,7 @@ class VoteCandidateState extends State<VoteCandidatePage>

_ok() async {
final election = widget.vote.election;
final ids = widget.vote.ids;
final ids = widget.vote.notes.map((n) => n.id).toList();
await load(() async {
final vote = await WarpApi.vote(
aa.coin, aa.id, ids, candidate, jsonEncode(election));
Expand Down
6 changes: 3 additions & 3 deletions lib/pages/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ class Note extends HasHeight {
final confirmations = latestHeight?.let((h) => h - height + 1);
return Note(id, height, confirmations, timestamp, value, orchard, excluded, selected);
}
factory Note.fromShieldedNote(ShieldedNote n) =>
Note(n.id, n.height, 0, toDateTime(n.timestamp), n.value / ZECUNIT,
factory Note.fromShieldedNote(ShieldedNoteT n) =>
Note(n.id, n.height, 0, toDateTime(n.timestamp), n.value / ZECUNIT,
n.orchard, n.excluded, false);

Note(this.id, this.height, this.confirmations, this.timestamp, this.value,
Expand All @@ -446,7 +446,7 @@ class Note extends HasHeight {
Note(id, height, confirmations, timestamp, value, orchard, !excluded, selected);

Note clone() => Note(id, height, confirmations, timestamp, value, orchard, excluded, selected);

}

@reflector
Expand Down
2 changes: 1 addition & 1 deletion lib/store2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ class Election with _$Election {
class Vote with _$Vote {
const factory Vote({
required Election election,
required List<int> ids,
required List<ShieldedNoteT> notes,
int? candidate,
}) = _Vote;
}
50 changes: 26 additions & 24 deletions lib/store2.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ abstract class _Election implements Election {
/// @nodoc
mixin _$Vote {
Election get election => throw _privateConstructorUsedError;
List<int> get ids => throw _privateConstructorUsedError;
List<ShieldedNoteT> get notes => throw _privateConstructorUsedError;
int? get candidate => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
Expand All @@ -1680,7 +1680,7 @@ abstract class $VoteCopyWith<$Res> {
factory $VoteCopyWith(Vote value, $Res Function(Vote) then) =
_$VoteCopyWithImpl<$Res, Vote>;
@useResult
$Res call({Election election, List<int> ids, int? candidate});
$Res call({Election election, List<ShieldedNoteT> notes, int? candidate});

$ElectionCopyWith<$Res> get election;
}
Expand All @@ -1699,18 +1699,18 @@ class _$VoteCopyWithImpl<$Res, $Val extends Vote>
@override
$Res call({
Object? election = null,
Object? ids = null,
Object? notes = null,
Object? candidate = freezed,
}) {
return _then(_value.copyWith(
election: null == election
? _value.election
: election // ignore: cast_nullable_to_non_nullable
as Election,
ids: null == ids
? _value.ids
: ids // ignore: cast_nullable_to_non_nullable
as List<int>,
notes: null == notes
? _value.notes
: notes // ignore: cast_nullable_to_non_nullable
as List<ShieldedNoteT>,
candidate: freezed == candidate
? _value.candidate
: candidate // ignore: cast_nullable_to_non_nullable
Expand All @@ -1734,7 +1734,7 @@ abstract class _$$VoteImplCopyWith<$Res> implements $VoteCopyWith<$Res> {
__$$VoteImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({Election election, List<int> ids, int? candidate});
$Res call({Election election, List<ShieldedNoteT> notes, int? candidate});

@override
$ElectionCopyWith<$Res> get election;
Expand All @@ -1751,18 +1751,18 @@ class __$$VoteImplCopyWithImpl<$Res>
@override
$Res call({
Object? election = null,
Object? ids = null,
Object? notes = null,
Object? candidate = freezed,
}) {
return _then(_$VoteImpl(
election: null == election
? _value.election
: election // ignore: cast_nullable_to_non_nullable
as Election,
ids: null == ids
? _value._ids
: ids // ignore: cast_nullable_to_non_nullable
as List<int>,
notes: null == notes
? _value._notes
: notes // ignore: cast_nullable_to_non_nullable
as List<ShieldedNoteT>,
candidate: freezed == candidate
? _value.candidate
: candidate // ignore: cast_nullable_to_non_nullable
Expand All @@ -1775,25 +1775,27 @@ class __$$VoteImplCopyWithImpl<$Res>
class _$VoteImpl implements _Vote {
const _$VoteImpl(
{required this.election, required final List<int> ids, this.candidate})
: _ids = ids;
{required this.election,
required final List<ShieldedNoteT> notes,
this.candidate})
: _notes = notes;

@override
final Election election;
final List<int> _ids;
final List<ShieldedNoteT> _notes;
@override
List<int> get ids {
if (_ids is EqualUnmodifiableListView) return _ids;
List<ShieldedNoteT> get notes {
if (_notes is EqualUnmodifiableListView) return _notes;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_ids);
return EqualUnmodifiableListView(_notes);
}

@override
final int? candidate;

@override
String toString() {
return 'Vote(election: $election, ids: $ids, candidate: $candidate)';
return 'Vote(election: $election, notes: $notes, candidate: $candidate)';
}

@override
Expand All @@ -1803,14 +1805,14 @@ class _$VoteImpl implements _Vote {
other is _$VoteImpl &&
(identical(other.election, election) ||
other.election == election) &&
const DeepCollectionEquality().equals(other._ids, _ids) &&
const DeepCollectionEquality().equals(other._notes, _notes) &&
(identical(other.candidate, candidate) ||
other.candidate == candidate));
}

@override
int get hashCode => Object.hash(runtimeType, election,
const DeepCollectionEquality().hash(_ids), candidate);
const DeepCollectionEquality().hash(_notes), candidate);

@JsonKey(ignore: true)
@override
Expand All @@ -1822,13 +1824,13 @@ class _$VoteImpl implements _Vote {
abstract class _Vote implements Vote {
const factory _Vote(
{required final Election election,
required final List<int> ids,
required final List<ShieldedNoteT> notes,
final int? candidate}) = _$VoteImpl;

@override
Election get election;
@override
List<int> get ids;
List<ShieldedNoteT> get notes;
@override
int? get candidate;
@override
Expand Down
2 changes: 1 addition & 1 deletion native/zcash-sync
2 changes: 1 addition & 1 deletion native/zcash-vote
Submodule zcash-vote updated 1 files
+1 −1 src/proof.rs
4 changes: 2 additions & 2 deletions packages/warp_api_ffi/lib/warp_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@ class WarpApi {
warp_api_lib.populate_vote_notes(coin, account, startHeight, endHeight);
}

static List<int> listVoteNotes(int coin, int account, int endHeight) {
static List<ShieldedNoteT> listVoteNotes(int coin, int account, int endHeight) {
final res = unwrapResultBytes(warp_api_lib.list_vote_notes(coin, account, endHeight));
return IdList(res).unpack().ids!;
return ShieldedNoteVec(res).unpack().notes!;
}

static Future<List<int>> vote(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.7.0+549
version: 1.7.0+550

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down

0 comments on commit d07d07d

Please sign in to comment.