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

[SC] Minor refactored #1285

Merged
merged 7 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
17 changes: 11 additions & 6 deletions node/sc/bridge_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import (

const (
DefaultBridgeTxGasLimit = 5000000

ParentOperatorStr = "parentOperator"
ChildOperatorStr = "childOperator"
ParentBridgeAccountName = "parent_bridge_account"
ChildBridgeAccountName = "child_bridge_account"
)

var (
Expand Down Expand Up @@ -76,8 +81,8 @@ type BridgeAccounts struct {
func (ba *BridgeAccounts) GetBridgeOperators() map[string]interface{} {
res := make(map[string]interface{})

res["parentOperator"] = ba.pAccount.GetAccountInfo()
res["childOperator"] = ba.cAccount.GetAccountInfo()
res[ParentOperatorStr] = ba.pAccount.GetAccountInfo()
res[ChildOperatorStr] = ba.cAccount.GetAccountInfo()

return res
}
Expand Down Expand Up @@ -108,22 +113,22 @@ func (ba *BridgeAccounts) SetChildOperatorFeePayer(feePayer common.Address) erro

// NewBridgeAccounts returns bridgeAccounts created by main/service bridge account keys.
func NewBridgeAccounts(am *accounts.Manager, dataDir string, db feePayerDB) (*BridgeAccounts, error) {
pKS, pAccAddr, isLock, err := InitializeBridgeAccountKeystore(path.Join(dataDir, "parent_bridge_account"))
pKS, pAccAddr, isLock, err := InitializeBridgeAccountKeystore(path.Join(dataDir, ParentBridgeAccountName))
if err != nil {
return nil, err
}

if isLock {
logger.Warn("parent_bridge_account is locked. Please unlock the account manually for Service Chain")
logger.Warn("parent bridge account is locked. Please unlock the account manually for Service Chain", "name", ParentBridgeAccountName)
}

cKS, cAccAddr, isLock, err := InitializeBridgeAccountKeystore(path.Join(dataDir, "child_bridge_account"))
cKS, cAccAddr, isLock, err := InitializeBridgeAccountKeystore(path.Join(dataDir, ChildBridgeAccountName))
if err != nil {
return nil, err
}

if isLock {
logger.Warn("child_bridge_account is locked. Please unlock the account manually for Service Chain")
logger.Warn("child bridge account is locked. Please unlock the account manually for Service Chain", "name", ChildBridgeAccountName)
}

logger.Info("bridge account is loaded", "parent", pAccAddr.String(), "child", cAccAddr.String())
Expand Down
10 changes: 9 additions & 1 deletion node/sc/bridge_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestBridgeManager(t *testing.T) {
}

bridgeManager, err := NewBridgeManager(sc)
assert.NoError(t, err)

testToken := big.NewInt(123)
testKLAY := big.NewInt(321)
Expand Down Expand Up @@ -377,6 +378,7 @@ func TestBridgeManagerERC721_notSupportURI(t *testing.T) {
}

bridgeManager, err := NewBridgeManager(sc)
assert.NoError(t, err)

// Deploy Bridge Contract
addr, err := bridgeManager.DeployBridgeTest(sim, false)
Expand Down Expand Up @@ -969,6 +971,7 @@ func TestBasicJournal(t *testing.T) {

// Prepare manager and deploy bridge contract.
bm, err := NewBridgeManager(sc)
assert.NoError(t, err)

localAddr, err := bm.DeployBridgeTest(sim, true)
assert.NoError(t, err)
Expand Down Expand Up @@ -1049,7 +1052,8 @@ func TestMethodRestoreBridges(t *testing.T) {
}

// Prepare manager and deploy bridge contract.
bm, _ := NewBridgeManager(sc)
bm, err := NewBridgeManager(sc)
assert.NoError(t, err)

var bridgeAddrs [4]common.Address
for i := 0; i < 4; i++ {
Expand Down Expand Up @@ -1274,7 +1278,9 @@ func TestErrorDuplicatedSetBridgeInfo(t *testing.T) {

// Prepare manager
bm, err := NewBridgeManager(sc)
assert.NoError(t, err)
addr, err := bm.DeployBridgeTest(sim, false)
assert.NoError(t, err)
bridgeInfo, _ := bm.GetBridgeInfo(addr)

// Try to call duplicated SetBridgeInfo
Expand Down Expand Up @@ -1339,6 +1345,7 @@ func TestScenarioSubUnsub(t *testing.T) {

// Prepare manager and deploy bridge contract.
bm, err := NewBridgeManager(sc)
assert.NoError(t, err)

localAddr, err := bm.DeployBridgeTest(sim, true)
if err != nil {
Expand Down Expand Up @@ -1444,6 +1451,7 @@ func TestErrorDupSubscription(t *testing.T) {

// 1. Prepare manager and subscribe event
bm, err := NewBridgeManager(sc)
assert.NoError(t, err)

addr, err := bm.DeployBridgeTest(sim, false)
bridgeInfo, _ := bm.GetBridgeInfo(addr)
Expand Down
40 changes: 20 additions & 20 deletions node/sc/local_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,45 @@ var errGasEstimationFailed = errors.New("gas required exceeds allowance or alway

// TODO-Klaytn currently LocalBackend is only for ServiceChain, especially Bridge SmartContract
type LocalBackend struct {
subbrige *SubBridge
subbridge *SubBridge

events *filters.EventSystem // Event system for filtering log events live
config *params.ChainConfig
}

func NewLocalBackend(main *SubBridge) (*LocalBackend, error) {
return &LocalBackend{
subbrige: main,
config: main.blockchain.Config(),
events: filters.NewEventSystem(main.EventMux(), &filterLocalBackend{main}, false),
subbridge: main,
config: main.blockchain.Config(),
events: filters.NewEventSystem(main.EventMux(), &filterLocalBackend{main}, false),
}, nil
}

func (lb *LocalBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
if blockNumber != nil && blockNumber.Cmp(lb.subbrige.blockchain.CurrentBlock().Number()) != 0 {
if blockNumber != nil && blockNumber.Cmp(lb.subbridge.blockchain.CurrentBlock().Number()) != 0 {
return nil, errBlockNumberUnsupported
}
statedb, _ := lb.subbrige.blockchain.State()
statedb, _ := lb.subbridge.blockchain.State()
return statedb.GetCode(contract), nil
}

func (lb *LocalBackend) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
if blockNumber != nil && blockNumber.Cmp(lb.subbrige.blockchain.CurrentBlock().Number()) != 0 {
if blockNumber != nil && blockNumber.Cmp(lb.subbridge.blockchain.CurrentBlock().Number()) != 0 {
return nil, errBlockNumberUnsupported
}
statedb, _ := lb.subbrige.blockchain.State()
statedb, _ := lb.subbridge.blockchain.State()
return statedb.GetBalance(account), nil
}

func (lb *LocalBackend) CallContract(ctx context.Context, call klaytn.CallMsg, blockNumber *big.Int) ([]byte, error) {
if blockNumber != nil && blockNumber.Cmp(lb.subbrige.blockchain.CurrentBlock().Number()) != 0 {
if blockNumber != nil && blockNumber.Cmp(lb.subbridge.blockchain.CurrentBlock().Number()) != 0 {
return nil, errBlockNumberUnsupported
}
currentState, err := lb.subbrige.blockchain.State()
currentState, err := lb.subbridge.blockchain.State()
if err != nil {
return nil, err
}
rval, _, _, err := lb.callContract(ctx, call, lb.subbrige.blockchain.CurrentBlock(), currentState)
rval, _, _, err := lb.callContract(ctx, call, lb.subbridge.blockchain.CurrentBlock(), currentState)
return rval, err
}

Expand Down Expand Up @@ -120,7 +120,7 @@ func (b *LocalBackend) callContract(ctx context.Context, call klaytn.CallMsg, bl
statedb.SetBalance(msg.ValidatedSender(), math.MaxBig256)
vmError := func() error { return nil }

context := blockchain.NewEVMContext(msg, block.Header(), b.subbrige.blockchain, nil)
context := blockchain.NewEVMContext(msg, block.Header(), b.subbridge.blockchain, nil)
evm := vm.NewEVM(context, statedb, b.config, &vm.Config{})
// Wait for the context to be done and cancel the evm. Even if the
// EVM has finished, cancelling may be done (repeatedly)
Expand All @@ -145,11 +145,11 @@ func (b *LocalBackend) callContract(ctx context.Context, call klaytn.CallMsg, bl

func (lb *LocalBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
// TODO-Klaytn this is not pending code but latest code
return lb.CodeAt(ctx, contract, lb.subbrige.blockchain.CurrentBlock().Number())
return lb.CodeAt(ctx, contract, lb.subbridge.blockchain.CurrentBlock().Number())
}

func (lb *LocalBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
return lb.subbrige.txPool.GetPendingNonce(account), nil
return lb.subbridge.txPool.GetPendingNonce(account), nil
}

func (lb *LocalBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
Expand All @@ -174,11 +174,11 @@ func (lb *LocalBackend) EstimateGas(ctx context.Context, call klaytn.CallMsg) (g
executable := func(gas uint64) bool {
call.Gas = gas

currentState, err := lb.subbrige.blockchain.State()
currentState, err := lb.subbridge.blockchain.State()
if err != nil {
return false
}
_, _, failed, err := lb.callContract(ctx, call, lb.subbrige.blockchain.CurrentBlock(), currentState)
_, _, failed, err := lb.callContract(ctx, call, lb.subbridge.blockchain.CurrentBlock(), currentState)
if err != nil || failed {
return false
}
Expand All @@ -203,7 +203,7 @@ func (lb *LocalBackend) EstimateGas(ctx context.Context, call klaytn.CallMsg) (g
}

func (lb *LocalBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
return lb.subbrige.txPool.AddLocal(tx)
return lb.subbridge.txPool.AddLocal(tx)
}

// ChainID can return the chain ID of the chain.
Expand All @@ -212,7 +212,7 @@ func (lb *LocalBackend) ChainID(ctx context.Context) (*big.Int, error) {
}

func (lb *LocalBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
receipt := lb.subbrige.blockchain.GetReceiptByTxHash(txHash)
receipt := lb.subbridge.blockchain.GetReceiptByTxHash(txHash)
if receipt != nil {
return receipt, nil
}
Expand All @@ -231,7 +231,7 @@ func (lb *LocalBackend) FilterLogs(ctx context.Context, query klaytn.FilterQuery
to := query.ToBlock.Int64()

// Construct and execute the filter
filter := filters.NewRangeFilter(&filterLocalBackend{lb.subbrige}, from, to, query.Addresses, query.Topics)
filter := filters.NewRangeFilter(&filterLocalBackend{lb.subbridge}, from, to, query.Addresses, query.Topics)

logs, err := filter.Logs(ctx)
if err != nil {
Expand Down Expand Up @@ -278,7 +278,7 @@ func (lb *LocalBackend) SubscribeFilterLogs(ctx context.Context, query klaytn.Fi

// CurrentBlockNumber returns a current block number.
func (lb *LocalBackend) CurrentBlockNumber(ctx context.Context) (uint64, error) {
return lb.subbrige.blockchain.CurrentBlock().NumberU64(), nil
return lb.subbridge.blockchain.CurrentBlock().NumberU64(), nil
}

type filterLocalBackend struct {
Expand Down