Skip to content

Commit 4a03cc0

Browse files
authored
Merge pull request ethereum#1204 from maticnetwork/consistent-revert
Add: Changes reverted in PR#1124
2 parents 2f61049 + 3e113ed commit 4a03cc0

File tree

10 files changed

+296
-30
lines changed

10 files changed

+296
-30
lines changed

consensus/bor/bor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State
883883
for addr, account := range allocs {
884884
log.Info("change contract code", "address", addr)
885885
state.SetCode(addr, account.Code)
886+
887+
if state.GetBalance(addr).Cmp(big.NewInt(0)) == 0 {
888+
state.SetBalance(addr, account.Balance)
889+
}
886890
}
887891
}
888892
}

consensus/bor/bor_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ func TestGenesisContractChange(t *testing.T) {
4141
"balance": "0x1000",
4242
},
4343
},
44+
"6": map[string]interface{}{
45+
addr0.Hex(): map[string]interface{}{
46+
"code": hexutil.Bytes{0x1, 0x4},
47+
"balance": "0x2000",
48+
},
49+
},
4450
},
4551
},
4652
}
@@ -87,24 +93,35 @@ func TestGenesisContractChange(t *testing.T) {
8793

8894
root := genesis.Root()
8995

90-
// code does not change
96+
// code does not change, balance remains 0
9197
root, statedb = addBlock(root, 1)
9298
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x1})
99+
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))
93100

94-
// code changes 1st time
101+
// code changes 1st time, balance remains 0
95102
root, statedb = addBlock(root, 2)
96103
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x2})
104+
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))
97105

98-
// code same as 1st change
106+
// code same as 1st change, balance remains 0
99107
root, statedb = addBlock(root, 3)
100108
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x2})
109+
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))
101110

102-
// code changes 2nd time
103-
_, statedb = addBlock(root, 4)
111+
// code changes 2nd time, balance updates to 4096
112+
root, statedb = addBlock(root, 4)
104113
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x3})
114+
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096))
105115

106-
// make sure balance change DOES NOT take effect
107-
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))
116+
// code same as 2nd change, balance remains 4096
117+
root, statedb = addBlock(root, 5)
118+
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x3})
119+
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096))
120+
121+
// code changes 3rd time, balance remains 4096
122+
_, statedb = addBlock(root, 6)
123+
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x4})
124+
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096))
108125
}
109126

110127
func TestEncodeSigHeaderJaipur(t *testing.T) {

core/txpool/legacypool/legacypool.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ var DefaultConfig = Config{
155155
AccountQueue: 64,
156156
GlobalQueue: 1024,
157157

158-
Lifetime: 3 * time.Hour,
158+
Lifetime: 3 * time.Hour,
159+
AllowUnprotectedTxs: false,
159160
}
160161

161162
// sanitize checks the provided user configurations and changes anything that's
@@ -601,7 +602,8 @@ func (pool *LegacyPool) local() map[common.Address]types.Transactions {
601602
// and does not require the pool mutex to be held.
602603
func (pool *LegacyPool) validateTxBasics(tx *types.Transaction, local bool) error {
603604
opts := &txpool.ValidationOptions{
604-
Config: pool.chainconfig,
605+
Config: pool.chainconfig,
606+
AllowUnprotectedTxs: pool.config.AllowUnprotectedTxs,
605607
Accept: 0 |
606608
1<<types.LegacyTxType |
607609
1<<types.AccessListTxType |
@@ -671,6 +673,11 @@ func (pool *LegacyPool) add(tx *types.Transaction, local bool) (replaced bool, e
671673
knownTxMeter.Mark(1)
672674
return false, txpool.ErrAlreadyKnown
673675
}
676+
677+
if pool.config.AllowUnprotectedTxs {
678+
pool.signer = types.NewFakeSigner(tx.ChainId())
679+
}
680+
674681
// Make the local flag. If it's from local source or it's from the network but
675682
// the sender is marked as local previously, treat it as the local transaction.
676683
isLocal := local || pool.locals.containsTx(tx)
@@ -984,6 +991,11 @@ func (pool *LegacyPool) Add(txs []*types.Transaction, local, sync bool) []error
984991
knownTxMeter.Mark(1)
985992
continue
986993
}
994+
995+
if pool.config.AllowUnprotectedTxs {
996+
pool.signer = types.NewFakeSigner(tx.ChainId())
997+
}
998+
987999
// Exclude transactions with basic errors, e.g invalid signatures and
9881000
// insufficient intrinsic gas as soon as possible and cache senders
9891001
// in transactions before obtaining lock

core/txpool/validation.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
type ValidationOptions struct {
3636
Config *params.ChainConfig // Chain configuration to selectively validate based on current fork rules
3737

38+
AllowUnprotectedTxs bool // Whether to allow unprotected transactions in the pool
39+
3840
Accept uint8 // Bitmap of transaction types that should be accepted for the calling pool
3941
MaxSize uint64 // Maximum size of a transaction that the caller can meaningfully handle
4042
MinTip *big.Int // Minimum gas tip needed to allow a transaction into the caller pool
@@ -91,7 +93,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
9193
return core.ErrTipAboveFeeCap
9294
}
9395
// Make sure the transaction is signed properly
94-
if _, err := types.Sender(signer, tx); err != nil {
96+
if _, err := types.Sender(signer, tx); err != nil && !opts.AllowUnprotectedTxs {
9597
return ErrInvalidSender
9698
}
9799
// Ensure the transaction has more gas than the bare minimum needed to cover

core/types/transaction_signing.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,38 @@ func deriveChainId(v *big.Int) *big.Int {
582582
v = new(big.Int).Sub(v, big.NewInt(35))
583583
return v.Div(v, big.NewInt(2))
584584
}
585+
586+
// FakeSigner implements the Signer interface and accepts unprotected transactions
587+
type FakeSigner struct{ londonSigner }
588+
589+
var _ Signer = FakeSigner{}
590+
591+
func NewFakeSigner(chainId *big.Int) Signer {
592+
signer := NewLondonSigner(chainId)
593+
ls, _ := signer.(londonSigner)
594+
return FakeSigner{londonSigner: ls}
595+
}
596+
597+
func (f FakeSigner) Sender(tx *Transaction) (common.Address, error) {
598+
return f.londonSigner.Sender(tx)
599+
}
600+
601+
func (f FakeSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error) {
602+
return f.londonSigner.SignatureValues(tx, sig)
603+
}
604+
605+
func (f FakeSigner) ChainID() *big.Int {
606+
return f.londonSigner.ChainID()
607+
}
608+
609+
// Hash returns 'signature hash', i.e. the transaction hash that is signed by the
610+
// private key. This hash does not uniquely identify the transaction.
611+
func (f FakeSigner) Hash(tx *Transaction) common.Hash {
612+
return f.londonSigner.Hash(tx)
613+
}
614+
615+
// Equal returns true if the given signer is the same as the receiver.
616+
func (f FakeSigner) Equal(Signer) bool {
617+
// Always return true
618+
return true
619+
}

eth/backend.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
174174

175175
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
176176
if eth.APIBackend.allowUnprotectedTxs {
177-
log.Debug(" ###########", "Unprotected transactions allowed")
178-
177+
log.Info("------Unprotected transactions allowed-------")
179178
config.TxPool.AllowUnprotectedTxs = true
180179
}
181180

eth/bor_api_backend.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ func (b *EthAPIBackend) GetVoteOnHash(ctx context.Context, starBlockNr uint64, e
8383
return false, fmt.Errorf("Hash mismatch: localChainHash %s, milestoneHash %s", localEndBlockHash, hash)
8484
}
8585

86-
ethHandler := (*ethHandler)(b.eth.handler)
87-
88-
bor, ok := ethHandler.chain.Engine().(*bor.Bor)
89-
90-
if !ok {
91-
return false, fmt.Errorf("Bor not available")
92-
}
93-
94-
err = bor.HeimdallClient.FetchMilestoneID(ctx, milestoneId)
95-
96-
if err != nil {
97-
downloader.UnlockMutex(false, "", endBlockNr, common.Hash{})
98-
return false, fmt.Errorf("Milestone ID doesn't exist in Heimdall")
99-
}
100-
10186
downloader.UnlockMutex(true, milestoneId, endBlockNr, localEndBlock.Hash())
10287

10388
return true, nil

internal/cli/server/config_legacy_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,22 @@ func TestConfigLegacy(t *testing.T) {
4242
readFile("./testdata/test.toml")
4343
})
4444
}
45+
46+
func TestDefaultConfigLegacy(t *testing.T) {
47+
readFile := func(path string) {
48+
expectedConfig, err := readLegacyConfig(path)
49+
assert.NoError(t, err)
50+
51+
testConfig := DefaultConfig()
52+
53+
testConfig.Identity = "Polygon-Devs"
54+
testConfig.DataDir = "/var/lib/bor"
55+
56+
assert.Equal(t, expectedConfig, testConfig)
57+
}
58+
59+
// read file in hcl format
60+
t.Run("toml", func(t *testing.T) {
61+
readFile("./testdata/default.toml")
62+
})
63+
}

0 commit comments

Comments
 (0)