From b5185d5d1af8c4f45fc20bab23986e35b8c5c225 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 22 Jul 2020 10:46:28 +0300 Subject: [PATCH] core: refactor System.Storage.Get[ReadOnly]Context interops Part of #1055. Split methods, as they have a lot of common code. This also fixex nil error of storageGetReadOnlyContext in case when contract does not have storage. --- pkg/core/interop_system.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index 17f6b00bd..745b20a9b 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -333,6 +333,17 @@ func storageGet(ic *interop.Context, v *vm.VM) error { // storageGetContext returns storage context (scripthash). func storageGetContext(ic *interop.Context, v *vm.VM) error { + return storageGetContextInternal(ic, v, false) +} + +// storageGetReadOnlyContext returns read-only context (scripthash). +func storageGetReadOnlyContext(ic *interop.Context, v *vm.VM) error { + return storageGetContextInternal(ic, v, true) +} + +// storageGetContextInternal is internal version of storageGetContext and +// storageGetReadOnlyContext which allows to specify ReadOnly context flag. +func storageGetContextInternal(ic *interop.Context, v *vm.VM, isReadOnly bool) error { contract, err := ic.DAO.GetContractState(v.GetCurrentScriptHash()) if err != nil { return err @@ -342,24 +353,7 @@ func storageGetContext(ic *interop.Context, v *vm.VM) error { } sc := &StorageContext{ ID: contract.ID, - ReadOnly: false, - } - v.Estack().PushVal(stackitem.NewInterop(sc)) - return nil -} - -// storageGetReadOnlyContext returns read-only context (scripthash). -func storageGetReadOnlyContext(ic *interop.Context, v *vm.VM) error { - contract, err := ic.DAO.GetContractState(v.GetCurrentScriptHash()) - if err != nil { - return err - } - if !contract.HasStorage() { - return err - } - sc := &StorageContext{ - ID: contract.ID, - ReadOnly: true, + ReadOnly: isReadOnly, } v.Estack().PushVal(stackitem.NewInterop(sc)) return nil