Skip to content

Commit

Permalink
[genesis] whitelist replay deployer (#4009)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jan 29, 2024
1 parent 593dba7 commit d657b34
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 45 deletions.
12 changes: 7 additions & 5 deletions action/rlp_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func TestRlpDecodeVerify(t *testing.T) {
}

var (
// deterministic deployment: https://github.com/Arachnid/deterministic-deployment-proxy
// see example at https://etherscan.io/tx/0xeddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26
deterministicDeploymentTx = "0xf8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222"

rlpTests = []struct {
actType string
raw string
Expand Down Expand Up @@ -379,7 +383,7 @@ var (
},
{
"unprotected",
"0xf8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222",
deterministicDeploymentTx,
0,
100000,
"100000000000",
Expand All @@ -388,7 +392,7 @@ var (
0,
iotextypes.Encoding_ETHEREUM_UNPROTECTED,
83,
"eddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26", // https://etherscan.io/tx/0xeddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26
"eddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26",
"040a98b1acb38ed9cd8d0e8f1f03b1588bae140586f8a8049197b65013a3c17690151ae422e3fdfb26be2e6a4465b1f9cf5c26a5635109929a0d0a11734124d50a",
"3fab184622dc19b6109349b94811493bf2a45362",
},
Expand Down Expand Up @@ -565,13 +569,11 @@ func TestIssue3944(t *testing.T) {
func TestBackwardComp(t *testing.T) {
r := require.New(t)
var (
// example unprotected tx at https://etherscan.io/tx/0xeddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26
unprotectedTx = "0xf8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222"
// example access list tx, created with a random chainID value = 0x7a69 = 31337
accessListTx = "0x01f8a0827a690184ee6b280082520894a0ee7a142d267c1f36714e4a8f75612f20a797206480f838f794a0ee7a142d267c1f36714e4a8f75612f20a79720e1a0000000000000000000000000000000000000000000000000000000000000000001a0eb211dfd353d76d43ea31a130ff36ac4eb7b379eae4d49fa2376741daf32f9ffa07ab673241d75e103f81ddd4aa34dd6849faf2f0f593eebe61a68fed74490a348"
)
for _, chainID := range []uint32{_evmNetworkID, _evmNetworkID + 1, 31337, 0} {
_, _, _, err := DecodeRawTx(unprotectedTx, chainID)
_, _, _, err := DecodeRawTx(deterministicDeploymentTx, chainID)
r.Equal(crypto.ErrInvalidKey, err)
_, _, _, err = DecodeRawTx(accessListTx, chainID)
r.ErrorContains(err, "typed transaction too short")
Expand Down
3 changes: 2 additions & 1 deletion actpool/actpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/iotexproject/go-pkgs/cache/ttl"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
Expand Down Expand Up @@ -282,7 +283,7 @@ func (ap *actPool) checkSelpWithoutState(ctx context.Context, selp *action.Seale
}

// Reject action if the gas price is lower than the threshold
if selp.GasPrice().Cmp(ap.cfg.MinGasPrice()) < 0 {
if selp.Encoding() != uint32(iotextypes.Encoding_ETHEREUM_UNPROTECTED) && selp.GasPrice().Cmp(ap.cfg.MinGasPrice()) < 0 {
_actpoolMtc.WithLabelValues("gasPriceLower").Inc()
actHash, _ := selp.Hash()
log.L().Debug("action rejected due to low gas price",
Expand Down
8 changes: 8 additions & 0 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ func (core *coreService) SendAction(ctx context.Context, in *iotextypes.Action)
if err := core.validateChainID(in.GetCore().GetChainID()); err != nil {
return "", err
}
// reject action if a replay tx is not whitelisted
var (
g = core.Genesis()
deployer = selp.SrcPubkey().Address()
)
if selp.Encoding() == uint32(iotextypes.Encoding_ETHEREUM_UNPROTECTED) && !g.IsDeployerWhitelisted(deployer) {
return "", status.Errorf(codes.InvalidArgument, "replay deployer %v not whitelisted", deployer.Hex())
}

// Add to local actpool
ctx = protocol.WithRegistry(ctx, core.registry)
Expand Down
26 changes: 25 additions & 1 deletion blockchain/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"go.uber.org/config"
"go.uber.org/zap"
Expand Down Expand Up @@ -76,7 +77,8 @@ func defaultConfig() Genesis {
ToBeEnabledBlockHeight: math.MaxUint64,
},
Account: Account{
InitBalanceMap: make(map[string]string),
InitBalanceMap: make(map[string]string),
ReplayDeployerWhitelist: []string{"0x3fab184622dc19b6109349b94811493bf2a45362"},
},
Poll: Poll{
PollMode: "nativeMix",
Expand Down Expand Up @@ -254,6 +256,8 @@ type (
Account struct {
// InitBalanceMap is the address and initial balance mapping before the first block.
InitBalanceMap map[string]string `yaml:"initBalances"`
// ReplayDeployerWhitelist is the whitelist address for unprotected (pre-EIP155) transaction
ReplayDeployerWhitelist []string `yaml:"replayDeployerWhitelist"`
}
// Poll contains the configs for poll protocol
Poll struct {
Expand Down Expand Up @@ -594,6 +598,26 @@ func (g *Blockchain) IsToBeEnabled(height uint64) bool {
return g.isPost(g.ToBeEnabledBlockHeight, height)
}

// IsDeployerWhitelisted returns if the replay deployer is whitelisted
func (a *Account) IsDeployerWhitelisted(deployer address.Address) bool {
for _, v := range a.ReplayDeployerWhitelist {
if v[:3] == "io1" {
if addr, err := address.FromString(v); err == nil {
if address.Equal(deployer, addr) {
return true
}
}
} else if common.IsHexAddress(v) {
if addr, err := address.FromHex(v); err == nil {
if address.Equal(deployer, addr) {
return true
}
}
}
}
return false
}

// InitBalances returns the address that have initial balances and the corresponding amounts. The i-th amount is the
// i-th address' balance.
func (a *Account) InitBalances() ([]address.Address, []*big.Int) {
Expand Down
21 changes: 20 additions & 1 deletion blockchain/genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-address/address"
)

func TestDefaultConfig(t *testing.T) {
Expand Down Expand Up @@ -39,10 +41,27 @@ func TestAccount_InitBalances(t *testing.T) {
InitBalanceMap := make(map[string]string, 0)
InitBalanceMap["io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6"] = "1"
InitBalanceMap["io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms"] = "2"
acc := Account{InitBalanceMap}
acc := Account{InitBalanceMap: InitBalanceMap}
adds, balances := acc.InitBalances()
require.Equal("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6", adds[0].String())
require.Equal("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", adds[1].String())
require.Equal(InitBalanceMap["io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6"], balances[0].Text(10))
require.Equal(InitBalanceMap["io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms"], balances[1].Text(10))
}

func TestDeployerWhitelist(t *testing.T) {
r := require.New(t)
g := Default
addr, err := address.FromHex("0x3fab184622dc19b6109349b94811493bf2a45362")
r.NoError(err)
r.True(g.IsDeployerWhitelisted(addr))
addr, err = address.FromString("io18743s33zmsvmvyynfxu5sy2f80e2g5mzk3y5ue")
r.NoError(err)
r.True(g.IsDeployerWhitelisted(addr))
addr, err = address.FromHex("0x3fab184622dc19b6109349b94811493bf2a45361")
r.NoError(err)
r.False(g.IsDeployerWhitelisted(addr))
addr, err = address.FromString("io18743s33zmsvmvyynfxu5sy2f80e2g5mpcz3zjx")
r.NoError(err)
r.False(g.IsDeployerWhitelisted(addr))
}
Loading

0 comments on commit d657b34

Please sign in to comment.