2020-07-07 22:26:33 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
cbornode "github.com/ipfs/go-ipld-cbor"
|
|
|
|
typegen "github.com/whyrusleeping/cbor-gen"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2020-07-23 02:05:11 +00:00
|
|
|
|
|
|
|
bstore "github.com/filecoin-project/lotus/lib/blockstore"
|
2020-07-07 22:26:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiffAdtArray(t *testing.T) {
|
|
|
|
ctxstoreA := newContextStore()
|
|
|
|
ctxstoreB := newContextStore()
|
|
|
|
|
|
|
|
arrA := adt.MakeEmptyArray(ctxstoreA)
|
|
|
|
arrB := adt.MakeEmptyArray(ctxstoreB)
|
|
|
|
|
|
|
|
require.NoError(t, arrA.Set(0, runtime.CBORBytes([]byte{0}))) // delete
|
|
|
|
|
|
|
|
require.NoError(t, arrA.Set(1, runtime.CBORBytes([]byte{0}))) // modify
|
|
|
|
require.NoError(t, arrB.Set(1, runtime.CBORBytes([]byte{1})))
|
|
|
|
|
|
|
|
require.NoError(t, arrA.Set(2, runtime.CBORBytes([]byte{1}))) // delete
|
|
|
|
|
|
|
|
require.NoError(t, arrA.Set(3, runtime.CBORBytes([]byte{0}))) // noop
|
|
|
|
require.NoError(t, arrB.Set(3, runtime.CBORBytes([]byte{0})))
|
|
|
|
|
|
|
|
require.NoError(t, arrA.Set(4, runtime.CBORBytes([]byte{0}))) // modify
|
|
|
|
require.NoError(t, arrB.Set(4, runtime.CBORBytes([]byte{6})))
|
|
|
|
|
|
|
|
require.NoError(t, arrB.Set(5, runtime.CBORBytes{8})) // add
|
|
|
|
require.NoError(t, arrB.Set(6, runtime.CBORBytes{9})) // add
|
|
|
|
|
2020-07-08 00:44:55 +00:00
|
|
|
changes := new(TestAdtDiff)
|
2020-07-07 22:26:33 +00:00
|
|
|
|
|
|
|
assert.NoError(t, DiffAdtArray(arrA, arrB, changes))
|
|
|
|
assert.NotNil(t, changes)
|
|
|
|
|
|
|
|
assert.Equal(t, 2, len(changes.Added))
|
2020-07-08 00:44:55 +00:00
|
|
|
// keys 5 and 6 were added
|
|
|
|
assert.EqualValues(t, uint64(5), changes.Added[0].key)
|
|
|
|
assert.EqualValues(t, []byte{8}, changes.Added[0].val)
|
|
|
|
assert.EqualValues(t, uint64(6), changes.Added[1].key)
|
|
|
|
assert.EqualValues(t, []byte{9}, changes.Added[1].val)
|
2020-07-07 22:26:33 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 2, len(changes.Modified))
|
2020-07-08 00:44:55 +00:00
|
|
|
// keys 1 and 4 were modified
|
|
|
|
assert.EqualValues(t, uint64(1), changes.Modified[0].From.key)
|
|
|
|
assert.EqualValues(t, []byte{0}, changes.Modified[0].From.val)
|
|
|
|
assert.EqualValues(t, uint64(1), changes.Modified[0].To.key)
|
|
|
|
assert.EqualValues(t, []byte{1}, changes.Modified[0].To.val)
|
|
|
|
assert.EqualValues(t, uint64(4), changes.Modified[1].From.key)
|
|
|
|
assert.EqualValues(t, []byte{0}, changes.Modified[1].From.val)
|
|
|
|
assert.EqualValues(t, uint64(4), changes.Modified[1].To.key)
|
|
|
|
assert.EqualValues(t, []byte{6}, changes.Modified[1].To.val)
|
2020-07-07 22:26:33 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 2, len(changes.Removed))
|
2020-07-08 00:44:55 +00:00
|
|
|
// keys 0 and 2 were deleted
|
|
|
|
assert.EqualValues(t, uint64(0), changes.Removed[0].key)
|
|
|
|
assert.EqualValues(t, []byte{0}, changes.Removed[0].val)
|
|
|
|
assert.EqualValues(t, uint64(2), changes.Removed[1].key)
|
|
|
|
assert.EqualValues(t, []byte{1}, changes.Removed[1].val)
|
|
|
|
}
|
|
|
|
|
|
|
|
type adtDiffResult struct {
|
|
|
|
key uint64
|
|
|
|
val runtime.CBORBytes
|
2020-07-07 22:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestAdtDiff struct {
|
2020-07-08 00:44:55 +00:00
|
|
|
Added []adtDiffResult
|
2020-07-07 22:26:33 +00:00
|
|
|
Modified []TestAdtDiffModified
|
2020-07-08 00:44:55 +00:00
|
|
|
Removed []adtDiffResult
|
2020-07-07 22:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ AdtArrayDiff = &TestAdtDiff{}
|
|
|
|
|
|
|
|
type TestAdtDiffModified struct {
|
2020-07-08 00:44:55 +00:00
|
|
|
From adtDiffResult
|
|
|
|
To adtDiffResult
|
2020-07-07 22:26:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 00:44:55 +00:00
|
|
|
func (t *TestAdtDiff) Add(key uint64, val *typegen.Deferred) error {
|
2020-07-07 22:26:33 +00:00
|
|
|
v := new(runtime.CBORBytes)
|
|
|
|
err := v.UnmarshalCBOR(bytes.NewReader(val.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 00:44:55 +00:00
|
|
|
t.Added = append(t.Added, adtDiffResult{
|
|
|
|
key: key,
|
|
|
|
val: *v,
|
|
|
|
})
|
2020-07-07 22:26:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:44:55 +00:00
|
|
|
func (t *TestAdtDiff) Modify(key uint64, from, to *typegen.Deferred) error {
|
2020-07-07 22:26:33 +00:00
|
|
|
vFrom := new(runtime.CBORBytes)
|
|
|
|
err := vFrom.UnmarshalCBOR(bytes.NewReader(from.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vTo := new(runtime.CBORBytes)
|
|
|
|
err = vTo.UnmarshalCBOR(bytes.NewReader(to.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(*vFrom, *vTo) {
|
|
|
|
t.Modified = append(t.Modified, TestAdtDiffModified{
|
2020-07-08 00:44:55 +00:00
|
|
|
From: adtDiffResult{
|
|
|
|
key: key,
|
|
|
|
val: *vFrom,
|
|
|
|
},
|
|
|
|
To: adtDiffResult{
|
|
|
|
key: key,
|
|
|
|
val: *vTo,
|
|
|
|
},
|
2020-07-07 22:26:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:44:55 +00:00
|
|
|
func (t *TestAdtDiff) Remove(key uint64, val *typegen.Deferred) error {
|
2020-07-07 22:26:33 +00:00
|
|
|
v := new(runtime.CBORBytes)
|
|
|
|
err := v.UnmarshalCBOR(bytes.NewReader(val.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-08 00:44:55 +00:00
|
|
|
t.Removed = append(t.Removed, adtDiffResult{
|
|
|
|
key: key,
|
|
|
|
val: *v,
|
|
|
|
})
|
2020-07-07 22:26:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newContextStore() *contextStore {
|
|
|
|
ctx := context.Background()
|
2020-07-23 02:05:11 +00:00
|
|
|
bs := bstore.NewTemporarySync()
|
2020-07-07 22:26:33 +00:00
|
|
|
store := cbornode.NewCborStore(bs)
|
|
|
|
return &contextStore{
|
|
|
|
ctx: ctx,
|
|
|
|
cst: store,
|
|
|
|
}
|
|
|
|
}
|