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

Feature/start with legacy data #91

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
57 changes: 56 additions & 1 deletion cmd/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,37 @@ func KeeperBootCmd(c *cli.Context) error {
return err
}
defer kd.Close()

var fromStart bool
legacyDB := c.String("legacy")
if legacyDB != "" {
req, err := kd.ReadLatestRequest(ctx)
if err != nil {
return err
}
actions, err := db.ListActions(ctx, mtg.ActionStateDone, 1)
if err != nil {
return err
}
if req == nil && len(actions) == 0 {
ld, err := keeper.OpenSQLite3StoreLegacy(legacyDB)
if err != nil {
return err
}
err = kd.ImportBackup(ctx, ld)
if err != nil {
return err
}
err = ld.Close()
if err != nil {
return err
}
fromStart = true
}
}

keeper := keeper.NewNode(kd, group, mc.Keeper, mc.Signer.MTG, client)
keeper.Boot(ctx)
keeper.Boot(ctx, fromStart)

if mmc := mc.Keeper.MonitorConversaionId; mmc != "" {
go MonitorKeeper(ctx, db, kd, mc.Keeper, group, mmc, version)
Expand Down Expand Up @@ -98,3 +127,29 @@ func KeeperFundRequest(c *cli.Context) error {
traceId := uuid.Must(uuid.NewV4()).String()
return makeKeeperPaymentRequest(c.String("config"), assetId, amount, traceId, "")
}

func KeeperExportLegacyData(c *cli.Context) error {
ctx := context.Background()

input := c.String("database")
if input == "" {
return fmt.Errorf("empty path of legacy database")
}
path := c.String("export")
if path == "" {
return fmt.Errorf("empty path of export database")
}

ld, err := keeper.OpenSQLite3StoreLegacy(input)
if err != nil {
return err
}
defer ld.Close()
sd, err := keeper.OpenSQLite3Store(path)
if err != nil {
return err
}
defer sd.Close()

return sd.ImportBackup(ctx, ld)
}
58 changes: 56 additions & 2 deletions cmd/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,40 @@ func SignerBootCmd(c *cli.Context) error {
return err
}

kd, err := signer.OpenSQLite3Store(mc.Signer.StoreDir + "/mpc.sqlite3")
kd, err := signer.OpenSQLite3Store(mc.Signer.StoreDir+"/mpc.sqlite3", false)
if err != nil {
return err
}
defer kd.Close()

var fromStart bool
legacyDB := c.String("legacy")
if legacyDB != "" {
state, err := kd.SessionsState(ctx)
if err != nil {
return err
}
actions, err := db.ListActions(ctx, mtg.ActionStateDone, 1)
if err != nil {
return err
}
if state.Done == 0 && len(actions) == 0 {
ld, err := signer.OpenSQLite3Store(legacyDB, true)
if err != nil {
return err
}
err = kd.ImportBackup(ctx, ld)
if err != nil {
return err
}
err = ld.Close()
if err != nil {
return err
}
fromStart = true
}
}

s := &mixin.Keystore{
ClientID: mc.Signer.MTG.App.AppId,
SessionID: mc.Signer.MTG.App.SessionId,
Expand All @@ -82,7 +110,7 @@ func SignerBootCmd(c *cli.Context) error {
mc.Signer.MTG.App.SpendPrivateKey = key.String()

node := signer.NewNode(kd, group, messenger, mc.Signer, mc.Keeper.MTG, client)
node.Boot(ctx)
node.Boot(ctx, fromStart)

if mmc := mc.Signer.MonitorConversaionId; mmc != "" {
go MonitorSigner(ctx, db, kd, mc.Signer, group, mmc, version)
Expand Down Expand Up @@ -190,3 +218,29 @@ func makeSignerPaymentRequest(conf *signer.Configuration, op *common.Operation,
qrterminal.GenerateHalfBlock(url, qrterminal.H, os.Stdout)
return nil
}

func SignerExportLegacyData(c *cli.Context) error {
ctx := context.Background()

input := c.String("database")
if input == "" {
return fmt.Errorf("empty path of legacy database")
}
path := c.String("export")
if path == "" {
return fmt.Errorf("empty path of export database")
}

ld, err := signer.OpenSQLite3Store(input, true)
if err != nil {
return err
}
defer ld.Close()
sd, err := signer.OpenSQLite3Store(path, false)
if err != nil {
return err
}
defer sd.Close()

return sd.ImportBackup(ctx, ld)
}
1 change: 1 addition & 0 deletions common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Request struct {
ExtraHEX string
State uint8
CreatedAt time.Time
UpdatedAt time.Time
Sequence uint64

Output *mtg.Action
Expand Down
6 changes: 5 additions & 1 deletion keeper/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ type Configuration struct {
MTG *mtg.Configuration `toml:"mtg"`
}

func OpenSQLite3StoreLegacy(path string) (*store.SQLite3Store, error) {
return store.OpenSQLite3Store(path, true)
}

func OpenSQLite3Store(path string) (*store.SQLite3Store, error) {
return store.OpenSQLite3Store(path)
return store.OpenSQLite3Store(path, false)
}

func OpenSQLite3ReadOnlyStore(path string) (*store.SQLite3Store, error) {
Expand Down
10 changes: 6 additions & 4 deletions keeper/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ func NewNode(store *store.SQLite3Store, group *mtg.Group, conf *Configuration, s
return node
}

func (node *Node) Boot(ctx context.Context) {
func (node *Node) Boot(ctx context.Context, fromStart bool) {
terminated, err := node.store.ReadTerminate(ctx)
if err != nil || terminated {
panic(err)
}
err = node.Migrate(ctx)
if err != nil {
panic(err)
if !fromStart {
err = node.Migrate(ctx)
if err != nil {
panic(err)
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions keeper/store/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ type SafeBalance struct {
UpdatedAt time.Time
}

func BalancFromRow(row *sql.Rows) (*SafeBalance, error) {
var sb SafeBalance
err := row.Scan(&sb.Address, &sb.AssetId, &sb.AssetAddress, &sb.balance, &sb.LatestTxHash, &sb.UpdatedAt)
if err != nil {
return nil, err
}
return &sb, nil
}

func (sb *SafeBalance) UpdateBalance(change *big.Int) {
balance := new(big.Int).Add(sb.BigBalance(), change)
if balance.Sign() < 0 {
Expand Down
Loading
Loading