Skip to content

Commit 67a2d1f

Browse files
committed
Block / REST explorer
1 parent b4eb150 commit 67a2d1f

30 files changed

+1680
-3
lines changed

.gitmodules

+8
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,11 @@
224224
url = https://github.com/status-im/nim-zlib.git
225225
ignore = untracked
226226
branch = master
227+
[submodule "vendor/DOtherSide"]
228+
path = vendor/DOtherSide
229+
url = https://github.com/filcuc/DOtherSide.git
230+
branch = master
231+
[submodule "vendor/nimqml"]
232+
path = vendor/nimqml
233+
url = https://github.com/filcuc/nimqml.git
234+
branch = master

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ $(TOOLS): | build deps
257257
MAKE="$(MAKE)" V="$(V)" $(ENV_SCRIPT) scripts/compile_nim_program.sh $@ "$${TOOL_DIR}/$@.nim" $(NIM_PARAMS) && \
258258
echo -e $(BUILD_END_MSG) "build/$@"
259259

260+
ngui/ngui: | build deps
261+
+ echo -e $(BUILD_MSG) "build/$@" && \
262+
MAKE="$(MAKE)" V="$(V)" $(ENV_SCRIPT) scripts/compile_nim_program.sh $@ "ngui.ngui.nim" $(NIM_PARAMS) && \
263+
echo -e $(BUILD_END_MSG) "ngui/ngui"
264+
260265
clean_eth2_network_simulation_data:
261266
rm -rf tests/simulation/data
262267

beacon_chain/spec/forks.nim

+7-3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ template getForkedBlockField*(x: ForkedSignedBeaconBlock | ForkedTrustedSignedBe
345345
of BeaconBlockFork.Altair: unsafeAddr x.altairData.message.y
346346
of BeaconBlockFork.Bellatrix: unsafeAddr x.bellatrixData.message.y)[]
347347

348+
template getForkedBodyField*(x: ForkedSignedBeaconBlock | ForkedTrustedSignedBeaconBlock, y: untyped): untyped =
349+
# unsafeAddr avoids a copy of the field in some cases
350+
(case x.kind
351+
of BeaconBlockFork.Phase0: unsafeAddr x.phase0Data.message.body.y
352+
of BeaconBlockFork.Altair: unsafeAddr x.altairData.message.body.y
353+
of BeaconBlockFork.Bellatrix: unsafeAddr x.bellatrixData.message.body.y)[]
354+
348355
template signature*(x: ForkedSignedBeaconBlock): ValidatorSig =
349356
withBlck(x): blck.signature
350357

@@ -354,9 +361,6 @@ template signature*(x: ForkedTrustedSignedBeaconBlock): TrustedSig =
354361
template root*(x: ForkedSignedBeaconBlock | ForkedTrustedSignedBeaconBlock): Eth2Digest =
355362
withBlck(x): blck.root
356363

357-
template slot*(x: ForkedSignedBeaconBlock | ForkedTrustedSignedBeaconBlock): Slot =
358-
withBlck(x): blck.message.slot
359-
360364
template shortLog*(x: ForkedBeaconBlock): auto =
361365
withBlck(x): shortLog(blck)
362366

ngui/attestationlist.nim

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import
2+
std/[sequtils, tables],
3+
NimQml,
4+
../beacon_chain/spec/eth2_apis/rest_beacon_client,
5+
../beacon_chain/spec/[eth2_merkleization, helpers],
6+
./objecttablemodel, ./utils
7+
8+
type
9+
AttestationInfo* = object
10+
slot*: int
11+
index*: int
12+
beacon_block_root*: string
13+
source_epoch*: int
14+
source_root*: string
15+
target_epoch*: int
16+
target_root*: string
17+
aggregation_bits*: string
18+
19+
proc toAttestationInfo*(v: Attestation): AttestationInfo =
20+
AttestationInfo(
21+
slot: v.data.slot.int,
22+
index: v.data.index.int,
23+
beacon_block_root: toBlockLink(v.data.beacon_block_root),
24+
source_epoch: v.data.source.epoch.int,
25+
source_root: toBlockLink(v.data.source.root),
26+
target_epoch: v.data.target.epoch.int,
27+
target_root: toBlockLink(v.data.target.root),
28+
aggregation_bits: $v.aggregation_bits,
29+
)
30+
31+
QtObject:
32+
type AttestationList* = ref object of QAbstractTableModel
33+
# TODO this could be a generic ObjectTableModel, except generics + method don't work..
34+
data: ObjectTableModelImpl[AttestationInfo]
35+
36+
proc setup(self: AttestationList) = self.QAbstractTableModel.setup
37+
38+
proc delete(self: AttestationList) =
39+
self.QAbstractTableModel.delete
40+
41+
proc newAttestationList*(data: seq[Attestation]): AttestationList =
42+
new(result, delete)
43+
result.data = ObjectTableModelImpl[AttestationInfo](items: data.mapIt(it.toAttestationInfo()))
44+
result.setup
45+
46+
method rowCount(self: AttestationList, index: QModelIndex = nil): int =
47+
self.data.rowCount(index)
48+
49+
method columnCount(self: AttestationList, index: QModelIndex = nil): int =
50+
self.data.columnCount(index)
51+
52+
method headerData*(self: AttestationList, section: int, orientation: QtOrientation, role: int): QVariant =
53+
self.data.headerData(section, orientation, role)
54+
55+
method data(self: AttestationList, index: QModelIndex, role: int): QVariant =
56+
self.data.data(index, role)
57+
58+
method roleNames(self: AttestationList): Table[int, string] =
59+
self.data.roleNames()
60+
61+
proc setNewData*(self: AttestationList, v: seq[Attestation]) =
62+
self.data.setNewData(self, v.mapIt(it.toAttestationInfo()))
63+
64+
proc sort*(self: AttestationList, section: int) {.slot.} =
65+
self.data.sort(self, section)

ngui/attesterslashinglist.nim

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import
2+
std/[sequtils, tables],
3+
NimQml,
4+
../beacon_chain/spec/eth2_apis/rest_beacon_client,
5+
../beacon_chain/spec/[helpers],
6+
./objecttablemodel, ./utils
7+
8+
type
9+
AttesterSlashingInfo* = object
10+
info*: string
11+
12+
proc toAttesterSlashingInfo*(v: AttesterSlashing): AttesterSlashingInfo =
13+
AttesterSlashingInfo(
14+
info: $v
15+
)
16+
17+
QtObject:
18+
type AttesterSlashingList* = ref object of QAbstractTableModel
19+
# TODO this could be a generic ObjectTableModel, except generics + method don't work..
20+
data: ObjectTableModelImpl[AttesterSlashingInfo]
21+
22+
proc setup(self: AttesterSlashingList) = self.QAbstractTableModel.setup
23+
24+
proc delete(self: AttesterSlashingList) =
25+
self.QAbstractTableModel.delete
26+
27+
proc newAttesterSlashingList*(data: openArray[AttesterSlashing]): AttesterSlashingList =
28+
new(result, delete)
29+
result.data = ObjectTableModelImpl[AttesterSlashingInfo](items: data.mapIt(it.toAttesterSlashingInfo()))
30+
result.setup
31+
32+
method rowCount(self: AttesterSlashingList, index: QModelIndex = nil): int =
33+
self.data.rowCount(index)
34+
35+
method columnCount(self: AttesterSlashingList, index: QModelIndex = nil): int =
36+
self.data.columnCount(index)
37+
38+
method headerData*(self: AttesterSlashingList, section: int, orientation: QtOrientation, role: int): QVariant =
39+
self.data.headerData(section, orientation, role)
40+
41+
method data(self: AttesterSlashingList, index: QModelIndex, role: int): QVariant =
42+
self.data.data(index, role)
43+
44+
method roleNames(self: AttesterSlashingList): Table[int, string] =
45+
self.data.roleNames()
46+
47+
proc setNewData*(self: AttesterSlashingList, v: openArray[AttesterSlashing]) =
48+
self.data.setNewData(self, v.mapIt(it.toAttesterSlashingInfo()))
49+
50+
proc sort*(self: AttesterSlashingList, section: int) {.slot.} =
51+
self.data.sort(self, section)

ngui/blockmodel.nim

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import NimQml
2+
3+
import
4+
std/[sequtils, times],
5+
NimQml,
6+
../beacon_chain/spec/eth2_apis/rest_beacon_client,
7+
../beacon_chain/spec/datatypes/[phase0, altair],
8+
"."/[
9+
attestationlist, depositlist, attesterslashinglist, proposerslashinglist,
10+
voluntaryexitlist, utils]
11+
12+
QtObject:
13+
type
14+
BlockModel* = ref object of QObject
15+
blck: ForkedSignedBeaconBlock
16+
attestationsx: AttestationList
17+
depositsx: DepositList
18+
attester_slashingsx: AttesterSlashingList
19+
proposer_slashingsx: ProposerSlashingList
20+
voluntary_exitsx: VoluntaryExitList
21+
genesis_time*: uint64
22+
23+
proc delete*(self: BlockModel) =
24+
self.QObject.delete
25+
26+
proc setup*(self: BlockModel) =
27+
self.QObject.setup
28+
29+
proc newBlockModel*(forked: ForkedSignedBeaconBlock, genesis_time: uint64): BlockModel =
30+
31+
let res = withBlck(forked): BlockModel(
32+
blck: forked,
33+
attestationsx: newAttestationList(blck.message.body.attestations.asSeq()),
34+
depositsx: newDepositList(blck.message.body.deposits.mapIt(it.toDepositInfo())),
35+
attester_slashingsx: newAttesterSlashingList(blck.message.body.attester_slashings.asSeq()),
36+
proposer_slashingsx: newProposerSlashingList(blck.message.body.proposer_slashings.asSeq()),
37+
voluntary_exitsx: newVoluntaryExitList(blck.message.body.voluntary_exits.asSeq()),
38+
genesis_time: genesis_time,
39+
)
40+
res.setup()
41+
res
42+
43+
proc `blck=`*(self: BlockModel, forked: ForkedSignedBeaconBlock) =
44+
self.blck = forked
45+
withBlck(forked):
46+
self.attestationsx.setNewData(blck.message.body.attestations.asSeq())
47+
self.depositsx.setNewData(blck.message.body.deposits.mapIt(it.toDepositInfo()))
48+
self.attester_slashingsx.setNewData(blck.message.body.attester_slashings.asSeq())
49+
self.proposer_slashingsx.setNewData(blck.message.body.proposer_slashings.asSeq())
50+
self.voluntary_exitsx.setNewData(blck.message.body.voluntary_exits.asSeq())
51+
52+
proc slot*(self: BlockModel): int {.slot.} = getForkedBlockField(self.blck, slot).int
53+
QtProperty[int] slot: read = slot
54+
55+
proc time*(self: BlockModel): string {.slot.} =
56+
let t = self.genesis_time + getForkedBlockField(self.blck, slot) * SECONDS_PER_SLOT
57+
$fromUnix(t.int64).utc
58+
QtProperty[string] time: read = time
59+
60+
proc root*(self: BlockModel): string {.slot.} = toDisplayHex(self.blck.root.data)
61+
QtProperty[string] root: read = root
62+
63+
proc proposer_index*(self: BlockModel): int {.slot.} = getForkedBlockField(self.blck, proposer_index).int
64+
QtProperty[int] proposer_index: read = proposer_index
65+
66+
proc parent_root*(self: BlockModel): string {.slot.} = toBlockLink(getForkedBlockField(self.blck, parent_root))
67+
QtProperty[string] parent_root: read = parent_root
68+
69+
proc state_root*(self: BlockModel): string {.slot.} = toDisplayHex(getForkedBlockField(self.blck, state_root).data)
70+
QtProperty[string] state_root: read = state_root
71+
72+
proc randao_reveal*(self: BlockModel): string {.slot.} = toDisplayHex(getForkedBodyField(self.blck, randao_reveal))
73+
QtProperty[string] randao_reveal: read = randao_reveal
74+
75+
proc eth1_data*(self: BlockModel): string {.slot.} = RestJson.encode(getForkedBodyField(self.blck, eth1_data), pretty=true)
76+
QtProperty[string] eth1_data: read = eth1_data
77+
78+
proc graffiti*(self: BlockModel): string {.slot.} = $getForkedBodyField(self.blck, graffiti)
79+
QtProperty[string] graffiti: read = graffiti
80+
81+
proc proposer_slashings*(self: BlockModel): QVariant {.slot.} = newQVariant(self.proposer_slashingsx)
82+
QtProperty[QVariant] proposer_slashings: read = proposer_slashings
83+
84+
proc attester_slashings*(self: BlockModel): QVariant {.slot.} = newQVariant(self.attester_slashingsx)
85+
QtProperty[QVariant] attester_slashings: read = attester_slashings
86+
87+
proc attestations*(self: BlockModel): QVariant {.slot.} = newQVariant(self.attestationsx)
88+
QtProperty[QVariant] attestations: read = attestations
89+
90+
proc deposits*(self: BlockModel): QVariant {.slot.} = newQVariant(self.depositsx)
91+
QtProperty[QVariant] deposits: read = deposits
92+
93+
proc voluntary_exits*(self: BlockModel): QVariant {.slot.} = newQVariant(self.voluntary_exitsx)
94+
QtProperty[QVariant] voluntary_exits: read = voluntary_exits
95+
96+
proc signature*(self: BlockModel): string {.slot.} = toDisplayHex(self.blck.signature)
97+
QtProperty[string] signature: read = signature

ngui/depositlist.nim

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import
2+
std/[tables],
3+
NimQml,
4+
../beacon_chain/spec/datatypes/base,
5+
./objecttablemodel, ./utils
6+
7+
type
8+
DepositInfo* = object
9+
pubkey*: string
10+
withdrawal_credentials*: string
11+
amount*: uint64
12+
signature*: string
13+
14+
proc toDepositInfo*(v: Deposit): DepositInfo =
15+
DepositInfo(
16+
pubkey: toDisplayHex(v.data.pubkey.toRaw()),
17+
withdrawal_credentials: toDisplayHex(v.data.withdrawal_credentials),
18+
amount: v.data.amount,
19+
signature: toDisplayHex(v.data.signature),
20+
)
21+
22+
QtObject:
23+
type DepositList* = ref object of QAbstractTableModel
24+
# TODO this could be a generic ObjectTableModel, except generics + method don't work..
25+
data: ObjectTableModelImpl[DepositInfo]
26+
27+
proc setup(self: DepositList) = self.QAbstractTableModel.setup
28+
29+
proc delete(self: DepositList) =
30+
self.QAbstractTableModel.delete
31+
32+
proc newDepositList*(data: seq[DepositInfo]): DepositList =
33+
new(result, delete)
34+
result.data = ObjectTableModelImpl[DepositInfo](items: data)
35+
result.setup
36+
37+
method rowCount(self: DepositList, index: QModelIndex = nil): int =
38+
self.data.rowCount(index)
39+
40+
method columnCount(self: DepositList, index: QModelIndex = nil): int =
41+
self.data.columnCount(index)
42+
43+
method headerData*(self: DepositList, section: int, orientation: QtOrientation, role: int): QVariant =
44+
self.data.headerData(section, orientation, role)
45+
46+
method data(self: DepositList, index: QModelIndex, role: int): QVariant =
47+
self.data.data(index, role)
48+
49+
method roleNames(self: DepositList): Table[int, string] =
50+
self.data.roleNames()
51+
52+
proc setNewData*(self: DepositList, v: seq[DepositInfo]) =
53+
self.data.setNewData(self, v)
54+
55+
proc sort*(self: DepositList, section: int) {.slot.} =
56+
self.data.sort(self, section)

ngui/epochmodel.nim

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import NimQml
2+
3+
import
4+
../beacon_chain/spec/eth2_apis/rest_beacon_client,
5+
./slotlist
6+
7+
QtObject:
8+
type
9+
EpochModel* = ref object of QObject
10+
client: RestClientRef
11+
epoch: int
12+
slotList: SlotList
13+
14+
proc delete*(self: EpochModel) =
15+
self.QObject.delete
16+
17+
proc setup*(self: EpochModel) =
18+
self.QObject.setup
19+
20+
proc newEpochModel*(client: RestClientRef, epoch: int): EpochModel =
21+
let data = client.loadSlots(epoch.Epoch)
22+
let res = EpochModel(client: client, epoch: epoch, slotList: newSlotList(data))
23+
res.setup()
24+
res
25+
26+
proc epoch*(self: EpochModel): int {.slot.} = self.epoch
27+
proc epochChanged*(self: EpochModel, v: int) {.signal.}
28+
QtProperty[int] epoch:
29+
read = epoch
30+
notify = epochChanged
31+
32+
proc getSlotList*(self: EpochModel): QVariant {.slot.} = newQVariant(self.slotList)
33+
QtProperty[QVariant] slotList: read = getSlotList
34+
35+
proc setNewData*(self: EpochModel, epoch: int, data: seq[SlotInfo]) =
36+
self.epoch = epoch
37+
self.epochChanged(epoch)
38+
39+
self.slotList.setNewData(data)
40+
41+
proc reload(self: EpochModel) {.slot.} =
42+
self.slotList.setNewData(self.client.loadSlots(self.epoch.Epoch))
43+
44+
proc next(self: EpochModel) {.slot.} =
45+
self.epoch = self.epoch + 1
46+
self.epochChanged(self.epoch)
47+
self.reload() # TODO listen to epochchanged
48+
49+
proc prev(self: EpochModel) {.slot.} =
50+
self.epoch = self.epoch - 1
51+
self.epochChanged(self.epoch)
52+
self.reload() # TODO listen to epochchanged

0 commit comments

Comments
 (0)