Skip to content

Commit

Permalink
Adding Neo.Transaction.GetUnspentCoins for issue #114 (#123)
Browse files Browse the repository at this point in the history
* Adding Neo.Transaction.GetUnspentCoins for issue #114

* Add TestBlockchain.GetUnspent(UInt256 hash)
  • Loading branch information
localhuman authored and Erik Zhang committed Dec 11, 2017
1 parent b3b195e commit 46b4ce0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions neo.UnitTests/TestBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public override TransactionOutput GetUnspent(UInt256 hash, ushort index)
throw new NotImplementedException();
}

public override IEnumerable<TransactionOutput> GetUnspent(UInt256 hash)
{
throw new NotImplementedException();
}

public override IEnumerable<VoteState> GetVotes(IEnumerable<Transaction> others)
{
VoteState vs = new VoteState() { Count = Fixed8.FromDecimal(1), PublicKeys = TestUtils.StandbyValidators};
Expand Down
3 changes: 3 additions & 0 deletions neo/Core/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ public Transaction GetTransaction(UInt256 hash)
/// <returns>返回一个交易输出,表示一个未花费的资产</returns>
public abstract TransactionOutput GetUnspent(UInt256 hash, ushort index);

public abstract IEnumerable<TransactionOutput> GetUnspent(UInt256 hash);


/// <summary>
/// 获取选票信息
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,30 @@ public override TransactionOutput GetUnspent(UInt256 hash, ushort index)
}
}

public override IEnumerable<TransactionOutput> GetUnspent(UInt256 hash)
{
ReadOptions options = new ReadOptions();
using (options.Snapshot = db.GetSnapshot())
{
List<TransactionOutput> outputs = new List<TransactionOutput>();
UnspentCoinState state = db.TryGet<UnspentCoinState>(options, DataEntryPrefix.ST_Coin, hash);
if (state != null)
{
int height;
Transaction tx = GetTransaction(options, hash, out height);
for (int i = 0; i < state.Items.Length; i++)
{
if (!state.Items[i].HasFlag(CoinState.Spent))
{
outputs.Add(tx.Outputs[i]);
}

}
}
return outputs;
}
}

public override IEnumerable<VoteState> GetVotes(IEnumerable<Transaction> others)
{
ReadOptions options = new ReadOptions();
Expand Down
1 change: 1 addition & 0 deletions neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ protected virtual long GetPriceForSysCall()
return 100;
case "Neo.Transaction.GetReferences":
case "AntShares.Transaction.GetReferences":
case "Neo.Transaction.GetUnspentCoins":
return 200;
case "Neo.Account.SetVotes":
case "AntShares.Account.SetVotes":
Expand Down
9 changes: 9 additions & 0 deletions neo/SmartContract/StateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public StateReader()
Register("Neo.Transaction.GetInputs", Transaction_GetInputs);
Register("Neo.Transaction.GetOutputs", Transaction_GetOutputs);
Register("Neo.Transaction.GetReferences", Transaction_GetReferences);
Register("Neo.Transaction.GetUnspentCoins", Transaction_GetUnspentCoins);
Register("Neo.Attribute.GetUsage", Attribute_GetUsage);
Register("Neo.Attribute.GetData", Attribute_GetData);
Register("Neo.Input.GetHash", Input_GetHash);
Expand Down Expand Up @@ -418,6 +419,14 @@ protected virtual bool Transaction_GetReferences(ExecutionEngine engine)
return true;
}

protected virtual bool Transaction_GetUnspentCoins(ExecutionEngine engine)
{
Transaction tx = engine.EvaluationStack.Pop().GetInterface<Transaction>();
if (tx == null) return false;
engine.EvaluationStack.Push(Blockchain.Default.GetUnspent(tx.Hash).Select(p => StackItem.FromInterface(p)).ToArray());
return true;
}

protected virtual bool Attribute_GetUsage(ExecutionEngine engine)
{
TransactionAttribute attr = engine.EvaluationStack.Pop().GetInterface<TransactionAttribute>();
Expand Down

0 comments on commit 46b4ce0

Please sign in to comment.