Skip to content

Commit 931fa28

Browse files
Merge pull request ethereum#83 from XinFinOrg/develop
Fix Bad_Block issue
2 parents f4bdcb1 + 8bf8ff6 commit 931fa28

File tree

24 files changed

+494
-131
lines changed

24 files changed

+494
-131
lines changed

.github/CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
Before you do a feature request please check and make sure that it isn't possible
44
through some other means. The JavaScript enabled console is a powerful feature
5-
in the right hands.
5+
in the right hands. Please check our [Wiki page](https://github.com/ethereum/go-ethereum/wiki) for more info
6+
and help.
67

78
## Contributing
89

910
If you'd like to contribute to go-ethereum please fork, fix, commit and
1011
send a pull request. Commits which do not comply with the coding standards
1112
are ignored (use gofmt!).
1213

14+
See [Developers' Guide](https://github.com/ethereum/go-ethereum/wiki/Developers'-Guide)
15+
for more details on configuring your environment, testing, and
16+
dependency management.

Dockerfile.node

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ FROM golang:1.10-alpine as builder
22

33
RUN apk add --no-cache make gcc musl-dev linux-headers
44

5-
ADD . /XDChain
5+
ADD . /XDCchain
66

7-
RUN cd /XDChain \
8-
&& make XDC\
7+
RUN cd /XDCchain \
8+
&& make XDC \
99
&& chmod +x /XDCchain/build/bin/XDC
1010

1111
FROM alpine:latest
1212

1313
LABEL maintainer="anil@xinfin.org"
1414

15-
WORKDIR /XDChain
15+
WORKDIR /XDCchain
1616

1717
COPY --from=builder /XDCchain/build/bin/XDC /usr/local/bin/XDC
1818

@@ -31,7 +31,7 @@ ENV ANNOUNCE_TXS ''
3131

3232
RUN apk add --no-cache ca-certificates
3333

34-
COPY docker/XDChain ./
34+
COPY docker/XDCchain ./
3535
COPY genesis/ ./
3636

3737
EXPOSE 8545 8546 30303 30303/udp

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ XDC:
1515
@echo "Done building."
1616
@echo "Run \"$(GOBIN)/XDC\" to launch XDC."
1717

18+
gc:
19+
build/env.sh go run build/ci.go install ./cmd/gc
20+
@echo "Done building."
21+
@echo "Run \"$(GOBIN)/gc\" to launch gc."
22+
1823
bootnode:
1924
build/env.sh go run build/ci.go install ./cmd/bootnode
2025
@echo "Done building."

cmd/XDCclean/main.go renamed to cmd/gc/main.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
7+
"os/signal"
8+
"runtime"
9+
"sync"
10+
"sync/atomic"
11+
"time"
12+
613
"github.com/ethereum/go-ethereum/cmd/utils"
714
"github.com/ethereum/go-ethereum/common"
815
"github.com/ethereum/go-ethereum/core"
@@ -14,17 +21,18 @@ import (
1421
"github.com/hashicorp/golang-lru"
1522
"github.com/syndtr/goleveldb/leveldb"
1623
"github.com/syndtr/goleveldb/leveldb/util"
17-
"os"
18-
"os/signal"
19-
"runtime"
20-
"sync"
21-
"sync/atomic"
22-
"time"
2324
)
2425

2526
var (
26-
dir = flag.String("dir", "", "dir to mainet chain data")
27-
cacheSize = flag.Int("size", 1000000, "dir to mainet chain data")
27+
dir = flag.String("dir", "", "directory to XDCChain chaindata")
28+
cacheSize = flag.Int("size", 1000000, "LRU cache size")
29+
sercureKey = []byte("secure-key-")
30+
nWorker = runtime.NumCPU() / 2
31+
cleanAddress = []common.Address{common.HexToAddress(common.BlockSigners)}
32+
cache *lru.Cache
33+
finish = int32(0)
34+
running = true
35+
stateRoots = make(chan TrieRoot)
2836
)
2937

3038
type TrieRoot struct {
@@ -42,14 +50,6 @@ type ResultProcessNode struct {
4250
keys [17]*[]byte
4351
}
4452

45-
var sercureKey = []byte("secure-key-")
46-
var nWorker = runtime.NumCPU() / 2
47-
var cleanAddress = []common.Address{common.HexToAddress(common.BlockSigners)}
48-
var cache *lru.Cache
49-
var finish = int32(0)
50-
var running = true
51-
var stateRoots = make(chan TrieRoot)
52-
5353
func main() {
5454
flag.Parse()
5555
lddb, _ := ethdb.NewLDBDatabase(*dir, eth.DefaultConfig.DatabaseCache, utils.MakeDatabaseHandles())

cmd/puppeth/wizard_genesis.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ func (w *wizard) makeGenesis() {
122122
genesis.Config.XDPoS.Period = uint64(w.readDefaultInt(2))
123123

124124
fmt.Println()
125-
fmt.Println("How many XDC should be rewarded to masternode? (default = 100)")
126-
genesis.Config.XDPoS.Reward = uint64(w.readDefaultInt(100))
125+
fmt.Println("How many XDC should be rewarded to masternode? (default = 350)")
126+
genesis.Config.XDPoS.Reward = uint64(w.readDefaultInt(350))
127127

128128
fmt.Println()
129129
fmt.Println("Who own the first masternodes? (mandatory)")
@@ -231,8 +231,8 @@ func (w *wizard) makeGenesis() {
231231
code, _ = contractBackend.CodeAt(ctx, multiSignWalletAddr, nil)
232232
storage = make(map[common.Hash]common.Hash)
233233
contractBackend.ForEachStorageAt(ctx, multiSignWalletAddr, nil, f)
234-
fBalance := big.NewInt(0) // 16m
235-
fBalance.Add(fBalance, big.NewInt(16*1000*1000))
234+
fBalance := big.NewInt(0) // 3 billion
235+
fBalance.Add(fBalance, big.NewInt(3*1000*1000*1000))
236236
fBalance.Mul(fBalance, big.NewInt(1000000000000000000))
237237
genesis.Alloc[common.HexToAddress(common.FoudationAddr)] = core.GenesisAccount{
238238
Balance: fBalance,
@@ -298,24 +298,24 @@ func (w *wizard) makeGenesis() {
298298
storage = make(map[common.Hash]common.Hash)
299299
contractBackend.ForEachStorageAt(ctx, multiSignWalletTeamAddr, nil, f)
300300
// Team balance.
301-
balance := big.NewInt(0) // 12m
302-
balance.Add(balance, big.NewInt(12*1000*1000))
303-
balance.Mul(balance, big.NewInt(1000000000000000000))
304-
subBalance := big.NewInt(0) // i * 50k
305-
subBalance.Add(subBalance, big.NewInt(int64(len(signers))*50*1000))
306-
subBalance.Mul(subBalance, big.NewInt(1000000000000000000))
307-
balance.Sub(balance, subBalance) // 12m - i * 50k
308-
genesis.Alloc[common.HexToAddress(common.TeamAddr)] = core.GenesisAccount{
301+
balance := big.NewInt(0) // 20 billion
302+
balance.Add(balance, big.NewInt(20*1000*1000*1000))
303+
balance.Mul(balance, big.NewInt(1000000000000000000))
304+
subBalance := big.NewInt(0) // i * 50k
305+
subBalance.Add(subBalance, big.NewInt(int64(len(signers))*50*1000))
306+
subBalance.Mul(subBalance, big.NewInt(1000000000000000000))
307+
balance.Sub(balance, subBalance) // 12m - i * 50k
308+
genesis.Alloc[common.HexToAddress(common.TeamAddr)] = core.GenesisAccount{
309309
Balance: balance,
310310
Code: code,
311311
Storage: storage,
312312
}
313313

314314
fmt.Println()
315-
fmt.Println("What is swap wallet address for fund 55m XDC?")
315+
fmt.Println("What is swap wallet address for fund 14.5Billion XDC?")
316316
swapAddr := *w.readAddress()
317-
baseBalance := big.NewInt(0) // 55m
318-
baseBalance.Add(baseBalance, big.NewInt(55*1000*1000))
317+
baseBalance := big.NewInt(0) // 14.5Billion
318+
baseBalance.Add(baseBalance, big.NewInt(145*1000*1000*100))
319319
baseBalance.Mul(baseBalance, big.NewInt(1000000000000000000))
320320
genesis.Alloc[swapAddr] = core.GenesisAccount{
321321
Balance: baseBalance,

common/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ const (
2222
MinimunMinerBlockPerEpoch = 1
2323
)
2424

25-
var TIP2019Block = big.NewInt(1050000)
25+
var TIP2019Block = big.NewInt(1)
2626
var TIPSigning = big.NewInt(3000000)
27+
var TIPRandomize = big.NewInt(3464000)
2728
var IsTestnet bool = false
2829
var StoreRewardFolder string
2930
var RollbackHash Hash

consensus/XDPoS/XDPoS.go

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// You should have received a copy of the GNU Lesser General Public License
1414
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

16-
// Package XDPoS implements the delegated-proof-of-stake consensus engine.
16+
// Package XDPoS implements the proof-of-stake-voting consensus engine.
1717
package XDPoS
1818

1919
import (
@@ -25,6 +25,8 @@ import (
2525
"math/big"
2626
"math/rand"
2727
"path/filepath"
28+
"reflect"
29+
"sort"
2830
"strconv"
2931
"sync"
3032
"time"
@@ -58,7 +60,7 @@ type Masternode struct {
5860
Stake *big.Int
5961
}
6062

61-
// XDPoS delegated-proof-of-stake protocol constants.
63+
// XDPoS proof-of-stake-voting protocol constants.
6264
var (
6365
epochLength = uint64(900) // Default number of blocks after which to checkpoint and reset the pending votes
6466

@@ -149,7 +151,7 @@ var (
149151
// backing account.
150152
//type SignerFn func(accounts.Account, []byte) ([]byte, error)
151153

152-
// sigHash returns the hash which is used as input for the delegated-proof-of-stake
154+
// sigHash returns the hash which is used as input for the proof-of-stake-voting
153155
// signing. It is the hash of the entire header apart from the 65 byte signature
154156
// contained at the end of the extra data.
155157
//
@@ -209,7 +211,7 @@ func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, er
209211
return signer, nil
210212
}
211213

212-
// XDPoS is the delegated-proof-of-stake consensus engine proposed to support the
214+
// XDPoS is the proof-of-stake-voting consensus engine proposed to support the
213215
// Ethereum testnet following the Ropsten attacks.
214216
type XDPoS struct {
215217
config *params.XDPoSConfig // Consensus engine configuration parameters
@@ -225,15 +227,15 @@ type XDPoS struct {
225227
signFn clique.SignerFn // Signer function to authorize hashes with
226228
lock sync.RWMutex // Protects the signer fields
227229

228-
BlockSigners *lru.Cache
229-
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error, map[string]interface{})
230+
BlockSigners *lru.Cache
231+
HookReward func(chain consensus.ChainReader, state *state.StateDB, header *types.Header) (error, map[string]interface{})
230232
HookPenalty func(chain consensus.ChainReader, blockNumberEpoc uint64) ([]common.Address, error)
231233
HookPenaltyTIPSigning func(chain consensus.ChainReader, header *types.Header, candidate []common.Address) ([]common.Address, error)
232234
HookValidator func(header *types.Header, signers []common.Address) ([]byte, error)
233235
HookVerifyMNs func(header *types.Header, signers []common.Address) error
234236
}
235237

236-
// New creates a XDPoS delegated-proof-of-stake consensus engine with the initial
238+
// New creates a XDPoS proof-of-stake-voting consensus engine with the initial
237239
// signers set to the ones provided by the user.
238240
func New(config *params.XDPoSConfig, db ethdb.Database) *XDPoS {
239241
// Set any missing consensus parameters to their defaults
@@ -424,9 +426,11 @@ func (c *XDPoS) verifyCascadingFields(chain consensus.ChainReader, header *types
424426
signers = RemovePenaltiesFromBlock(chain, signers, number-uint64(i)*c.config.Epoch)
425427
}
426428
}
427-
byteMasterNodes := common.ExtractAddressToBytes(signers)
428429
extraSuffix := len(header.Extra) - extraSeal
429-
if !bytes.Equal(header.Extra[extraVanity:extraSuffix], byteMasterNodes) {
430+
masternodesFromCheckpointHeader := common.ExtractAddressFromBytes(header.Extra[extraVanity:extraSuffix])
431+
validSigners := compareSignersLists(masternodesFromCheckpointHeader, signers)
432+
if !validSigners {
433+
log.Error("Masternodes lists are different in checkpoint header and snapshot", "number", number, "masternodes_from_checkpoint_header", masternodesFromCheckpointHeader, "masternodes_in_snapshot", signers, "penList", penPenalties)
430434
return errInvalidCheckpointSigners
431435
}
432436
if c.HookVerifyMNs != nil {
@@ -440,6 +444,21 @@ func (c *XDPoS) verifyCascadingFields(chain consensus.ChainReader, header *types
440444
return c.verifySeal(chain, header, parents, fullVerify)
441445
}
442446

447+
// compare 2 signers lists
448+
// return true if they are same elements, otherwise return false
449+
func compareSignersLists(list1 []common.Address, list2 []common.Address) bool {
450+
if len(list1) == 0 && len(list2) == 0 {
451+
return true
452+
}
453+
sort.Slice(list1, func(i, j int) bool {
454+
return list1[i].String() <= list1[j].String()
455+
})
456+
sort.Slice(list2, func(i, j int) bool {
457+
return list2[i].String() <= list2[j].String()
458+
})
459+
return reflect.DeepEqual(list1, list2)
460+
}
461+
443462
func (c *XDPoS) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*Snapshot, error) {
444463
number := header.Number.Uint64()
445464
log.Trace("take snapshot", "number", number, "hash", header.Hash())
@@ -739,7 +758,7 @@ func (c *XDPoS) GetValidator(creator common.Address, chain consensus.ChainReader
739758
return common.Address{}, fmt.Errorf("couldn't find checkpoint header")
740759
}
741760
}
742-
m, err := GetM1M2FromCheckpointHeader(cpHeader)
761+
m, err := GetM1M2FromCheckpointHeader(cpHeader, header, chain.Config())
743762
if err != nil {
744763
return common.Address{}, err
745764
}
@@ -977,7 +996,13 @@ func (c *XDPoS) Seal(chain consensus.ChainReader, block *types.Block, stop <-cha
977996
return nil, err
978997
}
979998
copy(header.Extra[len(header.Extra)-extraSeal:], sighash)
980-
999+
m2, err := c.GetValidator(signer, chain, header)
1000+
if err != nil {
1001+
return nil, fmt.Errorf("can't get block validator: %v", err)
1002+
}
1003+
if m2 == signer {
1004+
header.Validator = sighash
1005+
}
9811006
return block.WithSeal(header), nil
9821007
}
9831008

@@ -1119,24 +1144,39 @@ func GetMasternodesFromCheckpointHeader(checkpointHeader *types.Header) []common
11191144
}
11201145

11211146
// Get m2 list from checkpoint block.
1122-
func GetM1M2FromCheckpointHeader(checkpointHeader *types.Header) (map[common.Address]common.Address, error) {
1147+
func GetM1M2FromCheckpointHeader(checkpointHeader *types.Header, currentHeader *types.Header, config *params.ChainConfig) (map[common.Address]common.Address, error) {
11231148
if checkpointHeader.Number.Uint64()%common.EpocBlockRandomize != 0 {
11241149
return nil, errors.New("This block is not checkpoint block epoc.")
11251150
}
1126-
m1m2 := map[common.Address]common.Address{}
11271151
// Get signers from this block.
11281152
masternodes := GetMasternodesFromCheckpointHeader(checkpointHeader)
11291153
validators := ExtractValidatorsFromBytes(checkpointHeader.Validators)
1130-
1131-
if len(validators) < len(masternodes) {
1132-
return nil, errors.New("len(m2) is less than len(m1)")
1154+
m1m2, _, err := getM1M2(masternodes, validators, currentHeader, config)
1155+
if err != nil {
1156+
return map[common.Address]common.Address{}, err
11331157
}
1134-
if len(masternodes) > 0 {
1158+
return m1m2, nil
1159+
}
1160+
1161+
func getM1M2(masternodes []common.Address, validators []int64, currentHeader *types.Header, config *params.ChainConfig) (map[common.Address]common.Address, uint64, error) {
1162+
m1m2 := map[common.Address]common.Address{}
1163+
maxMNs := len(masternodes)
1164+
moveM2 := uint64(0)
1165+
if len(validators) < maxMNs {
1166+
return nil, moveM2, errors.New("len(m2) is less than len(m1)")
1167+
}
1168+
if maxMNs > 0 {
1169+
isForked := config.IsTIPRandomize(currentHeader.Number)
1170+
if isForked {
1171+
moveM2 = ((currentHeader.Number.Uint64() % config.XDPoS.Epoch) / uint64(maxMNs)) % uint64(maxMNs)
1172+
}
11351173
for i, m1 := range masternodes {
1136-
m1m2[m1] = masternodes[validators[i]%int64(len(masternodes))]
1174+
m2Index := uint64(validators[i] % int64(maxMNs))
1175+
m2Index = (m2Index + moveM2) % uint64(maxMNs)
1176+
m1m2[m1] = masternodes[m2Index]
11371177
}
11381178
}
1139-
return m1m2, nil
1179+
return m1m2, moveM2, nil
11401180
}
11411181

11421182
// Extract validators from byte array.

0 commit comments

Comments
 (0)