From c7a040ff817f556c8a190f54174b08f42ed41c50 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 14 May 2020 21:01:56 +0300 Subject: [PATCH] core: fix bug with native contracts methods initialisation `NewNEO()` and `NewGAS()` methods are trying to initialise both `onPersist` and `incBalance` methods of NEO and GAS AFTER nep5TokenNative is set to the VALUE of created nep5 token. In this situation an attemmpt to call the corresponding native contracts methods (e.g. transfer native GAS) leads to contract invocation failure, as far as `nep5TokenNative.incBalance` method is nil. Fixed this by initializing both `onPersist` and `incBalance` methods before getting the value of nep5 contract. --- pkg/core/native/native_gas.go | 7 ++++--- pkg/core/native/native_neo.go | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index cdc0cd97b..1c5351ef0 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -28,21 +28,22 @@ const initialGAS = 30000000 // NewGAS returns GAS native contract. func NewGAS() *GAS { + g := &GAS{} nep5 := newNEP5Native(gasSyscallName) nep5.name = "GAS" nep5.symbol = "gas" nep5.decimals = 8 nep5.factor = GASFactor + nep5.onPersist = chainOnPersist(g.onPersist, g.OnPersist) + nep5.incBalance = g.increaseBalance - g := &GAS{nep5TokenNative: *nep5} + g.nep5TokenNative = *nep5 desc := newDescriptor("getSysFeeAmount", smartcontract.IntegerType, manifest.NewParameter("index", smartcontract.IntegerType)) md := newMethodAndPrice(g.getSysFeeAmount, 1, smartcontract.NoneFlag) g.AddMethod(md, desc, true) - g.onPersist = chainOnPersist(g.onPersist, g.OnPersist) - g.incBalance = g.increaseBalance return g } diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index cd35e942a..e4bcc4246 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -60,13 +60,16 @@ func makeValidatorKey(key *keys.PublicKey) []byte { // NewNEO returns NEO native contract. func NewNEO() *NEO { + n := &NEO{} nep5 := newNEP5Native(neoSyscallName) nep5.name = "NEO" nep5.symbol = "neo" nep5.decimals = 0 nep5.factor = 1 + nep5.onPersist = chainOnPersist(n.onPersist, n.OnPersist) + nep5.incBalance = n.increaseBalance - n := &NEO{nep5TokenNative: *nep5} + n.nep5TokenNative = *nep5 desc := newDescriptor("unclaimedGas", smartcontract.IntegerType, manifest.NewParameter("account", smartcontract.Hash160Type), @@ -97,9 +100,6 @@ func NewNEO() *NEO { md = newMethodAndPrice(n.getNextBlockValidators, 1, smartcontract.NoneFlag) n.AddMethod(md, desc, true) - n.onPersist = chainOnPersist(n.onPersist, n.OnPersist) - n.incBalance = n.increaseBalance - return n }