From 1857c850d7b23ad497e09c90fc36060672a6c408 Mon Sep 17 00:00:00 2001 From: Yarik Bratashchuk Date: Thu, 20 Feb 2025 16:24:01 +0100 Subject: [PATCH 1/4] fix: add mu to Dummy executor (#58) * Add mu to Dummy executor * Addd GetStateRoot method --- test/dummy.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/test/dummy.go b/test/dummy.go index 74532ac..b9b5699 100644 --- a/test/dummy.go +++ b/test/dummy.go @@ -2,11 +2,11 @@ package test import ( "bytes" - "context" "crypto/sha512" "fmt" "slices" + "sync" "time" "github.com/rollkit/go-execution/types" @@ -14,6 +14,7 @@ import ( // DummyExecutor is a dummy implementation of the DummyExecutor interface for testing type DummyExecutor struct { + mu sync.RWMutex // Add mutex for thread safety stateRoot types.Hash pendingRoots map[uint64]types.Hash maxBytes uint64 @@ -32,6 +33,9 @@ func NewDummyExecutor() *DummyExecutor { // 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) { + e.mu.Lock() + defer e.mu.Unlock() + hash := sha512.New() hash.Write(e.stateRoot) e.stateRoot = hash.Sum(nil) @@ -40,17 +44,27 @@ func (e *DummyExecutor) InitChain(ctx context.Context, genesisTime time.Time, in // 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) { - txs := e.injectedTxs + e.mu.RLock() + defer e.mu.RUnlock() + + txs := make([]types.Tx, len(e.injectedTxs)) + copy(txs, e.injectedTxs) // Create a copy to avoid external modifications 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.mu.Lock() + defer e.mu.Unlock() + e.injectedTxs = append(e.injectedTxs, tx) } // 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.mu.Lock() + defer e.mu.Unlock() + hash := sha512.New() hash.Write(prevStateRoot) for _, tx := range txs { @@ -64,6 +78,9 @@ func (e *DummyExecutor) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHei // SetFinal marks block at given height as finalized. func (e *DummyExecutor) SetFinal(ctx context.Context, blockHeight uint64) error { + e.mu.Lock() + defer e.mu.Unlock() + if pending, ok := e.pendingRoots[blockHeight]; ok { e.stateRoot = pending delete(e.pendingRoots, blockHeight) @@ -77,3 +94,11 @@ func (e *DummyExecutor) removeExecutedTxs(txs []types.Tx) { return slices.ContainsFunc(txs, func(t types.Tx) bool { return bytes.Equal(tx, t) }) }) } + +// GetStateRoot returns the current state root in a thread-safe manner +func (e *DummyExecutor) GetStateRoot() types.Hash { + e.mu.RLock() + defer e.mu.RUnlock() + + return e.stateRoot +} From c1157f4b9272701a99b92402f96416107b57d02d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 16:31:48 +0100 Subject: [PATCH 2/4] chore(deps): bump google.golang.org/grpc from 1.69.4 to 1.70.0 (#57) Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.69.4 to 1.70.0. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.69.4...v1.70.0) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 6 +++--- go.sum | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index c104f8c..24a9ad1 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/celestiaorg/go-header v0.6.4 github.com/cosmos/gogoproto v1.7.0 github.com/stretchr/testify v1.10.0 - google.golang.org/grpc v1.69.4 + google.golang.org/grpc v1.70.0 ) require ( @@ -49,8 +49,8 @@ require ( golang.org/x/net v0.33.0 // indirect golang.org/x/sys v0.28.0 // indirect golang.org/x/text v0.21.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect - google.golang.org/protobuf v1.35.1 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a // indirect + google.golang.org/protobuf v1.35.2 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.2.1 // indirect diff --git a/go.sum b/go.sum index c8df442..04edeee 100644 --- a/go.sum +++ b/go.sum @@ -221,16 +221,16 @@ github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY= -go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE= -go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE= -go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY= -go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk= -go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0= -go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc= -go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8= -go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys= -go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A= +go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U= +go.opentelemetry.io/otel v1.32.0/go.mod h1:00DCVSB0RQcnzlwyTfqtxSm+DRr9hpYrHjNGiBHVQIg= +go.opentelemetry.io/otel/metric v1.32.0 h1:xV2umtmNcThh2/a/aCP+h64Xx5wsj8qqnkYZktzNa0M= +go.opentelemetry.io/otel/metric v1.32.0/go.mod h1:jH7CIbbK6SH2V2wE16W05BHCtIDzauciCRLoc/SyMv8= +go.opentelemetry.io/otel/sdk v1.32.0 h1:RNxepc9vK59A8XsgZQouW8ue8Gkb4jpWtJm9ge5lEG4= +go.opentelemetry.io/otel/sdk v1.32.0/go.mod h1:LqgegDBjKMmb2GC6/PrTnteJG39I8/vJCAP9LlJXEjU= +go.opentelemetry.io/otel/sdk/metric v1.32.0 h1:rZvFnvmvawYb0alrYkjraqJq0Z4ZUJAiyYCU9snn1CU= +go.opentelemetry.io/otel/sdk/metric v1.32.0/go.mod h1:PWeZlq0zt9YkYAp3gjKZ0eicRYvOh1Gd+X99x6GHpCQ= +go.opentelemetry.io/otel/trace v1.32.0 h1:WIC9mYrXf8TmY/EXuULKc8hR17vE+Hjv2cssQDe03fM= +go.opentelemetry.io/otel/trace v1.32.0/go.mod h1:+i4rkvCraA+tG6AzwloGaCtkx53Fa+L+V8e9a7YvhT8= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc= go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= @@ -303,12 +303,12 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 h1:X58yt85/IXCx0Y3ZwN6sEIKZzQtDEYaBWrDvErdXrRE= -google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI= -google.golang.org/grpc v1.69.4 h1:MF5TftSMkd8GLw/m0KM6V8CMOCY6NZ1NQDPGFgbTt4A= -google.golang.org/grpc v1.69.4/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4= -google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= -google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a h1:hgh8P4EuoxpsuKMXX/To36nOFD7vixReXgn8lPGnt+o= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU= +google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ= +google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= +google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= From 18a0a594a6285af67fd47ebc9038653a99e1aede Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:07:31 +0100 Subject: [PATCH 3/4] chore(deps): bump golangci/golangci-lint-action from 6.3.2 to 6.5.0 (#59) Bumps [golangci/golangci-lint-action](https://github.com/golangci/golangci-lint-action) from 6.3.2 to 6.5.0. - [Release notes](https://github.com/golangci/golangci-lint-action/releases) - [Commits](https://github.com/golangci/golangci-lint-action/compare/v6.3.2...v6.5.0) --- updated-dependencies: - dependency-name: golangci/golangci-lint-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index da2851d..c656a92 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -23,7 +23,7 @@ jobs: **/**.go go.mod go.sum - - uses: golangci/golangci-lint-action@v6.3.2 + - uses: golangci/golangci-lint-action@v6.5.0 with: version: latest args: --timeout 10m From ae86f6994505e08055310ff9c5a2fa0dd361045a Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:26:10 +0100 Subject: [PATCH 4/4] fix: uses of copy (#62) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomasz ZdybaƂ --- proxy/grpc/client.go | 4 ++-- proxy/grpc/client_server_test.go | 5 +++-- proxy/grpc/server.go | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/proxy/grpc/client.go b/proxy/grpc/client.go index 7b14f73..b8114ab 100644 --- a/proxy/grpc/client.go +++ b/proxy/grpc/client.go @@ -61,7 +61,7 @@ func (c *Client) InitChain(ctx context.Context, genesisTime time.Time, initialHe return types.Hash{}, 0, err } - var stateRoot types.Hash + stateRoot := make([]byte, len(resp.StateRoot)) copy(stateRoot[:], resp.StateRoot) return stateRoot, resp.MaxBytes, nil @@ -99,7 +99,7 @@ func (c *Client) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHeight uin return types.Hash{}, 0, err } - var updatedStateRoot types.Hash + updatedStateRoot := make([]byte, len(resp.UpdatedStateRoot)) copy(updatedStateRoot[:], resp.UpdatedStateRoot) return updatedStateRoot, resp.MaxBytes, nil diff --git a/proxy/grpc/client_server_test.go b/proxy/grpc/client_server_test.go index af9bc34..3500363 100644 --- a/proxy/grpc/client_server_test.go +++ b/proxy/grpc/client_server_test.go @@ -56,10 +56,11 @@ func TestClientServer(t *testing.T) { chainID := "test-chain" // initialize a new Hash with a fixed size - expectedStateRoot := make([]byte, 32) + expectedStateRoot := types.Hash(make([]byte, 32)) copy(expectedStateRoot, []byte{1, 2, 3}) - var stateRootHash types.Hash + stateRootHash := types.Hash(make([]byte, 32)) copy(stateRootHash[:], expectedStateRoot) + assert.Equal(t, expectedStateRoot, stateRootHash[:]) expectedMaxBytes := uint64(1000000) diff --git a/proxy/grpc/server.go b/proxy/grpc/server.go index 629360e..65953ed 100644 --- a/proxy/grpc/server.go +++ b/proxy/grpc/server.go @@ -83,7 +83,7 @@ func (s *Server) ExecuteTxs(ctx context.Context, req *pb.ExecuteTxsRequest) (*pb txs[i] = tx } - var prevStateRoot types.Hash + prevStateRoot := make([]byte, len(req.PrevStateRoot)) copy(prevStateRoot[:], req.PrevStateRoot) updatedStateRoot, maxBytes, err := s.exec.ExecuteTxs(