Lottery contract has infinite gas issue - infinite-loop

I'm getting this error "Gas costs:
Gas requirement of function Lottery.getPlayers is infinite:
If the gas requirement of a function is higher than the block gas limit, it cannot be executed.
Please avoid loops in your functions or actions that modify large areas of storage
(this includes clearing or copying arrays in storage)" Also I want to add a way to deposit extra tokens from the same wallet as the contract is deployed.
```
pragma solidity ^0.6.0;
contract Lottery {
address public manager;
address[] public players;
constructor() public {
manager = msg.sender;
}
modifier restricted() {
require(msg.sender == manager,"only manager has access");
_;
}
function balanceInPool()public view returns(uint){
return address(this).balance;
}
function enter() public payable {
require(msg.value > .01 ether,"the amount should be more than 0.1 ether");
players.push(msg.sender);
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty,now,players)));
}
function pickWinner() public restricted {
uint index = random() % players.length;
address payable winner = payable(players[index]);
players = new address[](0);
winner.transfer(address(this).balance);
}
function getPlayers()public view returns(address[] memory){
return players;
}
}
```

Related

Multiple chained API calls to fetch data, but doOnNext of PublishSubject is never reached

I have a problem to understand a chained "RXJava-Retrofit" API call. I got inspired by this and implement this class named ObservationLoader to load the data from the API bucket per bucket. When the end of data is reached the API sends a endOfRecords=true:
public Observable<PageObject<Observation>> getAllObservationDataByRegion(long taxonKey,
String regionId) {
final PublishSubject<PageObject<Observation>> subject = PublishSubject.create();
return subject.doOnSubscribe(disposable -> {
this.getData(taxonKey, regionId, 0).subscribe(subject);
})
.doOnNext(observationPageObject -> {
if (observationPageObject.isEndOfRecords()) {
// -> list is completely loaded
subject.onComplete();
} else {
int nextOffset = observationPageObject.getOffset() + 1;
this.getData(taxonKey, regionId, null, nextOffset).subscribe(subject);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
private Observable<PageObject<Observation>> getData(long id,
String regionId,
int offset) {
// Get your API response value
return this.api.getObservations(id, regionId, ObservationLoader.PAGE_LIMIT, offset);
}
In my Android fragment HomeFragment I subscribe to the ObservationLoader:
ObservationLoader loader = new ObservationLoader(this.getApi());
Observable<PageObject<Observation>> observable = loader
.getAllObservationDataByRegion(this.getSelectedSpecies(), this.getSelectedRegion());
observable.subscribe(new Observer<PageObject<Observation>>() {
#Override
public void onSubscribe(Disposable d) {
Log.i(TAG, "ON_SUBSCRIBE");
}
#Override
public void onNext(PageObject<Observation> observationPageObject) {
Log.i(TAG, "ON_NEXT");
}
#Override
public void onError(Throwable e) {
Log.i(TAG, "ERROR = " + e.getMessage());
}
#Override
public void onComplete() {
Log.i(TAG, "COMPLETED");
}
});
I can see that the onSubscribe() and doOnSubscribe() are called and even the getData() is reached. I assume the API is responding correctly (a previous attempt attempt with recursion worked fine). But I never reached the doOnNext function. The observer goes straight to onComplete() and no data is received. What could be the reason?
When doOnSubscribe runs, the doesn't see any consumers yet so if getData is synchronous, there won't be any first results to trigger further results. Also if getData ends, it will complete the setup so the next getData call in doOnNext will push to an already terminated subject, ingoring all data.
You'll need a differently organized feedback loop:
// we loop back the nextOffset, in a thread-safe manner
Subject<Integer> subject = PublishSubject.<Integer>create()
.toSerialized();
// bootstrap with 0 and keep open for more offsets
subject.mergeWith(Observable.just(0))
// get the data for the current offset
.concatMap(nextOffset -> getData(taxonKey, regionId, nextOffset)
.subscribeOn(Schedulers.io())
)
// if the response is end of records, stop
.takeWhile(observationPageObject -> !observationPageObject.isEndOfRecords())
// otherwise not end of records, feedback the new offset
.doOnNext(observationPageObject ->
subject.onNext(observationPageObject.getOffset() + 1)
)
// get the data on the main thread
.observeOn(AndroidSchedulers.mainThread());

The arithmetic operation can overflow is it possible to cause an arithmetic overflow?

I wanted some advice for the following contract function giving a possible overflow. Assert or require input spit back "ParserError: Expected ',' but got identifier
assert (uint i = 0; i < _index + 1; i++) {"
I am leaning towards the require implementation, giving the safe math library is already imported at the beginning of the contract. I have seen many different contracts with the same implementation but have some issues determining the correct approach. thanks so much for any help.
The arithmetic operation can overflow.
It is possible to cause an arithmetic overflow. Prevent the overflow by constraining inputs using the required () statement or the OpenZeppelin SafeMath library for integer arithmetic operations. Refer to the transaction trace generated for this issue to reproduce the overflow
I will attach as well the reproduction the for the vulnerability
[Instructions to reproduce this vulnerability (Test Case 1)]
contract FreezableToken is StandardToken {
// freezing chains
mapping (bytes32 => uint64) internal chains;
// freezing amounts for each chain
mapping (bytes32 => uint) internal freezings;
// total freezing balance per address
mapping (address => uint) internal freezingBalance;
event Freezed(address indexed to, uint64 release, uint amount);
event Released(address indexed owner, uint amount);
/**
* #dev Gets the balance of the specified address include freezing tokens.
* #param _owner The address to query the the balance of.
* #return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address _owner) public view returns (uint256 balance) {
return super.balanceOf(_owner) + freezingBalance[_owner];
}
/**
* #dev Gets the balance of the specified address without freezing tokens.
* #param _owner The address to query the the balance of.
* #return An uint256 representing the amount owned by the passed address.
*/
function actualBalanceOf(address _owner) public view returns (uint256 balance) {
return super.balanceOf(_owner);
}
function freezingBalanceOf(address _owner) public view returns (uint256 balance) {
return freezingBalance[_owner];
}
/**
* #dev gets freezing count
* #param _addr Address of freeze tokens owner.
*/
function freezingCount(address _addr) public view returns (uint count) {
uint64 release = chains[toKey(_addr, 0)];
while (release != 0) {
count++;
release = chains[toKey(_addr, release)];
}
}
/**
* #dev gets freezing end date and freezing balance for the freezing portion specified by index.
* #param _addr Address of freeze tokens owner.
* #param _index Freezing portion index. It ordered by release date descending.
*/
function getFreezing(address _addr, uint _index) public view returns (uint64 _release, uint _balance) {
for (uint i = 0; i < _index + 1; i++) { **<- Error Here ``< _index + 1; i++)``**
_release = chains[toKey(_addr, _release)];
if (_release == 0) {
return;
}
}
_balance = freezings[toKey(_addr, _release)];
}
/**
* #dev freeze your tokens to the specified address.
* Be careful, gas usage is not deterministic,
* and depends on how many freezes _to address already has.
* #param _to Address to which token will be freeze.
* #param _amount Amount of token to freeze.
* #param _until Release date, must be in future.
*/
function freezeTo(address _to, uint _amount, uint64 _until) public {
require(_to != address(0));
require(_amount <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(_amount);
bytes32 currentKey = toKey(_to, _until);
freezings[currentKey] = freezings[currentKey].add(_amount);
freezingBalance[_to] = freezingBalance[_to].add(_amount);
freeze(_to, _until);
emit Transfer(msg.sender, _to, _amount);
emit Freezed(_to, _until, _amount);
}
/**
* #dev release first available freezing tokens.
*/
function releaseOnce() public {
bytes32 headKey = toKey(msg.sender, 0);
uint64 head = chains[headKey];
require(head != 0);
require(uint64(block.timestamp) > head);
bytes32 currentKey = toKey(msg.sender, head);
uint64 next = chains[currentKey];
uint amount = freezings[currentKey];
balances[msg.sender] = balances[msg.sender].add(amount);
freezingBalance[msg.sender] = freezingBalance[msg.sender].sub(amount);
if (next == 0) {
} else {
chains[headKey] = next;
}
emit Released(msg.sender, amount);
}
/**
* #dev release all available for release freezing tokens. Gas usage is not deterministic!
* #return how many tokens was released
*/
function releaseAll() public returns (uint tokens) {
uint release;
uint balance;
(release, balance) = getFreezing(msg.sender, 0);
while (release != 0 && block.timestamp > release) {
releaseOnce();
tokens += balance;
(release, balance) = getFreezing(msg.sender, 0);
}
}
function toKey(address _addr, uint _release) internal pure returns (bytes32 result) {
// WISH masc to increase entropy
result = 0x5749534800000000000000000000000000000000000000000000000000000000;
assembly {
result := or(result, mul(_addr, 0x10000000000000000))
result := or(result, and(_release, 0xffffffffffffffff))
}
}
function freeze(address _to, uint64 _until) internal {
require (_until > block.timestamp);
bytes32 key = toKey(_to, _until);
bytes32 parentKey = toKey(_to, uint64(0));
uint64 next = chains[parentKey];
if (next == 0) {
chains[parentKey] = _until;
return;
}
bytes32 nextKey = toKey(_to, next);
uint parent;
while (next != 0 && _until > next) {
parent = next;
parentKey = nextKey;
next = chains[nextKey];
nextKey = toKey(_to, next);
}
if (_until == next) {
return;
}
if (next != 0) {
chains[key] = next;
}
chains[parentKey] = _until;
}
}```
[1]: https://i.stack.imgur.com/ayg2D.png
Based on the context, I'm assuming that the error message "The arithmetic operation can overflow." is from a static analysis tool.
Assuming that you're using Solidity version lower than 0.8.0, the for loop definition is theoretically vulnerable to integer overflow. But only if the _index is 2^256, the max value of uint. This value would make the _index + 1 expression to overflow.
It's not sufficient to just import the SafeMath library. You also need to use its functions instead of the native arithmetic operations to prevent overflow.
contract FreezableToken is StandardToken {
// allows to use functions of the library on `uint` type
using SafeMath for uint;
function getFreezing() public {
// use the `add()` function of the library instead of the `+` operation
for (uint i = 0; i < _index.add(1); i = i.add(1)) {
Or upgrade to Solidity version 0.8+ that checks for overflow on the language level, so that you won't have to use the SafeMath library.

Vault query for cash in flow test not working

Issuing some cash within a Flow test - the flow returns the transaction with the output showing the correct Cash state. However, when I vault query for cash states, nothing is returned. Am I missing something?
IssueTokensFlow
#StartableByRPC
public class IssueTokensFlow extends FlowLogic<SignedTransaction> {
private static Double amount;
public IssueTokensFlow(double amount) {
this.amount = amount;
}
#Suspendable
#Override
public SignedTransaction call() throws FlowException {
// We retrieve the notary identity from the network map.
final Party notary = getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0);
// Issue cash tokens equal to transfer amount
AbstractCashFlow.Result cashIssueResult = subFlow(new CashIssueFlow(
Currencies.DOLLARS(amount), OpaqueBytes.of(Byte.parseByte("1")), notary)
);
return cashIssueResult.getStx();
} }
IssueTokenFlow Test
#Test
public void testIssueCash() throws Exception {
IssueTokensFlow flow =
new IssueTokensFlow(100.00);
SignedTransaction transaction = a.startFlow(flow).get();
network.waitQuiescent();
Cash.State state = (Cash.State) transaction.getTx().getOutputStates().get(0);
assertEquals(state.getOwner(), chooseIdentity(a.getInfo()));
assertEquals(state.getAmount().getQuantity(), Currencies.DOLLARS(100.00).getQuantity());
// Above assertions pass
QueryCriteria.VaultQueryCriteria criteria = new QueryCriteria.VaultQueryCriteria(Vault.StateStatus.ALL);
Vault.Page<ContractState> results = a.getServices().getVaultService().queryBy(Cash.State.class, criteria);
assertTrue(results.getStates().size() > 0);
// ^ This assertion fails
}
In Corda 3, whenever you query a node’s database as part of a test (e.g. to extract information from the node’s vault), you must wrap the query in a database transaction, as follows:
node.transaction(tx -> {
// Perform query here.
}
So your test would become:
#Test
public void testIssueCash() throws Exception {
IssueTokensFlow2 flow = new IssueTokensFlow2(100.00);
SignedTransaction transaction = a.startFlow(flow).get();
network.waitQuiescent();
Cash.State state = (Cash.State) transaction.getTx().getOutputStates().get(0);
assertEquals(state.getOwner(), chooseIdentity(a.getInfo()));
assertEquals(state.getAmount().getQuantity(), Currencies.DOLLARS(100.00).getQuantity());
// Above assertions pass
QueryCriteria.VaultQueryCriteria criteria = new QueryCriteria.VaultQueryCriteria(Vault.StateStatus.ALL);
a.transaction(() -> {
Vault.Page<ContractState> results = a.getServices().getVaultService().queryBy(Cash.State.class, criteria);
assertTrue(results.getStates().size() > 0);
// ^ This assertion doesn't fail :)
return null;
});
}

Reading into collection and retrieving top 5

I was asked this recently in an interview. Application that is reading ticker and trade volume from a live feed,
E.g. AAPL 1000, TWTR 500, MSFT 500, AAPL 500 ...
So, AAPL total volume = 1500 and so on.
I have to read these into a collection and return the top 5 by volume.
I had suggested to use a hash map when storing and then sort or use a Treemap.
Any other way that is more efficient?
Assuming ticker symbol and trade volume are stored together in an instance of some class TickerAndTradeVolume, you could have references to objects contained in multiple data structures.
So a hash map may have ticker symbol as key and TickerAndTradeVolume as value. Then references to TickerAndTradeVolume instances can also be stored in a priority queue. Instances are re-inserted into the PQ on every volume update.
The top n by volume are always available in log(n) amortized time complexity to maintain priorities by trade volume, which is asymptotically faster than sorting via Treemap time and again.
Something like this
Map<String, TickerAndTradeVolume> feed;
PriorityQueue<TickerAndTradeVolume> pq;
class TickerAndTradeVolume implements Comparable<TickerAndTradeVolume> {
private String ticker;
private double volume;
TickerAndTradeVolume(String ticker, double volume) {
this.ticker = ticker;
this.volume = volume;
}
void increaseVolumeBy(double volume) {
this.volume += volume;
}
#Override
public int compareTo(TickerAndTradeVolume that) {
return (int) (that.volume - this.volume);
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if(obj instanceof String) {
TickerAndTradeVolume that = (TickerAndTradeVolume) obj;
return this.ticker.equals(that.ticker);
}
return false;
}
}
void addOrUpdateStockVolume(TickerAndTradeVolume tt) {
if(!feed.containsKey(tt.ticker)) {
feed.put(tt.ticker, tt);
pq.add(tt);
}
else {
feed.get(tt.ticker).increaseVolumeBy(tt.volume);
// take out and put back in to trigger heap operations
pq.remove(feed.get(tt.ticker));
pq.add(feed.get(tt.ticker));
}
}
List<TickerAndTradeVolume> getTopMaxFive() {
List<TickerAndTradeVolume> topFive = new ArrayList<TickerAndTradeVolume>(5);
int pqSize = pq.size();
for(int i = 0; i < 5 && i < pqSize; i++) {
// poll() takes from the top of the heap
topFive.add(pq.poll());
}
for(TickerAndTradeVolume tt : topFive) {
// put back what we took out
pq.add(tt);
}
return topFive;
}

Binary Tree and Return root node

..I'm building a binary tree where the root is given and the children are either root-3, root-2 or root-1 (that is, they hold those number of pennies). So 5 would have nodes of 2,3,4, and so on, until the leaves are 0. Here's my method for making such a tree. I don't understand why the method doesn't return the original node, in this case, the value should be 3.
Any guidance would be awesome.
public GameNode buildTree1(GameNode root){
int penn = root.getPennies();
if (penn < 0)
{
return null;
}
else {
root.print();
root.setLeft(buildTree1(new GameNode(penn-1)));
root.setMiddle(buildTree1(new GameNode(penn-2)));
root.setRight(buildTree1(new GameNode(penn-3)));
return root;
}
Get/Set Methods
public void setLeft(GameNode newNode) {
// TODO Auto-generated method stub
left = newNode;
}
Same for setMiddle and setRight;

Resources