Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ONT-1347 fix ONT ID storage #659

Merged
merged 3 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 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 @@ -31,6 +33,8 @@ import (
"github.com/ontio/ontology/core/store/overlaydb"
"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 @@ -319,3 +323,52 @@ 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
}
defer db.Close()

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 if err == nil {
return errors.New("check ontid storage: invalid version flag")
} else {
return err
laizy marked this conversation as resolved.
Show resolved Hide resolved
}
}

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 {
return err
}

laizy marked this conversation as resolved.
Show resolved Hide resolved
tag := states.StorageItem{}
tag.Value = []byte{ontid.FLAG_VERSION}
buf := bytes.NewBuffer(nil)
tag.Serialize(buf)
db.BatchPut(flag, buf.Bytes())
err = db.BatchCommit()

return err
}
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
22 changes: 14 additions & 8 deletions smartcontract/service/native/ontid/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"github.com/ontio/ontology/smartcontract/service/native/utils"
)

const flag_exist = 0x01

func checkIDExistence(srvc *native.NativeService, encID []byte) bool {
val, err := srvc.CacheDB.Get(encID)
if err == nil {
Expand All @@ -45,26 +43,34 @@ func checkIDExistence(srvc *native.NativeService, encID []byte) bool {
}

const (
FIELD_PK byte = 1 + iota
FIELD_ATTR
FIELD_RECOVERY
flag_exist = 0x01

FIELD_VERSION byte = 0
FLAG_VERSION byte = 0x01

FIELD_PK byte = 1
FIELD_ATTR byte = 2
FIELD_RECOVERY byte = 3
)

func encodeID(id []byte) ([]byte, error) {
length := len(id)
if length == 0 || length > 255 {
return nil, errors.New("encode ONT ID error: invalid ID length")
}
enc := []byte{byte(length)}
//enc := []byte{byte(length)}
enc := append(utils.OntIDContractAddress[:], byte(length))
enc = append(enc, id...)
return enc, nil
}

func decodeID(data []byte) ([]byte, error) {
if len(data) == 0 || len(data) != int(data[0])+1 {
prefix := len(utils.OntIDContractAddress)
size := len(data)
if size < prefix || size != int(data[prefix])+1+prefix {
return nil, errors.New("decode ONT ID error: invalid data length")
}
return data[1:], nil
return data[prefix+1:], nil
}

func setRecovery(srvc *native.NativeService, encID []byte, recovery com.Address) error {
Expand Down