Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cf67ed202 | |||
| 0c09cc0db0 | |||
| 75d8f343ea |
12
README.md
12
README.md
@ -8,7 +8,15 @@ An Package to interact with an mock stock exchange
|
||||
| ----- | ------ |
|
||||
| Key | 8 Bytes |
|
||||
| Operation | Byte |
|
||||
| Asset | Byte |
|
||||
| Asset | 2 Bytes |
|
||||
| Price | Float64 |
|
||||
| Volume | Float64 |
|
||||
| CRC | 2 Bytes |
|
||||
| Expire (Unix) | uint64 |
|
||||
| CRC | 2 Bytes |
|
||||
|
||||
## TODOS
|
||||
- [ ] Test it
|
||||
- [ ] Make it configureable
|
||||
- [x] Implement expire date
|
||||
- [ ] encrypt the data and key -> uid
|
||||
- [ ] Document it
|
||||
4
go.mod
4
go.mod
@ -1,5 +1,5 @@
|
||||
module git.barfuss.email/jan/SimpleFinancePackage
|
||||
module git.neunzweinull.com/jan/SimpleFinancePackage
|
||||
|
||||
go 1.22.1
|
||||
|
||||
require git.barfuss.email/jan/crc16 v0.1.0 // indirect
|
||||
require git.neunzweinull.com/jan/crc16 v0.1.3
|
||||
|
||||
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
||||
git.barfuss.email/jan/crc16 v0.1.0 h1:9u/m/JO1/njaR6w2WsGTVezW/niYlT1tBRK2mRDR92w=
|
||||
git.barfuss.email/jan/crc16 v0.1.0/go.mod h1:ibTarWCN7/nak76mWoU/pNhF+O0VzKX+8YpDftjc8LU=
|
||||
git.neunzweinull.com/jan/crc16 v0.1.3 h1:a+vF2DhR42cOj96h8gHP9sHOlkppD5iew9aWI2mBvW8=
|
||||
git.neunzweinull.com/jan/crc16 v0.1.3/go.mod h1:MOa6nMyDKWPreIQWb1eWRFJZfRBgbAWpaPmiPdqqiP4=
|
||||
|
||||
21
spf.go
21
spf.go
@ -6,26 +6,27 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
crc "git.barfuss.email/jan/crc16"
|
||||
crc "git.neunzweinull.com/jan/crc16"
|
||||
)
|
||||
|
||||
type SimpleFinancePackage struct {
|
||||
Key [8]byte // 8 bytes for the key
|
||||
Operation byte // 1 byte for operation
|
||||
Asset byte // 1 byte for asset
|
||||
Asset uint16 // 2 bytes for asset
|
||||
Price float64 // 8 bytes for price (float64 for precision)
|
||||
Volume float64 // 8 bytes for volume
|
||||
Expire uint64 // 8 bytes for expiration date
|
||||
CRC uint16 // 2 bytes for CRC
|
||||
}
|
||||
|
||||
func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
|
||||
if len(data) != 28 {
|
||||
return nil, errors.New("invalid packet size, expected 28 bytes")
|
||||
if len(data) != 37 {
|
||||
return nil, errors.New("invalid packet size, expected 37 bytes")
|
||||
}
|
||||
|
||||
//Check CRC
|
||||
check := binary.BigEndian.Uint16(data[26:])
|
||||
if check != crc.Calculate(data[:26]) {
|
||||
check := binary.BigEndian.Uint16(data[35:])
|
||||
if check != crc.Calculate(data[:35]) {
|
||||
return nil, errors.New("CRC check failed")
|
||||
}
|
||||
|
||||
@ -41,9 +42,12 @@ func ParseSimpleFinanacePackage(data []byte) (*SimpleFinancePackage, error) {
|
||||
if packet.Price < 0 || packet.Volume < 0 {
|
||||
return nil, errors.New("price and volume must be non-negative")
|
||||
}
|
||||
if packet.Operation > 127 { // Example: Valid operation values are 0-127
|
||||
if packet.Operation > 127 { // Valid operation values are 0-127
|
||||
return nil, errors.New("invalid operation value")
|
||||
}
|
||||
if packet.Expire < 946684800 { // Expire date must be after 2000-01-01
|
||||
return nil, errors.New("invalid expiration date")
|
||||
}
|
||||
|
||||
return &packet, nil
|
||||
}
|
||||
@ -67,6 +71,9 @@ func EncodeSimpleFinanacePackage(packet SimpleFinancePackage) ([]byte, error) {
|
||||
if err := binary.Write(buf, binary.BigEndian, packet.Volume); err != nil {
|
||||
return nil, fmt.Errorf("failed to encode volume: %w", err)
|
||||
}
|
||||
if err := binary.Write(buf, binary.BigEndian, packet.Expire); err != nil {
|
||||
return nil, fmt.Errorf("failed to encode expiration date: %w", err)
|
||||
}
|
||||
|
||||
// Calculate and write CRC
|
||||
packet.CRC = crc.Calculate(buf.Bytes())
|
||||
|
||||
@ -15,6 +15,7 @@ func TestEncodeDecodeCustomPacket(t *testing.T) {
|
||||
Asset: 2,
|
||||
Price: 1234.56,
|
||||
Volume: 7890.12,
|
||||
Expire: 946684801,
|
||||
}
|
||||
|
||||
// Encode the packet
|
||||
@ -24,8 +25,8 @@ func TestEncodeDecodeCustomPacket(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check the encoded length
|
||||
if len(encoded) != 28 {
|
||||
t.Fatalf("Encoded data has incorrect length: got %d, want 28", len(encoded))
|
||||
if len(encoded) != 37 {
|
||||
t.Fatalf("Encoded data has incorrect length: got %d, want 37", len(encoded))
|
||||
}
|
||||
|
||||
// Decode the packet
|
||||
@ -76,6 +77,7 @@ func TestInvalidPacketValues(t *testing.T) {
|
||||
Asset: 2,
|
||||
Price: 1234.56,
|
||||
Volume: 7890.12,
|
||||
Expire: 946684801,
|
||||
}
|
||||
encoded, _ := EncodeSimpleFinanacePackage(validPacket)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user