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.

58 lines
1.1 KiB

  1. package node
  2. import (
  3. "crypto/ecdsa"
  4. "github.com/arnaucube/slowlorisdb/core"
  5. "github.com/arnaucube/slowlorisdb/db"
  6. )
  7. type Node struct {
  8. PrivK *ecdsa.PrivateKey
  9. Addr core.Address
  10. Bc *core.Blockchain
  11. PendingTxs []core.Tx
  12. }
  13. func NewNode(db *db.Db, dif uint64) (*Node, error) {
  14. privK, err := core.NewKey()
  15. if err != nil {
  16. return nil, err
  17. }
  18. addr := core.AddressFromPrivK(privK)
  19. bc := core.NewBlockchain(db, dif)
  20. node := &Node{
  21. PrivK: privK,
  22. Addr: addr,
  23. Bc: bc,
  24. }
  25. return node, nil
  26. }
  27. func (node *Node) Sign(m []byte) (*core.Signature, error) {
  28. return core.Sign(node.PrivK, m)
  29. }
  30. func (node *Node) SignBlock(block *core.Block) (*core.Signature, error) {
  31. return core.Sign(node.PrivK, block.Hash[:])
  32. }
  33. func (node *Node) AddToPendingTxs(tx core.Tx) {
  34. node.PendingTxs = append(node.PendingTxs, tx)
  35. }
  36. func (node *Node) BlockFromPendingTxs() (*core.Block, error) {
  37. block := node.Bc.NewBlock(node.PendingTxs)
  38. err := block.CalculatePoW(node.Bc.Difficulty)
  39. if err != nil {
  40. return nil, err
  41. }
  42. sig, err := node.SignBlock(block)
  43. if err != nil {
  44. return nil, err
  45. }
  46. block.Signature = sig.Bytes()
  47. return block, nil
  48. }