Fix PR comments.

Refactor random addr generation to use a rand seed.
Remove unused lines in tests.
This commit is contained in:
Darko Brdareski 2022-02-17 14:24:42 +01:00
parent e6c8c9a6ab
commit 6feae1993d
2 changed files with 29 additions and 23 deletions

View File

@ -2,8 +2,8 @@ package mock
import (
"context"
"crypto/rand"
"fmt"
"math/rand"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
@ -90,9 +90,13 @@ func TipSet(blks ...*types.BlockHeader) *types.TipSet {
return ts
}
func RandomActorAddress() (*address.Address, error) {
// Generates count new addresses using the provided seed, and returns them
func RandomActorAddresses(seed int64, count int) ([]*address.Address, error) {
randAddrs := make([]*address.Address, count)
source := rand.New(rand.NewSource(seed))
for i := 0; i < count; i++ {
bytes := make([]byte, 32)
_, err := rand.Read(bytes)
_, err := source.Read(bytes)
if err != nil {
return nil, err
}
@ -101,8 +105,9 @@ func RandomActorAddress() (*address.Address, error) {
if err != nil {
return nil, err
}
return &addr, nil
randAddrs[i] = &addr
}
return randAddrs, nil
}
func UnsignedMessage(from, to address.Address, nonce uint64) *types.Message {

View File

@ -188,11 +188,11 @@ func TestChainGetMsg(t *testing.T) {
app, mockApi, buf, done := NewMockAppWithFullAPI(t, WithCategory("chain", ChainGetMsgCmd))
defer done()
from, err := mock.RandomActorAddress()
addrs, err := mock.RandomActorAddresses(12345, 2)
assert.NoError(t, err)
to, err := mock.RandomActorAddress()
assert.NoError(t, err)
from := addrs[0]
to := addrs[1]
msg := mock.UnsignedMessage(*from, *to, 0)
@ -283,11 +283,11 @@ func TestInspectUsage(t *testing.T) {
cmd := WithCategory("chain", ChainInspectUsage)
ts := mock.TipSet(mock.MkBlock(nil, 0, 0))
from, err := mock.RandomActorAddress()
addrs, err := mock.RandomActorAddresses(12345, 2)
assert.NoError(t, err)
to, err := mock.RandomActorAddress()
assert.NoError(t, err)
from := addrs[0]
to := addrs[1]
msg := mock.UnsignedMessage(*from, *to, 0)
msgs := []api.Message{{Cid: msg.Cid(), Message: msg}}
@ -321,6 +321,9 @@ func TestInspectUsage(t *testing.T) {
// output is plaintext, had to do string matching
assert.Contains(t, out, from.String())
assert.Contains(t, out, to.String())
// check for gas by sender
assert.Contains(t, out, "By Sender")
// check for gas by method
assert.Contains(t, out, "Send")
})
}
@ -332,13 +335,11 @@ func TestChainList(t *testing.T) {
blk.Height = 1
head := mock.TipSet(blk)
head.Height()
from, err := mock.RandomActorAddress()
addrs, err := mock.RandomActorAddresses(12345, 2)
assert.NoError(t, err)
to, err := mock.RandomActorAddress()
assert.NoError(t, err)
from := addrs[0]
to := addrs[1]
msg := mock.UnsignedMessage(*from, *to, 0)
msgs := []api.Message{{Cid: msg.Cid(), Message: msg}}