Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TCK): AccountCreateTransaction #1012

Merged
merged 11 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tck/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Go SDK TCK server

This is a server that implements the [SDK TCK specification](https://github.com/hashgraph/hedera-sdk-tck/) for the Go SDK.

## Running the server

To run the server you need to run

```
go run cmd/server.go
```

This will start the server on port **80**. You can change the port by setting the `TCK_PORT` environment variable or by adding a .env file with the same variable.
109 changes: 109 additions & 0 deletions tck/cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/creachadair/jrpc2/jhttp"
"github.com/hashgraph/hedera-sdk-go/tck/methods"
"github.com/hashgraph/hedera-sdk-go/tck/response"
"github.com/hashgraph/hedera-sdk-go/v2"
"github.com/joho/godotenv"
)

func main() {
// Load dotenv
_ = godotenv.Load()

// Initialize the SDK service
sdkService := new(methods.SDKService)
accountService := new(methods.AccountService)
accountService.SetSdkService(sdkService)

// Create a new RPC server
assigner := handler.Map{
"setup": postHandler(HandleError, handler.New(sdkService.Setup)),
"reset": postHandler(HandleError, handler.New(sdkService.Reset)),
"createAccount": postHandler(HandleError, handler.New(accountService.CreateAccount)),
"generateKey": postHandler(HandleError, handler.New(methods.GenerateKey)),
}

bridge := jhttp.NewBridge(assigner, nil)

// Listen and redirect to bridge
http.HandleFunc("/", bridge.ServeHTTP)
port := os.Getenv("TCK_PORT")
if port == "" {
port = "80"
}
fmt.Println("Server is listening on port: " + port)
0xivanov marked this conversation as resolved.
Show resolved Hide resolved

server := &http.Server{Addr: ":" + port}

// Start the server in a separate goroutine
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("Error starting server: %s\n", err)
}
}()

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)

// Wait for the termination signal
sig := <-signalCh
fmt.Printf("Received signal: %v\n", sig)

// Create a context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// Shutdown the server gracefully
if err := server.Shutdown(ctx); err != nil {
fmt.Printf("Error shutting down server: %s\n", err)
}

fmt.Println("Server shutdown complete.")
}

// Handler is a function that handles errors reported by a method handler.
type Handler func(context.Context, *jrpc2.Request, error) error

func HandleError(_ context.Context, request *jrpc2.Request, err error) error {
if err != nil {
// jrpc generic error
if jrpcError, ok := err.(*jrpc2.Error); ok {
return jrpcError
}
// hedera specific errors
if hederaErr, ok := err.(hedera.ErrHederaReceiptStatus); ok {
return response.NewHederaReceiptError(hederaErr)
}
if hederaErr, ok := err.(hedera.ErrHederaPreCheckStatus); ok {
return response.NewHederaPrecheckError(hederaErr)
}
// other errors
return response.InternalError
}
return nil
}

// this wraps the jrpc2.Handler as it invokes the ErrorHandler func if error is returned
func postHandler(handler Handler, h jrpc2.Handler) jrpc2.Handler {
return func(ctx context.Context, req *jrpc2.Request) (any, error) {
res, err := h(ctx, req)
if err != nil {
errorMessage := fmt.Sprintf("Error occurred processing JSON-RPC request: %s, Response error: %s", req, err)
fmt.Println(errorMessage)
return nil, handler(ctx, req, err)
}
return res, nil
}
}
52 changes: 52 additions & 0 deletions tck/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module github.com/hashgraph/hedera-sdk-go/tck

go 1.20

require (
github.com/creachadair/jrpc2 v1.1.2
github.com/hashgraph/hedera-sdk-go/v2 v2.42.0
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
)

replace github.com/hashgraph/hedera-sdk-go/v2 => ../

require (
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/creachadair/mds v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.1 // indirect
github.com/ethereum/go-ethereum v1.13.15 // indirect
github.com/hashgraph/hedera-protobufs-go v0.2.1-0.20240704081952-395063202d11 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading
Loading