-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6075 from onflow/fxamacker/port-6063-to-atree-inl…
…ining-cadence-0.42 Port atree-inlined-status cmd (PR 6063) to feature/atree-inlining-cadence-v0.42
- Loading branch information
Showing
7 changed files
with
603 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
cmd/util/cmd/atree_inlined_status/atree_inlined_status_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package atree_inlined_status | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/fxamacker/cbor/v2" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/ledger" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
func TestCheckAtreeInlinedStatus(t *testing.T) { | ||
const nWorkers = 10 | ||
|
||
t.Run("no payloads", func(t *testing.T) { | ||
var payloads []*ledger.Payload | ||
atreeInlinedPayloadCount, atreeNonInlinedPayloadCount, err := checkAtreeInlinedStatus(payloads, nWorkers) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, atreeInlinedPayloadCount) | ||
require.Equal(t, 0, atreeNonInlinedPayloadCount) | ||
}) | ||
|
||
t.Run("payloads no goroutine", func(t *testing.T) { | ||
payloadCount := rand.Intn(50) | ||
testCheckAtreeInlinedStatus(t, payloadCount, nWorkers) | ||
}) | ||
|
||
t.Run("payloads using goroutine", func(t *testing.T) { | ||
payloadCount := rand.Intn(numOfPayloadPerJob) + numOfPayloadPerJob | ||
testCheckAtreeInlinedStatus(t, payloadCount, nWorkers) | ||
}) | ||
} | ||
|
||
func testCheckAtreeInlinedStatus(t *testing.T, payloadCount int, nWorkers int) { | ||
atreeNoninlinedPayloadCount := rand.Intn(payloadCount + 1) | ||
atreeInlinedPayloadCount := payloadCount - atreeNoninlinedPayloadCount | ||
|
||
payloads := make([]*ledger.Payload, 0, payloadCount) | ||
for i := 0; i < atreeInlinedPayloadCount; i++ { | ||
key := getRandomKey() | ||
value := getAtreeInlinedPayload(t) | ||
payloads = append(payloads, ledger.NewPayload(key, value)) | ||
} | ||
for i := 0; i < atreeNoninlinedPayloadCount; i++ { | ||
key := getRandomKey() | ||
value := getAtreeNoninlinedPayload(t) | ||
payloads = append(payloads, ledger.NewPayload(key, value)) | ||
} | ||
|
||
rand.Shuffle(len(payloads), func(i, j int) { | ||
payloads[i], payloads[j] = payloads[j], payloads[i] | ||
}) | ||
|
||
gotAtreeInlinedPayloadCount, gotAtreeNoninlinedPayloadCount, err := checkAtreeInlinedStatus(payloads, nWorkers) | ||
require.NoError(t, err) | ||
require.Equal(t, atreeNoninlinedPayloadCount, gotAtreeNoninlinedPayloadCount) | ||
require.Equal(t, atreeInlinedPayloadCount, gotAtreeInlinedPayloadCount) | ||
} | ||
|
||
func getAtreeNoninlinedPayload(t *testing.T) []byte { | ||
num := rand.Uint64() | ||
encodedNum, err := cbor.Marshal(num) | ||
require.NoError(t, err) | ||
|
||
data := []byte{ | ||
// extra data | ||
// version | ||
0x00, | ||
// extra data flag | ||
0x80, | ||
// array of extra data | ||
0x81, | ||
// type info | ||
0x18, 0x2a, | ||
|
||
// version | ||
0x00, | ||
// array data slab flag | ||
0x80, | ||
// CBOR encoded array head (fixed size 3 byte) | ||
0x99, 0x00, 0x01, | ||
} | ||
|
||
return append(data, encodedNum...) | ||
} | ||
|
||
func getAtreeInlinedPayload(t *testing.T) []byte { | ||
num := rand.Uint64() | ||
encodedNum, err := cbor.Marshal(num) | ||
require.NoError(t, err) | ||
|
||
data := []byte{ | ||
// version | ||
0x10, | ||
// flag | ||
0x80, | ||
|
||
// extra data | ||
// array of extra data | ||
0x81, | ||
// type info | ||
0x18, 0x2a, | ||
|
||
// CBOR encoded array head (fixed size 3 byte) | ||
0x99, 0x00, 0x01, | ||
} | ||
|
||
return append(data, encodedNum...) | ||
} | ||
|
||
func getRandomKey() ledger.Key { | ||
var address [8]byte | ||
_, err := crand.Read(address[:]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var key [9]byte | ||
key[0] = flow.SlabIndexPrefix | ||
_, err = crand.Read(key[1:]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return ledger.Key{ | ||
KeyParts: []ledger.KeyPart{ | ||
{Type: uint16(0), Value: address[:]}, | ||
{Type: uint16(2), Value: key[:]}, | ||
}} | ||
} |
Oops, something went wrong.