示例#1
0
 def find_my_indices(self):
     epoch = self.chain.state.block_number // self.epoch_length
     print 'Finding indices for epoch %d' % epoch, self.call_casper(
         'getEpoch')
     for i in range(len(validator_sizes)):
         valcount = self.call_casper('getHistoricalValidatorCount',
                                     [epoch, i])
         print i, valcount, self.call_casper('getHistoricalValidatorCount',
                                             [0, i])
         for j in range(valcount):
             valcode = self.call_casper('getValidationCode', [i, j])
             print(valcode, self.validation_code)
             if valcode == self.validation_code:
                 self.indices = i, j
                 start = self.call_casper('getStartEpoch', [i, j])
                 end = self.call_casper('getEndEpoch', [i, j])
                 if start <= epoch < end:
                     self.active = True
                     self.next_skip_count = 0
                     self.next_skip_timestamp = get_timestamp(
                         self.chain, self.next_skip_count)
                     print 'In current validator set at (%d, %d)' % (i, j)
                     return
                 else:
                     self.indices = None
                     self.active = False
                     self.next_skip_count, self.next_skip_timestamp = 0, 0
                     print 'Registered at (%d, %d) but not in current set' % (
                         i, j)
                     return
     self.indices = None
     self.active = False
     self.next_skip_count, self.next_skip_timestamp = 0, 0
     print 'Not in current validator set'
示例#2
0
 def find_my_indices(self):
     epoch = self.chain.state.block_number // self.epoch_length
     print 'Finding indices for epoch %d' % epoch, self.call_casper('getEpoch')
     for i in range(len(validator_sizes)):
         valcount = self.call_casper('getHistoricalValidatorCount', [epoch, i])
         print i, valcount, self.call_casper('getHistoricalValidatorCount', [0, i])
         for j in range(valcount):
             valcode = self.call_casper('getValidationCode', [i, j])
             print (valcode, self.validation_code)
             if valcode == self.validation_code:
                 self.indices = i, j
                 start = self.call_casper('getStartEpoch', [i, j])
                 end = self.call_casper('getEndEpoch', [i, j])
                 if start <= epoch < end:
                     self.active = True
                     self.next_skip_count = 0
                     self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
                     print 'In current validator set at (%d, %d)' % (i, j)
                     return
                 else:
                     self.indices = None
                     self.active = False
                     self.next_skip_count, self.next_skip_timestamp = 0, 0
                     print 'Registered at (%d, %d) but not in current set' % (i, j)
                     return
     self.indices = None
     self.active = False
     self.next_skip_count, self.next_skip_timestamp = 0, 0
     print 'Not in current validator set'
示例#3
0
 def update_head(self):
     if self.cached_head == self.chain.head_hash:
         return
     self.cached_head = self.chain.head_hash
     if self.chain.state.block_number % EPOCH_LENGTH == 0:
         self.find_my_indices()
     if self.indices:
         self.next_skip_count = 0
         self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
     print 'Head changed: %s, will attempt creating a block at %d' % (self.chain.head_hash.encode('hex'), self.next_skip_timestamp)
示例#4
0
 def update_head(self):
     if self.cached_head == self.chain.head_hash:
         return
     self.cached_head = self.chain.head_hash
     if self.chain.state.block_number % self.epoch_length == 0:
         self.update_activity_status()
     if self.active:
         self.next_skip_count = 0
         self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
     print 'Head changed: %s, will attempt creating a block at %d' % (self.chain.head_hash.encode('hex'), self.next_skip_timestamp)
示例#5
0
 def update_activity_status(self):
     start_epoch = self.call_casper('getStartEpoch', [self.vchash])
     now_epoch = self.call_casper('getEpoch')
     end_epoch = self.call_casper('getEndEpoch', [self.vchash])
     if start_epoch <= now_epoch < end_epoch:
         self.active = True
         self.next_skip_count = 0
         self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
         print 'In current validator set'
     else:
         self.active = False
示例#6
0
 def update_head(self):
     if self.cached_head == self.chain.head_hash:
         return
     self.cached_head = self.chain.head_hash
     if self.chain.state.block_number % self.epoch_length == 0:
         self.find_my_indices()
     if self.indices:
         self.next_skip_count = 0
         self.next_skip_timestamp = get_timestamp(self.chain,
                                                  self.next_skip_count)
     print 'Head changed: %s, will attempt creating a block at %d' % (
         self.chain.head_hash.encode('hex'), self.next_skip_timestamp)
示例#7
0
文件: casper.py 项目: yep/research
 def update_activity_status(self):
     start_epoch = self.call_casper('getStartEpoch', [self.vchash])
     now_epoch = self.call_casper('getEpoch')
     end_epoch = self.call_casper('getEndEpoch', [self.vchash])
     if start_epoch <= now_epoch < end_epoch:
         self.active = True
         self.next_skip_count = 0
         self.next_skip_timestamp = get_timestamp(self.chain,
                                                  self.next_skip_count)
         print 'In current validator set'
     else:
         self.active = False
示例#8
0
 def tick(self):
     # Try to create a block
     # Conditions:
     # (i) you are an active validator,
     # (ii) you have not yet made a block with this parent
     if self.indices and self.chain.head_hash not in self.used_parents:
         t = self.get_timestamp()
         # Is it early enough to create the block?
         if t >= self.next_skip_timestamp and (not self.chain.head or t > self.chain.head.header.timestamp):
             print 'creating', t, self.next_skip_timestamp
             # Wrong validator; in this case, just wait for the next skip count
             if not check_skips(self.chain, self.indices, self.next_skip_count):
                 self.next_skip_count += 1
                 self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
                 print 'Incrementing proposed timestamp for block %d to %d' % \
                     (self.chain.head.header.number + 1 if self.chain.head else 0, self.next_skip_timestamp)
                 return
             self.used_parents[self.chain.head_hash] = True
             # Simulated 15% chance of validator failure to make a block
             if random.random() > 0.999:
                 print 'Simulating validator failure, block %d not created' % (self.chain.head.header.number + 1 if self.chain.head else 0)
                 return
             # Make the block, make sure it's valid
             pre_dunkle_count = call_casper(self.chain.state, 'getTotalDunklesIncluded', [])
             dunkle_txs = []
             for i, u in enumerate(self.get_uncles()[:4]):
                 start_nonce = self.chain.state.get_nonce(self.address)
                 print 'start_nonce', start_nonce
                 txdata = casper_ct.encode('includeDunkle', [rlp.encode(u)])
                 dunkle_txs.append(Transaction(start_nonce + i, 0, 650000, self.chain.config['CASPER_ADDR'], 0, txdata).sign(self.key))
             for dtx in dunkle_txs[::-1]:
                 self.chain.add_transaction(dtx, force=True)
             blk = make_block(self.chain, self.key, self.randao, self.indices, self.next_skip_count)
             global global_block_counter
             global_block_counter += 1
             for dtx in dunkle_txs:
                 assert dtx in blk.transactions, (dtx, blk.transactions)
             print 'made block with timestamp %d and %d dunkles' % (blk.timestamp, len(dunkle_txs))
             assert blk.timestamp >= self.next_skip_timestamp
             assert self.chain.add_block(blk)
             self.update_head()
             post_dunkle_count = call_casper(self.chain.state, 'getTotalDunklesIncluded', [])
             assert post_dunkle_count - pre_dunkle_count == len(dunkle_txs)
             self.received_objects[blk.hash] = True
             print 'Validator %d making block %d (%s)' % (self.id, blk.header.number, blk.header.hash[:8].encode('hex'))
             self.network.broadcast(self, blk)
     # Sometimes we received blocks too early or out of order;
     # run an occasional loop that processes these
     if random.random() < 0.02:
         self.chain.process_time_queue()
         self.chain.process_parent_queue()
         self.update_head()
示例#9
0
 def tick(self):
     # Try to create a block
     # Conditions:
     # (i) you are an active validator,
     # (ii) you have not yet made a block with this parent
     if self.active and self.chain.head_hash not in self.used_parents:
         t = self.get_timestamp()
         # Is it early enough to create the block?
         if t >= self.next_skip_timestamp and (not self.chain.head or t > self.chain.head.header.timestamp):
             # Wrong validator; in this case, just wait for the next skip count
             if not check_skips(self.chain, self.vchash, self.next_skip_count):
                 self.next_skip_count += 1
                 self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
                 # print 'Incrementing proposed timestamp for block %d to %d' % \
                 #     (self.chain.head.header.number + 1 if self.chain.head else 0, self.next_skip_timestamp)
                 return
             self.used_parents[self.chain.head_hash] = True
             # Simulated 15% chance of validator failure to make a block
             if random.random() > 0.999:
                 print 'Simulating validator failure, block %d not created' % (self.chain.head.header.number + 1 if self.chain.head else 0)
                 return
             # Make the block
             s1 = self.chain.state.trie.root_hash
             pre_dunkle_count = self.call_casper('getTotalDunklesIncluded')
             dunkle_txs = get_dunkle_candidates(self.chain, self.chain.state)
             blk = make_head_candidate(self.chain, self.txqueue)
             randao = self.randao.get_parent(self.call_casper('getRandao', [self.vchash]))
             blk = sign_block(blk, self.key, randao, self.vchash, self.next_skip_count)
             # Make sure it's valid
             global global_block_counter
             global_block_counter += 1
             for dtx in dunkle_txs:
                 assert dtx in blk.transactions, (dtx, blk.transactions)
             print 'made block with timestamp %d and %d dunkles' % (blk.timestamp, len(dunkle_txs))
             s2 = self.chain.state.trie.root_hash
             assert s1 == s2
             assert blk.timestamp >= self.next_skip_timestamp
             assert self.chain.add_block(blk)
             self.update_head()
             post_dunkle_count = self.call_casper('getTotalDunklesIncluded')
             assert post_dunkle_count - pre_dunkle_count == len(dunkle_txs)
             self.received_objects[blk.hash] = True
             print 'Validator %d making block %d (%s)' % (self.id, blk.header.number, blk.header.hash[:8].encode('hex'))
             self.network.broadcast(self, blk)
     # Sometimes we received blocks too early or out of order;
     # run an occasional loop that processes these
     if random.random() < 0.02:
         self.chain.process_time_queue()
         self.chain.process_parent_queue()
         self.update_head()
示例#10
0
 def find_my_indices(self):
     for i in range(len(validatorSizes)):
         epoch = self.chain.state.block_number // EPOCH_LENGTH
         valcount = call_casper(self.chain.state, 'getHistoricalValidatorCount', [epoch, i])
         for j in range(valcount):
             valcode = call_casper(self.chain.state, 'getValidationCode', [i, j])
             if valcode == self.validation_code:
                 self.indices = i, j
                 self.next_skip_count = 0
                 self.next_skip_timestamp = get_timestamp(self.chain, self.next_skip_count)
                 print 'In current validator set at (%d, %d)' % (i, j)
                 return
     self.indices = None
     self.next_skip_count, self.next_skip_timestamp = 0, 0
     print 'Not in current validator set'
示例#11
0
def make_block(chain, key, randao, vchash, skips):
    h, _ = make_head_candidate(chain, TransactionQueue(), timestamp=get_timestamp(chain, skips))
    return sign_block(h, key, randao.get_parent(call_casper(chain.state, 'getRandao', [vchash])), vchash, skips)
示例#12
0
 def tick(self):
     # Try to create a block
     # Conditions:
     # (i) you are an active validator,
     # (ii) you have not yet made a block with this parent
     if self.indices and self.chain.head_hash not in self.used_parents:
         t = self.get_timestamp()
         # Is it early enough to create the block?
         if t >= self.next_skip_timestamp and (
                 not self.chain.head
                 or t > self.chain.head.header.timestamp):
             # Wrong validator; in this case, just wait for the next skip count
             if not check_skips(self.chain, self.indices,
                                self.next_skip_count):
                 self.next_skip_count += 1
                 self.next_skip_timestamp = get_timestamp(
                     self.chain, self.next_skip_count)
                 # print 'Incrementing proposed timestamp for block %d to %d' % \
                 #     (self.chain.head.header.number + 1 if self.chain.head else 0, self.next_skip_timestamp)
                 return
             self.used_parents[self.chain.head_hash] = True
             # Simulated 15% chance of validator failure to make a block
             if random.random() > 0.999:
                 print 'Simulating validator failure, block %d not created' % (
                     self.chain.head.header.number +
                     1 if self.chain.head else 0)
                 return
             # Make the block, make sure it's valid
             pre_dunkle_count = call_casper(self.chain.state,
                                            'getTotalDunklesIncluded', [])
             dunkle_txs = []
             for i, u in enumerate(self.get_uncles()[:4]):
                 start_nonce = self.chain.state.get_nonce(self.address)
                 txdata = casper_ct.encode('includeDunkle', [rlp.encode(u)])
                 dunkle_txs.append(
                     Transaction(start_nonce + i, 0, 650000,
                                 self.chain.config['CASPER_ADDR'], 0,
                                 txdata).sign(self.key))
             for dtx in dunkle_txs[::-1]:
                 self.chain.add_transaction(dtx, force=True)
             blk = make_block(self.chain, self.key, self.randao,
                              self.indices, self.next_skip_count)
             global global_block_counter
             global_block_counter += 1
             for dtx in dunkle_txs:
                 assert dtx in blk.transactions, (dtx, blk.transactions)
             print 'made block with timestamp %d and %d dunkles' % (
                 blk.timestamp, len(dunkle_txs))
             assert blk.timestamp >= self.next_skip_timestamp
             assert self.chain.add_block(blk)
             self.update_head()
             post_dunkle_count = call_casper(self.chain.state,
                                             'getTotalDunklesIncluded', [])
             assert post_dunkle_count - pre_dunkle_count == len(dunkle_txs)
             self.received_objects[blk.hash] = True
             print 'Validator %d making block %d (%s)' % (
                 self.id, blk.header.number,
                 blk.header.hash[:8].encode('hex'))
             self.network.broadcast(self, blk)
     # Sometimes we received blocks too early or out of order;
     # run an occasional loop that processes these
     if random.random() < 0.02:
         self.chain.process_time_queue()
         self.chain.process_parent_queue()
         self.update_head()
示例#13
0
文件: casper.py 项目: yep/research
 def tick(self):
     # Try to create a block
     # Conditions:
     # (i) you are an active validator,
     # (ii) you have not yet made a block with this parent
     if self.active and self.chain.head_hash not in self.used_parents:
         t = self.get_timestamp()
         # Is it early enough to create the block?
         if t >= self.next_skip_timestamp and (
                 not self.chain.head
                 or t > self.chain.head.header.timestamp):
             # Wrong validator; in this case, just wait for the next skip count
             if not check_skips(self.chain, self.vchash,
                                self.next_skip_count):
                 self.next_skip_count += 1
                 self.next_skip_timestamp = get_timestamp(
                     self.chain, self.next_skip_count)
                 # print 'Incrementing proposed timestamp for block %d to %d' % \
                 #     (self.chain.head.header.number + 1 if self.chain.head else 0, self.next_skip_timestamp)
                 return
             self.used_parents[self.chain.head_hash] = True
             # Simulated 15% chance of validator failure to make a block
             if random.random() > 0.999:
                 print 'Simulating validator failure, block %d not created' % (
                     self.chain.head.header.number +
                     1 if self.chain.head else 0)
                 return
             # Make the block
             s1 = self.chain.state.trie.root_hash
             pre_dunkle_count = self.call_casper('getTotalDunklesIncluded')
             dunkle_txs = get_dunkle_candidates(self.chain,
                                                self.chain.state)
             blk = make_head_candidate(self.chain, self.txqueue)
             randao = self.randao.get_parent(
                 self.call_casper('getRandao', [self.vchash]))
             blk = sign_block(blk, self.key, randao, self.vchash,
                              self.next_skip_count)
             # Make sure it's valid
             global global_block_counter
             global_block_counter += 1
             for dtx in dunkle_txs:
                 assert dtx in blk.transactions, (dtx, blk.transactions)
             print 'made block with timestamp %d and %d dunkles' % (
                 blk.timestamp, len(dunkle_txs))
             s2 = self.chain.state.trie.root_hash
             assert s1 == s2
             assert blk.timestamp >= self.next_skip_timestamp
             assert self.chain.add_block(blk)
             self.update_head()
             post_dunkle_count = self.call_casper('getTotalDunklesIncluded')
             assert post_dunkle_count - pre_dunkle_count == len(dunkle_txs)
             self.received_objects[blk.hash] = True
             print 'Validator %d making block %d (%s)' % (
                 self.id, blk.header.number,
                 blk.header.hash[:8].encode('hex'))
             self.network.broadcast(self, blk)
     # Sometimes we received blocks too early or out of order;
     # run an occasional loop that processes these
     if random.random() < 0.02:
         self.chain.process_time_queue()
         self.chain.process_parent_queue()
         self.update_head()