Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Changes reverted in PR#1124 #1204

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,10 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State
for addr, account := range allocs {
log.Info("change contract code", "address", addr)
state.SetCode(addr, account.Code)

if state.GetBalance(addr).Cmp(big.NewInt(0)) == 0 {
state.SetBalance(addr, account.Balance)
}
}
}
}
Expand Down
31 changes: 24 additions & 7 deletions consensus/bor/bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func TestGenesisContractChange(t *testing.T) {
"balance": "0x1000",
},
},
"6": map[string]interface{}{
addr0.Hex(): map[string]interface{}{
"code": hexutil.Bytes{0x1, 0x4},
"balance": "0x2000",
},
},
},
},
}
Expand Down Expand Up @@ -87,24 +93,35 @@ func TestGenesisContractChange(t *testing.T) {

root := genesis.Root()

// code does not change
// code does not change, balance remains 0
root, statedb = addBlock(root, 1)
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x1})
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))

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

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

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

// make sure balance change DOES NOT take effect
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))
// code same as 2nd change, balance remains 4096
root, statedb = addBlock(root, 5)
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x3})
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096))

// code changes 3rd time, balance remains 4096
_, statedb = addBlock(root, 6)
require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x4})
require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096))
}

func TestEncodeSigHeaderJaipur(t *testing.T) {
Expand Down
16 changes: 14 additions & 2 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@
AccountQueue: 64,
GlobalQueue: 1024,

Lifetime: 3 * time.Hour,
Lifetime: 3 * time.Hour,
AllowUnprotectedTxs: false,
}

// sanitize checks the provided user configurations and changes anything that's
Expand Down Expand Up @@ -601,7 +602,8 @@
// and does not require the pool mutex to be held.
func (pool *LegacyPool) validateTxBasics(tx *types.Transaction, local bool) error {
opts := &txpool.ValidationOptions{
Config: pool.chainconfig,
Config: pool.chainconfig,
AllowUnprotectedTxs: pool.config.AllowUnprotectedTxs,
Accept: 0 |
1<<types.LegacyTxType |
1<<types.AccessListTxType |
Expand Down Expand Up @@ -671,6 +673,11 @@
knownTxMeter.Mark(1)
return false, txpool.ErrAlreadyKnown
}

if pool.config.AllowUnprotectedTxs {
pool.signer = types.NewFakeSigner(tx.ChainId())
}

Check warning on line 679 in core/txpool/legacypool/legacypool.go

View check run for this annotation

Codecov / codecov/patch

core/txpool/legacypool/legacypool.go#L678-L679

Added lines #L678 - L679 were not covered by tests

// Make the local flag. If it's from local source or it's from the network but
// the sender is marked as local previously, treat it as the local transaction.
isLocal := local || pool.locals.containsTx(tx)
Expand Down Expand Up @@ -984,6 +991,11 @@
knownTxMeter.Mark(1)
continue
}

if pool.config.AllowUnprotectedTxs {
pool.signer = types.NewFakeSigner(tx.ChainId())
}

Check warning on line 997 in core/txpool/legacypool/legacypool.go

View check run for this annotation

Codecov / codecov/patch

core/txpool/legacypool/legacypool.go#L996-L997

Added lines #L996 - L997 were not covered by tests

// Exclude transactions with basic errors, e.g invalid signatures and
// insufficient intrinsic gas as soon as possible and cache senders
// in transactions before obtaining lock
Expand Down
4 changes: 3 additions & 1 deletion core/txpool/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
type ValidationOptions struct {
Config *params.ChainConfig // Chain configuration to selectively validate based on current fork rules

AllowUnprotectedTxs bool // Whether to allow unprotected transactions in the pool

Accept uint8 // Bitmap of transaction types that should be accepted for the calling pool
MaxSize uint64 // Maximum size of a transaction that the caller can meaningfully handle
MinTip *big.Int // Minimum gas tip needed to allow a transaction into the caller pool
Expand Down Expand Up @@ -91,7 +93,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
return core.ErrTipAboveFeeCap
}
// Make sure the transaction is signed properly
if _, err := types.Sender(signer, tx); err != nil {
if _, err := types.Sender(signer, tx); err != nil && !opts.AllowUnprotectedTxs {
return ErrInvalidSender
}
// Ensure the transaction has more gas than the bare minimum needed to cover
Expand Down
35 changes: 35 additions & 0 deletions core/types/transaction_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,38 @@
v = new(big.Int).Sub(v, big.NewInt(35))
return v.Div(v, big.NewInt(2))
}

// FakeSigner implements the Signer interface and accepts unprotected transactions
type FakeSigner struct{ londonSigner }

var _ Signer = FakeSigner{}

func NewFakeSigner(chainId *big.Int) Signer {
signer := NewLondonSigner(chainId)
ls, _ := signer.(londonSigner)
return FakeSigner{londonSigner: ls}

Check warning on line 594 in core/types/transaction_signing.go

View check run for this annotation

Codecov / codecov/patch

core/types/transaction_signing.go#L591-L594

Added lines #L591 - L594 were not covered by tests
}

func (f FakeSigner) Sender(tx *Transaction) (common.Address, error) {
return f.londonSigner.Sender(tx)

Check warning on line 598 in core/types/transaction_signing.go

View check run for this annotation

Codecov / codecov/patch

core/types/transaction_signing.go#L597-L598

Added lines #L597 - L598 were not covered by tests
}

func (f FakeSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error) {
return f.londonSigner.SignatureValues(tx, sig)

Check warning on line 602 in core/types/transaction_signing.go

View check run for this annotation

Codecov / codecov/patch

core/types/transaction_signing.go#L601-L602

Added lines #L601 - L602 were not covered by tests
}

func (f FakeSigner) ChainID() *big.Int {
return f.londonSigner.ChainID()

Check warning on line 606 in core/types/transaction_signing.go

View check run for this annotation

Codecov / codecov/patch

core/types/transaction_signing.go#L605-L606

Added lines #L605 - L606 were not covered by tests
}

// Hash returns 'signature hash', i.e. the transaction hash that is signed by the
// private key. This hash does not uniquely identify the transaction.
func (f FakeSigner) Hash(tx *Transaction) common.Hash {
return f.londonSigner.Hash(tx)

Check warning on line 612 in core/types/transaction_signing.go

View check run for this annotation

Codecov / codecov/patch

core/types/transaction_signing.go#L611-L612

Added lines #L611 - L612 were not covered by tests
}

// Equal returns true if the given signer is the same as the receiver.
func (f FakeSigner) Equal(Signer) bool {
// Always return true
return true

Check warning on line 618 in core/types/transaction_signing.go

View check run for this annotation

Codecov / codecov/patch

core/types/transaction_signing.go#L616-L618

Added lines #L616 - L618 were not covered by tests
}
3 changes: 1 addition & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@

eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
if eth.APIBackend.allowUnprotectedTxs {
log.Debug(" ###########", "Unprotected transactions allowed")

log.Info("------Unprotected transactions allowed-------")

Check warning on line 177 in eth/backend.go

View check run for this annotation

Codecov / codecov/patch

eth/backend.go#L177

Added line #L177 was not covered by tests
config.TxPool.AllowUnprotectedTxs = true
}

Expand Down
15 changes: 0 additions & 15 deletions eth/bor_api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,6 @@ func (b *EthAPIBackend) GetVoteOnHash(ctx context.Context, starBlockNr uint64, e
return false, fmt.Errorf("Hash mismatch: localChainHash %s, milestoneHash %s", localEndBlockHash, hash)
}

ethHandler := (*ethHandler)(b.eth.handler)

bor, ok := ethHandler.chain.Engine().(*bor.Bor)

if !ok {
return false, fmt.Errorf("Bor not available")
}

err = bor.HeimdallClient.FetchMilestoneID(ctx, milestoneId)

if err != nil {
downloader.UnlockMutex(false, "", endBlockNr, common.Hash{})
return false, fmt.Errorf("Milestone ID doesn't exist in Heimdall")
}

downloader.UnlockMutex(true, milestoneId, endBlockNr, localEndBlock.Hash())

return true, nil
Expand Down
19 changes: 19 additions & 0 deletions internal/cli/server/config_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ func TestConfigLegacy(t *testing.T) {
readFile("./testdata/test.toml")
})
}

func TestDefaultConfigLegacy(t *testing.T) {
readFile := func(path string) {
expectedConfig, err := readLegacyConfig(path)
assert.NoError(t, err)

testConfig := DefaultConfig()

testConfig.Identity = "Polygon-Devs"
testConfig.DataDir = "/var/lib/bor"

assert.Equal(t, expectedConfig, testConfig)
}

// read file in hcl format
t.Run("toml", func(t *testing.T) {
readFile("./testdata/default.toml")
})
}
Loading
Loading