Skip to content

Instantly share code, notes, and snippets.

@dtynn
Last active January 16, 2020 10:27
Show Gist options
  • Save dtynn/94bc060260515146db332fa2f5622184 to your computer and use it in GitHub Desktop.
Save dtynn/94bc060260515146db332fa2f5622184 to your computer and use it in GitHub Desktop.
maybe a amt deletion bug?
package main
import (
"bytes"
"context"
"log"
"math/rand"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-amt-ipld"
cid "github.com/ipfs/go-cid"
hamt "github.com/ipfs/go-hamt-ipld"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/vm"
)
var _ amt.Blocks = (*mockStore)(nil)
type mockStore struct {
local *hamt.CborIpldStore
remote api.FullNode
}
func (ms *mockStore) Get(c cid.Cid, v cbg.CBORUnmarshaler) error {
err := ms.local.Get(context.Background(), c, v)
if err == nil {
return nil
}
b, err := ms.remote.ChainReadObj(context.Background(), c)
if err != nil {
return err
}
return v.UnmarshalCBOR(bytes.NewReader(b))
}
func (ms *mockStore) Put(v cbg.CBORMarshaler) (cid.Cid, error) {
return ms.local.Put(context.Background(), v)
}
func main() {
// use endpoint of any online lotus instance here
cli, closer, err := client.NewFullNodeRPC("ws://127.0.0.1:1234/rpc/v0", nil)
if err != nil {
log.Fatalln(err)
}
defer closer()
// chose any miner actor address, here we use t01475
actorAddr, _ := address.NewIDAddress(1475)
actor, err := cli.StateGetActor(context.Background(), actorAddr, nil)
if err != nil {
log.Fatalln("get actor", err)
}
log.Printf("actor: %v", actor)
obj, err := cli.ChainReadObj(context.Background(), actor.Head)
if err != nil {
log.Fatalln("chain read obj", err)
}
state, err := vm.DumpActorState(actor.Code, obj)
if err != nil {
log.Fatalln("dump actor state", err)
}
minerState := state.(actors.StorageMinerActorState)
blockstore := &mockStore{
local: hamt.NewCborStore(),
remote: cli,
}
provSets, err := cli.StateMinerProvingSet(context.Background(), actorAddr, nil)
if err != nil {
log.Fatalln("proving set", err)
}
sids := make([]uint64, 0, len(provSets))
for _, ps := range provSets {
sids = append(sids, ps.SectorID)
}
log.Println("on chain proving set count", len(sids))
log.Println("sectors state cid", minerState.Sectors)
root, err := amt.LoadAMT(blockstore, minerState.Sectors)
if err != nil {
log.Fatalln("load root", err)
}
choices := make([]uint64, len(sids))
copy(choices, sids)
try := len(sids) / 20
log.Println("expected to delete", try, "items")
deleted := 0
for ti := 0; ti < try; ti++ {
ci := rand.Intn(len(choices))
if err := root.Delete(choices[ci]); err != nil {
log.Println("fail to delete", choices[ci], err)
} else {
deleted++
}
choices[ci], choices[len(choices)-1] = choices[len(choices)-1], choices[ci]
choices = choices[:len(choices)-1]
}
log.Println("choises left", len(choices))
log.Println("we actually did", deleted, "deletions")
ncid, err := root.Flush()
if err != nil {
log.Fatalln("root flush", err)
}
log.Println("new root cid", ncid)
newRoot, err := amt.LoadAMT(blockstore, ncid)
if err != nil {
log.Fatalln("load new amt root", err)
}
// use https://github.com/filecoin-project/lotus/blob/master/chain/stmgr/utils.go#L289-L313
ssids := make([]uint64, 0, len(sids))
if err := newRoot.ForEach(func(i uint64, v *cbg.Deferred) error {
var comms [][]byte
if err := cbor.DecodeInto(v.Raw, &comms); err != nil {
return err
}
ssids = append(ssids, i)
return nil
}); err != nil {
log.Fatalln("decode into new set of sids")
}
log.Println("new state count", len(ssids))
}
@dtynn
Copy link
Author

dtynn commented Jan 16, 2020

2020/01/16 18:23:50 actor: &{bafkqac3gnfwc6mjpnvuw4zls bafy2bzacecs6yfgjmmuwilshzavxddy5vvs6jvju5wfsb5j2fgqrs5sweurza 0 1456417521132327522}
2020/01/16 18:23:50 on chain proving set count 1226
2020/01/16 18:23:50 sectors state cid bafy2bzacea4zmb6d5pk3sbekdymsmupshdhg2xjw4lvisykeajyr5ohwlr4oo
2020/01/16 18:23:50 expected to delete 61 items
2020/01/16 18:23:55 choises left 1165
2020/01/16 18:23:55 we actually did 61 deletions
2020/01/16 18:23:55 new root cid bafy2bzacecjgpybk37omxpwc64guzkth2ljowyhkv3tk57eckptwtju7juke4
2020/01/16 18:24:29 new state count 1194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment