mirror of
https://github.com/arnaucube/slowlorisdb.git
synced 2026-02-28 05:46:48 +01:00
tx inputs and outputs structure, add CheckTx
This commit is contained in:
@@ -6,31 +6,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
}
|
||||
type Output struct {
|
||||
}
|
||||
|
||||
// Tx holds the data structure of a transaction
|
||||
type Tx struct {
|
||||
From Address
|
||||
To Address
|
||||
InputCount uint64
|
||||
Inputs []Input
|
||||
Outputs []Output
|
||||
}
|
||||
|
||||
func NewTx(from, to Address, in []Input, out []Output) *Tx {
|
||||
tx := &Tx{
|
||||
From: from,
|
||||
To: to,
|
||||
InputCount: uint64(len(in)),
|
||||
Inputs: in,
|
||||
Outputs: out,
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
||||
// Block holds the data structure for the block
|
||||
type Block struct {
|
||||
Height uint64
|
||||
|
||||
@@ -59,13 +59,3 @@ func TestNewBlock(t *testing.T) {
|
||||
// CheckPoW
|
||||
assert.True(t, CheckPoW(h, difficulty))
|
||||
}
|
||||
|
||||
func TestTx(t *testing.T) {
|
||||
addr0 := Address(HashBytes([]byte("addr0")))
|
||||
addr1 := Address(HashBytes([]byte("addr1")))
|
||||
|
||||
tx := NewTx(addr0, addr1, []Input{}, []Output{})
|
||||
|
||||
assert.Equal(t, tx.From, addr0)
|
||||
assert.Equal(t, tx.To, addr1)
|
||||
}
|
||||
|
||||
50
core/tx.go
Normal file
50
core/tx.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package core
|
||||
|
||||
type Input struct {
|
||||
TxId Hash
|
||||
Vout int // index of the output from the TxId
|
||||
Value uint64
|
||||
}
|
||||
type Output struct {
|
||||
Value uint64
|
||||
}
|
||||
|
||||
// Tx holds the data structure of a transaction
|
||||
type Tx struct {
|
||||
From Address
|
||||
To Address
|
||||
InputCount uint64
|
||||
Inputs []Input
|
||||
Outputs []Output
|
||||
Signature []byte
|
||||
}
|
||||
|
||||
func NewTx(from, to Address, in []Input, out []Output) *Tx {
|
||||
tx := &Tx{
|
||||
From: from,
|
||||
To: to,
|
||||
InputCount: uint64(len(in)),
|
||||
Inputs: in,
|
||||
Outputs: out,
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
||||
// CheckTx checks if the transaction is consistent
|
||||
func CheckTx(tx *Tx) bool {
|
||||
// TODO check that inputs and outputs are not empty
|
||||
|
||||
// check that inputs == outputs
|
||||
totalIn := 0
|
||||
for _, in := range tx.Inputs {
|
||||
totalIn = totalIn + int(in.Value)
|
||||
}
|
||||
totalOut := 0
|
||||
for _, out := range tx.Outputs {
|
||||
totalOut = totalOut + int(out.Value)
|
||||
}
|
||||
if totalIn < totalOut {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
19
core/tx_test.go
Normal file
19
core/tx_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTx(t *testing.T) {
|
||||
addr0 := Address(HashBytes([]byte("addr0")))
|
||||
addr1 := Address(HashBytes([]byte("addr1")))
|
||||
|
||||
tx := NewTx(addr0, addr1, []Input{}, []Output{})
|
||||
|
||||
assert.Equal(t, tx.From, addr0)
|
||||
assert.Equal(t, tx.To, addr1)
|
||||
|
||||
assert.True(t, CheckTx(tx))
|
||||
}
|
||||
Reference in New Issue
Block a user