-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
adl.go
124 lines (106 loc) · 2.22 KB
/
adl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/ipld/go-car"
"github.com/urfave/cli/v2"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
adt13 "github.com/filecoin-project/go-state-types/builtin/v13/util/adt"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors/adt"
)
var adlCmd = &cli.Command{
Name: "adl",
Usage: "adl manipulation commands",
Subcommands: []*cli.Command{
adlAmtCmd,
},
}
var adlAmtCmd = &cli.Command{
Name: "amt",
Usage: "AMT manipulation commands",
Subcommands: []*cli.Command{
adlAmtGetCmd,
},
}
var adlAmtGetCmd = &cli.Command{
Name: "get",
Usage: "Get an element from an AMT",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "car-file",
Usage: "write a car file with two hamts (use lotus-shed export-car)",
},
&cli.IntFlag{
Name: "bitwidth",
Usage: "bitwidth of the HAMT",
Value: 5,
},
&cli.StringFlag{
Name: "root",
Usage: "root cid of the HAMT",
},
&cli.Int64Flag{
Name: "key",
Usage: "key to get",
},
},
Action: func(cctx *cli.Context) error {
bs := blockstore.NewMemorySync()
f, err := os.Open(cctx.String("car-file"))
if err != nil {
return err
}
defer func(f *os.File) {
_ = f.Close()
}(f)
cr, err := car.NewCarReader(f)
if err != nil {
return err
}
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
if err := bs.Put(cctx.Context, blk); err != nil {
return err
}
}
root, err := cid.Parse(cctx.String("root"))
if err != nil {
return err
}
m, err := adt13.AsArray(adt.WrapStore(cctx.Context, cbor.NewCborStore(bs)), root, cctx.Int("bitwidth"))
if err != nil {
return err
}
var out cbg.Deferred
ok, err := m.Get(cctx.Uint64("key"), &out)
if err != nil {
return err
}
if !ok {
return xerrors.Errorf("no such element")
}
fmt.Printf("RAW: %x\n", out.Raw)
fmt.Println("----")
var i interface{}
if err := cbor.DecodeInto(out.Raw, &i); err == nil {
ij, err := json.MarshalIndent(i, "", " ")
if err != nil {
return err
}
fmt.Println(string(ij))
}
return nil
},
}