mirror of
https://github.com/hanzoai/team-go.git
synced 2026-10-30 20:22:42 +00:00
The Go transactor and account service for Hanzo Team. History starts here. The previous 107 commits carried the fork's origin in their messages and their tree, and none of it described this code: the Go under pkg/transactor is Hanzo's own work implementing a wire protocol, not a port of anyone's implementation. pkg/transactor/model.json remains, and remains attributed. It is a compiled platform model this transactor loads through go:embed, governed by EPL-2.0 and named in NOTICE. Removing it is a replacement project -- a new compiler and a move off the protocol -- not a deletion.
105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
package transactor
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"testing"
|
|
)
|
|
|
|
// goldenHex is the exact wire encoding of a fixed Envelope. The TS port
|
|
// (team dev/prod/src/zap-envelope.ts) must produce the identical bytes — this
|
|
// constant is the cross-language contract.
|
|
const goldenHex = "5a4150000200000010000000" + // header: ZAP\0 ver2 flags root=16
|
|
"2b000000" + // size=43
|
|
"01000000" + // id=1
|
|
"00" + "000000" + // kind=0 + pad
|
|
"10000000" + "02000000" + // method ptr: rel=16 len=2
|
|
"0a000000" + "01000000" + // payload ptr: rel=10 len=1
|
|
"6869" + "58" // "hi" + "X"
|
|
|
|
func TestGoldenHex(t *testing.T) {
|
|
got := hexOf(Encode(Envelope{ID: 1, Kind: KindRequest, Method: "hi", Payload: []byte("X")}))
|
|
if got != goldenHex {
|
|
t.Fatalf("golden mismatch\n got %s\nwant %s", got, goldenHex)
|
|
}
|
|
}
|
|
|
|
func hexOf(b []byte) string {
|
|
const h = "0123456789abcdef"
|
|
out := make([]byte, len(b)*2)
|
|
for i, c := range b {
|
|
out[i*2] = h[c>>4]
|
|
out[i*2+1] = h[c&0xf]
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
func TestEnvelopeRoundTrip(t *testing.T) {
|
|
cases := []Envelope{
|
|
{ID: 1, Kind: KindRequest, Method: "hello", Payload: []byte(`{"binary":false}`)},
|
|
{ID: 0, Kind: KindResponse, Method: "", Payload: []byte(`{"result":"hello"}`)},
|
|
{ID: 4294967295, Kind: KindPush, Method: "tx", Payload: []byte("[]")},
|
|
{ID: 7, Kind: KindRequest, Method: "findAll", Payload: nil},
|
|
}
|
|
for _, c := range cases {
|
|
got, err := Decode(Encode(c))
|
|
if err != nil {
|
|
t.Fatalf("Decode(%q): %v", c.Method, err)
|
|
}
|
|
if got.ID != c.ID || got.Kind != c.Kind || got.Method != c.Method {
|
|
t.Fatalf("header mismatch: got %+v want %+v", got, c)
|
|
}
|
|
if !bytes.Equal(got.Payload, c.Payload) && !(len(got.Payload) == 0 && len(c.Payload) == 0) {
|
|
t.Fatalf("payload mismatch for %q: got %q want %q", c.Method, got.Payload, c.Payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestEnvelopeWireLayout locks the exact ZAP byte layout so the hand-ported TS
|
|
// codec in the front bundle can be verified byte-for-byte against it.
|
|
func TestEnvelopeWireLayout(t *testing.T) {
|
|
method := "loadModel"
|
|
payload := []byte(`{"last":0}`)
|
|
data := Encode(Envelope{ID: 0x01020304, Kind: KindRequest, Method: method, Payload: payload})
|
|
|
|
le := binary.LittleEndian
|
|
if string(data[0:4]) != "ZAP\x00" {
|
|
t.Fatalf("magic = %q", data[0:4])
|
|
}
|
|
if le.Uint16(data[4:6]) != 2 {
|
|
t.Fatalf("version = %d", le.Uint16(data[4:6]))
|
|
}
|
|
root := int(le.Uint32(data[8:12]))
|
|
if root != 16 {
|
|
t.Fatalf("rootOffset = %d want 16", root)
|
|
}
|
|
if int(le.Uint32(data[12:16])) != len(data) {
|
|
t.Fatalf("size = %d want %d", le.Uint32(data[12:16]), len(data))
|
|
}
|
|
// fixed section @16
|
|
if le.Uint32(data[16:20]) != 0x01020304 {
|
|
t.Fatalf("id = %x", le.Uint32(data[16:20]))
|
|
}
|
|
if data[20] != byte(KindRequest) {
|
|
t.Fatalf("kind = %d", data[20])
|
|
}
|
|
// method ptr @24: relOffset must point to fixed-section end (16+24=40 => rel 16)
|
|
mRel, mLen := le.Uint32(data[24:28]), le.Uint32(data[28:32])
|
|
if mRel != 16 || int(mLen) != len(method) {
|
|
t.Fatalf("method ptr rel=%d len=%d want 16,%d", mRel, mLen, len(method))
|
|
}
|
|
mAbs := 24 + int(mRel)
|
|
if string(data[mAbs:mAbs+int(mLen)]) != method {
|
|
t.Fatalf("method bytes = %q", data[mAbs:mAbs+int(mLen)])
|
|
}
|
|
// payload ptr @32: relOffset = 8 + len(method)
|
|
pRel, pLen := le.Uint32(data[32:36]), le.Uint32(data[36:40])
|
|
if int(pRel) != 8+len(method) || int(pLen) != len(payload) {
|
|
t.Fatalf("payload ptr rel=%d len=%d want %d,%d", pRel, pLen, 8+len(method), len(payload))
|
|
}
|
|
pAbs := 32 + int(pRel)
|
|
if !bytes.Equal(data[pAbs:pAbs+int(pLen)], payload) {
|
|
t.Fatalf("payload bytes = %q", data[pAbs:pAbs+int(pLen)])
|
|
}
|
|
}
|