2018-04-16 20:15:30 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-04-16 20:15:30 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SpentCoins is mapping between transactions and their spent
|
|
|
|
// coin state.
|
|
|
|
type SpentCoins map[util.Uint256]*SpentCoinState
|
|
|
|
|
|
|
|
func (s SpentCoins) getAndUpdate(store storage.Store, hash util.Uint256) (*SpentCoinState, error) {
|
|
|
|
if spent, ok := s[hash]; ok {
|
|
|
|
return spent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
spent := &SpentCoinState{}
|
|
|
|
key := storage.AppendPrefix(storage.STSpentCoin, hash.BytesReverse())
|
|
|
|
if b, err := store.Get(key); err == nil {
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := spent.DecodeBinary(io.NewBinReaderFromBuf(b)); err != nil {
|
2018-04-16 20:15:30 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode (UnspentCoinState): %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
spent = &SpentCoinState{
|
|
|
|
items: make(map[uint16]uint32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s[hash] = spent
|
|
|
|
return spent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SpentCoins) commit(b storage.Batch) error {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2018-04-16 20:15:30 +00:00
|
|
|
for hash, state := range s {
|
2019-09-16 09:18:13 +00:00
|
|
|
if err := state.EncodeBinary(buf.BinWriter); err != nil {
|
2018-04-16 20:15:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STSpentCoin, hash.BytesReverse())
|
|
|
|
b.Put(key, buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpentCoinState represents the state of a spent coin.
|
|
|
|
type SpentCoinState struct {
|
|
|
|
txHash util.Uint256
|
|
|
|
txHeight uint32
|
|
|
|
|
|
|
|
// A mapping between the index of the prevIndex and block height.
|
|
|
|
items map[uint16]uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSpentCoinState returns a new SpentCoinState object.
|
|
|
|
func NewSpentCoinState(hash util.Uint256, height uint32) *SpentCoinState {
|
|
|
|
return &SpentCoinState{
|
|
|
|
txHash: hash,
|
|
|
|
txHeight: height,
|
|
|
|
items: make(map[uint16]uint32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Payload interface.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (s *SpentCoinState) DecodeBinary(br *io.BinReader) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&s.txHash)
|
|
|
|
br.ReadLE(&s.txHeight)
|
2018-04-16 20:15:30 +00:00
|
|
|
|
|
|
|
s.items = make(map[uint16]uint32)
|
2019-08-28 16:27:06 +00:00
|
|
|
lenItems := br.ReadVarUint()
|
2018-04-16 20:15:30 +00:00
|
|
|
for i := 0; i < int(lenItems); i++ {
|
|
|
|
var (
|
|
|
|
key uint16
|
|
|
|
value uint32
|
|
|
|
)
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&key)
|
|
|
|
br.ReadLE(&value)
|
2018-04-16 20:15:30 +00:00
|
|
|
s.items[key] = value
|
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
return br.Err
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Payload interface.
|
2019-09-16 09:18:13 +00:00
|
|
|
func (s *SpentCoinState) EncodeBinary(bw *io.BinWriter) error {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(s.txHash)
|
|
|
|
bw.WriteLE(s.txHeight)
|
|
|
|
bw.WriteVarUint(uint64(len(s.items)))
|
2018-04-16 20:15:30 +00:00
|
|
|
for k, v := range s.items {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(k)
|
|
|
|
bw.WriteLE(v)
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
return bw.Err
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|