solution.go raw

   1  package sol
   2  
   3  import (
   4  	"bytes"
   5  	
   6  	"github.com/niubaoshu/gotiny"
   7  	
   8  	"github.com/p9c/p9/pkg/wire"
   9  )
  10  
  11  // Magic is the marker for packets containing a solution
  12  var Magic = []byte{'s', 'o', 'l', 1}
  13  
  14  type Solution struct {
  15  	Nonce uint64
  16  	UUID  uint64
  17  	// *wire.Block
  18  	Bytes []byte
  19  }
  20  
  21  // Encode a message for a solution
  22  func Encode(nonce uint64, uuid uint64, mb *wire.BlockHeader) []byte {
  23  	var buf []byte
  24  	wr := bytes.NewBuffer(buf)
  25  	var e error
  26  	if e = mb.Serialize(wr); E.Chk(e) {
  27  	}
  28  	s := Solution{Nonce: nonce, UUID: uuid, Bytes: wr.Bytes()} // Block: mb}
  29  	return gotiny.Marshal(&s)
  30  }
  31  
  32  // Decode an encoded solution message to a wire.BlockHeader
  33  func (s *Solution) Decode() (mb *wire.BlockHeader, e error) {
  34  	buf := bytes.NewBuffer(s.Bytes)
  35  	mb = &wire.BlockHeader{}
  36  	if e = mb.Deserialize(buf); E.Chk(e) {
  37  	}
  38  	return
  39  }
  40  
  41  //
  42  // type Container struct {
  43  // 	simplebuffer.Container
  44  // }
  45  //
  46  // func GetSolContainer(port uint32, b *wire.Block) *Container {
  47  // 	mB := Block.New().Put(b)
  48  // 	srs := simplebuffer.Serializers{Int32.New().Put(int32(port)), mB}.CreateContainer(Magic)
  49  // 	return &Container{*srs}
  50  // }
  51  //
  52  // func LoadSolContainer(b []byte) (out *Container) {
  53  // 	out = &Container{}
  54  // 	out.Data = b
  55  // 	return
  56  // }
  57  //
  58  //
  59  // func (sC *Container) GetMsgBlock() *wire.Block {
  60  // 	// Traces(sC.Data)
  61  // 	buff := sC.Encode(1)
  62  // 	// Traces(buff)
  63  // 	decoded := Block.New().DecodeOne(buff)
  64  // 	// Traces(decoded)
  65  // 	got := decoded.Encode()
  66  // 	// Traces(got)
  67  // 	return got
  68  // }
  69  
  70  //
  71  // func (sC *Container) GetSenderPort() int32 {
  72  // 	buff := sC.Encode(0)
  73  // 	decoded := Int32.New().DecodeOne(buff)
  74  // 	got := decoded.Encode()
  75  // 	return got
  76  // }
  77