From a2f5f7a74a120a3e9df14374330663bf99801e04 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 30 Nov 2021 12:44:05 +0300 Subject: [PATCH] [#184] *: Change `panic` to `Log`+`ABORT` in `OnNEP17Payment` Signed-off-by: Pavel Karpy --- alphabet/alphabet_contract.go | 2 +- common/transfer.go | 12 ++++++++++++ neofs/neofs_contract.go | 8 ++++---- processing/processing_contract.go | 2 +- proxy/proxy_contract.go | 2 +- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/alphabet/alphabet_contract.go b/alphabet/alphabet_contract.go index c1723ab..830396b 100644 --- a/alphabet/alphabet_contract.go +++ b/alphabet/alphabet_contract.go @@ -28,7 +28,7 @@ const ( func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { caller := runtime.GetCallingScriptHash() if !common.BytesEqual(caller, []byte(gas.Hash)) && !common.BytesEqual(caller, []byte(neo.Hash)) { - panic("alphabet contract accepts GAS and NEO only") + common.AbortWithMessage("alphabet contract accepts GAS and NEO only") } } diff --git a/common/transfer.go b/common/transfer.go index e545745..133c8e9 100644 --- a/common/transfer.go +++ b/common/transfer.go @@ -1,5 +1,10 @@ package common +import ( + "github.com/nspcc-dev/neo-go/pkg/interop/runtime" + "github.com/nspcc-dev/neo-go/pkg/interop/util" +) + var ( mintPrefix = []byte{0x01} burnPrefix = []byte{0x02} @@ -33,3 +38,10 @@ func UnlockTransferDetails(epoch int) []byte { func ContainerFeeTransferDetails(cid []byte) []byte { return append(containerFeePrefix, cid...) } + +// AbortWithMessage calls `runtime.Log` with passed message +// and calls `ABORT` opcode. +func AbortWithMessage(msg string) { + runtime.Log(msg) + util.Abort() +} diff --git a/neofs/neofs_contract.go b/neofs/neofs_contract.go index 5f75964..4502b54 100644 --- a/neofs/neofs_contract.go +++ b/neofs/neofs_contract.go @@ -244,14 +244,14 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { } if amount <= 0 { - panic("amount must be positive") + common.AbortWithMessage("amount must be positive") } else if maxBalanceAmountGAS < amount { - panic("out of max amount limit") + common.AbortWithMessage("out of max amount limit") } caller := runtime.GetCallingScriptHash() if !common.BytesEqual(caller, interop.Hash160(gas.Hash)) { - panic("only GAS can be accepted for deposit") + common.AbortWithMessage("only GAS can be accepted for deposit") } switch len(rcv) { @@ -259,7 +259,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { case 0: rcv = from default: - panic("invalid data argument, expected Hash160") + common.AbortWithMessage("invalid data argument, expected Hash160") } runtime.Log("funds have been transferred") diff --git a/processing/processing_contract.go b/processing/processing_contract.go index de70135..1bf6c78 100644 --- a/processing/processing_contract.go +++ b/processing/processing_contract.go @@ -22,7 +22,7 @@ const ( func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { caller := runtime.GetCallingScriptHash() if !common.BytesEqual(caller, []byte(gas.Hash)) { - panic("processing contract accepts GAS only") + common.AbortWithMessage("processing contract accepts GAS only") } } diff --git a/proxy/proxy_contract.go b/proxy/proxy_contract.go index 6c967af..c4f71e2 100644 --- a/proxy/proxy_contract.go +++ b/proxy/proxy_contract.go @@ -19,7 +19,7 @@ const ( func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { caller := runtime.GetCallingScriptHash() if !common.BytesEqual(caller, []byte(gas.Hash)) { - panic("proxy contract accepts GAS only") + common.AbortWithMessage("proxy contract accepts GAS only") } }