diff --git a/core/store/ledgerstore/state_store.go b/core/store/ledgerstore/state_store.go index 3658aae70d..3e9e0206a8 100644 --- a/core/store/ledgerstore/state_store.go +++ b/core/store/ledgerstore/state_store.go @@ -20,7 +20,9 @@ package ledgerstore import ( "bytes" + "errors" "fmt" + "os" "github.com/ontio/ontology/common" "github.com/ontio/ontology/common/serialization" @@ -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 ( @@ -310,3 +314,62 @@ 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[:]...) + 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{0x2a, 0x64, 0x69, 0x64} + + type Item struct { + key []byte + value []byte + } + set := make([]Item, 0) + iter := db.NewIterator(append([]byte{byte(scom.ST_STORAGE)}, prefix1...)) + if !iter.First() { + iter.Release() + return errors.New("storage item not found") + } + for { + key := append([]byte(nil), iter.Key()...) + val := append([]byte(nil), iter.Value()...) + set = append(set, Item{key, val}) + if !iter.Next() { + break + } + } + iter.Release() + + db.NewBatch() + for _, i := range set { + key := append(prefix, i.key[1:]...) + db.BatchPut(key, i.value) + db.BatchDelete(i.key) + } + 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() +} diff --git a/main.go b/main.go index 98716b538b..c431afd3bb 100644 --- a/main.go +++ b/main.go @@ -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" @@ -239,6 +240,7 @@ func initLedger(ctx *cli.Context) (*ledger.Ledger, error) { var err error dbDir := utils.GetStoreDirPath(config.DefConfig.Common.DataDir, config.DefConfig.P2PNode.NetworkName) + ledgerstore.CheckStorage(dbDir) ledger.DefLedger, err = ledger.NewLedger(dbDir) if err != nil { return nil, fmt.Errorf("NewLedger error:%s", err) diff --git a/smartcontract/service/native/ontid/utils.go b/smartcontract/service/native/ontid/utils.go index 6ffa50b779..23e0bd87d2 100644 --- a/smartcontract/service/native/ontid/utils.go +++ b/smartcontract/service/native/ontid/utils.go @@ -18,7 +18,6 @@ package ontid import ( - "bytes" "encoding/hex" "errors" @@ -26,7 +25,6 @@ import ( 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" @@ -49,6 +47,7 @@ const ( flag_exist = 0x01 FIELD_VERSION byte = 0 + FLAG_VERSION byte = 0x01 FIELD_PK byte = 1 + iota FIELD_ATTR