From 9a41356a3b491fba60bfdf9aa50fdb09e8d6de99 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 4 Apr 2022 12:33:29 +0300 Subject: [PATCH] core: add getUint256FromItem helper to native ledger --- pkg/core/native/ledger.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/core/native/ledger.go b/pkg/core/native/ledger.go index a5d56133b..d82342373 100644 --- a/pkg/core/native/ledger.go +++ b/pkg/core/native/ledger.go @@ -166,25 +166,29 @@ func getBlockHashFromItem(bc interop.Ledger, item stackitem.Item) util.Uint256 { } return bc.GetHeaderHash(int(index)) } - bytes, err := item.TryBytes() - if err != nil { - panic(err) - } - hash, err := util.Uint256DecodeBytesBE(bytes) + hash, err := getUint256FromItem(item) if err != nil { panic(err) } return hash } +func getUint256FromItem(item stackitem.Item) (util.Uint256, error) { + hashbytes, err := item.TryBytes() + if err != nil { + return util.Uint256{}, fmt.Errorf("failed to get hash bytes: %w", err) + } + hash, err := util.Uint256DecodeBytesBE(hashbytes) + if err != nil { + return util.Uint256{}, fmt.Errorf("failed to decode hash: %w", err) + } + return hash, nil +} + // getTransactionAndHeight returns transaction and its height if it's present // on the chain. It panics if anything goes wrong. func getTransactionAndHeight(d *dao.Simple, item stackitem.Item) (*transaction.Transaction, uint32, error) { - hashbytes, err := item.TryBytes() - if err != nil { - panic(err) - } - hash, err := util.Uint256DecodeBytesBE(hashbytes) + hash, err := getUint256FromItem(item) if err != nil { panic(err) }