Skip to content

Commit

Permalink
feat: remove redundant code. rework block response. remove ctx redund…
Browse files Browse the repository at this point in the history
…ancy checker
  • Loading branch information
Bidon15 committed Aug 25, 2022
1 parent ee79c11 commit 435a1ec
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 73 deletions.
23 changes: 13 additions & 10 deletions testkit/appkit/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,33 @@ func (ak *AppKit) PayForData(accAdr string, msg int, krbackend, chainId, home st
ak.Cmd.SetArgs([]string{
"tx", "payment", "payForData", fmt.Sprint(msg),
"--from", accAdr, "-b", "block", "-y", "--gas", "1000000000",
"--fees", "100000000000utia", //15
"--fees", "100000000000utia",
"--keyring-backend", krbackend, "--chain-id", chainId, "--home", home, "--keyring-dir", home,
})

return svrcmd.Execute(ak.Cmd, appcmd.EnvPrefix, app.DefaultNodeHome)
}

func getResultBlockResponse(uri string) (coretypes.ResultBlock, error) {
func getResultBlockResponse(uri string) (*coretypes.ResultBlock, error) {
resp, err := http.Get(uri)
if err != nil {
return coretypes.ResultBlock{}, err
return nil, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return coretypes.ResultBlock{}, err
return nil, err
}

var rpcResponse types.RPCResponse
if err := rpcResponse.UnmarshalJSON(body); err != nil {
return coretypes.ResultBlock{}, err
return nil, err
}

var resBlock coretypes.ResultBlock
var resBlock *coretypes.ResultBlock
if err := tmjson.Unmarshal(rpcResponse.Result, &resBlock); err != nil {
return coretypes.ResultBlock{}, err
return nil, err
}

return resBlock, nil
Expand Down Expand Up @@ -182,9 +182,12 @@ func updateConfig(path, key string, value interface{}) error {
}

func AddSeedPeers(path string, peers []string) error {
var peersStr bytes.Buffer
var port int = 26656
var separator string = ","
var (
peersStr bytes.Buffer
port int = 26656
separator string = ","
)

for k, peer := range peers {
if k == (len(peers) - 1) {
separator = ""
Expand Down
1 change: 0 additions & 1 deletion testkit/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ var (
AppStartedState = sync.State("app-started")
BridgeStartedState = sync.State("bridge-started")
FinishState = sync.State("test-finished")
FinalGenesisState = sync.State("final-genesis")
)
9 changes: 1 addition & 8 deletions tests/app-sync/run_seeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func RunSeed(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
Network: "default",
Enable: true,
Default: network.LinkShape{
// Latency: 100 * time.Millisecond,
Bandwidth: 4 << 26, // 256Mib
},
CallbackState: "network-configured",
Expand Down Expand Up @@ -61,11 +60,6 @@ func RunSeed(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
return err
}

err = <-syncclient.MustBarrier(ctx, testkit.FinalGenesisState, 1).C
if err != nil {
return err
}

initGenCh := make(chan string)
sub, err := syncclient.Subscribe(ctx, testkit.InitialGenenesisTopic, initGenCh)
if err != nil {
Expand Down Expand Up @@ -104,10 +98,9 @@ func RunSeed(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
go cmd.StartNode(home, "info")

// // wait for a new block to be produced
time.Sleep(10 * time.Minute)
time.Sleep(1 * time.Minute)

runenv.RecordSuccess()
// }

return nil
}
3 changes: 0 additions & 3 deletions tests/app-sync/run_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func RunValidator(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
Network: "default",
Enable: true,
Default: network.LinkShape{
// Latency: 100 * time.Millisecond,
// Bandwidth: 4 << 26, // 256Mib
Bandwidth: 5 << 26, // 320Mib
// Bandwidth: 4 << 27, // 512Mib
},
CallbackState: "network-configured",
RoutingPolicy: network.AllowAll,
Expand Down
25 changes: 25 additions & 0 deletions tests/node-sync/run_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/testground/sdk-go/network"
"github.com/testground/sdk-go/run"
"github.com/testground/sdk-go/runtime"
"github.com/testground/sdk-go/sync"
)

func RunBridgeNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
Expand Down Expand Up @@ -156,3 +157,27 @@ func RunBridgeNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error {

return nil
}

func GetBridgeNode(ctx context.Context, syncclient sync.Client, id int64, amountOfBridges int) (*testkit.BridgeNodeInfo, error) {
bridgeCh := make(chan *testkit.BridgeNodeInfo, amountOfBridges)
sub, err := syncclient.Subscribe(ctx, testkit.BridgeNodeTopic, bridgeCh)
if err != nil {
return nil, err
}

for {
select {
case err = <-sub.Done():
if err != nil {
return nil,
fmt.Errorf("no bridge address has been sent to this light node to connect to")
}
case bridge := <-bridgeCh:
fmt.Printf("Received Bridge ID = %d", bridge.ID)
if (int(id) % amountOfBridges) == (bridge.ID % amountOfBridges) {
return bridge, nil
}
}
}

}
26 changes: 1 addition & 25 deletions tests/node-sync/run_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,7 @@ func RunFullNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
return err
}

bridgeCh := make(chan *testkit.BridgeNodeInfo, runenv.IntParam("bridge"))
sub, err := syncclient.Subscribe(ctx, testkit.BridgeNodeTopic, bridgeCh)
if err != nil {
return err
}

bridgeNode, err := func(total int) (*testkit.BridgeNodeInfo, error) {
for {
select {
case err = <-sub.Done():
if err != nil {
return nil, err
}
case <-ctx.Done():
return nil,
fmt.Errorf("no bridge address has been sent to this light node to connect to")
case bridge := <-bridgeCh:
runenv.RecordMessage("Received Bridge ID = %d", bridge.ID)
if (int(initCtx.GlobalSeq) % total) == (bridge.ID % total) {
return bridge, nil
}
}
}
}(runenv.IntParam("bridge"))

bridgeNode, err := GetBridgeNode(ctx, syncclient, initCtx.GlobalSeq, runenv.IntParam("bridge"))
if err != nil {
return err
}
Expand Down
27 changes: 1 addition & 26 deletions tests/node-sync/run_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,7 @@ func RunLightNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
return err
}

bridgeCh := make(chan *testkit.BridgeNodeInfo, runenv.IntParam("bridge")) //4
sub, err := syncclient.Subscribe(ctx, testkit.BridgeNodeTopic, bridgeCh)
if err != nil {
return err
}

bridgeNode, err := func(total int) (*testkit.BridgeNodeInfo, error) {
for {
select {
case err = <-sub.Done():
if err != nil {
return nil, err
}
case <-ctx.Done():
return nil,
fmt.Errorf("no bridge address has been sent to this light node to connect to")
case bridge := <-bridgeCh:
runenv.RecordMessage("Received Bridge ID = %d", bridge.ID)
if (int(initCtx.GlobalSeq) % total) == (bridge.ID % total) {
return bridge, nil
}
}
}
}(runenv.IntParam("bridge"))

bridgeNode, err := GetBridgeNode(ctx, syncclient, initCtx.GlobalSeq, runenv.IntParam("bridge"))
if err != nil {
return err
}
Expand Down Expand Up @@ -109,7 +85,6 @@ func RunLightNode(runenv *runtime.RunEnv, initCtx *run.InitContext) error {
}

runenv.RecordMessage("Reached Block#6 contains Hash: %s", eh.Commit.BlockID.Hash.String())
runenv.RecordSuccess()

err = nd.Stop(ctx)
if err != nil {
Expand Down

0 comments on commit 435a1ec

Please sign in to comment.