2020-07-14 20:02:44 +00:00
|
|
|
package processor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-18 20:43:14 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-09-28 19:48:08 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
|
2020-09-28 20:13:18 +00:00
|
|
|
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
|
2020-09-28 19:48:08 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2020-09-18 20:43:14 +00:00
|
|
|
_init "github.com/filecoin-project/lotus/chain/actors/builtin/init"
|
2020-08-12 23:50:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/events/state"
|
2020-08-13 18:14:58 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
cw_util "github.com/filecoin-project/lotus/cmd/lotus-chainwatch/util"
|
2020-07-14 20:02:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Processor) setupCommonActors() error {
|
|
|
|
tx, err := p.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tx.Exec(`
|
|
|
|
create table if not exists id_address_map
|
|
|
|
(
|
|
|
|
id text not null,
|
|
|
|
address text not null,
|
|
|
|
constraint id_address_map_pk
|
|
|
|
primary key (id, address)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index if not exists id_address_map_id_uindex
|
|
|
|
on id_address_map (id);
|
|
|
|
|
|
|
|
create unique index if not exists id_address_map_address_uindex
|
|
|
|
on id_address_map (address);
|
|
|
|
|
|
|
|
create table if not exists actors
|
|
|
|
(
|
|
|
|
id text not null
|
|
|
|
constraint id_address_map_actors_id_fk
|
|
|
|
references id_address_map (id),
|
|
|
|
code text not null,
|
|
|
|
head text not null,
|
|
|
|
nonce int not null,
|
|
|
|
balance text not null,
|
|
|
|
stateroot text
|
|
|
|
);
|
|
|
|
|
|
|
|
create index if not exists actors_id_index
|
|
|
|
on actors (id);
|
|
|
|
|
|
|
|
create index if not exists id_address_map_address_index
|
|
|
|
on id_address_map (address);
|
|
|
|
|
|
|
|
create index if not exists id_address_map_id_index
|
|
|
|
on id_address_map (id);
|
|
|
|
|
|
|
|
create or replace function actor_tips(epoch bigint)
|
|
|
|
returns table (id text,
|
|
|
|
code text,
|
|
|
|
head text,
|
|
|
|
nonce int,
|
|
|
|
balance text,
|
|
|
|
stateroot text,
|
|
|
|
height bigint,
|
|
|
|
parentstateroot text) as
|
|
|
|
$body$
|
|
|
|
select distinct on (id) * from actors
|
|
|
|
inner join state_heights sh on sh.parentstateroot = stateroot
|
|
|
|
where height < $1
|
|
|
|
order by id, height desc;
|
|
|
|
$body$ language sql;
|
|
|
|
|
|
|
|
create table if not exists actor_states
|
|
|
|
(
|
|
|
|
head text not null,
|
|
|
|
code text not null,
|
|
|
|
state json not null
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index if not exists actor_states_head_code_uindex
|
|
|
|
on actor_states (head, code);
|
|
|
|
|
|
|
|
create index if not exists actor_states_head_index
|
|
|
|
on actor_states (head);
|
|
|
|
|
|
|
|
create index if not exists actor_states_code_head_index
|
|
|
|
on actor_states (head, code);
|
|
|
|
|
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Processor) HandleCommonActorsChanges(ctx context.Context, actors map[cid.Cid]ActorTips) error {
|
|
|
|
if err := p.storeActorAddresses(ctx, actors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
grp, _ := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
grp.Go(func() error {
|
|
|
|
if err := p.storeActorHeads(actors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
grp.Go(func() error {
|
|
|
|
if err := p.storeActorStates(actors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return grp.Wait()
|
|
|
|
}
|
|
|
|
|
2020-08-12 23:50:00 +00:00
|
|
|
type UpdateAddresses struct {
|
|
|
|
Old state.AddressPair
|
|
|
|
New state.AddressPair
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:02:44 +00:00
|
|
|
func (p Processor) storeActorAddresses(ctx context.Context, actors map[cid.Cid]ActorTips) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2020-07-16 14:45:31 +00:00
|
|
|
log.Debugw("Stored Actor Addresses", "duration", time.Since(start).String())
|
2020-07-14 20:02:44 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
addressToID := map[address.Address]address.Address{}
|
|
|
|
// HACK until genesis storage is figured out:
|
2020-09-28 20:13:18 +00:00
|
|
|
addressToID[builtin2.SystemActorAddr] = builtin2.SystemActorAddr
|
|
|
|
addressToID[builtin2.InitActorAddr] = builtin2.InitActorAddr
|
|
|
|
addressToID[builtin2.RewardActorAddr] = builtin2.RewardActorAddr
|
|
|
|
addressToID[builtin2.CronActorAddr] = builtin2.CronActorAddr
|
|
|
|
addressToID[builtin2.StoragePowerActorAddr] = builtin2.StoragePowerActorAddr
|
|
|
|
addressToID[builtin2.StorageMarketActorAddr] = builtin2.StorageMarketActorAddr
|
|
|
|
addressToID[builtin2.VerifiedRegistryActorAddr] = builtin2.VerifiedRegistryActorAddr
|
|
|
|
addressToID[builtin2.BurntFundsActorAddr] = builtin2.BurntFundsActorAddr
|
|
|
|
initActor, err := p.node.StateGetActor(ctx, builtin2.InitActorAddr, types.EmptyTSK)
|
2020-08-13 18:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 20:43:14 +00:00
|
|
|
initActorState, err := _init.Load(cw_util.NewAPIIpldStore(ctx, p.node), initActor)
|
2020-08-13 18:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// gross..
|
2020-09-18 20:43:14 +00:00
|
|
|
if err := initActorState.ForEachActor(func(id abi.ActorID, addr address.Address) error {
|
|
|
|
idAddr, err := address.NewIDAddress(uint64(id))
|
2020-08-13 18:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-18 20:43:14 +00:00
|
|
|
addressToID[addr] = idAddr
|
2020-08-13 18:14:58 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2020-07-14 20:02:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx, err := p.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := tx.Exec(`
|
|
|
|
create temp table iam (like id_address_map excluding constraints) on commit drop;
|
|
|
|
`); err != nil {
|
|
|
|
return xerrors.Errorf("prep temp: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(`copy iam (id, address) from STDIN `)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for a, i := range addressToID {
|
|
|
|
if i == address.Undef {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := stmt.Exec(
|
|
|
|
i.String(),
|
|
|
|
a.String(),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := stmt.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-13 18:14:58 +00:00
|
|
|
// HACK until chain watch can handle reorgs we need to update this table when ID -> PubKey mappings change
|
|
|
|
if _, err := tx.Exec(`insert into id_address_map select * from iam on conflict (id) do update set address = EXCLUDED.address`); err != nil {
|
|
|
|
log.Warnw("Failed to update id_address_map table, this is a known issue")
|
|
|
|
return nil
|
2020-07-14 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Processor) storeActorHeads(actors map[cid.Cid]ActorTips) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2020-07-16 14:45:31 +00:00
|
|
|
log.Debugw("Stored Actor Heads", "duration", time.Since(start).String())
|
2020-07-14 20:02:44 +00:00
|
|
|
}()
|
|
|
|
// Basic
|
|
|
|
tx, err := p.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
2020-08-17 21:31:17 +00:00
|
|
|
create temp table a_tmp (like actors excluding constraints) on commit drop;
|
2020-07-14 20:02:44 +00:00
|
|
|
`); err != nil {
|
|
|
|
return xerrors.Errorf("prep temp: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:31:17 +00:00
|
|
|
stmt, err := tx.Prepare(`copy a_tmp (id, code, head, nonce, balance, stateroot) from stdin `)
|
2020-07-14 20:02:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for code, actTips := range actors {
|
2020-08-14 02:59:30 +00:00
|
|
|
actorName := code.String()
|
2020-08-17 21:36:00 +00:00
|
|
|
if builtin.IsBuiltinActor(code) {
|
|
|
|
actorName = builtin.ActorNameByCode(code)
|
2020-08-14 02:59:30 +00:00
|
|
|
}
|
2020-07-14 20:02:44 +00:00
|
|
|
for _, actorInfo := range actTips {
|
|
|
|
for _, a := range actorInfo {
|
2020-08-14 02:59:30 +00:00
|
|
|
if _, err := stmt.Exec(a.addr.String(), actorName, a.act.Head.String(), a.act.Nonce, a.act.Balance.String(), a.stateroot.String()); err != nil {
|
2020-07-14 20:02:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stmt.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:31:17 +00:00
|
|
|
if _, err := tx.Exec(`insert into actors select * from a_tmp on conflict do nothing `); err != nil {
|
2020-07-14 20:02:44 +00:00
|
|
|
return xerrors.Errorf("actor put: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Processor) storeActorStates(actors map[cid.Cid]ActorTips) error {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2020-07-16 14:45:31 +00:00
|
|
|
log.Debugw("Stored Actor States", "duration", time.Since(start).String())
|
2020-07-14 20:02:44 +00:00
|
|
|
}()
|
|
|
|
// States
|
|
|
|
tx, err := p.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(`
|
2020-08-17 21:31:17 +00:00
|
|
|
create temp table as_tmp (like actor_states excluding constraints) on commit drop;
|
2020-07-14 20:02:44 +00:00
|
|
|
`); err != nil {
|
|
|
|
return xerrors.Errorf("prep temp: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:31:17 +00:00
|
|
|
stmt, err := tx.Prepare(`copy as_tmp (head, code, state) from stdin `)
|
2020-07-14 20:02:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for code, actTips := range actors {
|
2020-08-14 02:59:30 +00:00
|
|
|
actorName := code.String()
|
2020-08-17 21:36:00 +00:00
|
|
|
if builtin.IsBuiltinActor(code) {
|
|
|
|
actorName = builtin.ActorNameByCode(code)
|
2020-08-14 02:59:30 +00:00
|
|
|
}
|
2020-07-14 20:02:44 +00:00
|
|
|
for _, actorInfo := range actTips {
|
|
|
|
for _, a := range actorInfo {
|
2020-08-14 02:59:30 +00:00
|
|
|
if _, err := stmt.Exec(a.act.Head.String(), actorName, a.state); err != nil {
|
2020-07-14 20:02:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stmt.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:31:17 +00:00
|
|
|
if _, err := tx.Exec(`insert into actor_states select * from as_tmp on conflict do nothing `); err != nil {
|
2020-07-14 20:02:44 +00:00
|
|
|
return xerrors.Errorf("actor put: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|