Skip to content

Commit

Permalink
fix bug: find method ignore current db cache data (DNAProject#20)
Browse files Browse the repository at this point in the history
* fix bug: find method ignore current db cache data

* set amount is nil default value

* Add test case
  • Loading branch information
tanZiWen authored and laizy committed Apr 17, 2018
1 parent 521beb2 commit 4083876
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/store/common/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type MemoryCacheStore interface {
Delete(prefix byte, key []byte)
//Get all updated key-value set
GetChangeSet() map[string]*StateItem
// Get all key-value in store
Find() []*StateItem
}

//EventStore save event notify
Expand Down Expand Up @@ -108,7 +110,7 @@ type StateItem struct {
Trie bool //no use
}

func (e *StateItem) copy() *StateItem {
func (e *StateItem) Copy() *StateItem {
c := *e
return &c
}
8 changes: 8 additions & 0 deletions core/store/statestore/memory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func (db *MemoryStore) Delete(prefix byte, key []byte) {

}

func (db *MemoryStore) Find() []*common.StateItem {
var memory []*common.StateItem
for _, v := range db.memory {
memory = append(memory, v)
}
return memory
}

func (db *MemoryStore) GetChangeSet() map[string]*common.StateItem {
m := make(map[string]*common.StateItem)
for k, v := range db.memory {
Expand Down
23 changes: 17 additions & 6 deletions core/store/statestore/state_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ontio/ontology/core/states"
"github.com/ontio/ontology/core/store/common"
"github.com/syndtr/goleveldb/leveldb"
"strings"
)

type StateBatch struct {
Expand All @@ -42,15 +43,25 @@ func NewStateStoreBatch(memoryStore common.MemoryCacheStore, store common.Persis

func (self *StateBatch) Find(prefix common.DataEntryPrefix, key []byte) ([]*common.StateItem, error) {
var states []*common.StateItem
iter := self.store.NewIterator(append([]byte{byte(prefix)}, key...))
bp := []byte{byte(prefix)}
iter := self.store.NewIterator(append(bp, key...))
for iter.Next() {
key := iter.Key()
value := iter.Value()
state, err := getStateObject(prefix, value)
if err != nil {
return nil, err
keyV := key[1:]
if self.memoryStore.Get(byte(prefix), keyV) == nil {
value := iter.Value()
state, err := getStateObject(prefix, value)
if err != nil {
return nil, err
}
states = append(states, &common.StateItem{Key: string(keyV), Value: state})
}
}
keyP := string(append(bp, key...))
for _, v := range self.memoryStore.Find() {
if v.State != common.Deleted && strings.HasPrefix(v.Key, keyP) {
states = append(states, v.Copy())
}
states = append(states, &common.StateItem{Key: string(key[1:]), Value: state})
}
return states, nil
}
Expand Down
3 changes: 3 additions & 0 deletions smartcontract/service/native/states/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (this *State) Serialize(w io.Writer) error {
if err := this.To.Serialize(w); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[State] Serialize To error!")
}
if this.Value == nil {
this.Value = new(big.Int)
}
if err := serialization.WriteVarBytes(w, this.Value.Bytes()); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[State] Serialize Value error!")
}
Expand Down
3 changes: 3 additions & 0 deletions smartcontract/service/native/states/transfer_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (this *TransferFrom) Serialize(w io.Writer) error {
if err := this.To.Serialize(w); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[TransferFrom] Serialize to error!")
}
if this.Value == nil {
this.Value = new(big.Int)
}
if err := serialization.WriteVarBytes(w, this.Value.Bytes()); err != nil {
return errors.NewDetailErr(err, errors.ErrNoCode, "[TransferFrom] Serialize value error!")
}
Expand Down
40 changes: 40 additions & 0 deletions smartcontract/service/native/states/transfer_from_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package states

import (
"testing"
"github.com/ontio/ontology/common"
"bytes"
"math/big"
)

func TestTransferFrom_Serialize_Deserialize(t *testing.T) {
se := "011aac8bb717039d78e9840de35e82ba62723594"
fr := "011aac8bb717039d78e9840de35e82ba62723594"
to := "011aac8bb717039d78e9840de35e82ba62723594"

sb, _ := common.HexToBytes(se)
fb, _ := common.HexToBytes(fr)
tb, _ := common.HexToBytes(to)

sa, _ := common.AddressParseFromBytes(sb)
fa, _ := common.AddressParseFromBytes(fb)
ta, _ := common.AddressParseFromBytes(tb)

tf := &TransferFrom{
Version: 0,
Sender: sa,
From: fa,
To: ta,
Value: big.NewInt(1),
}

bf := new(bytes.Buffer)
if err := tf.Serialize(bf); err != nil {
t.Fatalf("TransferFrom serialize error: %v", err)
}

trf := new(TransferFrom)
if err := trf.Deserialize(bf); err != nil {
t.Fatalf("TransferFrom deserialize error: %v", err)
}
}
69 changes: 69 additions & 0 deletions smartcontract/service/native/states/transfer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package states

import (
"testing"
"github.com/ontio/ontology/common"
"math/big"
"bytes"
)

func TestState_Serialize_Deserialize(t *testing.T) {
fr := "011aac8bb717039d78e9840de35e82ba62723594"
to := "011aac8bb717039d78e9840de35e82ba62723595"

fb, _ := common.HexToBytes(fr)
tb, _ := common.HexToBytes(to)

fa, _ := common.AddressParseFromBytes(fb)
ta, _ := common.AddressParseFromBytes(tb)

s := &State{
Version: 0,
From: fa,
To: ta,
Value: big.NewInt(1),
}

bf := new(bytes.Buffer)
if err := s.Serialize(bf); err != nil {
t.Fatalf("State serialize error: %v", err)
}

st := new(State)
if err := st.Deserialize(bf); err != nil {
t.Fatalf("State deserialize error: %v", err)
}
}

func TestTransfers_Serialize_Deserialize(t *testing.T) {
fr := "011aac8bb717039d78e9840de35e82ba62723594"
to := "011aac8bb717039d78e9840de35e82ba62723595"

fb, _ := common.HexToBytes(fr)
tb, _ := common.HexToBytes(to)

fa, _ := common.AddressParseFromBytes(fb)
ta, _ := common.AddressParseFromBytes(tb)

s := &State{
Version: 0,
From: fa,
To: ta,
Value: big.NewInt(1),
}

ts := &Transfers{
Version: 0,
States: []*State{s},
}

bf := new(bytes.Buffer)
if err := ts.Serialize(bf); err != nil {
t.Fatalf("Transfers serialize error: %v", err)
}

trs := new(Transfers)
if err := trs.Deserialize(bf); err != nil {
t.Fatalf("Transfers deserialize error: %v", err)
}
}
34 changes: 34 additions & 0 deletions smartcontract/states/contract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package states

import (
"bytes"
"testing"

"github.com/ontio/ontology/smartcontract/types"
)

func TestContract_Serialize_Deserialize(t *testing.T) {
vmcode := types.VmCode{
VmType: types.Native,
Code: []byte{1},
}

addr := vmcode.AddressFromVmCode()

c := &Contract{
Version: 0,
Code: []byte{1},
Address: addr,
Method: "init",
Args: []byte{2},
}
bf := new(bytes.Buffer)
if err := c.Serialize(bf); err != nil {
t.Fatalf("Contract serialize error: %v", err)
}

v := new(Contract)
if err := v.Deserialize(bf); err != nil {
t.Fatalf("Contract deserialize error: %v", err)
}
}

0 comments on commit 4083876

Please sign in to comment.