|
4 | 4 | "context" |
5 | 5 | "crypto/ecdsa" |
6 | 6 | "errors" |
| 7 | + "math" |
7 | 8 | "math/big" |
8 | 9 | "sync" |
9 | 10 | "testing" |
@@ -729,6 +730,64 @@ func TestExecutorOCCConflictingTransfersMatchSequential(t *testing.T) { |
729 | 730 | require.Equal(t, big.NewInt(int64(txCount*3)), occState.GetBalance(recipient)) |
730 | 731 | } |
731 | 732 |
|
| 733 | +// A block large enough for the parallel validation pass and the sharded merge, mixing independent |
| 734 | +// transfers, nonce chains from one sender, and a hot recipient, must produce the sequential result |
| 735 | +// exactly: transaction results, receipts, cumulative gas and the canonical changeset. |
| 736 | +func TestExecutorOCCLargeMixedBlockMatchesSequential(t *testing.T) { |
| 737 | + chainID := big.NewInt(testChainID) |
| 738 | + hot := testAddress(0xaa) |
| 739 | + seqState := NewMemoryState() |
| 740 | + occState := NewMemoryState() |
| 741 | + var rawTxs [][]byte |
| 742 | + sign := func(key *ecdsa.PrivateKey, nonce uint64, to common.Address, value int64) { |
| 743 | + rawTxs = append(rawTxs, signLegacyTxWithGasPrice(t, key, chainID, nonce, &to, big.NewInt(value), nil, 100_000, big.NewInt(1))) |
| 744 | + } |
| 745 | + fund := func(key *ecdsa.PrivateKey) { |
| 746 | + sender := crypto.PubkeyToAddress(key.PublicKey) |
| 747 | + seqState.SetBalance(sender, big.NewInt(1_000_000_000)) |
| 748 | + occState.SetBalance(sender, big.NewInt(1_000_000_000)) |
| 749 | + } |
| 750 | + for i := range 300 { |
| 751 | + key, err := crypto.GenerateKey() |
| 752 | + require.NoError(t, err) |
| 753 | + fund(key) |
| 754 | + switch { |
| 755 | + case i%7 == 0: |
| 756 | + sign(key, 0, hot, 3) |
| 757 | + case i%29 == 0: |
| 758 | + for nonce := range 3 { |
| 759 | + sign(key, uint64(nonce), common.BigToAddress(big.NewInt(int64(50_000+i))), 5) //nolint:gosec // nonce is non-negative. |
| 760 | + } |
| 761 | + default: |
| 762 | + sign(key, 0, common.BigToAddress(big.NewInt(int64(50_000+i))), 7) |
| 763 | + } |
| 764 | + } |
| 765 | + // A late transaction reads the hot balance that the earlier ones credit, so it is rerun after the |
| 766 | + // parallel pass has accepted the run before it. |
| 767 | + hotKey, err := crypto.GenerateKey() |
| 768 | + require.NoError(t, err) |
| 769 | + hotSender := crypto.PubkeyToAddress(hotKey.PublicKey) |
| 770 | + seqState.SetBalance(hotSender, big.NewInt(1_000_000_000)) |
| 771 | + occState.SetBalance(hotSender, big.NewInt(1_000_000_000)) |
| 772 | + seqState.SetBalance(hot, big.NewInt(1)) |
| 773 | + occState.SetBalance(hot, big.NewInt(1)) |
| 774 | + sign(hotKey, 0, hot, 1) |
| 775 | + |
| 776 | + req := BlockRequest{Context: blockContext(chainID), Txs: rawTxs} |
| 777 | + seqResult, err := NewExecutor(Config{MinGasPrice: big.NewInt(0)}, withTestState(seqState)).ExecuteBlock(t.Context(), req) |
| 778 | + require.NoError(t, err) |
| 779 | + occResult, err := NewExecutor(Config{MinGasPrice: big.NewInt(0), OCCWorkers: 4}, withTestState(occState)).ExecuteBlock(t.Context(), req) |
| 780 | + require.NoError(t, err) |
| 781 | + |
| 782 | + require.True(t, occResult.OCCStats.Attempted) |
| 783 | + require.False(t, occResult.OCCStats.Fallback, occResult.OCCStats.FallbackReason) |
| 784 | + require.Greater(t, occResult.OCCStats.RerunCount, uint64(0)) |
| 785 | + require.Equal(t, seqResult.GasUsed, occResult.GasUsed) |
| 786 | + require.Equal(t, seqResult.Txs, occResult.Txs) |
| 787 | + require.Equal(t, seqResult.Receipts, occResult.Receipts) |
| 788 | + require.Equal(t, seqResult.ChangeSet, occResult.ChangeSet) |
| 789 | +} |
| 790 | + |
732 | 791 | func TestExecutorOCCFeePayingTransfersDoNotConflictOnCoinbase(t *testing.T) { |
733 | 792 | chainID := big.NewInt(testChainID) |
734 | 793 | txCount := 4 |
@@ -2114,7 +2173,7 @@ func TestStateDBGetCodeHashTracksCodelessAccountExistenceReads(t *testing.T) { |
2114 | 2173 | {kind: stateAccessBalance, address: eoa}: {}, |
2115 | 2174 | }) |
2116 | 2175 | validation := occValidationResult{} |
2117 | | - accepted := validateSTMResultAgainstPrefix(&validation, writes, occTxExecution{gasLimit: 1, readSet: readSet}, 0, 10, 0) |
| 2176 | + accepted := validateSTMResultAgainstPrefix(&validation, writes, occTxExecution{gasLimit: 1, readSet: readSet}, 0, 10, 0, math.MaxInt) |
2118 | 2177 | require.False(t, accepted) |
2119 | 2178 | require.Equal(t, occFallbackReasonConflict, validation.fallbackReason) |
2120 | 2179 | } |
@@ -2226,7 +2285,7 @@ func TestValidateSTMConflictMatrix(t *testing.T) { |
2226 | 2285 | t.Fatalf("unknown access mode %q", access) |
2227 | 2286 | } |
2228 | 2287 | validation := occValidationResult{} |
2229 | | - accepted := validateSTMResultAgainstPrefix(&validation, writes, result, 0, 10, 0) |
| 2288 | + accepted := validateSTMResultAgainstPrefix(&validation, writes, result, 0, 10, 0, math.MaxInt) |
2230 | 2289 | return accepted, validation |
2231 | 2290 | } |
2232 | 2291 |
|
@@ -2268,7 +2327,7 @@ func TestValidateSTMConflictSourcePrefix(t *testing.T) { |
2268 | 2327 | readSet: map[stateAccessKey]struct{}{key: {}}, |
2269 | 2328 | gasLimit: 1, |
2270 | 2329 | gasUsed: 1, |
2271 | | - }, 0, 10, sourcePrefix) |
| 2330 | + }, 0, 10, sourcePrefix, math.MaxInt) |
2272 | 2331 | return accepted, validation |
2273 | 2332 | } |
2274 | 2333 | cases := []struct { |
|
0 commit comments