diff --git a/Makefile b/Makefile index d90e876939..71e00bc10b 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ build: check-go check-git $(eval COMMIT_HASH = $(shell git rev-parse HEAD)) $(eval BRANCH = $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n')) $(eval TIME = $(shell date)) - go build -race -o polygon-edge -ldflags="\ + go build -o polygon-edge -ldflags="\ -X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)' \ -X 'github.com/0xPolygon/polygon-edge/versioning.Commit=$(COMMIT_HASH)'\ -X 'github.com/0xPolygon/polygon-edge/versioning.Branch=$(BRANCH)'\ @@ -59,7 +59,7 @@ generate-bsd-licenses: check-git .PHONY: test test: check-go - go test -race -shuffle=on -coverprofile coverage.out -timeout 20m `go list ./... | grep -v e2e` + go test -coverprofile coverage.out -timeout 20m `go list ./... | grep -v e2e` .PHONY: fuzz-test fuzz-test: check-go diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index ad36b3584d..13acc0de75 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -1002,7 +1002,7 @@ func (b *Blockchain) writeBody(batchWriter *storage.BatchWriter, block *types.Bl // Write txn lookups (txHash -> block) for _, txn := range block.Transactions { - batchWriter.PutTxLookup(txn.GetHash(), block.Hash()) + batchWriter.PutTxLookup(txn.Hash, block.Hash()) } return nil @@ -1046,7 +1046,7 @@ func (b *Blockchain) recoverFromFieldsInTransactions(transactions []*types.Trans sender, err := b.txSigner.Sender(tx) if err != nil { - b.logger.Warn("failed to recover from address in Tx", "hash", tx.GetHash(), "err", err) + b.logger.Warn("failed to recover from address in Tx", "hash", tx.Hash, "err", err) continue } diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index 5157c2b9cc..33b624d9cb 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -1096,7 +1096,7 @@ func TestBlockchain_VerifyBlockParent(t *testing.T) { // Set up the storage callback storageCallback := func(storage *storage.MockStorage) { storage.HookReadHeader(func(hash types.Hash) (*types.Header, error) { - return emptyHeader.Copy(), nil + return emptyHeader, nil }) } @@ -1110,7 +1110,7 @@ func TestBlockchain_VerifyBlockParent(t *testing.T) { // Create a dummy block whose parent hash will // not match the computed parent hash block := &types.Block{ - Header: emptyHeader.Copy(), + Header: emptyHeader, } assert.ErrorIs(t, blockchain.verifyBlockParent(block), ErrParentHashMismatch) @@ -1122,7 +1122,7 @@ func TestBlockchain_VerifyBlockParent(t *testing.T) { // Set up the storage callback storageCallback := func(storage *storage.MockStorage) { storage.HookReadHeader(func(hash types.Hash) (*types.Header, error) { - return emptyHeader.Copy(), nil + return emptyHeader, nil }) } @@ -1149,7 +1149,7 @@ func TestBlockchain_VerifyBlockParent(t *testing.T) { // Set up the storage callback storageCallback := func(storage *storage.MockStorage) { storage.HookReadHeader(func(hash types.Hash) (*types.Header, error) { - return emptyHeader.Copy(), nil + return emptyHeader, nil }) } @@ -1164,7 +1164,7 @@ func TestBlockchain_VerifyBlockParent(t *testing.T) { block := &types.Block{ Header: &types.Header{ Number: 10, - ParentHash: emptyHeader.Copy().Hash, + ParentHash: emptyHeader.Hash, }, } @@ -1180,7 +1180,7 @@ func TestBlockchain_VerifyBlockParent(t *testing.T) { // Set up the storage callback storageCallback := func(storage *storage.MockStorage) { storage.HookReadHeader(func(hash types.Hash) (*types.Header, error) { - return emptyHeader.Copy(), nil + return emptyHeader, nil }) } @@ -1286,7 +1286,7 @@ func TestBlockchain_VerifyBlockBody(t *testing.T) { storageCallback := func(storage *storage.MockStorage) { // This is used for parent fetching storage.HookReadHeader(func(hash types.Hash) (*types.Header, error) { - return emptyHeader.Copy(), nil + return emptyHeader, nil }) } @@ -1326,7 +1326,7 @@ func TestBlockchain_VerifyBlockBody(t *testing.T) { storageCallback := func(storage *storage.MockStorage) { // This is used for parent fetching storage.HookReadHeader(func(hash types.Hash) (*types.Header, error) { - return emptyHeader.Copy(), nil + return emptyHeader, nil }) } @@ -1385,13 +1385,12 @@ func TestBlockchain_CalculateBaseFee(t *testing.T) { } for i, test := range tests { - i := i test := test - t.Run(fmt.Sprintf("test case #%d", i+1), func(t *testing.T) { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { t.Parallel() - blockchain := &Blockchain{ + blockchain := Blockchain{ config: &chain.Chain{ Params: &chain.Params{ Forks: &chain.Forks{ diff --git a/chain/params.go b/chain/params.go index 23666b8df6..199dc5d9d5 100644 --- a/chain/params.go +++ b/chain/params.go @@ -132,16 +132,6 @@ func (f *Forks) At(block uint64) ForksInTime { } } -// Copy creates a deep copy of Forks map -func (f Forks) Copy() *Forks { - copiedForks := make(Forks, len(f)) - for key, value := range f { - copiedForks[key] = value.Copy() - } - - return &copiedForks -} - type Fork struct { Block uint64 `json:"block"` Params *forkmanager.ForkParams `json:"params,omitempty"` @@ -155,19 +145,6 @@ func (f Fork) Active(block uint64) bool { return block >= f.Block } -// Copy creates a deep copy of Fork -func (f Fork) Copy() Fork { - var fp *forkmanager.ForkParams - if f.Params != nil { - fp = f.Params.Copy() - } - - return Fork{ - Block: f.Block, - Params: fp, - } -} - // ForksInTime should contain all supported forks by current edge version type ForksInTime struct { Homestead, diff --git a/command/bridge/deposit/erc20/deposit_erc20.go b/command/bridge/deposit/erc20/deposit_erc20.go index 6ff0f6225a..95c8258236 100644 --- a/command/bridge/deposit/erc20/deposit_erc20.go +++ b/command/bridge/deposit/erc20/deposit_erc20.go @@ -195,8 +195,6 @@ func runCommand(cmd *cobra.Command, _ []string) { return fmt.Errorf("failed to create tx input: %w", err) } - var receipt *ethgo.Receipt - receipt, err = txRelayer.SendTransaction(depositTxn, depositorKey) if err != nil { return fmt.Errorf("receiver: %s, amount: %s, error: %w", receiver, amount, err) diff --git a/command/rootchain/fund/fund.go b/command/rootchain/fund/fund.go index 03238494d3..c1a2b9b3aa 100644 --- a/command/rootchain/fund/fund.go +++ b/command/rootchain/fund/fund.go @@ -120,10 +120,7 @@ func runCommand(cmd *cobra.Command, _ []string) { fundAddr := ethgo.Address(validatorAddr) txn := helper.CreateTransaction(ethgo.ZeroAddress, &fundAddr, nil, params.amountValues[i], true) - var ( - receipt *ethgo.Receipt - err error - ) + var receipt *ethgo.Receipt if params.deployerPrivateKey != "" { receipt, err = txRelayer.SendTransaction(txn, deployerKey) diff --git a/consensus/ibft/signer/extra_test.go b/consensus/ibft/signer/extra_test.go index 55622d38e8..49b11bc9da 100644 --- a/consensus/ibft/signer/extra_test.go +++ b/consensus/ibft/signer/extra_test.go @@ -21,6 +21,8 @@ func JSONMarshalHelper(t *testing.T, extra *IstanbulExtra) string { } func TestIstanbulExtraMarshalAndUnmarshal(t *testing.T) { + t.Parallel() + tests := []struct { name string extra *IstanbulExtra @@ -97,6 +99,8 @@ func TestIstanbulExtraMarshalAndUnmarshal(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() + // create original data originalExtraJSON := JSONMarshalHelper(t, test.extra) @@ -115,6 +119,8 @@ func TestIstanbulExtraMarshalAndUnmarshal(t *testing.T) { } func Test_packProposerSealIntoExtra(t *testing.T) { + t.Parallel() + newProposerSeal := []byte("new proposer seal") tests := []struct { @@ -191,6 +197,8 @@ func Test_packProposerSealIntoExtra(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() + originalProposerSeal := test.extra.ProposerSeal // create expected data @@ -225,6 +233,8 @@ func Test_packProposerSealIntoExtra(t *testing.T) { } func Test_packCommittedSealsAndRoundNumberIntoExtra(t *testing.T) { + t.Parallel() + tests := []struct { name string extra *IstanbulExtra @@ -321,6 +331,8 @@ func Test_packCommittedSealsAndRoundNumberIntoExtra(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() + originalCommittedSeals := test.extra.CommittedSeals // create expected data @@ -358,6 +370,8 @@ func Test_packCommittedSealsAndRoundNumberIntoExtra(t *testing.T) { } func Test_unmarshalRLPForParentCS(t *testing.T) { + t.Parallel() + tests := []struct { name string extra *IstanbulExtra @@ -409,6 +423,8 @@ func Test_unmarshalRLPForParentCS(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() + bytesData := test.extra.MarshalRLPTo(nil) assert.NoError(t, test.targetExtra.unmarshalRLPForParentCS(bytesData)) @@ -424,6 +440,8 @@ func Test_unmarshalRLPForParentCS(t *testing.T) { } func Test_putIbftExtra(t *testing.T) { + t.Parallel() + tests := []struct { name string header *types.Header @@ -475,6 +493,8 @@ func Test_putIbftExtra(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() + putIbftExtra(test.header, test.extra) expectedExtraHeader := make([]byte, IstanbulExtraVanity) diff --git a/consensus/ibft/signer/signer_test.go b/consensus/ibft/signer/signer_test.go index 36d89bba3d..c85ad29164 100644 --- a/consensus/ibft/signer/signer_test.go +++ b/consensus/ibft/signer/signer_test.go @@ -737,6 +737,8 @@ func TestVerifyCommittedSeal(t *testing.T) { } func TestSignerWriteCommittedSeals(t *testing.T) { + t.Parallel() + var round0 uint64 = 0 tests := []struct { @@ -847,6 +849,8 @@ func TestSignerWriteCommittedSeals(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + t.Parallel() + signer := newTestSingleKeyManagerSigner(test.keyManager) header, err := signer.WriteCommittedSeals(test.header, test.roundNumber, test.sealMap) diff --git a/consensus/polybft/block_builder.go b/consensus/polybft/block_builder.go index 5058654995..67e2e7d54d 100644 --- a/consensus/polybft/block_builder.go +++ b/consensus/polybft/block_builder.go @@ -140,7 +140,7 @@ func (b *BlockBuilder) Build(handler func(h *types.Header)) (*types.FullBlock, e // WriteTx applies given transaction to the state. If transaction apply fails, it reverts the saved snapshot. func (b *BlockBuilder) WriteTx(tx *types.Transaction) error { if tx.Gas > b.params.GasLimit { - b.params.Logger.Info("Transaction gas limit exceedes block gas limit", "hash", tx.GetHash(), + b.params.Logger.Info("Transaction gas limit exceedes block gas limit", "hash", tx.Hash, "tx gas limit", tx.Gas, "block gas limt", b.params.GasLimit) return txpool.ErrBlockLimitExceeded @@ -171,7 +171,7 @@ write: // execute transactions one by one finished, err := b.writeTxPoolTransaction(tx) if err != nil { - b.params.Logger.Debug("Fill transaction error", "hash", tx.GetHash(), "err", err) + b.params.Logger.Debug("Fill transaction error", "hash", tx.Hash, "err", err) } if finished { diff --git a/consensus/polybft/fsm.go b/consensus/polybft/fsm.go index 56d35f4f17..9a6176a68c 100644 --- a/consensus/polybft/fsm.go +++ b/consensus/polybft/fsm.go @@ -426,26 +426,24 @@ func (f *fsm) VerifyStateTransactions(transactions []*types.Transaction) error { continue } - txHash := tx.GetHash() - decodedStateTx, err := decodeStateTransaction(tx.Input) if err != nil { - return fmt.Errorf("unknown state transaction: tx = %v, err = %w", txHash, err) + return fmt.Errorf("unknown state transaction: tx = %v, err = %w", tx.Hash, err) } switch stateTxData := decodedStateTx.(type) { case *CommitmentMessageSigned: if !f.isEndOfSprint { - return fmt.Errorf("found commitment tx in block which should not contain it (tx hash=%s)", txHash) + return fmt.Errorf("found commitment tx in block which should not contain it (tx hash=%s)", tx.Hash) } if commitmentTxExists { - return fmt.Errorf("only one commitment tx is allowed per block (tx hash=%s)", txHash) + return fmt.Errorf("only one commitment tx is allowed per block (tx hash=%s)", tx.Hash) } commitmentTxExists = true - if err = verifyBridgeCommitmentTx(f.Height(), txHash, stateTxData, f.validators); err != nil { + if err = verifyBridgeCommitmentTx(f.Height(), tx.Hash, stateTxData, f.validators); err != nil { return err } case *contractsapi.CommitEpochValidatorSetFn: diff --git a/consensus/polybft/polybft.go b/consensus/polybft/polybft.go index 603bf5a336..e8aaac59d4 100644 --- a/consensus/polybft/polybft.go +++ b/consensus/polybft/polybft.go @@ -749,23 +749,21 @@ func (p *Polybft) PreCommitState(block *types.Block, _ *state.Transition) error continue } - txHash := tx.GetHash() - decodedStateTx, err := decodeStateTransaction(tx.Input) if err != nil { - return fmt.Errorf("unknown state transaction: tx=%v, error: %w", txHash, err) + return fmt.Errorf("unknown state transaction: tx=%v, error: %w", tx.Hash, err) } if signedCommitment, ok := decodedStateTx.(*CommitmentMessageSigned); ok { if commitmentTxExists { - return fmt.Errorf("only one commitment state tx is allowed per block: %v", txHash) + return fmt.Errorf("only one commitment state tx is allowed per block: %v", tx.Hash) } commitmentTxExists = true if err := verifyBridgeCommitmentTx( block.Number(), - txHash, + tx.Hash, signedCommitment, validator.NewValidatorSet(validators, p.logger)); err != nil { return err diff --git a/consensus/polybft/signer/signature_test.go b/consensus/polybft/signer/signature_test.go index ef2b62831e..3de35c682a 100644 --- a/consensus/polybft/signer/signature_test.go +++ b/consensus/polybft/signer/signature_test.go @@ -47,26 +47,20 @@ func Test_VerifySignature_NegativeCases(t *testing.T) { require.True(t, signature.Verify(blsKey.PublicKey(), validTestMsg, DomainValidatorSet)) - rawSig, err := signature.Marshal() - require.NoError(t, err) - t.Run("Wrong public key", func(t *testing.T) { t.Parallel() - sigTemp, err := UnmarshalSignature(rawSig) - require.NoError(t, err) - for i := 0; i < 100; i++ { x, randomG2, err := bn256.RandomG2(rand.Reader) require.NoError(t, err) publicKey := blsKey.PublicKey() publicKey.g2.Add(publicKey.g2, randomG2) // change public key g2 point - require.False(t, sigTemp.Verify(publicKey, validTestMsg, DomainValidatorSet)) + require.False(t, signature.Verify(publicKey, validTestMsg, DomainValidatorSet)) publicKey = blsKey.PublicKey() publicKey.g2.ScalarMult(publicKey.g2, x) // change public key g2 point - require.False(t, sigTemp.Verify(publicKey, validTestMsg, DomainValidatorSet)) + require.False(t, signature.Verify(publicKey, validTestMsg, DomainValidatorSet)) } }) @@ -76,14 +70,11 @@ func Test_VerifySignature_NegativeCases(t *testing.T) { msgCopy := make([]byte, len(validTestMsg)) copy(msgCopy, validTestMsg) - sigTemp, err := UnmarshalSignature(rawSig) - require.NoError(t, err) - for i := 0; i < len(msgCopy); i++ { b := msgCopy[i] msgCopy[i] = b + 1 - require.False(t, sigTemp.Verify(blsKey.PublicKey(), msgCopy, DomainValidatorSet)) + require.False(t, signature.Verify(blsKey.PublicKey(), msgCopy, DomainValidatorSet)) msgCopy[i] = b } }) @@ -95,13 +86,16 @@ func Test_VerifySignature_NegativeCases(t *testing.T) { x, randomG1, err := bn256.RandomG1(rand.Reader) require.NoError(t, err) - sigCopy, err := UnmarshalSignature(rawSig) + raw, err := signature.Marshal() + require.NoError(t, err) + + sigCopy, err := UnmarshalSignature(raw) require.NoError(t, err) sigCopy.g1.Add(sigCopy.g1, randomG1) // change signature require.False(t, sigCopy.Verify(blsKey.PublicKey(), validTestMsg, DomainValidatorSet)) - sigCopy, err = UnmarshalSignature(rawSig) + sigCopy, err = UnmarshalSignature(raw) require.NoError(t, err) sigCopy.g1.ScalarMult(sigCopy.g1, x) // change signature diff --git a/forkmanager/fork.go b/forkmanager/fork.go index 3e95a8a7cd..b55d40e882 100644 --- a/forkmanager/fork.go +++ b/forkmanager/fork.go @@ -40,23 +40,6 @@ type ForkParams struct { BlockTimeDrift *uint64 `json:"blockTimeDrift,omitempty"` } -// Copy creates a deep copy of ForkParams -func (fp *ForkParams) Copy() *ForkParams { - maxValSetSize := *fp.MaxValidatorSetSize - epochSize := *fp.EpochSize - sprintSize := *fp.SprintSize - blockTime := *fp.BlockTime - blockTimeDrift := *fp.BlockTimeDrift - - return &ForkParams{ - MaxValidatorSetSize: &maxValSetSize, - EpochSize: &epochSize, - SprintSize: &sprintSize, - BlockTime: &blockTime, - BlockTimeDrift: &blockTimeDrift, - } -} - // forkHandler defines one custom handler type forkHandler struct { // Handler should be active from block `FromBlockNumber`` diff --git a/gasprice/gasprice.go b/gasprice/gasprice.go index 0f9817083c..7c04dfd299 100644 --- a/gasprice/gasprice.go +++ b/gasprice/gasprice.go @@ -159,7 +159,7 @@ func (g *GasHelper) MaxPriorityFeePerGas() (*big.Int, error) { sender, err := signer.Sender(tx) if err != nil { - return fmt.Errorf("could not get sender of transaction: %s. Error: %w", tx.GetHash(), err) + return fmt.Errorf("could not get sender of transaction: %s. Error: %w", tx.Hash, err) } if sender != blockMiner { diff --git a/jsonrpc/debug_endpoint.go b/jsonrpc/debug_endpoint.go index 644ed52065..577b9a1622 100644 --- a/jsonrpc/debug_endpoint.go +++ b/jsonrpc/debug_endpoint.go @@ -168,7 +168,7 @@ func (d *Debug) TraceTransaction( defer cancel() - return d.store.TraceTxn(block, tx.GetHash(), tracer) + return d.store.TraceTxn(block, tx.Hash, tracer) }, ) } diff --git a/jsonrpc/filter_manager.go b/jsonrpc/filter_manager.go index 281b3d0c41..e618e80d8a 100644 --- a/jsonrpc/filter_manager.go +++ b/jsonrpc/filter_manager.go @@ -503,7 +503,7 @@ func (f *FilterManager) getLogsFromBlock(query *LogQuery, block *types.Block) ([ Data: log.Data, BlockNumber: argUint64(block.Header.Number), BlockHash: block.Header.Hash, - TxHash: block.Transactions[idx].GetHash(), + TxHash: block.Transactions[idx].Hash, TxIndex: argUint64(idx), LogIndex: argUint64(logIdx), }) @@ -813,7 +813,7 @@ func (f *FilterManager) appendLogsToFilters(header *block) error { for indx, receipt := range receipts { if receipt.TxHash == types.ZeroHash { // Extract tx Hash - receipt.TxHash = block.Transactions[indx].GetHash() + receipt.TxHash = block.Transactions[indx].Hash } // check the logs with the filters for _, log := range receipt.Logs { diff --git a/jsonrpc/types.go b/jsonrpc/types.go index 859db2736b..eab7ee9929 100644 --- a/jsonrpc/types.go +++ b/jsonrpc/types.go @@ -68,7 +68,7 @@ func toTransaction( V: argBig(*t.V), R: argBig(*t.R), S: argBig(*t.S), - Hash: t.GetHash(), + Hash: t.Hash, From: t.From, Type: argUint64(t.Type), BlockNumber: blockNumber, @@ -180,7 +180,7 @@ func toBlock(b *types.Block, fullTx bool) *block { } else { res.Transactions = append( res.Transactions, - transactionHash(txn.GetHash()), + transactionHash(txn.Hash), ) } } diff --git a/network/e2e_testing.go b/network/e2e_testing.go index b94edf6b84..91569c8d10 100644 --- a/network/e2e_testing.go +++ b/network/e2e_testing.go @@ -340,9 +340,9 @@ func MeshJoin(servers ...*Server) []error { var wg sync.WaitGroup - for sourceIdx := 0; sourceIdx < numServers; sourceIdx++ { - for destIdx := 0; destIdx < numServers; destIdx++ { - if destIdx > sourceIdx { + for indx := 0; indx < numServers; indx++ { + for innerIndx := 0; innerIndx < numServers; innerIndx++ { + if innerIndx > indx { wg.Add(1) go func(src, dest int) { @@ -354,9 +354,9 @@ func MeshJoin(servers ...*Server) []error { DefaultBufferTimeout, DefaultJoinTimeout, ); joinErr != nil { - appendJoinError(fmt.Errorf("unable to join peers %d -> %d, %w", src, dest, joinErr)) + appendJoinError(fmt.Errorf("unable to join peers, %w", joinErr)) } - }(sourceIdx, destIdx) + }(indx, innerIndx) } } } diff --git a/network/gossip_test.go b/network/gossip_test.go index 114f634c89..9655c7762d 100644 --- a/network/gossip_test.go +++ b/network/gossip_test.go @@ -9,7 +9,6 @@ import ( testproto "github.com/0xPolygon/polygon-edge/network/proto" "github.com/libp2p/go-libp2p/core/peer" - "github.com/stretchr/testify/require" ) func NumSubscribers(srv *Server, topic string) int { @@ -31,11 +30,13 @@ func WaitForSubscribers(ctx context.Context, srv *Server, topic string, expected } func TestSimpleGossip(t *testing.T) { - numServers := 9 + numServers := 10 sentMessage := fmt.Sprintf("%d", time.Now().UTC().Unix()) - servers, createErr := createServers(numServers, nil) - require.NoError(t, createErr, "Unable to create servers") + + if createErr != nil { + t.Fatalf("Unable to create servers, %v", createErr) + } messageCh := make(chan *testproto.GenericMessage) @@ -45,25 +46,32 @@ func TestSimpleGossip(t *testing.T) { }) joinErrors := MeshJoin(servers...) - require.Empty(t, joinErrors, "Unable to join servers [%d], %v", len(joinErrors), joinErrors) + if len(joinErrors) != 0 { + t.Fatalf("Unable to join servers [%d], %v", len(joinErrors), joinErrors) + } topicName := "msg-pub-sub" serverTopics := make([]*Topic, numServers) for i := 0; i < numServers; i++ { topic, topicErr := servers[i].NewTopic(topicName, &testproto.GenericMessage{}) - require.NoError(t, topicErr, "Unable to create topic") + if topicErr != nil { + t.Fatalf("Unable to create topic, %v", topicErr) + } serverTopics[i] = topic - subscribeErr := topic.Subscribe(func(obj interface{}, _ peer.ID) { + if subscribeErr := topic.Subscribe(func(obj interface{}, _ peer.ID) { // Everyone should relay they got the message genericMessage, ok := obj.(*testproto.GenericMessage) - require.True(t, ok, "invalid type assert") + if !ok { + t.Fatalf("invalid type assert") + } messageCh <- genericMessage - }) - require.NoError(t, subscribeErr, "Unable to subscribe to topic") + }); subscribeErr != nil { + t.Fatalf("Unable to subscribe to topic, %v", subscribeErr) + } } publisher := servers[0] @@ -72,14 +80,16 @@ func TestSimpleGossip(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - err := WaitForSubscribers(ctx, publisher, topicName, len(servers)-1) - require.NoError(t, err, "Unable to wait for subscribers") + if waitErr := WaitForSubscribers(ctx, publisher, topicName, len(servers)-1); waitErr != nil { + t.Fatalf("Unable to wait for subscribers, %v", waitErr) + } - err = publisherTopic.Publish( + if publishErr := publisherTopic.Publish( &testproto.GenericMessage{ Message: sentMessage, - }) - require.NoError(t, err, "Unable to publish message") + }); publishErr != nil { + t.Fatalf("Unable to publish message, %v", publishErr) + } messagesGossiped := 0 diff --git a/scripts/fuzzAll b/scripts/fuzzAll index cdca89749e..9f49af24d2 100755 --- a/scripts/fuzzAll +++ b/scripts/fuzzAll @@ -13,6 +13,6 @@ do do echo "Fuzzing $func in $file" parentDir=$(dirname $file) - go test $parentDir -race -run=$func -fuzz=$func -fuzztime=${fuzzTime}s + go test $parentDir -run=$func -fuzz=$func -fuzztime=${fuzzTime}s done done diff --git a/state/runtime/tracer/structtracer/tracer.go b/state/runtime/tracer/structtracer/tracer.go index 54dfcff20f..5136fe84a6 100644 --- a/state/runtime/tracer/structtracer/tracer.go +++ b/state/runtime/tracer/structtracer/tracer.go @@ -80,9 +80,6 @@ func (t *StructTracer) cancelled() bool { } func (t *StructTracer) Clear() { - t.cancelLock.Lock() - defer t.cancelLock.Unlock() - t.reason = nil t.interrupt = false t.logs = t.logs[:0] @@ -331,9 +328,6 @@ type StructTraceResult struct { } func (t *StructTracer) GetResult() (interface{}, error) { - t.cancelLock.RLock() - defer t.cancelLock.RUnlock() - if t.reason != nil { return nil, t.reason } diff --git a/syncer/client_test.go b/syncer/client_test.go index 37b91eced1..9a35d68998 100644 --- a/syncer/client_test.go +++ b/syncer/client_test.go @@ -45,7 +45,6 @@ func newTestSyncPeerClient(network Network, blockchain Blockchain) *syncPeerClie id: network.AddrInfo().ID.String(), peerStatusUpdateCh: make(chan *NoForkPeer, 1), peerConnectionUpdateCh: make(chan *event.PeerEvent, 1), - closeCh: make(chan struct{}), } // need to register protocol @@ -212,7 +211,7 @@ func TestStatusPubSub(t *testing.T) { assert.NoError(t, err) // close channel and wait for events - close(client.closeCh) + close(client.peerConnectionUpdateCh) wg.Wait() diff --git a/syncer/peers_test.go b/syncer/peers_test.go index 0dcb19aeef..765fcfe776 100644 --- a/syncer/peers_test.go +++ b/syncer/peers_test.go @@ -9,8 +9,8 @@ import ( "github.com/stretchr/testify/assert" ) -func getAllTestPeers() []*NoForkPeer { - return []*NoForkPeer{ +var ( + peers = []*NoForkPeer{ { ID: peer.ID("A"), Number: 10, @@ -27,7 +27,7 @@ func getAllTestPeers() []*NoForkPeer { Distance: big.NewInt(1), }, } -} +) func cloneNoForkPeers(peers []*NoForkPeer) []*NoForkPeer { clone := make([]*NoForkPeer, len(peers)) @@ -75,11 +75,14 @@ func peerMapToPeers(peerMap *PeerMap) []*NoForkPeer { func TestConstructor(t *testing.T) { t.Parallel() - peers := getAllTestPeers() + peers := peers + peerMap := NewPeerMap(peers) + expected := sortNoForkPeers( cloneNoForkPeers(peers), ) + actual := peerMapToPeers(peerMap) assert.Equal( @@ -92,9 +95,8 @@ func TestConstructor(t *testing.T) { func TestPutPeer(t *testing.T) { t.Parallel() - allPeers := getAllTestPeers() - initialPeers := allPeers[:1] - peers := allPeers[1:] + initialPeers := peers[:1] + peers := peers[1:] peerMap := NewPeerMap(initialPeers) @@ -116,8 +118,6 @@ func TestPutPeer(t *testing.T) { func TestBestPeer(t *testing.T) { t.Parallel() - allPeers := getAllTestPeers() - tests := []struct { name string skipList map[peer.ID]bool @@ -127,8 +127,8 @@ func TestBestPeer(t *testing.T) { { name: "should return best peer", skipList: nil, - peers: allPeers, - result: allPeers[2], + peers: peers, + result: peers[2], }, { name: "should return null in case of empty map", @@ -141,8 +141,8 @@ func TestBestPeer(t *testing.T) { skipList: map[peer.ID]bool{ peer.ID("C"): true, }, - peers: allPeers, - result: allPeers[1], + peers: peers, + result: peers[1], }, } diff --git a/txpool/event_subscription_test.go b/txpool/event_subscription_test.go index 5b0a0f9f0e..4a0abb8389 100644 --- a/txpool/event_subscription_test.go +++ b/txpool/event_subscription_test.go @@ -141,7 +141,7 @@ func TestEventSubscription_ProcessedEvents(t *testing.T) { } wg.Wait() - eventWaitCtx, eventWaitFn := context.WithTimeout(context.Background(), 10*time.Second) + eventWaitCtx, eventWaitFn := context.WithTimeout(context.Background(), time.Second*5) defer eventWaitFn() if _, err := tests.RetryUntilTimeout(eventWaitCtx, func() (interface{}, bool) { return nil, atomic.LoadInt64(&processed) < int64(testCase.expectedProcessed) diff --git a/txpool/operator.go b/txpool/operator.go index a11c1aa449..4d7bdb5186 100644 --- a/txpool/operator.go +++ b/txpool/operator.go @@ -43,7 +43,7 @@ func (p *TxPool) AddTxn(ctx context.Context, raw *proto.AddTxnReq) (*proto.AddTx } return &proto.AddTxnResp{ - TxHash: txn.GetHash().String(), + TxHash: txn.Hash.String(), }, nil } diff --git a/txpool/queue_account.go b/txpool/queue_account.go index 4d1d5c6044..d0e8a9dc24 100644 --- a/txpool/queue_account.go +++ b/txpool/queue_account.go @@ -100,14 +100,6 @@ func (q *accountQueue) length() uint64 { return uint64(q.queue.Len()) } -// lengthWithLock returns the number of transactions in the queue (thread-safe) -func (q *accountQueue) lengthWithLock() uint64 { - q.lock(false) - defer q.unlock() - - return q.length() -} - // transactions sorted by nonce (ascending) type minNonceQueue []*types.Transaction diff --git a/txpool/txpool.go b/txpool/txpool.go index 27e8e7bd8b..b7ae64338d 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -441,7 +441,7 @@ func (p *TxPool) dropAccount(account *account, nextNonce uint64, tx *types.Trans dropped = account.enqueued.clear() clearAccountQueue(dropped) - p.eventManager.signalEvent(proto.EventType_DROPPED, tx.GetHash()) + p.eventManager.signalEvent(proto.EventType_DROPPED, tx.Hash) if p.logger.IsDebug() { p.logger.Debug("dropped account txs", @@ -475,7 +475,7 @@ func (p *TxPool) Demote(tx *types.Transaction) { account.incrementDemotions() - p.eventManager.signalEvent(proto.EventType_DEMOTED, tx.GetHash()) + p.eventManager.signalEvent(proto.EventType_DEMOTED, tx.Hash) } // ResetWithHeaders processes the transactions from the new @@ -769,7 +769,7 @@ func (p *TxPool) pruneAccountsWithNonceHoles() { // (only once) and an enqueueRequest is signaled. func (p *TxPool) addTx(origin txOrigin, tx *types.Transaction) error { if p.logger.IsDebug() { - p.logger.Debug("add tx", "origin", origin.String(), "hash", tx.GetHash().String()) + p.logger.Debug("add tx", "origin", origin.String(), "hash", tx.Hash.String()) } // validate incoming tx @@ -814,7 +814,7 @@ func (p *TxPool) addTx(origin txOrigin, tx *types.Transaction) error { // try to find if there is transaction with same nonce for this account oldTxWithSameNonce := account.nonceToTx.get(tx.Nonce) if oldTxWithSameNonce != nil { - if oldTxWithSameNonce.GetHash() == tx.GetHash() { + if oldTxWithSameNonce.Hash == tx.Hash { metrics.IncrCounter([]string{txPoolMetrics, "already_known_tx"}, 1) return ErrAlreadyKnown @@ -882,14 +882,13 @@ func (p *TxPool) addTx(origin txOrigin, tx *types.Transaction) error { } func (p *TxPool) invokePromotion(tx *types.Transaction, callPromote bool) { - txHash := tx.GetHash() - p.eventManager.signalEvent(proto.EventType_ADDED, txHash) + p.eventManager.signalEvent(proto.EventType_ADDED, tx.Hash) if p.logger.IsDebug() { - p.logger.Debug("enqueue request", "hash", txHash.String()) + p.logger.Debug("enqueue request", "hash", tx.Hash.String()) } - p.eventManager.signalEvent(proto.EventType_ENQUEUED, txHash) + p.eventManager.signalEvent(proto.EventType_ENQUEUED, tx.Hash) if callPromote { select { @@ -955,13 +954,13 @@ func (p *TxPool) addGossipTx(obj interface{}, _ peer.ID) { if err := p.addTx(gossip, tx); err != nil { if errors.Is(err, ErrAlreadyKnown) { if p.logger.IsDebug() { - p.logger.Debug("rejecting known tx (gossip)", "hash", tx.GetHash().String()) + p.logger.Debug("rejecting known tx (gossip)", "hash", tx.Hash.String()) } return } - p.logger.Error("failed to add broadcast tx", "err", err, "hash", tx.GetHash().String()) + p.logger.Error("failed to add broadcast tx", "err", err, "hash", tx.Hash.String()) } } @@ -1083,7 +1082,7 @@ func (p *TxPool) Length() uint64 { // toHash returns the hash(es) of given transaction(s) func toHash(txs ...*types.Transaction) (hashes []types.Hash) { for _, tx := range txs { - hashes = append(hashes, tx.GetHash()) + hashes = append(hashes, tx.Hash) } return diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index 7bd23c254c..db43b6d551 100644 --- a/txpool/txpool_test.go +++ b/txpool/txpool_test.go @@ -34,6 +34,14 @@ const ( validGasLimit uint64 = 4712350 ) +var ( + forks = (&chain.Forks{ + chain.Homestead: chain.NewFork(0), + chain.Istanbul: chain.NewFork(0), + chain.London: chain.NewFork(0), + }) +) + // addresses used in tests var ( addr1 = types.Address{0x1} @@ -83,7 +91,7 @@ func newTestPoolWithSlots(maxSlots uint64, mockStore ...store) (*TxPool, error) return NewTxPool( hclog.NewNullLogger(), - getDefaultEnabledForks(), + forks, storeToUse, nil, nil, @@ -2129,7 +2137,7 @@ func Test_TxPool_validateTx(t *testing.T) { t.Run("tx input larger than the TxPoolMaxInitCodeSize", func(t *testing.T) { t.Parallel() pool := setupPool() - pool.forks = chain.AllForksEnabled.Copy() + pool.forks = chain.AllForksEnabled input := make([]byte, state.TxPoolMaxInitCodeSize+1) _, err := rand.Read(input) @@ -2148,7 +2156,7 @@ func Test_TxPool_validateTx(t *testing.T) { t.Run("tx input the same as TxPoolMaxInitCodeSize", func(t *testing.T) { t.Parallel() pool := setupPool() - pool.forks = chain.AllForksEnabled.Copy() + pool.forks = chain.AllForksEnabled input := make([]byte, state.TxPoolMaxInitCodeSize) _, err := rand.Read(input) @@ -2157,7 +2165,6 @@ func Test_TxPool_validateTx(t *testing.T) { tx := newTx(defaultAddr, 0, 1) tx.To = nil tx.Input = input - tx.GasPrice = new(big.Int).SetUint64(pool.GetBaseFee()) assert.NoError(t, pool.validateTx(signTx(tx)), @@ -2272,7 +2279,7 @@ func Test_TxPool_validateTx(t *testing.T) { t.Run("eip-1559 tx placed without eip-1559 fork enabled", func(t *testing.T) { t.Parallel() pool := setupPool() - pool.forks = chain.AllForksEnabled.Copy() + pool.forks = chain.AllForksEnabled pool.forks.RemoveFork(chain.London) tx := newTx(defaultAddr, 0, 1) @@ -2606,10 +2613,10 @@ func TestResetAccounts_Enqueued(t *testing.T) { newTx(addr1, 4, 1), }, addr2: { - newTx(addr2, 3, 3), - newTx(addr2, 4, 3), - newTx(addr2, 5, 3), - newTx(addr2, 6, 3), + newTx(addr2, 3, 1), + newTx(addr2, 4, 1), + newTx(addr2, 5, 1), + newTx(addr2, 6, 1), }, addr3: { newTx(addr3, 7, 1), @@ -3521,7 +3528,7 @@ func TestAddTxsInOrder(t *testing.T) { wg.Wait() - time.Sleep(100 * time.Millisecond) + time.Sleep(time.Second * 2) pool.Close() @@ -3529,8 +3536,8 @@ func TestAddTxsInOrder(t *testing.T) { acc := pool.accounts.get(addrtx.addr) require.NotNil(t, acc) - assert.Equal(t, uint64(0), acc.enqueued.lengthWithLock()) - assert.Len(t, acc.nonceToTx.mapping, int(acc.promoted.length())) + assert.Equal(t, uint64(0), acc.enqueued.length()) + assert.Equal(t, len(acc.nonceToTx.mapping), int(acc.promoted.length())) } } @@ -3691,16 +3698,6 @@ func TestAddTx_TxReplacement(t *testing.T) { assert.Equal(t, ac2.enqueued.queue[0], tx1) } -// getDefaultEnabledForks returns hardcoded set of forks -// that are enabled by default from the genesis block -func getDefaultEnabledForks() *chain.Forks { - return &chain.Forks{ - chain.Homestead: chain.NewFork(0), - chain.Istanbul: chain.NewFork(0), - chain.London: chain.NewFork(0), - } -} - func BenchmarkAddTxTime(b *testing.B) { b.Run("benchmark add one tx", func(b *testing.B) { signer := crypto.NewEIP155Signer(100, true) diff --git a/txrelayer/txrelayer.go b/txrelayer/txrelayer.go index daec6397ac..9517a65a99 100644 --- a/txrelayer/txrelayer.go +++ b/txrelayer/txrelayer.go @@ -207,38 +207,31 @@ func (t *TxRelayerImpl) sendTransactionLocked(txn *ethgo.Transaction, key ethgo. // SendTransactionLocal sends non-signed transaction // (this function is meant only for testing purposes and is about to be removed at some point) func (t *TxRelayerImpl) SendTransactionLocal(txn *ethgo.Transaction) (*ethgo.Receipt, error) { - txnHash, err := t.sendTransactionLocalLocked(txn) - if err != nil { - return nil, err - } - - return t.waitForReceipt(txnHash) -} - -func (t *TxRelayerImpl) sendTransactionLocalLocked(txn *ethgo.Transaction) (ethgo.Hash, error) { - t.lock.Lock() - defer t.lock.Unlock() - accounts, err := t.client.Eth().Accounts() if err != nil { - return ethgo.ZeroHash, err + return nil, err } if len(accounts) == 0 { - return ethgo.ZeroHash, errNoAccounts + return nil, errNoAccounts } txn.From = accounts[0] gasLimit, err := t.client.Eth().EstimateGas(ConvertTxnToCallMsg(txn)) if err != nil { - return ethgo.ZeroHash, err + return nil, err } txn.Gas = gasLimit txn.GasPrice = defaultGasPrice - return t.client.Eth().SendTransaction(txn) + txnHash, err := t.client.Eth().SendTransaction(txn) + if err != nil { + return nil, err + } + + return t.waitForReceipt(txnHash) } func (t *TxRelayerImpl) waitForReceipt(hash ethgo.Hash) (*ethgo.Receipt, error) { diff --git a/types/transaction.go b/types/transaction.go index 7d613702fd..126545f585 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -3,7 +3,6 @@ package types import ( "fmt" "math/big" - "sync" "sync/atomic" "github.com/0xPolygon/polygon-edge/helper/common" @@ -66,8 +65,6 @@ type Transaction struct { ChainID *big.Int - lock sync.RWMutex - // Cache size atomic.Pointer[uint64] } @@ -86,16 +83,7 @@ func (t *Transaction) ComputeHash(blockNumber uint64) *Transaction { func (t *Transaction) Copy() *Transaction { tt := new(Transaction) - tt.Nonce = t.Nonce - tt.From = t.From - tt.Gas = t.Gas - tt.Type = t.Type - tt.Hash = t.Hash - - if t.To != nil { - newAddress := *t.To - tt.To = &newAddress - } + *tt = *t tt.GasPrice = new(big.Int) if t.GasPrice != nil { @@ -129,32 +117,12 @@ func (t *Transaction) Copy() *Transaction { tt.S = new(big.Int).Set(t.S) } - if t.ChainID != nil { - tt.ChainID = new(big.Int).Set(t.ChainID) - } - tt.Input = make([]byte, len(t.Input)) copy(tt.Input[:], t.Input[:]) return tt } -// GetHash reads transaction hash in a thread-safe manner -func (t *Transaction) GetHash() Hash { - t.lock.RLock() - defer t.lock.RUnlock() - - return t.Hash -} - -// SetHash sets transaction hash in a thread-safe manner -func (t *Transaction) SetHash(hash Hash) { - t.lock.Lock() - defer t.lock.Unlock() - - t.Hash = hash -} - // Cost returns gas * gasPrice + value func (t *Transaction) Cost() *big.Int { var factor *big.Int diff --git a/types/transaction_fork_hash.go b/types/transaction_fork_hash.go index 77fb6ac0fc..6c84461e5d 100644 --- a/types/transaction_fork_hash.go +++ b/types/transaction_fork_hash.go @@ -47,9 +47,7 @@ func (th *TransactionHashForkV1) ComputeHash(t *Transaction) { t.ChainID = big.NewInt(0) v := t.MarshalRLPWith(ar) - hashTmp := ZeroHash - hash.WriteRlp(hashTmp[:0], v) - t.SetHash(hashTmp) + hash.WriteRlp(t.Hash[:0], v) t.ChainID = chainID @@ -66,9 +64,7 @@ func (th *TransactionHashForkV2) SerializeForRootCalculation(t *Transaction, _ * func (th *TransactionHashForkV2) ComputeHash(t *Transaction) { hash := keccak.DefaultKeccakPool.Get() - hashTmp := ZeroHash - hash.WriteFn(hashTmp[:0], t.MarshalRLPTo) - t.SetHash(hashTmp) + hash.WriteFn(t.Hash[:0], t.MarshalRLPTo) keccak.DefaultKeccakPool.Put(hash) }