diff --git a/pkg/core/contract_state.go b/pkg/core/contract_state.go index 157e7dd60..cd3c8aea9 100644 --- a/pkg/core/contract_state.go +++ b/pkg/core/contract_state.go @@ -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{}) { diff --git a/pkg/core/contract_state_test.go b/pkg/core/contract_state_test.go index 276e34e5f..1507ec566 100644 --- a/pkg/core/contract_state_test.go +++ b/pkg/core/contract_state_test.go @@ -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) +}