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{
ContractBase: state.ContractBase{
Hash: ih, Hash: ih,
NEF: *innerNef, NEF: *innerNef,
Manifest: *m, 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{
ContractBase: state.ContractBase{
Hash: barH, Hash: barH,
NEF: *barNef, NEF: *barNef,
Manifest: *mBar, 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{
ContractBase: state.ContractBase{
NEF: *ne, NEF: *ne,
Hash: hash.Hash160(script), Hash: hash.Hash160(script),
Manifest: *m, Manifest: *m,
ID: 123, 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{
ContractBase: state.ContractBase{
Hash: h, Hash: h,
Manifest: *m, Manifest: *m,
ID: 42, 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{
ContractBase: state.ContractBase{
NEF: *ne, NEF: *ne,
Hash: hash.Hash160(currScript), Hash: hash.Hash160(currScript),
Manifest: *m, Manifest: *m,
ID: 123, ID: 123,
},
} }
} }
@ -906,12 +912,14 @@ 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{
ContractBase: state.ContractBase{
ID: 15, ID: 15,
Hash: contractScriptHash, Hash: contractScriptHash,
NEF: *ne, NEF: *ne,
Manifest: manifest.Manifest{ Manifest: manifest.Manifest{
Groups: []manifest.Group{{PublicKey: pk.PublicKey(), Signature: make([]byte, keys.SignatureLen)}}, 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))
loadScriptWithHashAndFlags(ic, contractScript, contractScriptHash, callflag.All) loadScriptWithHashAndFlags(ic, contractScript, contractScriptHash, callflag.All)

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{
ContractBase: state.ContractBase{
ID: id, ID: id,
Hash: h, Hash: h,
NEF: *neff, NEF: *neff,
Manifest: *manif, 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{
ContractBase: state.ContractBase{
ID: md.ContractID, ID: md.ContractID,
Hash: md.Hash, Hash: md.Hash,
NEF: md.NEF, NEF: md.NEF,
Manifest: md.Manifest, 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{
ContractBase: state.ContractBase{
ID: 1, ID: 1,
NEF: tn.meta.NEF, NEF: tn.meta.NEF,
Hash: tn.meta.Hash, Hash: tn.meta.Hash,
Manifest: tn.meta.Manifest, 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{
ContractBase: state.ContractBase{
ID: 1, ID: 1,
NEF: tn.meta.NEF, NEF: tn.meta.NEF,
Manifest: tn.meta.Manifest, 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{
ContractBase: state.ContractBase{
ID: 1, ID: 1,
Hash: tn.meta.Hash, Hash: tn.meta.Hash,
NEF: tn.meta.NEF, NEF: tn.meta.NEF,
Manifest: tn.meta.Manifest, 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{
ContractBase: state.ContractBase{
NEF: *ne, NEF: *ne,
Hash: hash.Hash160(script), Hash: hash.Hash160(script),
Manifest: *m, Manifest: *m,
ID: 42, ID: 42,
},
} }
} }

View file

@ -18,8 +18,13 @@ 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"`
}
// ContractBase represents part shared by native and user-deployed contracts.
type ContractBase struct {
ID int32 `json:"id"`
Hash util.Uint160 `json:"hash"` Hash util.Uint160 `json:"hash"`
NEF nef.File `json:"nef"` NEF nef.File `json:"nef"`
Manifest manifest.Manifest `json:"manifest"` Manifest manifest.Manifest `json:"manifest"`

View file

@ -34,8 +34,9 @@ func TestEncodeDecodeContractState(t *testing.T) {
ReturnType: smartcontract.BoolType, ReturnType: smartcontract.BoolType,
}} }}
contract := &Contract{ contract := &Contract{
ID: 123,
UpdateCounter: 42, UpdateCounter: 42,
ContractBase: ContractBase{
ID: 123,
Hash: h, Hash: h,
NEF: nef.File{ NEF: nef.File{
Header: nef.Header{ Header: nef.Header{
@ -47,6 +48,7 @@ func TestEncodeDecodeContractState(t *testing.T) {
Checksum: 0, Checksum: 0,
}, },
Manifest: *m, 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{
ContractBase: state.ContractBase{
ID: 0, ID: 0,
Hash: hash.Hash160(script), Hash: hash.Hash160(script),
NEF: newTestNEF(script), NEF: newTestNEF(script),
Manifest: *m, 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{
ContractBase: state.ContractBase{
ID: 0, ID: 0,
Hash: hash.Hash160(script), Hash: hash.Hash160(script),
NEF: newTestNEF(script), NEF: newTestNEF(script),
Manifest: *m, 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{
ContractBase: state.ContractBase{
ID: 0, ID: 0,
Hash: hash.Hash160(script), Hash: hash.Hash160(script),
NEF: newTestNEF(script), NEF: newTestNEF(script),
Manifest: *m, Manifest: *m,
},
} }
return cs return cs
}, },