2018-03-21 16:11:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2018-03-25 10:45:54 +00:00
|
|
|
"fmt"
|
2018-03-21 16:11:04 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnspentCoins is mapping between transactions and their unspent
|
|
|
|
// coin state.
|
|
|
|
type UnspentCoins map[util.Uint256]*UnspentCoinState
|
|
|
|
|
2018-04-16 20:15:30 +00:00
|
|
|
func (u UnspentCoins) getAndUpdate(s storage.Store, hash util.Uint256) (*UnspentCoinState, error) {
|
2018-03-21 16:11:04 +00:00
|
|
|
if unspent, ok := u[hash]; ok {
|
|
|
|
return unspent, nil
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
unspent := &UnspentCoinState{}
|
2018-03-21 16:11:04 +00:00
|
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesReverse())
|
|
|
|
if b, err := s.Get(key); err == nil {
|
|
|
|
if err := unspent.DecodeBinary(bytes.NewReader(b)); err != nil {
|
2018-03-25 10:45:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode (UnspentCoinState): %s", err)
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unspent = &UnspentCoinState{
|
|
|
|
states: []CoinState{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u[hash] = unspent
|
|
|
|
return unspent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnspentCoinState hold the state of a unspent coin.
|
|
|
|
type UnspentCoinState struct {
|
|
|
|
states []CoinState
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:15:30 +00:00
|
|
|
// NewUnspentCoinState returns a new unspent coin state with N confirmed states.
|
|
|
|
func NewUnspentCoinState(n int) *UnspentCoinState {
|
|
|
|
u := &UnspentCoinState{
|
|
|
|
states: make([]CoinState, n),
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
u.states[i] = CoinStateConfirmed
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:11:04 +00:00
|
|
|
// commit writes all unspent coin states to the given Batch.
|
2019-01-22 12:14:52 +00:00
|
|
|
func (u UnspentCoins) commit(b storage.Batch) error {
|
2018-03-21 16:11:04 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2019-01-22 12:14:52 +00:00
|
|
|
for hash, state := range u {
|
2018-03-21 16:11:04 +00:00
|
|
|
if err := state.EncodeBinary(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesReverse())
|
|
|
|
b.Put(key, buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary encodes UnspentCoinState to the given io.Writer.
|
|
|
|
func (s *UnspentCoinState) EncodeBinary(w io.Writer) error {
|
|
|
|
if err := util.WriteVarUint(w, uint64(len(s.states))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, state := range s.states {
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, byte(state)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:14:52 +00:00
|
|
|
// DecodeBinary decodes UnspentCoinState from the given io.Reader.
|
2018-03-21 16:11:04 +00:00
|
|
|
func (s *UnspentCoinState) DecodeBinary(r io.Reader) error {
|
|
|
|
lenStates := util.ReadVarUint(r)
|
|
|
|
s.states = make([]CoinState, lenStates)
|
|
|
|
for i := 0; i < int(lenStates); i++ {
|
|
|
|
var state uint8
|
|
|
|
if err := binary.Read(r, binary.LittleEndian, &state); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.states[i] = CoinState(state)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|