Skip to content

Commit a1e2e57

Browse files
committed
solve c compilation error
1 parent 4c998e6 commit a1e2e57

File tree

4 files changed

+233
-135
lines changed

4 files changed

+233
-135
lines changed

nimbus_verified_proxy/rpc/accounts.nim

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,53 +106,69 @@ proc getAccount*(
106106
lcProxy: VerifiedRpcProxy,
107107
address: Address,
108108
blockNumber: base.BlockNumber,
109-
stateRoot: Root): Future[Account] {.async: (raises: [ValueError, CatchableError]).} =
109+
stateRoot: Root): Future[Result[Account, string]] {.async: (raises: []).} =
110+
111+
info "Forwarding eth_getAccount", blockNumber
112+
110113
let
111-
proof = await lcProxy.rpcClient.eth_getProof(address, @[], blockId(blockNumber))
114+
proof =
115+
try:
116+
await lcProxy.rpcClient.eth_getProof(address, @[], blockId(blockNumber))
117+
except CatchableError as e:
118+
return err(e.msg)
119+
112120
account = getAccountFromProof(
113121
stateRoot, proof.address, proof.balance, proof.nonce, proof.codeHash,
114122
proof.storageHash, proof.accountProof,
115-
).valueOr:
116-
raise newException(ValueError, error)
123+
)
117124

118125
return account
119126

120127
proc getCode*(
121128
lcProxy: VerifiedRpcProxy,
122129
address: Address,
123130
blockNumber: base.BlockNumber,
124-
stateRoot: Root): Future[seq[byte]] {.async: (raises: [ValueError, CatchableError]).} =
131+
stateRoot: Root): Future[Result[seq[byte], string]] {.async: (raises: []).} =
125132
# get verified account details for the address at blockNumber
126-
let account = await lcProxy.getAccount(address, blockNumber, stateRoot)
133+
let account = (await lcProxy.getAccount(address, blockNumber, stateRoot)).valueOr:
134+
return err(error)
127135

128136
# if the account does not have any code, return empty hex data
129137
if account.codeHash == EMPTY_CODE_HASH:
130-
return @[]
138+
return ok(newSeq[byte]())
131139

132140
info "Forwarding eth_getCode", blockNumber
133141

134-
let code = await lcProxy.rpcClient.eth_getCode(address, blockId(blockNumber))
142+
let code =
143+
try:
144+
await lcProxy.rpcClient.eth_getCode(address, blockId(blockNumber))
145+
except CatchableError as e:
146+
return err(e.msg)
135147

136148
# verify the byte code. since we verified the account against
137149
# the state root we just need to verify the code hash
138150
if account.codeHash == keccak256(code):
139-
return code
151+
return ok(code)
140152
else:
141-
raise newException(ValueError, "received code doesn't match the account code hash")
153+
return err("received code doesn't match the account code hash")
142154

143155
proc getStorageAt*(
144156
lcProxy: VerifiedRpcProxy,
145157
address: Address,
146158
slot: UInt256,
147159
blockNumber: base.BlockNumber,
148160
stateRoot: Root
149-
): Future[UInt256] {.async: (raises: [ValueError, CatchableError]).} =
161+
): Future[Result[UInt256, string]] {.async: (raises: []).} =
150162

151163
info "Forwarding eth_getStorageAt", blockNumber
152164

153165
let
154-
proof = await lcProxy.rpcClient.eth_getProof(address, @[slot], blockId(blockNumber))
155-
slotValue = getStorageFromProof(stateRoot, slot, proof).valueOr:
156-
raise newException(ValueError, error)
166+
proof =
167+
try:
168+
await lcProxy.rpcClient.eth_getProof(address, @[slot], blockId(blockNumber))
169+
except CatchableError as e:
170+
return err(e.msg)
171+
172+
slotValue = getStorageFromProof(stateRoot, slot, proof)
157173

158-
slotValue
174+
return slotValue

nimbus_verified_proxy/rpc/blocks.nim

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ template rpcClient(vp: VerifiedRpcProxy): RpcClient =
2828

2929
proc resolveTag(
3030
self: VerifiedRpcProxy, blockTag: BlockTag
31-
): base.BlockNumber {.raises: [ValueError].} =
31+
): Result[base.BlockNumber, string] =
3232
if blockTag.kind == bidAlias:
3333
let tag = blockTag.alias.toLowerAscii()
3434
case tag
3535
of "latest":
3636
let hLatest = self.headerStore.latest()
3737
if hLatest.isSome:
38-
return hLatest.get().number
38+
return ok(hLatest.get().number)
3939
else:
40-
raise newException(ValueError, "Couldn't get the latest block number from header store")
40+
return err("Couldn't get the latest block number from header store")
4141
else:
42-
raise newException(ValueError, "No support for block tag " & $blockTag)
42+
return err("No support for block tag " & $blockTag)
4343
else:
44-
return base.BlockNumber(distinctBase(blockTag.number))
44+
return ok(base.BlockNumber(distinctBase(blockTag.number)))
4545

46-
proc convHeader(blk: BlockObject): Header =
46+
proc convHeader(blk: eth_api_types.BlockObject): Header =
4747
let
4848
nonce = if blk.nonce.isSome: blk.nonce.get
4949
else: default(Bytes8)
@@ -77,16 +77,22 @@ proc walkBlocks(
7777
sourceNum: base.BlockNumber,
7878
targetNum: base.BlockNumber,
7979
sourceHash: Hash32,
80-
targetHash: Hash32): Future[bool] {.async: (raises: [ValueError, CatchableError]).} =
80+
targetHash: Hash32): Future[bool] {.async: (raises: []).} =
8181

8282
var nextHash = sourceHash
8383
info "starting block walk to verify", blockHash=targetHash
8484

8585
# TODO: use batch calls to get all blocks at once by number
8686
for i in 0 ..< sourceNum - targetNum:
8787
# TODO: use a verified hash cache
88-
let blk = await self.rpcClient.eth_getBlockByHash(nextHash, false)
89-
info "getting next block", hash=nextHash, number=blk.number, remaining=distinctBase(blk.number) - targetNum
88+
let blk =
89+
try:
90+
await self.rpcClient.eth_getBlockByHash(nextHash, false)
91+
except:
92+
# TODO: retry before failing?
93+
return false
94+
95+
trace "getting next block", hash=nextHash, number=blk.number, remaining=distinctBase(blk.number) - targetNum
9096

9197
if blk.parentHash == targetHash:
9298
return true
@@ -97,145 +103,167 @@ proc walkBlocks(
97103

98104
proc getBlockByHash*(
99105
self: VerifiedRpcProxy, blockHash: Hash32, fullTransactions: bool
100-
): Future[BlockObject] {.async: (raises: [ValueError, CatchableError]).} =
106+
): Future[Result[eth_api_types.BlockObject, string]] {.async: (raises: []).} =
101107
# get the target block
102-
let blk = await self.rpcClient.eth_getBlockByHash(blockHash, fullTransactions)
108+
let blk =
109+
try:
110+
await self.rpcClient.eth_getBlockByHash(blockHash, fullTransactions)
111+
except CatchableError as e:
112+
return err(e.msg)
113+
103114
let header = convHeader(blk)
104115

105116
# verify header hash
106117
if header.rlpHash != blockHash:
107-
raise newException(ValueError, "hashed block header doesn't match with blk.hash(downloaded)")
118+
return err("hashed block header doesn't match with blk.hash(downloaded)")
108119

109120
if blockHash != blk.hash:
110-
raise newException(ValueError, "the downloaded block hash doesn't match with the requested hash")
121+
return err("the downloaded block hash doesn't match with the requested hash")
111122

112123
let earliestHeader = self.headerStore.earliest.valueOr:
113-
raise newException(ValueError, "Syncing")
124+
return err("syncing")
114125

115126
# walk blocks backwards(time) from source to target
116127
let isLinked = await self.walkBlocks(earliestHeader.number, header.number, earliestHeader.parentHash, blockHash)
117128

118129
if not isLinked:
119-
raise newException(ValueError, "the requested block is not part of the canonical chain")
130+
return err("the requested block is not part of the canonical chain")
120131

121132
# verify transactions
122133
if fullTransactions:
123134
let verified = verifyTransactions(header.transactionsRoot, blk.transactions).valueOr:
124-
raise newException(ValueError, "error while verifying transactions root")
135+
return err("error while verifying transactions root")
125136
if not verified:
126-
raise newException(ValueError, "transactions within the block do not yield the same transaction root")
137+
return err("transactions within the block do not yield the same transaction root")
127138

128139
# verify withdrawals
129140
if blk.withdrawals.isSome():
130141
if blk.withdrawalsRoot.get() != orderedTrieRoot(blk.withdrawals.get()):
131-
raise newException(ValueError, "withdrawals within the block do not yield the same withdrawals root")
142+
return err("withdrawals within the block do not yield the same withdrawals root")
132143

133-
return blk
144+
return ok(blk)
134145

135146
proc getBlockByTag*(
136147
self: VerifiedRpcProxy, blockTag: BlockTag, fullTransactions: bool
137-
): Future[BlockObject] {.async: (raises: [ValueError, CatchableError]).} =
138-
let n = self.resolveTag(blockTag)
148+
): Future[Result[BlockObject, string]] {.async: (raises: []).} =
149+
let n = self.resolveTag(blockTag).valueOr:
150+
return err(error)
139151

140152
# get the target block
141-
let blk = await self.rpcClient.eth_getBlockByNumber(blockTag, false)
153+
let blk =
154+
try:
155+
await self.rpcClient.eth_getBlockByNumber(blockTag, false)
156+
except CatchableError as e:
157+
return err(e.msg)
158+
142159
let header = convHeader(blk)
143160

144161
# verify header hash
145162
if header.rlpHash != blk.hash:
146-
raise newException(ValueError, "hashed block header doesn't match with blk.hash(downloaded)")
163+
return err("hashed block header doesn't match with blk.hash(downloaded)")
147164

148165
if n != header.number:
149-
raise newException(ValueError, "the downloaded block number doesn't match with the requested block number")
166+
return err("the downloaded block number doesn't match with the requested block number")
150167

151168
# get the source block
152169
let earliestHeader = self.headerStore.earliest.valueOr:
153-
raise newException(ValueError, "Syncing")
170+
return err("Syncing")
154171

155172
# walk blocks backwards(time) from source to target
156173
let isLinked = await self.walkBlocks(earliestHeader.number, header.number, earliestHeader.parentHash, blk.hash)
157174

158175
if not isLinked:
159-
raise newException(ValueError, "the requested block is not part of the canonical chain")
176+
return err("the requested block is not part of the canonical chain")
160177

161178
# verify transactions
162179
if fullTransactions:
163180
let verified = verifyTransactions(header.transactionsRoot, blk.transactions).valueOr:
164-
raise newException(ValueError, "error while verifying transactions root")
181+
return err("error while verifying transactions root")
165182
if not verified:
166-
raise newException(ValueError, "transactions within the block do not yield the same transaction root")
183+
return err("transactions within the block do not yield the same transaction root")
167184

168185
# verify withdrawals
169186
if blk.withdrawals.isSome():
170187
if blk.withdrawalsRoot.get() != orderedTrieRoot(blk.withdrawals.get()):
171-
raise newException(ValueError, "withdrawals within the block do not yield the same withdrawals root")
188+
return err("withdrawals within the block do not yield the same withdrawals root")
172189

173-
return blk
190+
return ok(blk)
174191

175192
proc getHeaderByHash*(
176193
self: VerifiedRpcProxy, blockHash: Hash32
177-
): Future[Header] {.async: (raises: [ValueError, CatchableError]).} =
194+
): Future[Result[Header, string]] {.async: (raises: []).} =
178195
let cachedHeader = self.headerStore.get(blockHash)
179196

180197
if cachedHeader.isNone():
181198
debug "did not find the header in the cache", blockHash=blockHash
182199
else:
183-
return cachedHeader.get()
200+
return ok(cachedHeader.get())
184201

185202
# get the source block
186203
let earliestHeader = self.headerStore.earliest.valueOr:
187-
raise newException(ValueError, "Syncing")
204+
return err("Syncing")
188205

189206
# get the target block
190-
let blk = await self.rpcClient.eth_getBlockByHash(blockHash, false)
207+
let blk =
208+
try:
209+
await self.rpcClient.eth_getBlockByHash(blockHash, false)
210+
except CatchableError as e:
211+
return err(e.msg)
212+
191213
let header = convHeader(blk)
192214

193215
# verify header hash
194216
if header.rlpHash != blk.hash:
195-
raise newException(ValueError, "hashed block header doesn't match with blk.hash(downloaded)")
217+
return err("hashed block header doesn't match with blk.hash(downloaded)")
196218

197219
if blockHash != blk.hash:
198-
raise newException(ValueError, "the blk.hash(downloaded) doesn't match with the provided hash")
220+
return err("the blk.hash(downloaded) doesn't match with the provided hash")
199221

200222
# walk blocks backwards(time) from source to target
201223
let isLinked = await self.walkBlocks(earliestHeader.number, header.number, earliestHeader.parentHash, blockHash)
202224

203225
if not isLinked:
204-
raise newException(ValueError, "the requested block is not part of the canonical chain")
226+
return err("the requested block is not part of the canonical chain")
205227

206-
return header
228+
return ok(header)
207229

208230
proc getHeaderByTag*(
209231
self: VerifiedRpcProxy, blockTag: BlockTag
210-
): Future[Header] {.async: (raises: [ValueError, CatchableError]).} =
232+
): Future[Result[Header, string]] {.async: (raises: []).} =
211233
let
212-
n = self.resolveTag(blockTag)
234+
n = self.resolveTag(blockTag).valueOr:
235+
return err(error)
213236
cachedHeader = self.headerStore.get(n)
214237

215238
if cachedHeader.isNone():
216239
debug "did not find the header in the cache", blockTag=blockTag
217240
else:
218-
return cachedHeader.get()
241+
return ok(cachedHeader.get())
219242

220243
# get the source block
221244
let earliestHeader = self.headerStore.earliest.valueOr:
222-
raise newException(ValueError, "Syncing")
245+
return err("Syncing")
223246

224247
# get the target block
225-
let blk = await self.rpcClient.eth_getBlockByNumber(blockTag, false)
248+
let blk =
249+
try:
250+
await self.rpcClient.eth_getBlockByNumber(blockTag, false)
251+
except CatchableError as e:
252+
return err(e.msg)
253+
226254
let header = convHeader(blk)
227255

228256
# verify header hash
229257
if header.rlpHash != blk.hash:
230-
raise newException(ValueError, "hashed block header doesn't match with blk.hash(downloaded)")
258+
return err("hashed block header doesn't match with blk.hash(downloaded)")
231259

232260
if n != header.number:
233-
raise newException(ValueError, "the downloaded block number doesn't match with the requested block number")
261+
return err("the downloaded block number doesn't match with the requested block number")
234262

235263
# walk blocks backwards(time) from source to target
236264
let isLinked = await self.walkBlocks(earliestHeader.number, header.number, earliestHeader.parentHash, blk.hash)
237265

238266
if not isLinked:
239-
raise newException(ValueError, "the requested block is not part of the canonical chain")
267+
return err("the requested block is not part of the canonical chain")
240268

241-
return header
269+
return ok(header)

0 commit comments

Comments
 (0)