Skip to content

Commit

Permalink
fix ontid storage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlverLyu committed Sep 26, 2018
1 parent ff1ee07 commit 63cb96d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
51 changes: 51 additions & 0 deletions core/store/ledgerstore/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package ledgerstore

import (
"bytes"
"errors"
"fmt"
"os"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/serialization"
Expand All @@ -30,6 +32,8 @@ import (
"github.com/ontio/ontology/core/store/leveldbstore"
"github.com/ontio/ontology/core/store/statestore"
"github.com/ontio/ontology/merkle"
"github.com/ontio/ontology/smartcontract/service/native/ontid"
"github.com/ontio/ontology/smartcontract/service/native/utils"
)

var (
Expand Down Expand Up @@ -310,3 +314,50 @@ func (self *StateStore) ClearAll() error {
func (self *StateStore) Close() error {
return self.store.Close()
}

func CheckStorage(dir string) error {
path := dir + string(os.PathSeparator) + DBDirState
db, err := leveldbstore.NewLevelDBStore(path)
if err != nil {
return err
}

prefix := append([]byte{byte(scom.ST_STORAGE)}, utils.OntIDContractAddress[:]...) //prefix of new storage key
flag := append(prefix, ontid.FIELD_VERSION)
val, err := db.Get(flag)
if err == nil {
item := &states.StorageItem{}
buf := bytes.NewBuffer(val)
err := item.Deserialize(buf)
if err == nil && item.Value[0] == ontid.FLAG_VERSION {
return nil
} else {
return err
}
}

prefix1 := []byte{byte(scom.ST_STORAGE), 0x2a, 0x64, 0x69, 0x64} //prefix of old storage key

iter := db.NewIterator(prefix1)
db.NewBatch()
for ok := iter.First(); ok; ok = iter.Next() {
key := append(prefix, iter.Key()[1:]...)
db.BatchPut(key, iter.Value())
db.BatchDelete(iter.Key())
}
iter.Release()
err := iter.Error()
if err != nil {
db.Close()
return err
}

tag := states.StorageItem{}
tag.Value = []byte{ontid.FLAG_VERSION}
buf := bytes.NewBuffer(nil)
tag.Serialize(buf)
db.BatchPut(flag, buf.Bytes())
db.BatchCommit()

return db.Close()
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/ontio/ontology/consensus"
"github.com/ontio/ontology/core/genesis"
"github.com/ontio/ontology/core/ledger"
"github.com/ontio/ontology/core/store/ledgerstore"
"github.com/ontio/ontology/events"
bactor "github.com/ontio/ontology/http/base/actor"
hserver "github.com/ontio/ontology/http/base/actor"
Expand Down Expand Up @@ -239,6 +240,10 @@ func initLedger(ctx *cli.Context) (*ledger.Ledger, error) {

var err error
dbDir := utils.GetStoreDirPath(config.DefConfig.Common.DataDir, config.DefConfig.P2PNode.NetworkName)
err = ledgerstore.CheckStorage(dbDir)
if err != nil {
return nil, fmt.Errorf("Check storage error: %s", err)
}
ledger.DefLedger, err = ledger.NewLedger(dbDir)
if err != nil {
return nil, fmt.Errorf("NewLedger error:%s", err)
Expand Down
3 changes: 1 addition & 2 deletions smartcontract/service/native/ontid/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
package ontid

import (
"bytes"
"encoding/hex"
"errors"

"github.com/ontio/ontology-crypto/keypair"
com "github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/states"
"github.com/ontio/ontology/core/store/common"
"github.com/ontio/ontology/core/store/leveldbstore"
"github.com/ontio/ontology/core/types"
"github.com/ontio/ontology/smartcontract/service/native"
"github.com/ontio/ontology/smartcontract/service/native/utils"
Expand All @@ -49,6 +47,7 @@ const (
flag_exist = 0x01

FIELD_VERSION byte = 0
FLAG_VERSION byte = 0x01

FIELD_PK byte = 1 + iota
FIELD_ATTR
Expand Down

0 comments on commit 63cb96d

Please sign in to comment.