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)
require.NoError(t, err)
return &state.Contract{
Hash: ih,
NEF: *innerNef,
Manifest: *m,
ContractBase: state.ContractBase{
Hash: ih,
NEF: *innerNef,
Manifest: *m,
},
}, nil
} else if h.Equals(barH) {
barNef, err := nef.NewFile(barCtr)
require.NoError(t, err)
return &state.Contract{
Hash: barH,
NEF: *barNef,
Manifest: *mBar,
ContractBase: state.ContractBase{
Hash: barH,
NEF: *barNef,
Manifest: *mBar,
},
}, nil
}
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)
require.NoError(t, err)
contractState := &state.Contract{
NEF: *ne,
Hash: hash.Hash160(script),
Manifest: *m,
ID: 123,
ContractBase: state.ContractBase{
NEF: *ne,
Hash: hash.Hash160(script),
Manifest: *m,
ID: 123,
},
}
chain := newTestChain(t)

View file

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

View file

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

View file

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

View file

@ -18,11 +18,16 @@ import (
// Contract holds information about a smart contract in the NEO blockchain.
type Contract struct {
ID int32 `json:"id"`
UpdateCounter uint16 `json:"updatecounter"`
Hash util.Uint160 `json:"hash"`
NEF nef.File `json:"nef"`
Manifest manifest.Manifest `json:"manifest"`
ContractBase
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"`
NEF nef.File `json:"nef"`
Manifest manifest.Manifest `json:"manifest"`
}
// DecodeBinary implements Serializable interface.

View file

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

View file

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