native: simplify code somewhat, get Metadata() only once

This commit is contained in:
Roman Khimov 2022-12-16 23:48:04 +03:00
parent 5d478b5514
commit 3339824fe9

View file

@ -18,30 +18,31 @@ func Call(ic *interop.Context) error {
if version != 0 { if version != 0 {
return fmt.Errorf("native contract of version %d is not active", version) return fmt.Errorf("native contract of version %d is not active", version)
} }
var c interop.Contract var meta *interop.ContractMD
curr := ic.VM.GetCurrentScriptHash() curr := ic.VM.GetCurrentScriptHash()
for _, ctr := range ic.Natives { for _, ctr := range ic.Natives {
if ctr.Metadata().Hash == curr { m := ctr.Metadata()
c = ctr if m.Hash == curr {
meta = m
break break
} }
} }
if c == nil { if meta == nil {
return fmt.Errorf("native contract %s (version %d) not found", curr.StringLE(), version) return fmt.Errorf("native contract %s (version %d) not found", curr.StringLE(), version)
} }
history := c.Metadata().UpdateHistory history := meta.UpdateHistory
if len(history) == 0 { if len(history) == 0 {
return fmt.Errorf("native contract %s is disabled", c.Metadata().Name) return fmt.Errorf("native contract %s is disabled", meta.Name)
} }
if history[0] > ic.BlockHeight() { if history[0] > ic.BlockHeight() {
return fmt.Errorf("native contract %s is active after height = %d", c.Metadata().Name, history[0]) return fmt.Errorf("native contract %s is active after height = %d", meta.Name, history[0])
} }
m, ok := c.Metadata().GetMethodByOffset(ic.VM.Context().IP()) m, ok := meta.GetMethodByOffset(ic.VM.Context().IP())
if !ok { if !ok {
return fmt.Errorf("method not found") return fmt.Errorf("method not found")
} }
reqFlags := m.RequiredFlags reqFlags := m.RequiredFlags
if !ic.IsHardforkEnabled(config.HFAspidochelone) && c.Metadata().ID == ManagementContractID && if !ic.IsHardforkEnabled(config.HFAspidochelone) && meta.ID == ManagementContractID &&
(m.MD.Name == "deploy" || m.MD.Name == "update") { (m.MD.Name == "deploy" || m.MD.Name == "update") {
reqFlags &= callflag.States | callflag.AllowNotify reqFlags &= callflag.States | callflag.AllowNotify
} }