Skip to content

Commit 44a058f

Browse files
authored
Merge pull request #158 from getamis/feature/cyclic_import
define casper interface to avoid cyclic import
2 parents e6d573f + 676bf87 commit 44a058f

File tree

6 files changed

+122
-22
lines changed

6 files changed

+122
-22
lines changed

contracts/casper.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package contracts
18+
19+
import (
20+
"math/big"
21+
)
22+
23+
// Casper defines the casper contract interface
24+
type Casper interface {
25+
// GetLastJustifiedEpoch returns the last justified epoch
26+
GetLastJustifiedEpoch() (*big.Int, error)
27+
// GetLastFinalizedEpoch returns the last finalized epoch
28+
GetLastFinalizedEpoch() (*big.Int, error)
29+
// GetCheckpointHashes returns the checkpoint hashes
30+
GetCheckpointHashes(*big.Int) ([32]byte, error)
31+
}

contracts/casper/contract.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

core/blockchain.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import (
2727
"sync/atomic"
2828
"time"
2929

30+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
3031
"github.com/ethereum/go-ethereum/common"
3132
"github.com/ethereum/go-ethereum/common/mclock"
3233
"github.com/ethereum/go-ethereum/consensus"
34+
"github.com/ethereum/go-ethereum/contracts"
3335
"github.com/ethereum/go-ethereum/core/state"
3436
"github.com/ethereum/go-ethereum/core/types"
3537
"github.com/ethereum/go-ethereum/core/vm"
@@ -127,7 +129,8 @@ type BlockChain struct {
127129
validator Validator // block and state validator interface
128130
vmConfig vm.Config
129131

130-
badBlocks *lru.Cache // Bad block cache
132+
badBlocks *lru.Cache // Bad block cache
133+
casperGen func(bind.ContractBackend) (contracts.Casper, error) // casperGen generates the casper instance at specific state
131134
}
132135

133136
// NewBlockChain returns a fully initialised block chain using information

core/casper_util.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@
1717
package core
1818

1919
import (
20+
"errors"
2021
"math/big"
2122

2223
"github.com/ethereum/go-ethereum/accounts/abi/bind"
2324
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/contracts/casper"
25+
"github.com/ethereum/go-ethereum/contracts"
2526
"github.com/ethereum/go-ethereum/core/state"
2627
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/ethereum/go-ethereum/log"
2829
)
2930

31+
var ErrNilCasperGen = errors.New("nil casper generator")
32+
33+
// SetCasperGen sets casperGen
34+
func (bc *BlockChain) SetCasperGen(casperGen func(bind.ContractBackend) (contracts.Casper, error)) {
35+
bc.casperGen = casperGen
36+
}
37+
3038
func (bc *BlockChain) acceptNewCasperBlock(currentBlock *types.Block, newBlock *types.Block, newState *state.StateDB) (bool, error) {
3139
currentScore, err := bc.getScore(currentBlock)
3240
if err != nil {
@@ -62,17 +70,21 @@ func (bc *BlockChain) getScore(block *types.Block) (*big.Int, error) {
6270

6371
// getLastJustifiedEpoch returns the last justified epoch for a given block
6472
func (bc *BlockChain) getLastJustifiedEpoch(block *types.Block) (*big.Int, error) {
73+
if bc.casperGen == nil {
74+
log.Warn("Casper generator is not initialized")
75+
return nil, ErrNilCasperGen
76+
}
6577
state, err := bc.StateAt(block.Root())
6678
if err != nil {
6779
return nil, err
6880
}
6981
stateBackend := NewStateBackend(block, state, bc)
70-
contract, err := casper.New(stateBackend)
82+
contract, err := bc.casperGen(stateBackend)
7183
if err != nil {
7284
log.Warn("Failed to get Casper contract", "err", err)
7385
return nil, err
7486
}
75-
justified, err := contract.GetLastJustifiedEpoch(&bind.CallOpts{})
87+
justified, err := contract.GetLastJustifiedEpoch()
7688
if err != nil {
7789
log.Warn("Failed to get current chain status from Casper", "err", err)
7890
return nil, err
@@ -82,18 +94,22 @@ func (bc *BlockChain) getLastJustifiedEpoch(block *types.Block) (*big.Int, error
8294

8395
// safeForLastFinalizedBlock returns true if the new head will NOT revert the last finalized block
8496
func (bc *BlockChain) safeForLastFinalizedBlock(newBlock *types.Block, newState *state.StateDB) (bool, error) {
97+
if bc.casperGen == nil {
98+
log.Warn("Casper generator is not initialized")
99+
return false, ErrNilCasperGen
100+
}
85101
stateBackend := NewStateBackend(newBlock, newState, bc)
86-
contract, err := casper.New(stateBackend)
102+
contract, err := bc.casperGen(stateBackend)
87103
if err != nil {
88104
log.Warn("Failed to get Casper contract", "err", err)
89105
return false, err
90106
}
91-
blockNumber, err := contract.GetLastFinalizedEpoch(&bind.CallOpts{})
107+
blockNumber, err := contract.GetLastFinalizedEpoch()
92108
if err != nil {
93109
log.Warn("Failed to get current chain status from Casper", "err", err)
94110
return false, err
95111
}
96-
hashBytes, err := contract.GetCheckpointHashes(&bind.CallOpts{}, blockNumber)
112+
hashBytes, err := contract.GetCheckpointHashes(blockNumber)
97113
if err != nil {
98114
log.Warn("Failed to get current chain status from Casper", "err", err)
99115
return false, err

eth/backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
152152
if err != nil {
153153
return nil, err
154154
}
155+
// TODO: Intialize the casper gen only when the casper address is specified in genesis config
156+
// Set casper gen
157+
eth.blockchain.SetCasperGen(NewCasperGen(common.HexToAddress("0xbd832b0cd3291c39ef67691858f35c71dfb3bf21")))
158+
155159
// Rewind the chain in case of an incompatible config upgrade.
156160
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
157161
log.Warn("Rewinding chain to upgrade configuration", "err", compat)

eth/casper.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package eth implements the Ethereum protocol.
18+
package eth
19+
20+
import (
21+
"math/big"
22+
23+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
24+
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/contracts"
26+
casperContract "github.com/ethereum/go-ethereum/contracts/casper"
27+
)
28+
29+
type casper struct {
30+
contract *casperContract.Casper
31+
}
32+
33+
// NewCasperGen returns a func which can generate capser instance at specific state and given address
34+
func NewCasperGen(address common.Address) func(bind.ContractBackend) (contracts.Casper, error) {
35+
return func(backend bind.ContractBackend) (contracts.Casper, error) {
36+
return NewCasper(address, backend)
37+
}
38+
}
39+
40+
// NewCasper returns casper instance at specific state and address
41+
func NewCasper(address common.Address, backend bind.ContractBackend) (contracts.Casper, error) {
42+
contract, err := casperContract.NewCasper(address, backend)
43+
return &casper{
44+
contract: contract,
45+
}, err
46+
}
47+
48+
// GetLastJustifiedEpoch returns the last justified epoch in specific backend
49+
func (c *casper) GetLastJustifiedEpoch() (*big.Int, error) {
50+
return c.contract.GetLastJustifiedEpoch(&bind.CallOpts{})
51+
}
52+
53+
// GetLastFinalizedEpoch returns the last finalized epoch in specific backend
54+
func (c *casper) GetLastFinalizedEpoch() (*big.Int, error) {
55+
return c.contract.GetLastFinalizedEpoch(&bind.CallOpts{})
56+
}
57+
58+
// GetCheckpointHashes returns the checkpoint hashes in specific backend
59+
func (c *casper) GetCheckpointHashes(number *big.Int) ([32]byte, error) {
60+
return c.contract.GetCheckpointHashes(&bind.CallOpts{}, number)
61+
}

0 commit comments

Comments
 (0)