Skip to content

Commit

Permalink
manifest/compiler: drop hashes from ABI and debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov committed Nov 18, 2020
1 parent d8157fa commit 59537ae
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 114 deletions.
7 changes: 1 addition & 6 deletions pkg/compiler/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
"unicode"
"unicode/utf8"

"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util"
)

// DebugInfo represents smart-contract debug information.
type DebugInfo struct {
MainPkg string `json:"-"`
Hash util.Uint160 `json:"hash"`
Documents []string `json:"documents"`
Methods []MethodDebugInfo `json:"methods"`
Events []EventDebugInfo `json:"events"`
Expand Down Expand Up @@ -113,7 +110,6 @@ func (c *codegen) saveSequencePoint(n ast.Node) {
func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo {
d := &DebugInfo{
MainPkg: c.mainPkg.Pkg.Name(),
Hash: hash.Hash160(contract),
Events: []EventDebugInfo{},
Documents: c.documents,
}
Expand Down Expand Up @@ -424,15 +420,14 @@ func (di *DebugInfo) ConvertToManifest(events []manifest.Event, supportedStandar
}
}

result := manifest.NewManifest(di.Hash)
result := manifest.NewManifest()
if supportedStandards != nil {
result.SupportedStandards = supportedStandards
}
if events == nil {
events = make([]manifest.Event, 0)
}
result.ABI = manifest.ABI{
Hash: di.Hash,
Methods: methods,
Events: events,
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/compiler/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package compiler
import (
"testing"

"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
Expand Down Expand Up @@ -69,10 +68,6 @@ func _deploy(isUpdate bool) {}
d := c.emitDebugInfo(buf)
require.NotNil(t, d)

t.Run("hash", func(t *testing.T) {
require.True(t, hash.Hash160(buf).Equals(d.Hash))
})

t.Run("return types", func(t *testing.T) {
returnTypes := map[string]string{
"MethodInt": "Integer",
Expand Down Expand Up @@ -154,7 +149,6 @@ func _deploy(isUpdate bool) {}
// note: offsets are hard to predict, so we just take them from the output
expected := &manifest.Manifest{
ABI: manifest.ABI{
Hash: hash.Hash160(buf),
Methods: []manifest.Method{
{
Name: "_deploy",
Expand Down Expand Up @@ -261,7 +255,6 @@ func _deploy(isUpdate bool) {}
},
Extra: nil,
}
require.True(t, expected.ABI.Hash.Equals(actual.ABI.Hash))
require.ElementsMatch(t, expected.ABI.Methods, actual.ABI.Methods)
require.Equal(t, expected.ABI.Events, actual.ABI.Events)
require.Equal(t, expected.Groups, actual.Groups)
Expand Down Expand Up @@ -303,7 +296,6 @@ func TestSequencePoints(t *testing.T) {

func TestDebugInfo_MarshalJSON(t *testing.T) {
d := &DebugInfo{
Hash: util.Uint160{10, 11, 12, 13},
Documents: []string{"/path/to/file"},
Methods: []MethodDebugInfo{
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/dao/cacheddao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCachedDaoContracts(t *testing.T) {
_, err := dao.GetContractState(sh)
require.NotNil(t, err)

m := manifest.NewManifest(hash.Hash160(script))
m := manifest.NewManifest()

cs := &state.Contract{
ID: 123,
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/interop/callback/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func CreateFromMethod(ic *interop.Context) error {
return errors.New("invalid method name")
}
currCs, err := ic.DAO.GetContractState(ic.VM.GetCurrentScriptHash())
if err == nil && !currCs.Manifest.CanCall(&cs.Manifest, method) {
if err == nil && !currCs.Manifest.CanCall(h, &cs.Manifest, method) {
return errors.New("method call is not allowed")
}
md := cs.Manifest.ABI.GetMethod(method)
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/interop/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func NewContractMD(name string) *ContractMD {

c.Script = w.Bytes()
c.Hash = hash.Hash160(c.Script)
c.Manifest = *manifest.DefaultManifest(c.Hash)
c.Manifest = *manifest.DefaultManifest()

return c
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/interop/contract/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func callExInternal(ic *interop.Context, h []byte, name string, args []stackitem
}
curr, err := ic.DAO.GetContractState(ic.VM.GetCurrentScriptHash())
if err == nil {
if !curr.Manifest.CanCall(&cs.Manifest, name) {
if !curr.Manifest.CanCall(u, &cs.Manifest, name) {
return errors.New("disallowed method call")
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/core/interop_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ func contractUpdate(ic *interop.Context) error {
Script: script,
Manifest: contract.Manifest,
}
contract.Manifest.ABI.Hash = newHash
if err := ic.DAO.PutContractState(contract); err != nil {
return fmt.Errorf("failed to update script: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/core/interop_neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
Expand Down Expand Up @@ -293,7 +292,7 @@ func createVMAndPushTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop

func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interop.Context, *Blockchain) {
script := []byte("testscript")
m := manifest.NewManifest(hash.Hash160(script))
m := manifest.NewManifest()
contractState := &state.Contract{
Script: script,
Manifest: *m,
Expand Down
45 changes: 7 additions & 38 deletions pkg/core/interop_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func getTestContractState() (*state.Contract, *state.Contract) {

script := w.Bytes()
h := hash.Hash160(script)
m := manifest.NewManifest(h)
m := manifest.NewManifest()
m.ABI.Methods = []manifest.Method{
{
Name: "add",
Expand Down Expand Up @@ -490,7 +490,7 @@ func getTestContractState() (*state.Contract, *state.Contract) {
}

currScript := []byte{byte(opcode.RET)}
m = manifest.NewManifest(hash.Hash160(currScript))
m = manifest.NewManifest()
perm := manifest.NewPermission(manifest.PermissionHash, h)
perm.Methods.Add("add")
perm.Methods.Add("drop")
Expand Down Expand Up @@ -534,7 +534,7 @@ func TestContractCall(t *testing.T) {
require.NoError(t, ic.DAO.PutContractState(currCs))

currScript := currCs.Script
h := cs.Manifest.ABI.Hash
h := hash.Hash160(cs.Script)

addArgs := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(2)})
t.Run("Good", func(t *testing.T) {
Expand Down Expand Up @@ -652,13 +652,6 @@ func TestContractCreate(t *testing.T) {
compareContractStates(t, cs, actual)
})

t.Run("invalid scripthash", func(t *testing.T) {
cs.Script = append(cs.Script, 0x01)
putArgsOnStack()

require.Error(t, contractCreate(ic))
})

t.Run("contract already exists", func(t *testing.T) {
cs.Script = cs.Script[:len(cs.Script)-1]
require.NoError(t, ic.DAO.PutContractState(cs))
Expand Down Expand Up @@ -741,9 +734,7 @@ func TestContractUpdate(t *testing.T) {
ID: 95,
Script: duplicateScript,
Manifest: manifest.Manifest{
ABI: manifest.ABI{
Hash: hash.Hash160(duplicateScript),
},
ABI: manifest.ABI{},
},
}))
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
Expand Down Expand Up @@ -775,7 +766,6 @@ func TestContractUpdate(t *testing.T) {
Script: newScript,
Manifest: cs.Manifest,
}
expected.Manifest.ABI.Hash = hash.Hash160(newScript)
_ = expected.ScriptHash()
require.Equal(t, expected, actual)

Expand All @@ -792,27 +782,10 @@ func TestContractUpdate(t *testing.T) {
require.Error(t, contractUpdate(ic))
})

t.Run("update manifest, bad contract hash", func(t *testing.T) {
require.NoError(t, ic.DAO.PutContractState(cs))
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
manifest := &manifest.Manifest{
ABI: manifest.ABI{
Hash: util.Uint160{4, 5, 6},
},
}
manifestBytes, err := json.Marshal(manifest)
require.NoError(t, err)
putArgsOnStack(stackitem.Null{}, manifestBytes)

require.Error(t, contractUpdate(ic))
})

t.Run("update manifest, positive", func(t *testing.T) {
require.NoError(t, ic.DAO.PutContractState(cs))
manifest := &manifest.Manifest{
ABI: manifest.ABI{
Hash: cs.ScriptHash(),
},
ABI: manifest.ABI{},
}
manifestBytes, err := json.Marshal(manifest)
require.NoError(t, err)
Expand Down Expand Up @@ -844,9 +817,7 @@ func TestContractUpdate(t *testing.T) {
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
newScript := []byte{12, 13, 14}
newManifest := manifest.Manifest{
ABI: manifest.ABI{
Hash: hash.Hash160(newScript),
},
ABI: manifest.ABI{},
}
newManifestBytes, err := json.Marshal(newManifest)
require.NoError(t, err)
Expand All @@ -863,7 +834,6 @@ func TestContractUpdate(t *testing.T) {
Script: newScript,
Manifest: newManifest,
}
expected.Manifest.ABI.Hash = hash.Hash160(newScript)
_ = expected.ScriptHash()
require.Equal(t, expected, actual)

Expand Down Expand Up @@ -909,7 +879,6 @@ func TestContractCreateDeploy(t *testing.T) {
Script: append(cs.Script, byte(opcode.RET)),
Manifest: cs.Manifest,
}
newCs.Manifest.ABI.Hash = hash.Hash160(newCs.Script)
putArgs(newCs)
require.NoError(t, contractUpdate(ic))
require.NoError(t, v.Run())
Expand Down Expand Up @@ -975,7 +944,7 @@ func TestMethodCallback(t *testing.T) {
require.NoError(t, ic.DAO.PutContractState(currCs))

ic.Functions = append(ic.Functions, systemInterops)
rawHash := cs.Manifest.ABI.Hash.BytesBE()
rawHash := hash.Hash160(cs.Script).BytesBE()

t.Run("Invalid", func(t *testing.T) {
runInvalid := func(args ...interface{}) func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/native_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func TestNativeContract_InvokeOtherContract(t *testing.T) {
// put some other contract into chain (this contract just pushes `5` on stack)
avm := []byte{byte(opcode.PUSH5), byte(opcode.RET)}
contractHash := hash.Hash160(avm)
m := manifest.NewManifest(contractHash)
m := manifest.NewManifest()
m.ABI.Methods = []manifest.Method{
{
Name: "five",
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/native_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getOracleContractState(h util.Uint160) *state.Contract {
emit.Syscall(w.BinWriter, interopnames.SystemStoragePut)
emit.Opcodes(w.BinWriter, opcode.RET)

m := manifest.NewManifest(h)
m := manifest.NewManifest()
m.ABI.Methods = []manifest.Method{
{
Name: "requestURL",
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/state/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestEncodeDecodeContractState(t *testing.T) {
script := []byte("testscript")

h := hash.Hash160(script)
m := manifest.NewManifest(h)
m := manifest.NewManifest()
m.ABI.Methods = []manifest.Method{{
Name: "main",
Parameters: []manifest.Parameter{
Expand Down
Loading

0 comments on commit 59537ae

Please sign in to comment.