MCPcopy Create free account
hub / github.com/ethstorage/es-node / EsNodeSync

Function EsNodeSync

cmd/es-node/main.go:305–356  ·  view source on GitHub ↗
(ctx *cli.Context)

Source from the content-addressed store, hash-verified

303}
304
305func EsNodeSync(ctx *cli.Context) error {
306 lg.Info("Sync data for specified kv")
307 if !ctx.IsSet(kvIndexFlagName) {
308 return fmt.Errorf("kv_index must be specified")
309 }
310 kvIndex := uint64(ctx.Int(kvIndexFlagName))
311 lg.Info("Read flag", "name", kvIndexFlagName, "value", kvIndex)
312 if !ctx.IsSet(esRpcFlagName) {
313 return fmt.Errorf("es_rpc must be specified")
314 }
315 esRpc := ctx.String(esRpcFlagName)
316 lg.Info("Read flag", "name", esRpcFlagName, "value", esRpc)
317 // query meta
318 contract := readRequiredFlag(ctx, flags.StorageL1Contract)
319 if !common.IsHexAddress(contract) {
320 return fmt.Errorf("invalid contract address %s", contract)
321 }
322 l1contract := common.HexToAddress(contract)
323 l1Rpc := readRequiredFlag(ctx, flags.L1NodeAddr)
324 pClient, err := eth.Dial(l1Rpc, l1contract, 2, lg)
325 if err != nil {
326 return fmt.Errorf("failed to dial eth client: %w", err)
327 }
328 meta, err := pClient.GetKvMetas([]uint64{kvIndex}, rpc.LatestBlockNumber.Int64())
329 if err != nil {
330 return fmt.Errorf("failed to get meta: %w", err)
331 }
332 lg.Info("Query meta from contract done", "kvIndex", kvIndex, "meta", common.Hash(meta[0]).Hex())
333 // query blob
334 var commit common.Hash
335 copy(commit[:], meta[0][32-ethstorage.HashSizeInContract:32])
336 blob, err := downloadBlobFromRPC(esRpc, kvIndex, commit)
337 if err != nil {
338 return fmt.Errorf("failed to download blob from RPC: %w", err)
339 }
340 lg.Info("Download blob from RPC done", "kvIndex", kvIndex, "commit", commit.Hex())
341 // write blob and meta
342 shardManager, err := initShardManager(ctx, l1Rpc, l1contract)
343 if err != nil {
344 return fmt.Errorf("failed to init shard manager: %w", err)
345 }
346 preparedCommit := ethstorage.PrepareCommit(commit)
347 ok, err := shardManager.TryWrite(kvIndex, blob, preparedCommit)
348 if err != nil {
349 return fmt.Errorf("failed to write kv: %w", err)
350 }
351 if !ok {
352 return fmt.Errorf("failed to write kv: kv index not in the shard")
353 }
354 lg.Info("Sync data finished", "kvIndex", kvIndex, "commit", commit.Hex())
355 return nil
356}

Callers

nothing calls this directly

Calls 10

DialFunction · 0.92
PrepareCommitFunction · 0.92
readRequiredFlagFunction · 0.85
IsSetMethod · 0.80
TryWriteMethod · 0.80
downloadBlobFromRPCFunction · 0.70
initShardManagerFunction · 0.70
GetKvMetasMethod · 0.65
HashMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected