-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(task): add actor, actor-state, and init actor processing (#14)
* feat(task): add actor and actor state processing * feat(task): add init actor processing
- Loading branch information
Showing
12 changed files
with
590 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"github.com/go-pg/pg/v10" | ||
) | ||
|
||
type Actor struct { | ||
ID string `pg:",pk,notnull"` | ||
StateRoot string `pg:",pk,notnull"` | ||
Code string `pg:",notnull"` | ||
Head string `pg:",notnull"` | ||
Balance string `pg:",notnull"` | ||
Nonce uint64 `pg:",use_zero"` | ||
} | ||
|
||
func (a *Actor) PersistWithTx(ctx context.Context, tx *pg.Tx) error { | ||
if _, err := tx.ModelContext(ctx, a). | ||
OnConflict("do nothing"). | ||
Insert(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type ActorState struct { | ||
Head string `pg:",pk,notnull"` | ||
Code string `pg:",pk,notnull"` | ||
State string `pg:",notnull"` | ||
} | ||
|
||
func (s *ActorState) PersistWithTx(ctx context.Context, tx *pg.Tx) error { | ||
if _, err := tx.ModelContext(ctx, s). | ||
OnConflict("do nothing"). | ||
Insert(); err != nil { | ||
return err | ||
} | ||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"github.com/go-pg/pg/v10" | ||
) | ||
|
||
type ActorTaskResult struct { | ||
Actor *Actor | ||
State *ActorState | ||
} | ||
|
||
func (a *ActorTaskResult) Persist(ctx context.Context, db *pg.DB) error { | ||
return db.RunInTransaction(ctx, func(tx *pg.Tx) error { | ||
if err := a.Actor.PersistWithTx(ctx, tx); err != nil { | ||
return err | ||
} | ||
if err := a.State.PersistWithTx(ctx, tx); err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package init | ||
|
||
import ( | ||
"context" | ||
"github.com/go-pg/pg/v10" | ||
) | ||
|
||
type IdAddress struct { | ||
ID string `pg:",pk,notnull"` | ||
Address string `pg:",pk,notnull"` | ||
StateRoot string `pg:",pk,notnull"` | ||
} | ||
|
||
func (ia *IdAddress) PersistWithTx(ctx context.Context, tx *pg.Tx) error { | ||
if _, err := tx.ModelContext(ctx, ia). | ||
OnConflict("do nothing"). | ||
Insert(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
type IdAddressList []*IdAddress | ||
|
||
func (ias IdAddressList) PersistWithTx(ctx context.Context, tx *pg.Tx) error { | ||
for _, ia := range ias { | ||
if err := ia.PersistWithTx(ctx, tx); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ias IdAddressList) Persist(ctx context.Context, db *pg.DB) error { | ||
return db.RunInTransaction(ctx, func(tx *pg.Tx) error { | ||
return ias.PersistWithTx(ctx, tx) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.