core: add put/delete functions for ContractState

These are gonna be used by interops.
This commit is contained in:
Roman Khimov 2019-10-11 13:11:54 +03:00
parent 58d9b79fe3
commit 2156d5db73
2 changed files with 42 additions and 0 deletions

View file

@ -75,6 +75,23 @@ func (a *ContractState) EncodeBinary(bw *io.BinWriter) {
bw.WriteString(a.Description)
}
// putContractStateIntoStore puts given contract state into the given store.
func putContractStateIntoStore(s storage.Store, cs *ContractState) error {
buf := io.NewBufBinWriter()
cs.EncodeBinary(buf.BinWriter)
if buf.Err != nil {
return buf.Err
}
key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().Bytes())
return s.Put(key, buf.Bytes())
}
// deleteContractStateInStore deletes given contract state in the given store.
func deleteContractStateInStore(s storage.Store, hash util.Uint160) error {
key := storage.AppendPrefix(storage.STContract, hash.Bytes())
return s.Delete(key)
}
// ScriptHash returns a contract script hash.
func (a *ContractState) ScriptHash() util.Uint160 {
if a.scriptHash.Equals(util.Uint160{}) {

View file

@ -3,6 +3,7 @@ package core
import (
"testing"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/smartcontract"
@ -50,3 +51,27 @@ func TestContractStateProperties(t *testing.T) {
assert.Equal(t, false, nonFlaggedContract.HasDynamicInvoke())
assert.Equal(t, false, nonFlaggedContract.IsPayable())
}
func TestPutGetDeleteContractState(t *testing.T) {
s := storage.NewMemoryStore()
script := []byte("testscript")
contract := &ContractState{
Script: script,
ParamList: []smartcontract.ParamType{smartcontract.StringType, smartcontract.IntegerType, smartcontract.Hash160Type},
ReturnType: smartcontract.BoolType,
Properties: smartcontract.HasStorage,
Name: "Contracto",
CodeVersion: "1.0.0",
Author: "Joe Random",
Email: "joe@example.com",
Description: "Test contract",
}
assert.NoError(t, putContractStateIntoStore(s, contract))
csRead := getContractStateFromStore(s, contract.ScriptHash())
assert.NotNil(t, csRead)
assert.Equal(t, contract, csRead)
assert.NoError(t, deleteContractStateInStore(s, contract.ScriptHash()))
csRead2 := getContractStateFromStore(s, contract.ScriptHash())
assert.Nil(t, csRead2)
}