Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Apr 2, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 5
test: enhance DummyExecutor#39
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb575a2
test: enhance DummyExecutor
tzdybal aea8bf3
chore: fix linter errors
tzdybal 6f65726
feat: handle signals, use channel instead of WaitGroup
tzdybal 09d6483
refactor: improve flag parsing and logs
tzdybal 71c9376
fix: remove block from pending after finalization
tzdybal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package main | ||
| import ( | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "net" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| "google.golang.org/grpc" | ||
| grpcproxy "github.com/rollkit/go-execution/proxy/grpc" | ||
| "github.com/rollkit/go-execution/test" | ||
| pb "github.com/rollkit/go-execution/types/pb/execution" | ||
| ) | ||
| func main() { | ||
| listenAddress, err := parseListenAddress() | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse listen address: %v\n", err) | ||
| } | ||
| listener, err := net.Listen("tcp4", listenAddress) | ||
| if err != nil { | ||
| log.Fatalf("Failed to listen on %q: %v\n", listenAddress, err) | ||
| } | ||
| defer func() { | ||
| _ = listener.Close() | ||
| }() | ||
| log.Println("Creating Dummy Executor and gRPC server") | ||
| dummy := test.NewDummyExecutor() | ||
| server := grpcproxy.NewServer(dummy, grpcproxy.DefaultConfig()) | ||
| s := grpc.NewServer() | ||
| pb.RegisterExecutionServiceServer(s, server) | ||
| // Setup signal handling | ||
| sigChan := make(chan os.Signal, 1) | ||
| signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) | ||
| doneChan := make(chan interface{}, 1) | ||
| go func() { | ||
| log.Printf("Serving (%s)...\n", listenAddress) | ||
| log.Println("Type Ctrl+C to shutdown") | ||
| if err := s.Serve(listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) { | ||
| log.Fatalf("Server exited with error: %v\n", err) | ||
| } | ||
| doneChan <- nil | ||
| }() | ||
| // Handle shutdown signal | ||
| go func() { | ||
| <-sigChan | ||
| log.Println("Received shutdown signal") | ||
| s.GracefulStop() | ||
| }() | ||
| <-doneChan | ||
| log.Println("Server stopped") | ||
| } | ||
| func parseListenAddress() (string, error) { | ||
| var listenAddress string | ||
| flag.StringVar(&listenAddress, "address", "127.0.0.1:40041", "gRPC server listen address") | ||
| flag.Parse() | ||
| _, port, err := net.SplitHostPort(listenAddress) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid address format %q: %v", listenAddress, err) | ||
| } | ||
| if port == "" { | ||
| return "", errors.New("port cannot be empty") | ||
| } | ||
| return listenAddress, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,45 +2,69 @@ package test | ||
| import ( | ||
| "context" | ||
| "crypto/sha512" | ||
| "fmt" | ||
| "time" | ||
| "github.com/rollkit/go-execution/types" | ||
| ) | ||
| // DummyExecutor is a dummy implementation of the DummyExecutor interface for testing | ||
| type DummyExecutor struct { | ||
| stateRoot types.Hash | ||
| maxBytes uint64 | ||
| txs []types.Tx | ||
| stateRoot types.Hash | ||
| pendingRoots map[uint64]types.Hash | ||
| maxBytes uint64 | ||
| injectedTxs []types.Tx | ||
| } | ||
| // NewDummyExecutor creates a new dummy DummyExecutor instance | ||
| func NewDummyExecutor() *DummyExecutor { | ||
| return &DummyExecutor{ | ||
| stateRoot: types.Hash{1, 2, 3}, | ||
| maxBytes: 1000000, | ||
| txs: make([]types.Tx, 0), | ||
| stateRoot: types.Hash{1, 2, 3}, | ||
| pendingRoots: make(map[uint64]types.Hash), | ||
| maxBytes: 1000000, | ||
| } | ||
| } | ||
| // InitChain initializes the chain state with the given genesis time, initial height, and chain ID. | ||
| // It returns the state root hash, the maximum byte size, and an error if the initialization fails. | ||
| func (e *DummyExecutor) InitChain(ctx context.Context, genesisTime time.Time, initialHeight uint64, chainID string) (types.Hash, uint64, error) { | ||
| hash := sha512.New() | ||
| hash.Write(e.stateRoot) | ||
| e.stateRoot = hash.Sum(nil) | ||
| return e.stateRoot, e.maxBytes, nil | ||
| } | ||
| // GetTxs returns the list of transactions (types.Tx) within the DummyExecutor instance and an error if any. | ||
| func (e *DummyExecutor) GetTxs(context.Context) ([]types.Tx, error) { | ||
| return e.txs, nil | ||
| txs := e.injectedTxs | ||
| e.injectedTxs = nil | ||
| return txs, nil | ||
| } | ||
| // InjectTx adds a transaction to the internal list of injected transactions in the DummyExecutor instance. | ||
| func (e *DummyExecutor) InjectTx(tx types.Tx) { | ||
| e.injectedTxs = append(e.injectedTxs, tx) | ||
tzdybal marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // ExecuteTxs simulate execution of transactions. | ||
| func (e *DummyExecutor) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHeight uint64, timestamp time.Time, prevStateRoot types.Hash) (types.Hash, uint64, error) { | ||
| e.txs = append(e.txs, txs...) | ||
| return e.stateRoot, e.maxBytes, nil | ||
| hash := sha512.New() | ||
| hash.Write(prevStateRoot) | ||
| for _, tx := range txs { | ||
| hash.Write(tx) | ||
| } | ||
| pending := hash.Sum(nil) | ||
| e.pendingRoots[blockHeight] = pending | ||
| return pending, e.maxBytes, nil | ||
| } | ||
| // SetFinal marks block at given height as finalized. Currently not implemented. | ||
| // SetFinal marks block at given height as finalized. | ||
| func (e *DummyExecutor) SetFinal(ctx context.Context, blockHeight uint64) error { | ||
| return nil | ||
| if pending, ok := e.pendingRoots[blockHeight]; ok { | ||
| e.stateRoot = pending | ||
| delete(e.pendingRoots, blockHeight) | ||
| return nil | ||
| } | ||
| return fmt.Errorf("cannot set finalized block at height %d", blockHeight) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.