Skip to content

Commit

Permalink
Add test AddVerifiedClient
Browse files Browse the repository at this point in the history
  • Loading branch information
jennijuju committed May 24, 2021
1 parent 0f83515 commit 1f4162c
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 5 deletions.
5 changes: 4 additions & 1 deletion api/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"github.com/filecoin-project/lotus/chain/wallet"

logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"

Expand Down Expand Up @@ -86,6 +88,7 @@ type FullNodeOpts struct {
// storage array defines storage nodes, numbers in the array specify full node
// index the storage node 'belongs' to
type APIBuilder func(t *testing.T, full []FullNodeOpts, storage []StorageMiner) ([]TestNode, []TestStorageNode)
type APIBuilderWithRKH func(t *testing.T, full []FullNodeOpts, storage []StorageMiner, rkh wallet.Key) ([]TestNode, []TestStorageNode)
type testSuite struct {
makeNodes APIBuilder
}
Expand Down Expand Up @@ -290,4 +293,4 @@ func (ts *testSuite) testConnectTwo(t *testing.T) {
if len(p) != 1 {
t.Error("Node 0 doesn't have 1 peer")
}
}
}
139 changes: 139 additions & 0 deletions api/test/verifreg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package test

import (
"context"
"fmt"
"os"
"strings"

"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/node/impl"
verifreg4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/verifreg"

"testing"
"time"

"github.com/filecoin-project/go-state-types/big"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
logging "github.com/ipfs/go-log/v2"
)

func init() {
logging.SetAllLoggers(logging.LevelInfo)
err := os.Setenv("BELLMAN_NO_GPU", "1")
if err != nil {
panic(fmt.Sprintf("failed to set BELLMAN_NO_GPU env variable: %s", err))
}
build.InsecurePoStValidation = true
}

func AddVerifiedClient(t *testing.T, b APIBuilderWithRKH) {

rkhKey, err := wallet.GenerateKey(types.KTSecp256k1)
if err != nil {
return
}

nodes, miners := b(t, []FullNodeOpts{FullNodeWithLatestActorsAt(-1)}, OneMiner, *rkhKey)
api := nodes[0].FullNode.(*impl.FullNodeAPI)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

//Add verifier
verifier, err := api.WalletDefaultAddress(ctx)
if err != nil {
t.Fatal(err)
}
api.WalletImport(ctx, &rkhKey.KeyInfo)
params, err := actors.SerializeParams(&verifreg4.AddVerifierParams{Address: verifier, Allowance: big.NewInt(100000000000)})
if err != nil {
t.Fatal(err)
}
msg := &types.Message{
To: verifreg.Address,
From: rkhKey.Address,
Method: verifreg.Methods.AddVerifier,
Params: params,
Value: big.Zero(),
}

bm := NewBlockMiner(ctx, t, miners[0], 100*time.Millisecond)
bm.MineBlocks()
defer bm.Stop()

sm, err := api.MpoolPushMessage(ctx, msg, nil)
if err != nil {
t.Fatal("AddVerifier failed: ", err)
}
res, err := api.StateWaitMsg(ctx, sm.Cid(), 1, lapi.LookbackNoLimit, true)
if err != nil {
t.Fatal(err)
}
if res.Receipt.ExitCode != 0 {
t.Fatal("did not successfully send message")
}

//Assign datacap to a client
datacap := big.NewInt(10000)
clientAddress, err := api.WalletNew(ctx, types.KTBLS)
if err != nil {
t.Fatal(err)
}

params, err = actors.SerializeParams(&verifreg4.AddVerifiedClientParams{Address: clientAddress, Allowance: datacap})
if err != nil {
t.Fatal(err)
}

msg = &types.Message{
To: verifreg.Address,
From: verifier,
Method: verifreg.Methods.AddVerifiedClient,
Params: params,
Value: big.Zero(),
}

sm, err = api.MpoolPushMessage(ctx, msg, nil)
if err != nil {
t.Fatal("AddVerifiedClient faield: ", err)
}
res, err = api.StateWaitMsg(ctx, sm.Cid(), 1, lapi.LookbackNoLimit, true)
if err != nil {
t.Fatal(err)
}
if res.Receipt.ExitCode != 0 {
t.Fatal("did not successfully send message")
}

//check datacap balance
dcap, err := api.StateVerifiedClientStatus(ctx, clientAddress, types.EmptyTSK)
if err != nil {
t.Fatal(err)
}
if !dcap.Equals(datacap) {
t.Fatal("")
}

//try to assign datacap to the same client should fail for actor v4 and below
params, err = actors.SerializeParams(&verifreg4.AddVerifiedClientParams{Address: clientAddress, Allowance: datacap})
if err != nil {
t.Fatal(err)
}

msg = &types.Message{
To: verifreg.Address,
From: verifier,
Method: verifreg.Methods.AddVerifiedClient,
Params: params,
Value: big.Zero(),
}

sm, err = api.MpoolPushMessage(ctx, msg, nil)
if !strings.Contains(err.Error(), "verified client already exists") {
t.Fatal("Add datacap to an exist verified client should fail")
}
}
6 changes: 6 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,9 @@ func TestDeadlineToggling(t *testing.T) {

test.TestDeadlineToggling(t, builder.MockSbBuilder, 2*time.Millisecond)
}

func TestVerifiedClientTopUp(t *testing.T) {
logging.SetLogLevel("storageminer", "FATAL")
logging.SetLogLevel("chain", "ERROR")
test.AddVerifiedClient(t, builder.MockRKHBuilder)
}
25 changes: 21 additions & 4 deletions node/test/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,26 @@ func storageBuilder(parentNode test.TestNode, mn mocknet.Mocknet, opts node.Opti
}

func Builder(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner) ([]test.TestNode, []test.TestStorageNode) {
return mockBuilderOpts(t, fullOpts, storage, false)
return mockBuilderOpts(t, fullOpts, storage, false, wallet.Key{})
}

func MockSbBuilder(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner) ([]test.TestNode, []test.TestStorageNode) {
return mockSbBuilderOpts(t, fullOpts, storage, false)
}

func RPCBuilder(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner) ([]test.TestNode, []test.TestStorageNode) {
return mockBuilderOpts(t, fullOpts, storage, true)
return mockBuilderOpts(t, fullOpts, storage, true, wallet.Key{})
}

func RPCMockSbBuilder(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner) ([]test.TestNode, []test.TestStorageNode) {
return mockSbBuilderOpts(t, fullOpts, storage, true)
}

func mockBuilderOpts(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner, rpc bool) ([]test.TestNode, []test.TestStorageNode) {
func MockRKHBuilder(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner, rkhKey wallet.Key) ([]test.TestNode, []test.TestStorageNode) {
return mockBuilderOpts(t, fullOpts, storage, false, rkhKey)
}

func mockBuilderOpts(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.StorageMiner, rpc bool, rkhKey wallet.Key) ([]test.TestNode, []test.TestStorageNode) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

Expand Down Expand Up @@ -276,12 +280,24 @@ func mockBuilderOpts(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.
maddrs = append(maddrs, maddr)
genms = append(genms, *genm)
}

var vrk genesis.Actor
if &rkhKey != nil {
vrk = genesis.Actor{
Type: genesis.TAccount,
Balance: big.Mul(big.NewInt(400000000), types.NewInt(build.FilecoinPrecision)),
Meta: (&genesis.AccountMeta{Owner: rkhKey.Address}).ActorMeta(),
}
} else {
vrk = gen.DefaultVerifregRootkeyActor
}

templ := &genesis.Template{
Accounts: genaccs,
Miners: genms,
NetworkName: "test",
Timestamp: uint64(time.Now().Unix() - 10000), // some time sufficiently far in the past
VerifregRootKey: gen.DefaultVerifregRootkeyActor,
VerifregRootKey: vrk,
RemainderAccount: gen.DefaultRemainderAccountActor,
}

Expand All @@ -306,6 +322,7 @@ func mockBuilderOpts(t *testing.T, fullOpts []test.FullNodeOpts, storage []test.

fullOpts[i].Opts(fulls),
)

if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 1f4162c

Please sign in to comment.