Skip to content

Commit dd140be

Browse files
committed
Merge pull request #1443 from Gustav-Simonsson/core_uint64_ts
Core uint64 ts
2 parents 06afabb + 5d6d40f commit dd140be

19 files changed

+1114
-1749
lines changed

core/block_processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, check
386386
return BlockEqualTSErr
387387
}
388388

389-
expd := CalcDifficulty(int64(block.Time), int64(parent.Time()), parent.Difficulty())
389+
expd := CalcDifficulty(block.Time, parent.Time(), parent.Difficulty())
390390
if expd.Cmp(block.Difficulty) != 0 {
391391
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
392392
}

core/chain_makers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header {
171171
Root: state.Root(),
172172
ParentHash: parent.Hash(),
173173
Coinbase: parent.Coinbase(),
174-
Difficulty: CalcDifficulty(int64(time), int64(parent.Time()), parent.Difficulty()),
174+
Difficulty: CalcDifficulty(time, parent.Time(), parent.Difficulty()),
175175
GasLimit: CalcGasLimit(parent),
176176
GasUsed: new(big.Int),
177177
Number: new(big.Int).Add(parent.Number(), common.Big1),

core/chain_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
611611
// Allow up to MaxFuture second in the future blocks. If this limit
612612
// is exceeded the chain is discarded and processed at a later time
613613
// if given.
614-
if max := time.Now().Unix() + maxTimeFutureBlocks; int64(block.Time()) > max {
614+
if max := uint64(time.Now().Unix()) + maxTimeFutureBlocks; block.Time() > max {
615615
return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
616616
}
617617

core/chain_util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ import (
3131
// CalcDifficulty is the difficulty adjustment algorithm. It returns
3232
// the difficulty that a new block b should have when created at time
3333
// given the parent block's time and difficulty.
34-
func CalcDifficulty(time int64, parentTime int64, parentDiff *big.Int) *big.Int {
34+
func CalcDifficulty(time, parentTime uint64, parentDiff *big.Int) *big.Int {
3535
diff := new(big.Int)
3636
adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
37-
if big.NewInt(time-parentTime).Cmp(params.DurationLimit) < 0 {
37+
bigTime := new(big.Int)
38+
bigParentTime := new(big.Int)
39+
40+
bigTime.SetUint64(time)
41+
bigParentTime.SetUint64(parentTime)
42+
43+
if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
3844
diff.Add(parentDiff, adjust)
3945
} else {
4046
diff.Sub(parentDiff, adjust)

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func (self *worker) commitNewWork() {
427427
header := &types.Header{
428428
ParentHash: parent.Hash(),
429429
Number: num.Add(num, common.Big1),
430-
Difficulty: core.CalcDifficulty(int64(tstamp), int64(parent.Time()), parent.Difficulty()),
430+
Difficulty: core.CalcDifficulty(uint64(tstamp), parent.Time(), parent.Difficulty()),
431431
GasLimit: core.CalcGasLimit(parent),
432432
GasUsed: new(big.Int),
433433
Coinbase: self.coinbase,

tests/block_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ func TestBcGasPricer(t *testing.T) {
9494
t.Fatal(err)
9595
}
9696
}
97+
98+
// TODO: iterate over files once we got more than a few
99+
func TestBcRandom(t *testing.T) {
100+
err := RunBlockTest(filepath.Join(blockTestDir, "RandomTests/bl201507071825GO.json"), BlockSkipTests)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
}

tests/block_test_util.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,22 @@ func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, erro
208208
db := ethereum.StateDb()
209209
statedb := state.New(common.Hash{}, db)
210210
for addrString, acct := range t.preAccounts {
211-
addr, _ := hex.DecodeString(addrString)
212-
code, _ := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
213-
balance, _ := new(big.Int).SetString(acct.Balance, 0)
214-
nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64)
211+
addr, err := hex.DecodeString(addrString)
212+
if err != nil {
213+
return nil, err
214+
}
215+
code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
216+
if err != nil {
217+
return nil, err
218+
}
219+
balance, ok := new(big.Int).SetString(acct.Balance, 0)
220+
if !ok {
221+
return nil, err
222+
}
223+
nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
224+
if err != nil {
225+
return nil, err
226+
}
215227

216228
if acct.PrivateKey != "" {
217229
privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x"))
@@ -365,10 +377,22 @@ func (s *BlockTest) validateBlockHeader(h *btHeader, h2 *types.Header) error {
365377
func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
366378
for addrString, acct := range t.preAccounts {
367379
// XXX: is is worth it checking for errors here?
368-
addr, _ := hex.DecodeString(addrString)
369-
code, _ := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
370-
balance, _ := new(big.Int).SetString(acct.Balance, 0)
371-
nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64)
380+
addr, err := hex.DecodeString(addrString)
381+
if err != nil {
382+
return err
383+
}
384+
code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
385+
if err != nil {
386+
return err
387+
}
388+
balance, ok := new(big.Int).SetString(acct.Balance, 0)
389+
if !ok {
390+
return err
391+
}
392+
nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64)
393+
if err != nil {
394+
return err
395+
}
372396

373397
// address is indirectly verified by the other fields, as it's the db key
374398
code2 := statedb.GetCode(common.BytesToAddress(addr))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"randomBlockTest" : {
3+
"blocks" : [
4+
{
5+
"rlp" : "0xf90200f901fba02f6b2f4abf04038f851128f127ce83d21003487102c7276af6cbe8a48b93cbbfa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479484c14767a434cf1ac572ab0852c2254e4e64399fa07d8fad01557373365967bfffd34ceee8bfef2b11ed23aa4d5df15b6b3588b0cda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004001832fefd88088f332233a811f07bf80a04fb868837384a546941f355fe83aaf1a931e0c5bb22a4452a62c3eba5a9ecae08868bc5a38e3f02565c0c0"
6+
}
7+
],
8+
"genesisBlockHeader" : {
9+
"bloom" : "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
10+
"coinbase" : "84c14767a434cf1ac572ab0852c2254e4e64399f",
11+
"difficulty" : "0x020000",
12+
"extraData" : "0x",
13+
"gasLimit" : "0x2fefd8",
14+
"gasUsed" : "0x00",
15+
"hash" : "2f6b2f4abf04038f851128f127ce83d21003487102c7276af6cbe8a48b93cbbf",
16+
"mixHash" : "857afac057efaa7e1f89663d0b672b263449b72dc5020fe323a190bac09ed5db",
17+
"nonce" : "46701876a4d4ec53",
18+
"number" : "0x00",
19+
"parentHash" : "0000000000000000000000000000000000000000000000000000000000000000",
20+
"receiptTrie" : "ce3fe73c3c6c026099f72ec7fadd76ad197d88bcfaa63f7382df548652478eb5",
21+
"stateRoot" : "bba90a92c15bad8d48a14174ca1841c490a4f947faa3d6f24f981e25fdcd00e3",
22+
"timestamp" : "0xf332233a811f07be",
23+
"transactionsTrie" : "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
24+
"uncleHash" : "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
25+
},
26+
"genesisRLP" : "0xf90200f901fba00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479484c14767a434cf1ac572ab0852c2254e4e64399fa0bba90a92c15bad8d48a14174ca1841c490a4f947faa3d6f24f981e25fdcd00e3a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0ce3fe73c3c6c026099f72ec7fadd76ad197d88bcfaa63f7382df548652478eb5b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd88088f332233a811f07be80a0857afac057efaa7e1f89663d0b672b263449b72dc5020fe323a190bac09ed5db8846701876a4d4ec53c0c0",
27+
"lastblockhash" : "2f6b2f4abf04038f851128f127ce83d21003487102c7276af6cbe8a48b93cbbf",
28+
"postState" : {
29+
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
30+
"balance" : "0x2ae0e538",
31+
"code" : "0x60076008600f5f601c6012601960196019601f983873d3aaa5c6ca8b5cf34e0718e20743b752e75b1731649d08ca20c368b19777f2852275ecd2695ac2dde8ad14dd1ebe726e5c3091e3c1ccbe14777b86620c65886f6ed37757749bc2e3f7d230f9a35ac6957df2d8c828b180843a6a74d261aa7ffc2adf034304254347303ca0298c73616969f140933964d758c58e62df54666c87f9e93670be8ea7ef72b8fd8d68edad39f5f6ea82e166783955dcd67a4cafc4b1dc75c8e5371f3305159ce31041090f35753c503bf35417fa0bae261d28993348837f70f89b26676e3c7a5d84bf2b92170e1064730ee171675071a61b0240be019d6cea31689f3ecf69588c90d33610652ce06ff1b296036004601a601260176002601a600e60056010600f6003600460176010600c600c60139f",
32+
"nonce" : "0x00",
33+
"storage" : {
34+
}
35+
},
36+
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
37+
"balance" : "0x4f4102",
38+
"code" : "0x",
39+
"nonce" : "0x00",
40+
"storage" : {
41+
}
42+
}
43+
},
44+
"pre" : {
45+
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
46+
"balance" : "0x2ae0e538",
47+
"code" : "0x60076008600f5f601c6012601960196019601f983873d3aaa5c6ca8b5cf34e0718e20743b752e75b1731649d08ca20c368b19777f2852275ecd2695ac2dde8ad14dd1ebe726e5c3091e3c1ccbe14777b86620c65886f6ed37757749bc2e3f7d230f9a35ac6957df2d8c828b180843a6a74d261aa7ffc2adf034304254347303ca0298c73616969f140933964d758c58e62df54666c87f9e93670be8ea7ef72b8fd8d68edad39f5f6ea82e166783955dcd67a4cafc4b1dc75c8e5371f3305159ce31041090f35753c503bf35417fa0bae261d28993348837f70f89b26676e3c7a5d84bf2b92170e1064730ee171675071a61b0240be019d6cea31689f3ecf69588c90d33610652ce06ff1b296036004601a601260176002601a600e60056010600f6003600460176010600c600c60139f",
48+
"nonce" : "0x00",
49+
"storage" : {
50+
}
51+
},
52+
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
53+
"balance" : "0x4f4102",
54+
"code" : "0x",
55+
"nonce" : "0x00",
56+
"storage" : {
57+
}
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)