|
| 1 | +package testutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "testing" |
| 8 | + |
| 9 | + graphsync "github.com/filecoin-project/boost-graphsync" |
| 10 | + _ "github.com/ipld/go-ipld-prime/codec/raw" |
| 11 | + |
| 12 | + blocks "github.com/ipfs/go-block-format" |
| 13 | + "github.com/ipfs/go-cid" |
| 14 | + "github.com/ipld/go-ipld-prime" |
| 15 | + "github.com/ipld/go-ipld-prime/codec/dagjson" |
| 16 | + "github.com/ipld/go-ipld-prime/datamodel" |
| 17 | + "github.com/ipld/go-ipld-prime/fluent/qp" |
| 18 | + "github.com/ipld/go-ipld-prime/linking" |
| 19 | + cidlink "github.com/ipld/go-ipld-prime/linking/cid" |
| 20 | + "github.com/ipld/go-ipld-prime/node/basicnode" |
| 21 | + "github.com/multiformats/go-multihash" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +// roughly duplicated in github.com/ipld/go-trustless-utils/testutil |
| 26 | + |
| 27 | +type TestIdentityDAG struct { |
| 28 | + t testing.TB |
| 29 | + loader ipld.BlockReadOpener |
| 30 | + |
| 31 | + RootLink ipld.Link |
| 32 | + AllLinks []ipld.Link |
| 33 | +} |
| 34 | + |
| 35 | +/* ugly, but it makes a DAG with paths that look like this but doesn't involved dag-pb or unixfs */ |
| 36 | +var identityDagPaths = []string{ |
| 37 | + "", |
| 38 | + "a/!foo", |
| 39 | + "a/b/!bar", |
| 40 | + "a/b/c/!baz/identity jump", |
| 41 | + "a/b/c/!baz/identity jump/these are my children/blip", |
| 42 | + "a/b/c/!baz/identity jump/these are my children/bloop", |
| 43 | + "a/b/c/!baz/identity jump/these are my children/bloop/ leaf ", |
| 44 | + "a/b/c/!baz/identity jump/these are my children/blop", |
| 45 | + "a/b/c/!baz/identity jump/these are my children/leaf", |
| 46 | + "a/b/c/d/!leaf", |
| 47 | +} |
| 48 | + |
| 49 | +func SetupIdentityDAG( |
| 50 | + ctx context.Context, |
| 51 | + t testing.TB, |
| 52 | + lsys ipld.LinkSystem) *TestIdentityDAG { |
| 53 | + |
| 54 | + allLinks := make([]ipld.Link, 0) |
| 55 | + store := func(lp datamodel.LinkPrototype, n datamodel.Node) datamodel.Link { |
| 56 | + l, err := lsys.Store(linking.LinkContext{}, lp, n) |
| 57 | + require.NoError(t, err) |
| 58 | + allLinks = append(allLinks, l) |
| 59 | + return l |
| 60 | + } |
| 61 | + |
| 62 | + rootLeaf := store(rawlp, basicnode.NewBytes([]byte("leaf node in the root"))) |
| 63 | + bazLeaf := store(rawlp, basicnode.NewBytes([]byte("leaf node in baz"))) |
| 64 | + blop := store(djlp, must(qp.BuildList(basicnode.Prototype.Any, -1, func(la datamodel.ListAssembler) { |
| 65 | + qp.ListEntry(la, qp.Int(100)) |
| 66 | + qp.ListEntry(la, qp.Int(200)) |
| 67 | + qp.ListEntry(la, qp.Int(300)) |
| 68 | + }))(t)) |
| 69 | + bloopLeaf := store(rawlp, basicnode.NewBytes([]byte("leaf node in bloop"))) |
| 70 | + bloop := store(djlp, must(qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) { |
| 71 | + qp.MapEntry(ma, "desc", qp.List(-1, func(la datamodel.ListAssembler) { |
| 72 | + qp.ListEntry(la, qp.String("this")) |
| 73 | + qp.ListEntry(la, qp.String("is")) |
| 74 | + qp.ListEntry(la, qp.String("bloop")) |
| 75 | + })) |
| 76 | + qp.MapEntry(ma, " leaf ", qp.Link(bloopLeaf)) |
| 77 | + }))(t)) |
| 78 | + blip := store(djlp, basicnode.NewString("blip!")) |
| 79 | + baz := store(djlp, must(qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) { |
| 80 | + qp.MapEntry(ma, "desc", qp.List(-1, func(la datamodel.ListAssembler) { |
| 81 | + qp.ListEntry(la, qp.String("this")) |
| 82 | + qp.ListEntry(la, qp.String("is")) |
| 83 | + qp.ListEntry(la, qp.String("baz")) |
| 84 | + })) |
| 85 | + qp.MapEntry(ma, "these are my children", qp.Map(-1, func(ma datamodel.MapAssembler) { |
| 86 | + qp.MapEntry(ma, "blip", qp.Link(blip)) |
| 87 | + qp.MapEntry(ma, "bloop", qp.Link(bloop)) |
| 88 | + qp.MapEntry(ma, "blop", qp.Link(blop)) |
| 89 | + qp.MapEntry(ma, "leaf", qp.Link(bazLeaf)) |
| 90 | + })) |
| 91 | + }))(t)) |
| 92 | + var bazIdent datamodel.Link |
| 93 | + { |
| 94 | + // not stored, shouldn't count as a block |
| 95 | + ident := must(qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) { |
| 96 | + qp.MapEntry(ma, "identity jump", qp.Link(baz)) |
| 97 | + }))(t) |
| 98 | + identBytes := must(ipld.Encode(ident, dagjson.Encode))(t) |
| 99 | + mh := must(multihash.Sum(identBytes, multihash.IDENTITY, len(identBytes)))(t) |
| 100 | + bazIdent = cidlink.Link{Cid: cid.NewCidV1(cid.DagJSON, mh)} |
| 101 | + } |
| 102 | + bar := store(djlp, basicnode.NewInt(2020202020202020)) |
| 103 | + foo := store(djlp, basicnode.NewInt(1010101010101010)) |
| 104 | + root := store(djlp, must(qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) { |
| 105 | + qp.MapEntry(ma, "a", qp.Map(-1, func(ma datamodel.MapAssembler) { |
| 106 | + qp.MapEntry(ma, "b", qp.Map(-1, func(ma datamodel.MapAssembler) { |
| 107 | + qp.MapEntry(ma, "c", qp.Map(-1, func(ma datamodel.MapAssembler) { |
| 108 | + qp.MapEntry(ma, "d", qp.Map(-1, func(ma datamodel.MapAssembler) { |
| 109 | + qp.MapEntry(ma, "!leaf", qp.Link(rootLeaf)) |
| 110 | + })) |
| 111 | + qp.MapEntry(ma, "!baz", qp.Link(bazIdent)) |
| 112 | + })) |
| 113 | + qp.MapEntry(ma, "!bar", qp.Link(bar)) |
| 114 | + })) |
| 115 | + qp.MapEntry(ma, "!foo", qp.Link(foo)) |
| 116 | + })) |
| 117 | + }))(t)) |
| 118 | + |
| 119 | + return &TestIdentityDAG{ |
| 120 | + t: t, |
| 121 | + loader: lsys.StorageReadOpener, |
| 122 | + RootLink: root, |
| 123 | + AllLinks: reverse(allLinks), // TODO: slices.Reverse post 1.21 |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func (tid *TestIdentityDAG) AllBlocks() []blocks.Block { |
| 128 | + blks := make([]blocks.Block, len(tid.AllLinks)) |
| 129 | + for ii, link := range tid.AllLinks { |
| 130 | + reader, err := tid.loader(ipld.LinkContext{}, link) |
| 131 | + require.NoError(tid.t, err) |
| 132 | + data, err := io.ReadAll(reader) |
| 133 | + require.NoError(tid.t, err) |
| 134 | + blk, err := blocks.NewBlockWithCid(data, link.(cidlink.Link).Cid) |
| 135 | + require.NoError(tid.t, err) |
| 136 | + blks[ii] = blk |
| 137 | + } |
| 138 | + return blks |
| 139 | +} |
| 140 | + |
| 141 | +// VerifyWholeDAGWithTypes verifies the given response channel returns the expected responses for the whole DAG |
| 142 | +// and that the types in the response are the expected types for the DAG |
| 143 | +func (tid *TestIdentityDAG) VerifyWholeDAG(ctx context.Context, responseChan <-chan graphsync.ResponseProgress) { |
| 144 | + responses := CollectResponses(ctx, tid.t, responseChan) |
| 145 | + tid.checkResponses(responses) |
| 146 | +} |
| 147 | + |
| 148 | +func (tid *TestIdentityDAG) checkResponses(responses []graphsync.ResponseProgress) { |
| 149 | + var pathIndex int |
| 150 | + for ii, response := range responses { |
| 151 | + // only check the paths that have links, assume the rest are just describing |
| 152 | + // the non-link nodes of the DAG |
| 153 | + if response.Path.String() == identityDagPaths[pathIndex] { |
| 154 | + if response.LastBlock.Link != nil { |
| 155 | + expectedLink := tid.AllLinks[pathIndex] |
| 156 | + require.Equal(tid.t, expectedLink.String(), response.LastBlock.Link.String(), fmt.Sprintf("response %d has correct link (%d)", ii, pathIndex)) |
| 157 | + } |
| 158 | + pathIndex++ |
| 159 | + } |
| 160 | + } |
| 161 | + require.Equal(tid.t, len(identityDagPaths), pathIndex, "traverses all nodes") |
| 162 | +} |
| 163 | + |
| 164 | +func must[T any](v T, err error) func(t testing.TB) T { |
| 165 | + return func(t testing.TB) T { |
| 166 | + t.Helper() |
| 167 | + if err != nil { |
| 168 | + t.Fatal(err) |
| 169 | + } |
| 170 | + return v |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +var djlp = cidlink.LinkPrototype{ |
| 175 | + Prefix: cid.Prefix{ |
| 176 | + Version: 1, |
| 177 | + Codec: cid.DagJSON, |
| 178 | + MhType: multihash.SHA2_256, |
| 179 | + MhLength: 32, |
| 180 | + }, |
| 181 | +} |
| 182 | + |
| 183 | +var rawlp = cidlink.LinkPrototype{ |
| 184 | + Prefix: cid.Prefix{ |
| 185 | + Version: 1, |
| 186 | + Codec: cid.Raw, |
| 187 | + MhType: multihash.SHA2_256, |
| 188 | + MhLength: 32, |
| 189 | + }, |
| 190 | +} |
| 191 | + |
| 192 | +func reverse[S ~[]E, E any](s S) S { |
| 193 | + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { |
| 194 | + s[i], s[j] = s[j], s[i] |
| 195 | + } |
| 196 | + return s |
| 197 | +} |
0 commit comments