You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
957 B

  1. package core
  2. type Input struct {
  3. TxId Hash
  4. Vout int // index of the output from the TxId
  5. Value uint64
  6. }
  7. type Output struct {
  8. Value uint64
  9. }
  10. // Tx holds the data structure of a transaction
  11. type Tx struct {
  12. From Address
  13. To Address
  14. InputCount uint64
  15. Inputs []Input
  16. Outputs []Output
  17. Signature []byte
  18. }
  19. func NewTx(from, to Address, in []Input, out []Output) *Tx {
  20. tx := &Tx{
  21. From: from,
  22. To: to,
  23. InputCount: uint64(len(in)),
  24. Inputs: in,
  25. Outputs: out,
  26. }
  27. return tx
  28. }
  29. // CheckTx checks if the transaction is consistent
  30. func CheckTx(tx *Tx) bool {
  31. // TODO check that inputs and outputs are not empty
  32. // check that inputs == outputs
  33. totalIn := 0
  34. for _, in := range tx.Inputs {
  35. totalIn = totalIn + int(in.Value)
  36. }
  37. totalOut := 0
  38. for _, out := range tx.Outputs {
  39. totalOut = totalOut + int(out.Value)
  40. }
  41. if totalIn < totalOut {
  42. return false
  43. }
  44. // TODO check signature
  45. return true
  46. }