Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Merge branch 'neo-tx-unspent' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Dec 10, 2017
2 parents 473b105 + 6ad7fe1 commit b71806f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
4 changes: 4 additions & 0 deletions neo/Core/Blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ def GetUnspent(self, hash, index):
# abstract
pass

def GetAllUnspent(self, hash):
# abstract
pass

def GetVotes(self, transactions):
# abstract
pass
Expand Down
12 changes: 7 additions & 5 deletions neo/Core/State/UnspentCoinState.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ def FromTXOutputsConfirmed(outputs):
uns = UnspentCoinState()
uns.Items = [0] * len(outputs)
for i in range(0, len(outputs)):
uns.Items[i] = CoinState.Confirmed
uns.Items[i] = int(CoinState.Confirmed)
return uns

def Size(self):
return super(UnspentCoinState, self).Size() + sys.getsizeof(self.Items)

@property
def IsAllSpent(self):
for item in self.Items:
if item & CoinState.Spent > 0:
if item == CoinState.Confirmed:
return False
return True

def OrEqValueForItemAt(self, index, value):

length = len(self.Items)

while length < index + 1:
self.Items.append(0)
length = len(self.Items)
Expand All @@ -50,7 +50,7 @@ def Deserialize(self, reader):
blen = reader.ReadVarInt()
self.Items = [0] * blen
for i in range(0, blen):
self.Items[i] = reader.ReadByte()
self.Items[i] = int.from_bytes(reader.ReadByte(do_ord=False), 'little')

@staticmethod
def DeserializeFromDB(buffer):
Expand All @@ -68,4 +68,6 @@ def Serialize(self, writer):

writer.WriteVarInt(len(self.Items))

[writer.WriteByte(item) for item in self.Items]
for item in self.Items:
byt = item.to_bytes(1, 'little')
writer.WriteByte(byt)
25 changes: 24 additions & 1 deletion neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,28 @@ def GetSpentCoins(self, tx_hash):

return result

def GetUnspent(self, hash, index):
# abstract
pass

def GetAllUnspent(self, hash):

unspents = []

sn = self._db.snapshot()
unspentcoins = DBCollection(self._db, sn, DBPrefix.ST_Coin, UnspentCoinState)

state = unspentcoins.TryGet(keyval=hash.ToBytes())

if state:
tx, height = self.GetTransaction(hash)

for index, item in enumerate(state.Items):
if item & CoinState.Spent == 0:
unspents.append(tx.outputs[index])

return unspents

def GetUnclaimed(self, hash):

tx, height = self.GetTransaction(hash)
Expand Down Expand Up @@ -675,7 +697,8 @@ def Persist(self, block):

# filte out unspent coins to delete then commit
for key, unspent in unspentcoins.Current.items():
unspentcoins.Remove(key)
if unspent.IsAllSpent:
unspentcoins.Remove(key)
unspentcoins.Commit(wb)

# filter out spent coins to delete then commit to db
Expand Down
5 changes: 4 additions & 1 deletion neo/SmartContract/ApplicationEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ def GetPriceForSysCall(self):
elif api == "Neo.Blockchain.GetContract":
return 100

elif api == "Neo.Blockchain.GetReferences":
elif api == "Neo.Transaction.GetReferences":
return 200

elif api == "Neo.Transaction.GetUnspentCoins":
return 200

elif api == "Neo.Account.SetVotes":
Expand Down
14 changes: 14 additions & 0 deletions neo/SmartContract/StateReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from neo.SmartContract.TriggerType import Application, Verification

from neo.VM.InteropService import StackItem, stack_item_to_py
import json


class StateReader(InteropService):
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self):
self.Register("Neo.Transaction.GetInputs", self.Transaction_GetInputs)
self.Register("Neo.Transaction.GetOutputs", self.Transaction_GetOutputs)
self.Register("Neo.Transaction.GetReferences", self.Transaction_GetReferences)
self.Register("Neo.Transaction.GetUnspentCoins", self.Transaction_GetUnspentCoins)

self.Register("Neo.Attribute.GetData", self.Attribute_GetData)
self.Register("Neo.Attribute.GetUsage", self.Attribute_GetUsage)
Expand Down Expand Up @@ -568,6 +570,18 @@ def Transaction_GetReferences(self, engine):
engine.EvaluationStack.PushT(refs)
return True

def Transaction_GetUnspentCoins(self, engine):

tx = engine.EvaluationStack.Pop().GetInterface()

if tx is None:
return False

refs = [StackItem.FromInterface(unspent) for unspent in Blockchain.Default().GetAllUnspent(tx.Hash)]

engine.EvaluationStack.PushT(refs)
return True

def Attribute_GetUsage(self, engine):

attr = engine.EvaluationStack.Pop().GetInterface()
Expand Down

0 comments on commit b71806f

Please sign in to comment.