2018-03-21 16:11:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-03-25 10:45:54 +00:00
|
|
|
"fmt"
|
2018-03-21 16:11:04 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-21 16:11:04 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnspentCoins is mapping between transactions and their unspent
|
|
|
|
// coin state.
|
|
|
|
type UnspentCoins map[util.Uint256]*UnspentCoinState
|
|
|
|
|
2019-09-26 15:14:00 +00:00
|
|
|
// getAndUpdate retreives UnspentCoinState from temporary or persistent Store
|
|
|
|
// and return it. If it's not present in both stores, returns a new
|
|
|
|
// UnspentCoinState.
|
2019-10-16 13:41:50 +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
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:41:50 +00:00
|
|
|
unspent, err := getUnspentCoinStateFromStore(s, hash)
|
2019-09-26 15:14:00 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrKeyNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-16 13:41:50 +00:00
|
|
|
unspent = &UnspentCoinState{
|
|
|
|
states: []CoinState{},
|
2019-09-26 15:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u[hash] = unspent
|
|
|
|
return unspent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getUnspentCoinStateFromStore retrieves UnspentCoinState from the given store
|
|
|
|
func getUnspentCoinStateFromStore(s storage.Store, hash util.Uint256) (*UnspentCoinState, error) {
|
2018-03-25 10:45:54 +00:00
|
|
|
unspent := &UnspentCoinState{}
|
2019-11-27 09:23:18 +00:00
|
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesLE())
|
2018-03-21 16:11:04 +00:00
|
|
|
if b, err := s.Get(key); err == nil {
|
2019-09-16 16:31:49 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
unspent.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode (UnspentCoinState): %s", r.Err)
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-26 15:14:00 +00:00
|
|
|
return nil, err
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
return unspent, nil
|
|
|
|
}
|
|
|
|
|
2019-10-16 14:29:21 +00:00
|
|
|
// putUnspentCoinStateIntoStore puts given UnspentCoinState into the given store.
|
|
|
|
func putUnspentCoinStateIntoStore(store storage.Store, hash util.Uint256, ucs *UnspentCoinState) error {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
ucs.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
key := storage.AppendPrefix(storage.STCoin, hash.BytesLE())
|
2019-10-16 14:29:21 +00:00
|
|
|
return store.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:11:04 +00:00
|
|
|
// 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-10-16 14:29:21 +00:00
|
|
|
func (u UnspentCoins) commit(store storage.Store) error {
|
2019-01-22 12:14:52 +00:00
|
|
|
for hash, state := range u {
|
2019-10-16 14:29:21 +00:00
|
|
|
if err := putUnspentCoinStateIntoStore(store, hash, state); err != nil {
|
|
|
|
return err
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
// EncodeBinary encodes UnspentCoinState to the given BinWriter.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (s *UnspentCoinState) EncodeBinary(bw *io.BinWriter) {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteVarUint(uint64(len(s.states)))
|
2018-03-21 16:11:04 +00:00
|
|
|
for _, state := range s.states {
|
2019-12-06 15:22:21 +00:00
|
|
|
bw.WriteBytes([]byte{byte(state)})
|
2018-03-21 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
// DecodeBinary decodes UnspentCoinState from the given BinReader.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (s *UnspentCoinState) DecodeBinary(br *io.BinReader) {
|
2019-08-28 16:27:06 +00:00
|
|
|
lenStates := br.ReadVarUint()
|
2018-03-21 16:11:04 +00:00
|
|
|
s.states = make([]CoinState, lenStates)
|
|
|
|
for i := 0; i < int(lenStates); i++ {
|
|
|
|
var state uint8
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&state)
|
2018-03-21 16:11:04 +00:00
|
|
|
s.states[i] = CoinState(state)
|
|
|
|
}
|
|
|
|
}
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
|
|
|
|
// IsDoubleSpend verifies that the input transactions are not double spent.
|
|
|
|
func IsDoubleSpend(s storage.Store, tx *transaction.Transaction) bool {
|
|
|
|
if len(tx.Inputs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for prevHash, inputs := range tx.GroupInputsByPrevHash() {
|
|
|
|
unspent := &UnspentCoinState{}
|
2019-11-27 09:23:18 +00:00
|
|
|
key := storage.AppendPrefix(storage.STCoin, prevHash.BytesLE())
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
if b, err := s.Get(key); err == nil {
|
2019-09-16 16:31:49 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
unspent.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, input := range inputs {
|
|
|
|
if int(input.PrevIndex) >= len(unspent.states) || unspent.states[input.PrevIndex] == CoinStateSpent {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 09:40:11 +00:00
|
|
|
} else {
|
|
|
|
return true
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|