github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/core/node.go (about)

     1  package core
     2  
     3  import (
     4  	"github.com/machinefi/w3bstream/pkg/core/access"
     5  	"github.com/machinefi/w3bstream/pkg/core/storage"
     6  	"github.com/machinefi/w3bstream/pkg/core/types"
     7  	"github.com/machinefi/w3bstream/pkg/core/web3"
     8  )
     9  
    10  type (
    11  	// Processor defines an interface which processes an input message with given authentication
    12  	Processor interface {
    13  		Process(access.Authentication, storage.KVStore, string, uint8, []byte) error
    14  	}
    15  	Config struct {
    16  	}
    17  
    18  	// Node defines a w3bstream node
    19  	Node struct {
    20  		ac        access.Control
    21  		client    web3.Client
    22  		rawDB     storage.KVStore
    23  		processor Processor
    24  	}
    25  )
    26  
    27  // NewNode creates a new w3bstream node
    28  func NewNode(cfg Config, ac access.Control, client web3.Client, rawDB storage.KVStore) (*Node, error) {
    29  	// TODO: create processor according to cfg, with node
    30  	return &Node{
    31  		ac:        ac,
    32  		client:    client,
    33  		rawDB:     rawDB,
    34  		processor: nil,
    35  	}, nil
    36  }
    37  
    38  // Put puts message into db after processing
    39  //
    40  //	Single node mode, without consensus module
    41  func (node *Node) Put(msg types.Message) error {
    42  	auth, err := node.ac.Check(msg.Sender, msg.Nonce, msg.Hash(), msg.Authentication)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	// TODO: replace wrap node.db into a database with access control and some instantiated interface
    47  	db := node.rawDB
    48  	if err := node.processor.Process(auth, db, msg.Sender, msg.Type, msg.Data); err != nil {
    49  		return err
    50  	}
    51  
    52  	return node.commit(msg, db)
    53  }
    54  
    55  func (node *Node) commit(msg types.Message, db storage.KVStore) error {
    56  	return nil
    57  }
    58  
    59  // AccessControl returns the instance of access control
    60  func (node *Node) AccessControl() access.Control {
    61  	return node.ac
    62  }
    63  
    64  // Web3Client returns the web3 client
    65  func (node *Node) Web3Client() web3.Client {
    66  	return node.client
    67  }