state: split ContractBase and UpdateCounter

Latter doesn't make sense for native contracts.
This commit is contained in:
Evgeniy Stratonikov 2021-02-09 12:02:38 +03:00
parent 29b1581ea8
commit e1d2a5b5b7
9 changed files with 113 additions and 74 deletions

View file

@ -166,17 +166,21 @@ func TestAppCall(t *testing.T) {
innerNef, err := nef.NewFile(inner) innerNef, err := nef.NewFile(inner)
require.NoError(t, err) require.NoError(t, err)
return &state.Contract{ return &state.Contract{
Hash: ih, ContractBase: state.ContractBase{
NEF: *innerNef, Hash: ih,
Manifest: *m, NEF: *innerNef,
Manifest: *m,
},
}, nil }, nil
} else if h.Equals(barH) { } else if h.Equals(barH) {
barNef, err := nef.NewFile(barCtr) barNef, err := nef.NewFile(barCtr)
require.NoError(t, err) require.NoError(t, err)
return &state.Contract{ return &state.Contract{
Hash: barH, ContractBase: state.ContractBase{
NEF: *barNef, Hash: barH,
Manifest: *mBar, NEF: *barNef,
Manifest: *mBar,
},
}, nil }, nil
} }
return nil, errors.New("not found") return nil, errors.New("not found")

View file

@ -262,10 +262,12 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interop.C
ne, err := nef.NewFile(script) ne, err := nef.NewFile(script)
require.NoError(t, err) require.NoError(t, err)
contractState := &state.Contract{ contractState := &state.Contract{
NEF: *ne, ContractBase: state.ContractBase{
Hash: hash.Hash160(script), NEF: *ne,
Manifest: *m, Hash: hash.Hash160(script),
ID: 123, Manifest: *m,
ID: 123,
},
} }
chain := newTestChain(t) chain := newTestChain(t)

View file

@ -68,7 +68,8 @@ func TestContractIsStandard(t *testing.T) {
pub := priv.PublicKey() pub := priv.PublicKey()
ne, err := nef.NewFile(pub.GetVerificationScript()) ne, err := nef.NewFile(pub.GetVerificationScript())
require.NoError(t, err) require.NoError(t, err)
err = chain.contracts.Management.PutContractState(ic.DAO, &state.Contract{ID: 42, Hash: pub.GetScriptHash(), NEF: *ne}) err = chain.contracts.Management.PutContractState(ic.DAO,
&state.Contract{ContractBase: state.ContractBase{ID: 42, Hash: pub.GetScriptHash(), NEF: *ne}})
require.NoError(t, err) require.NoError(t, err)
v.Estack().PushVal(pub.GetScriptHash().BytesBE()) v.Estack().PushVal(pub.GetScriptHash().BytesBE())
@ -79,7 +80,8 @@ func TestContractIsStandard(t *testing.T) {
script := []byte{byte(opcode.PUSHT)} script := []byte{byte(opcode.PUSHT)}
ne, err := nef.NewFile(script) ne, err := nef.NewFile(script)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, chain.contracts.Management.PutContractState(ic.DAO, &state.Contract{ID: 24, Hash: hash.Hash160(script), NEF: *ne})) require.NoError(t, chain.contracts.Management.PutContractState(ic.DAO,
&state.Contract{ContractBase: state.ContractBase{ID: 24, Hash: hash.Hash160(script), NEF: *ne}}))
v.Estack().PushVal(crypto.Hash160(script).BytesBE()) v.Estack().PushVal(crypto.Hash160(script).BytesBE())
require.NoError(t, contractIsStandard(ic)) require.NoError(t, contractIsStandard(ic))
@ -540,9 +542,11 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) {
m.Permissions[1].Methods.Add("method") m.Permissions[1].Methods.Add("method")
cs := &state.Contract{ cs := &state.Contract{
Hash: h, ContractBase: state.ContractBase{
Manifest: *m, Hash: h,
ID: 42, Manifest: *m,
ID: 42,
},
} }
ne, err := nef.NewFile(script) ne, err := nef.NewFile(script)
if err != nil { if err != nil {
@ -582,10 +586,12 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) {
} }
return cs, &state.Contract{ return cs, &state.Contract{
NEF: *ne, ContractBase: state.ContractBase{
Hash: hash.Hash160(currScript), NEF: *ne,
Manifest: *m, Hash: hash.Hash160(currScript),
ID: 123, Manifest: *m,
ID: 123,
},
} }
} }
@ -906,11 +912,13 @@ func TestRuntimeCheckWitness(t *testing.T) {
ne, err := nef.NewFile(contractScript) ne, err := nef.NewFile(contractScript)
require.NoError(t, err) require.NoError(t, err)
contractState := &state.Contract{ contractState := &state.Contract{
ID: 15, ContractBase: state.ContractBase{
Hash: contractScriptHash, ID: 15,
NEF: *ne, Hash: contractScriptHash,
Manifest: manifest.Manifest{ NEF: *ne,
Groups: []manifest.Group{{PublicKey: pk.PublicKey(), Signature: make([]byte, keys.SignatureLen)}}, Manifest: manifest.Manifest{
Groups: []manifest.Group{{PublicKey: pk.PublicKey(), Signature: make([]byte, keys.SignatureLen)}},
},
}, },
} }
require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, contractState)) require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, contractState))

View file

@ -280,10 +280,12 @@ func (m *Management) Deploy(d dao.DAO, sender util.Uint160, neff *nef.File, mani
return nil, err return nil, err
} }
newcontract := &state.Contract{ newcontract := &state.Contract{
ID: id, ContractBase: state.ContractBase{
Hash: h, ID: id,
NEF: *neff, Hash: h,
Manifest: *manif, NEF: *neff,
Manifest: *manif,
},
} }
err = m.PutContractState(d, newcontract) err = m.PutContractState(d, newcontract)
if err != nil { if err != nil {
@ -451,10 +453,12 @@ func (m *Management) OnPersist(ic *interop.Context) error {
md := native.Metadata() md := native.Metadata()
cs := &state.Contract{ cs := &state.Contract{
ID: md.ContractID, ContractBase: state.ContractBase{
Hash: md.Hash, ID: md.ContractID,
NEF: md.NEF, Hash: md.Hash,
Manifest: md.Manifest, NEF: md.NEF,
Manifest: md.Manifest,
},
} }
err := m.PutContractState(ic.DAO, cs) err := m.PutContractState(ic.DAO, cs)
if err != nil { if err != nil {

View file

@ -172,10 +172,12 @@ func TestNativeContract_Invoke(t *testing.T) {
chain.registerNative(tn) chain.registerNative(tn)
err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{ err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{
ID: 1, ContractBase: state.ContractBase{
NEF: tn.meta.NEF, ID: 1,
Hash: tn.meta.Hash, NEF: tn.meta.NEF,
Manifest: tn.meta.Manifest, Hash: tn.meta.Hash,
Manifest: tn.meta.Manifest,
},
}) })
require.NoError(t, err) require.NoError(t, err)
@ -210,9 +212,11 @@ func TestNativeContract_InvokeInternal(t *testing.T) {
chain.registerNative(tn) chain.registerNative(tn)
err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{ err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{
ID: 1, ContractBase: state.ContractBase{
NEF: tn.meta.NEF, ID: 1,
Manifest: tn.meta.Manifest, NEF: tn.meta.NEF,
Manifest: tn.meta.Manifest,
},
}) })
require.NoError(t, err) require.NoError(t, err)
@ -253,10 +257,12 @@ func TestNativeContract_InvokeOtherContract(t *testing.T) {
chain.registerNative(tn) chain.registerNative(tn)
err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{ err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{
ID: 1, ContractBase: state.ContractBase{
Hash: tn.meta.Hash, ID: 1,
NEF: tn.meta.NEF, Hash: tn.meta.Hash,
Manifest: tn.meta.Manifest, NEF: tn.meta.NEF,
Manifest: tn.meta.Manifest,
},
}) })
require.NoError(t, err) require.NoError(t, err)

View file

@ -92,10 +92,12 @@ func getOracleContractState(h util.Uint160) *state.Contract {
panic(err) panic(err)
} }
return &state.Contract{ return &state.Contract{
NEF: *ne, ContractBase: state.ContractBase{
Hash: hash.Hash160(script), NEF: *ne,
Manifest: *m, Hash: hash.Hash160(script),
ID: 42, Manifest: *m,
ID: 42,
},
} }
} }

View file

@ -18,11 +18,16 @@ import (
// Contract holds information about a smart contract in the NEO blockchain. // Contract holds information about a smart contract in the NEO blockchain.
type Contract struct { type Contract struct {
ID int32 `json:"id"` ContractBase
UpdateCounter uint16 `json:"updatecounter"` UpdateCounter uint16 `json:"updatecounter"`
Hash util.Uint160 `json:"hash"` }
NEF nef.File `json:"nef"`
Manifest manifest.Manifest `json:"manifest"` // ContractBase represents part shared by native and user-deployed contracts.
type ContractBase struct {
ID int32 `json:"id"`
Hash util.Uint160 `json:"hash"`
NEF nef.File `json:"nef"`
Manifest manifest.Manifest `json:"manifest"`
} }
// DecodeBinary implements Serializable interface. // DecodeBinary implements Serializable interface.

View file

@ -34,19 +34,21 @@ func TestEncodeDecodeContractState(t *testing.T) {
ReturnType: smartcontract.BoolType, ReturnType: smartcontract.BoolType,
}} }}
contract := &Contract{ contract := &Contract{
ID: 123,
UpdateCounter: 42, UpdateCounter: 42,
Hash: h, ContractBase: ContractBase{
NEF: nef.File{ ID: 123,
Header: nef.Header{ Hash: h,
Magic: nef.Magic, NEF: nef.File{
Compiler: "neo-go.test-test", Header: nef.Header{
Magic: nef.Magic,
Compiler: "neo-go.test-test",
},
Tokens: []nef.MethodToken{},
Script: script,
Checksum: 0,
}, },
Tokens: []nef.MethodToken{}, Manifest: *m,
Script: script,
Checksum: 0,
}, },
Manifest: *m,
} }
contract.NEF.Checksum = contract.NEF.CalculateChecksum() contract.NEF.Checksum = contract.NEF.CalculateChecksum()

View file

@ -350,10 +350,12 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
} }
m := manifest.NewManifest("Test") m := manifest.NewManifest("Test")
cs := &state.Contract{ cs := &state.Contract{
ID: 0, ContractBase: state.ContractBase{
Hash: hash.Hash160(script), ID: 0,
NEF: newTestNEF(script), Hash: hash.Hash160(script),
Manifest: *m, NEF: newTestNEF(script),
Manifest: *m,
},
} }
return cs return cs
}, },
@ -371,10 +373,12 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
} }
m := manifest.NewManifest("Test") m := manifest.NewManifest("Test")
cs := &state.Contract{ cs := &state.Contract{
ID: 0, ContractBase: state.ContractBase{
Hash: hash.Hash160(script), ID: 0,
NEF: newTestNEF(script), Hash: hash.Hash160(script),
Manifest: *m, NEF: newTestNEF(script),
Manifest: *m,
},
} }
return cs return cs
}, },
@ -392,10 +396,12 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
} }
m := manifest.NewManifest("Test") m := manifest.NewManifest("Test")
cs := &state.Contract{ cs := &state.Contract{
ID: 0, ContractBase: state.ContractBase{
Hash: hash.Hash160(script), ID: 0,
NEF: newTestNEF(script), Hash: hash.Hash160(script),
Manifest: *m, NEF: newTestNEF(script),
Manifest: *m,
},
} }
return cs return cs
}, },