2020-03-09 13:56:37 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-03-09 15:56:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-03-09 13:56:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnspentCoin hold the state of a unspent coin.
|
|
|
|
type UnspentCoin struct {
|
2020-03-09 15:56:24 +00:00
|
|
|
Height uint32
|
|
|
|
States []OutputState
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputState combines transaction output (UTXO) and its state
|
|
|
|
// (spent/claimed...) along with the height of spend (if it's spent).
|
|
|
|
type OutputState struct {
|
|
|
|
transaction.Output
|
|
|
|
|
|
|
|
SpendHeight uint32
|
|
|
|
State Coin
|
2020-03-09 13:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUnspentCoin returns a new unspent coin state with N confirmed states.
|
2020-03-09 15:56:24 +00:00
|
|
|
func NewUnspentCoin(height uint32, tx *transaction.Transaction) *UnspentCoin {
|
2020-03-09 13:56:37 +00:00
|
|
|
u := &UnspentCoin{
|
2020-03-09 15:56:24 +00:00
|
|
|
Height: height,
|
|
|
|
States: make([]OutputState, len(tx.Outputs)),
|
2020-03-09 13:56:37 +00:00
|
|
|
}
|
2020-03-09 15:56:24 +00:00
|
|
|
for i := range tx.Outputs {
|
|
|
|
u.States[i] = OutputState{Output: tx.Outputs[i]}
|
2020-03-09 13:56:37 +00:00
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary encodes UnspentCoin to the given BinWriter.
|
|
|
|
func (s *UnspentCoin) EncodeBinary(bw *io.BinWriter) {
|
2020-03-09 15:56:24 +00:00
|
|
|
bw.WriteU32LE(s.Height)
|
|
|
|
bw.WriteArray(s.States)
|
2020-03-09 13:56:37 +00:00
|
|
|
bw.WriteVarUint(uint64(len(s.States)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary decodes UnspentCoin from the given BinReader.
|
|
|
|
func (s *UnspentCoin) DecodeBinary(br *io.BinReader) {
|
2020-03-09 15:56:24 +00:00
|
|
|
s.Height = br.ReadU32LE()
|
|
|
|
br.ReadArray(&s.States)
|
2020-12-31 10:54:42 +00:00
|
|
|
if br.Err == nil {
|
|
|
|
for i := range s.States {
|
|
|
|
s.States[i].Output.Position = i
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 15:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (o *OutputState) EncodeBinary(w *io.BinWriter) {
|
|
|
|
o.Output.EncodeBinary(w)
|
|
|
|
w.WriteU32LE(o.SpendHeight)
|
|
|
|
w.WriteB(byte(o.State))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (o *OutputState) DecodeBinary(r *io.BinReader) {
|
|
|
|
o.Output.DecodeBinary(r)
|
|
|
|
o.SpendHeight = r.ReadU32LE()
|
|
|
|
o.State = Coin(r.ReadB())
|
2020-03-09 13:56:37 +00:00
|
|
|
}
|