Skip to content

Commit

Permalink
modify current block snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekRes authored and findbug2019 committed Apr 29, 2019
1 parent 842c195 commit 4dda0fc
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 608 deletions.
15 changes: 9 additions & 6 deletions accountmanager/accountmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/crypto"
"github.com/fractalplatform/fractal/params"
"github.com/fractalplatform/fractal/snapshot"
"github.com/fractalplatform/fractal/state"
"github.com/fractalplatform/fractal/types"
"github.com/fractalplatform/fractal/utils/rlp"
Expand Down Expand Up @@ -75,7 +76,7 @@ type IncAsset struct {

//AccountManager represents account management model.
type AccountManager struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}

Expand Down Expand Up @@ -385,7 +386,8 @@ func (am *AccountManager) GetAccountByTime(accountName common.Name, time uint64)
return nil, err
}

b, err := am.sdb.GetSnapshot(acctManagerName, acctInfoPrefix+strconv.FormatUint(accountID, 10), time)
snapshotManager := snapshot.NewSnapshotManager(am.sdb)
b, err := snapshotManager.GetSnapshotMsg(acctManagerName, acctInfoPrefix+strconv.FormatUint(accountID, 10), time)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -795,12 +797,13 @@ func (am *AccountManager) GetAccountLastChange(accountName common.Name) (uint64,
//GetSnapshotTime get snapshot time
//num = 0 current snapshot time , 1 preview snapshot time , 2 next snapshot time
func (am *AccountManager) GetSnapshotTime(num uint64, time uint64) (uint64, error) {
snapshotManager := snapshot.NewSnapshotManager(am.sdb)
if num == 0 {
return am.sdb.GetSnapshotLast()
return snapshotManager.GetLastSnapshotTime()
} else if num == 1 {
return am.sdb.GetSnapshotPrev(time)
return snapshotManager.GetPrevSnapshotTime(time)
} else if num == 2 {
t, err := am.sdb.GetSnapshotLast()
t, err := snapshotManager.GetLastSnapshotTime()
if err != nil {
return 0, err
}
Expand All @@ -809,7 +812,7 @@ func (am *AccountManager) GetSnapshotTime(num uint64, time uint64) (uint64, erro
return 0, ErrSnapshotTimeNotExist
} else {
for {
if t1, err := am.sdb.GetSnapshotPrev(t); err != nil {
if t1, err := snapshotManager.GetPrevSnapshotTime(t); err != nil {
return t, nil
} else if t1 <= time {
return t, nil
Expand Down
60 changes: 30 additions & 30 deletions accountmanager/accountmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestAccountManager_CreateAccount(t *testing.T) {

func TestAccountManager_AccountIsExist(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestAccountManager_AccountIsExist(t *testing.T) {

func TestAccountManager_AccountIsEmpty(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -260,7 +260,7 @@ func GeneragePubKey() (common.PubKey, *ecdsa.PrivateKey) {

func TestAccountManager_GetAccountByName(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
//pubkey2 := new(common.PubKey)
Expand Down Expand Up @@ -309,7 +309,7 @@ func TestAccountManager_GetAccountByName(t *testing.T) {

func TestAccountManager_SetAccount(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestAccountManager_SetAccount(t *testing.T) {

func TestAccountManager_SetNonce(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestAccountManager_SetNonce(t *testing.T) {

func TestAccountManager_GetNonce(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestAccountManager_GetNonce(t *testing.T) {
}
func TestAccountManager_DeleteAccountByName(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -528,7 +528,7 @@ func TestAccountManager_DeleteAccountByName(t *testing.T) {

func TestAccountManager_IsValidSign(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -562,7 +562,7 @@ func TestAccountManager_IsValidSign(t *testing.T) {

func TestAccountManager_GetAccountBalanceByID(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -619,7 +619,7 @@ func TestAccountManager_GetAccountBalanceByID(t *testing.T) {

func TestAccountManager_GetAssetInfoByName(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -659,7 +659,7 @@ func TestAccountManager_GetAssetInfoByName(t *testing.T) {

func TestAccountManager_GetAssetInfoByID(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestAccountManager_GetAssetInfoByID(t *testing.T) {

func TestAccountManager_GetAssetAmountByTime(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -769,7 +769,7 @@ func TestAccountManager_GetAssetAmountByTime(t *testing.T) {

func TestAccountManager_GetAccountLastChange(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -802,7 +802,7 @@ func TestAccountManager_GetAccountLastChange(t *testing.T) {

func TestAccountManager_GetSnapshotTime(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -836,7 +836,7 @@ func TestAccountManager_GetSnapshotTime(t *testing.T) {

func TestAccountManager_GetBalanceByTime(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -871,7 +871,7 @@ func TestAccountManager_GetBalanceByTime(t *testing.T) {

func TestAccountManager_GetAssetFounder(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -904,7 +904,7 @@ func TestAccountManager_GetAssetFounder(t *testing.T) {

func TestAccountManager_GetChargeRatio(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -937,7 +937,7 @@ func TestAccountManager_GetChargeRatio(t *testing.T) {

func TestAccountManager_GetAssetChargeRatio(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -970,7 +970,7 @@ func TestAccountManager_GetAssetChargeRatio(t *testing.T) {

func TestAccountManager_SubAccountBalanceByID(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1001,7 +1001,7 @@ func TestAccountManager_SubAccountBalanceByID(t *testing.T) {

func TestAccountManager_AddAccountBalanceByID(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1031,7 +1031,7 @@ func TestAccountManager_AddAccountBalanceByID(t *testing.T) {

func TestAccountManager_AddAccountBalanceByName(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1061,7 +1061,7 @@ func TestAccountManager_AddAccountBalanceByName(t *testing.T) {

func TestAccountManager_EnoughAccountBalance(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func TestAccountManager_EnoughAccountBalance(t *testing.T) {

func TestAccountManager_GetCode(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1176,7 +1176,7 @@ func TestAccountManager_GetCode(t *testing.T) {

func TestAccountManager_GetCodeSize(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1244,7 +1244,7 @@ func TestAccountManager_GetCodeSize(t *testing.T) {

func TestAccountManager_CanTransfer(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1281,7 +1281,7 @@ func TestAccountManager_CanTransfer(t *testing.T) {

func TestAccountManager_TransferAsset(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1328,7 +1328,7 @@ func TestAccountManager_TransferAsset(t *testing.T) {

func TestAccountManager_IssueAsset(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1403,7 +1403,7 @@ func TestAccountManager_IssueAsset(t *testing.T) {

func TestAccountManager_IncAsset2Acct(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1465,7 +1465,7 @@ func TestAccountManager_IncAsset2Acct(t *testing.T) {

func TestAccountManager_Process(t *testing.T) {
type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down Expand Up @@ -1667,7 +1667,7 @@ func TestAccountManager_Process(t *testing.T) {
func TestAccountManager_SubAccount(t *testing.T) {

type fields struct {
sdb SdbIf
sdb *state.StateDB
ast *asset.Asset
}
type args struct {
Expand Down
8 changes: 4 additions & 4 deletions accountmanager/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ type IAccountManager interface {
type SdbIf interface {
Put(account string, key string, value []byte)
Get(account string, key string) ([]byte, error)
GetSnapshot(accountName string, key string, time uint64) ([]byte, error)
GetSnapshotLast() (uint64, error)
GetSnapshotPrev(time uint64) (uint64, error)
Snapshot() int
// GetSnapshot(accountName string, key string, time uint64) ([]byte, error)
// GetSnapshotLast() (uint64, error)
// GetSnapshotPrev(time uint64) (uint64, error)
// Snapshot() int
RevertToSnapshot(revid int)
}

Expand Down
5 changes: 4 additions & 1 deletion asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/ethereum/go-ethereum/log"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/snapshot"
"github.com/fractalplatform/fractal/state"
"github.com/fractalplatform/fractal/utils/rlp"
)
Expand Down Expand Up @@ -97,7 +98,9 @@ func (a *Asset) GetAssetObjectByTime(assetID uint64, time uint64) (*AssetObject,
if assetID == 0 {
return nil, ErrAssetIdInvalid
}
b, err := a.sdb.GetSnapshot(assetManagerName, assetObjectPrefix+strconv.FormatUint(assetID, 10), time)

snapshotManager := snapshot.NewSnapshotManager(a.sdb)
b, err := snapshotManager.GetSnapshotMsg(assetManagerName, assetObjectPrefix+strconv.FormatUint(assetID, 10), time)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4dda0fc

Please sign in to comment.