From 6b6878706a62c69678703354519fd3241b3f00c6 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 23 Nov 2024 17:20:20 +0300 Subject: [PATCH 1/2] interop: fix ActiveFrom comparison when building HF-specific MD These conditions are about filtering methods out. A method is excluded if its ActiveFrom is strictly higher than the current HF, since if it's the same then it should be present. Otherwise contract is broken at the height of this particular HF. Signed-off-by: Roman Khimov --- pkg/core/interop/context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index d9ff394263..b23690fb6f 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -294,7 +294,7 @@ func (c *ContractMD) buildHFSpecificMD(hf config.Hardfork) { w := io.NewBufBinWriter() for i := range c.methods { m := c.methods[i] - if !(m.ActiveFrom == nil || (hf != config.HFDefault && (*m.ActiveFrom).Cmp(hf) >= 0)) || + if (m.ActiveFrom != nil && (*m.ActiveFrom).Cmp(hf) > 0) || (m.ActiveTill != nil && (*m.ActiveTill).Cmp(hf) <= 0) { continue } @@ -317,7 +317,7 @@ func (c *ContractMD) buildHFSpecificMD(hf config.Hardfork) { } for i := range c.events { e := c.events[i] - if !(e.ActiveFrom == nil || (hf != config.HFDefault && (*e.ActiveFrom).Cmp(hf) >= 0)) || + if (e.ActiveFrom != nil && (*e.ActiveFrom).Cmp(hf) > 0) || (e.ActiveTill != nil && (*e.ActiveTill).Cmp(hf) <= 0) { continue } From 9e7fd5180a123e54217a3d5aa018311528865d28 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 23 Nov 2024 17:31:00 +0300 Subject: [PATCH 2/2] interop: slightly rephrase BuildHFSpecificMD logic Technically, we can always buildHFSpecificMD() if contract is active, but we just optimize the build out in some cases. switch slightly obfuscates this and requires having the call in two branches. Signed-off-by: Roman Khimov --- pkg/core/interop/context.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index b23690fb6f..bf600acacc 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -266,18 +266,16 @@ func (c *ContractMD) BuildHFSpecificMD(activeIn *config.Hardfork) { } for _, hf := range append([]config.Hardfork{config.HFDefault}, config.Hardforks...) { - switch { - case hf.Cmp(start) < 0: + if hf.Cmp(start) < 0 { continue - case hf.Cmp(start) == 0: - c.buildHFSpecificMD(hf) - default: - if _, ok := c.ActiveHFs[hf]; !ok { - // Intentionally omit HFSpecificContractMD structure copying since mdCache is read-only. - c.mdCache[hf] = c.mdCache[hf.Prev()] - continue - } + } + _, contractHasChanges := c.ActiveHFs[hf] + if hf.Cmp(start) == 0 || contractHasChanges { c.buildHFSpecificMD(hf) + } else { + // Optimize out MD rebuild, the contract is the same. + // Intentionally omit HFSpecificContractMD structure copying since mdCache is read-only. + c.mdCache[hf] = c.mdCache[hf.Prev()] } } }