Skip to content

Commit

Permalink
Added tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusbechwind committed Dec 18, 2023
1 parent 0e5cbbf commit cc2ecd1
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 1 deletion.
28 changes: 28 additions & 0 deletions v2/examples/getbakerearliestwintime/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"fmt"
"log"

v2 "github.com/Concordium/concordium-go-sdk/v2"
"github.com/Concordium/concordium-go-sdk/v2/pb"
)

// This example retrieves and prints projected earliest wintime of a baker.
func main() {
client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"})
if err != nil {
log.Fatalf("Failed to instantiate client, err: %v", err)
}

// sending empty context, can also use any other context instead.
resp, err := client.GetBakerEarliestWinTime(context.TODO(), &pb.BakerId{
Value: 1,
})
if err != nil {
log.Fatalf("failed to get wintime, err: %v", err)
}

fmt.Println("wintime: ", resp)
}
36 changes: 36 additions & 0 deletions v2/examples/getbakersrewardperiod/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"fmt"
"io"
"log"

v2 "github.com/Concordium/concordium-go-sdk/v2"
)

// This example retrieves and prints the info of the bakers in the reward period of a block.
func main() {
client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"})
if err != nil {
log.Fatalf("Failed to instantiate client, err: %v", err)
}

// sending empty context, can also use any other context instead.
stream, err := client.GetBakersRewardPeriod(context.TODO(), v2.BlockHashInputBest{})
if err != nil {
log.Fatalf("failed to get BakerRewardPeriodInfos, err: %v", err)
}

for err == nil {
bakerRewardPeriodInfo, err := stream.Recv()
if err != nil {
if err == io.EOF {
// All BakerRewardPeriodInfo recieved.
break
}
log.Fatalf("Could not receive BakerRewardPeriodInfo, err: %v", err)
}
fmt.Println("BakerRewardPeriodInfo: ", bakerRewardPeriodInfo)
}
}
25 changes: 25 additions & 0 deletions v2/examples/getblockcertificates/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"context"
"fmt"
"log"

v2 "github.com/Concordium/concordium-go-sdk/v2"
)

// This example retrieves and prints the blockcertificates of a non-genesis block.
func main() {
client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"})
if err != nil {
log.Fatalf("Failed to instantiate client, err: %v", err)
}

// sending empty context, can also use any other context instead.
resp, err := client.GetBlockCertificates(context.TODO(), v2.BlockHashInputBest{})
if err != nil {
log.Fatalf("failed to get certificates, err: %v", err)
}

fmt.Println("certificates: ", resp)
}
2 changes: 1 addition & 1 deletion v2/examples/getconsensusinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

"github.com/Concordium/concordium-go-sdk/v2"
v2 "github.com/Concordium/concordium-go-sdk/v2"
)

// in this example we receive and print consensus info.
Expand Down
27 changes: 27 additions & 0 deletions v2/examples/getfirstblockepoch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"fmt"
"log"

v2 "github.com/Concordium/concordium-go-sdk/v2"
)

// This example retrieves and prints the block hash of the first finalized block in a specific epoch.
func main() {
client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"})
if err != nil {
log.Fatalf("Failed to instantiate client, err: %v", err)
}

// sending empty context, can also use any other context instead.
resp, err := client.GetFirstBlockEpoch(context.TODO(), v2.EpochRequestBlockHash{
BlockHash: v2.BlockHashInputLastFinal{},
})
if err != nil {
log.Fatalf("failed to get first block, err: %v", err)
}

fmt.Println("hash: ", resp)
}
39 changes: 39 additions & 0 deletions v2/examples/getwinningbakersepoch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"fmt"
"io"
"log"

v2 "github.com/Concordium/concordium-go-sdk/v2"
)

// This example retrieves and prints the bakers that won the lottery in a particular historical epoch.
func main() {
client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"})
if err != nil {
log.Fatalf("Failed to instantiate client, err: %v", err)
}

// sending empty context, can also use any other context instead.
stream, err := client.GetWinningBakersEpoch(context.TODO(), v2.EpochRequestRelativeEpoch{
GenesisIndex: v2.GenesisIndex{Value: 3},
Epoch: v2.Epoch{Value: 5},
})
if err != nil {
log.Fatalf("failed to get winning bakers, err: %v", err)
}

for err == nil {
winningBaker, err := stream.Recv()
if err != nil {
if err == io.EOF {
// All WinningBakers recieved.
break
}
log.Fatalf("Could not receive winning baker, err: %v", err)
}
fmt.Println("Winning baker: ", winningBaker)
}
}
44 changes: 44 additions & 0 deletions v2/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,48 @@ func TestExamples(t *testing.T) {
require.NoError(t, err)
require.Nil(t, blockItems)
})

t.Run("GetFirstBlockEpoch", func(t *testing.T) {
blochHash, err := client.GetFirstBlockEpoch(context.Background(), v2.EpochRequestRelativeEpoch{
GenesisIndex: v2.GenesisIndex{Value: 3},
Epoch: v2.Epoch{Value: 5},
})
require.NoError(t, err)
require.NotNil(t, blochHash)
})

t.Run("GetWinningBakersEpoch", func(t *testing.T) {
winningBakerStream, err := client.GetWinningBakersEpoch(context.Background(), v2.EpochRequestRelativeEpoch{
GenesisIndex: v2.GenesisIndex{Value: 3},
Epoch: v2.Epoch{Value: 5},
})
require.NoError(t, err)
require.NotNil(t, winningBakerStream)

winningBaker, err := winningBakerStream.Recv()
require.NoError(t, err)
require.NotNil(t, winningBaker)
})

t.Run("GetBlockCertificates", func(t *testing.T) {
certificate, err := client.GetBlockCertificates(context.Background(), v2.BlockHashInputBest{})
require.NoError(t, err)
require.NotNil(t, certificate)
})

t.Run("GetBakersRewardPeriod", func(t *testing.T) {
bakerRewardPeriodInfoStream, err := client.GetBakersRewardPeriod(context.Background(), v2.BlockHashInputBest{})
require.NoError(t, err)
require.NotNil(t, bakerRewardPeriodInfoStream)

bakerRewardPeriodInfo, err := bakerRewardPeriodInfoStream.Recv()
require.NoError(t, err)
require.NotNil(t, bakerRewardPeriodInfo)
})

t.Run("GetBakerEarliestWintime", func(t *testing.T) {
timestamp, err := client.GetBakerEarliestWinTime(context.Background(), &pb.BakerId{Value: 1})
require.NoError(t, err)
require.NotNil(t, timestamp)
})
}

0 comments on commit cc2ecd1

Please sign in to comment.