gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/dependencies.go (about)

     1  package skymodules
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net"
     6  	"os"
     7  	"time"
     8  
     9  	"gitlab.com/NebulousLabs/fastrand"
    10  	"go.sia.tech/siad/modules"
    11  	"go.sia.tech/siad/persist"
    12  	"go.sia.tech/siad/types"
    13  )
    14  
    15  var (
    16  	// skynetAddress is an address owned by Skynet Labs
    17  	skynetAddress = [32]byte{83, 122, 255, 169, 118, 147, 164, 142, 35, 145, 142, 89, 206, 40, 18, 67, 242, 215, 72, 187, 139, 253, 173, 164, 149, 53, 197, 250, 173, 62, 250, 194}
    18  
    19  	// SkydProdDependencies act as a global instance of the skynet dependencies
    20  	// to avoid having to instantiate new dependencies every time we want to
    21  	// pass skynet dependencies.
    22  	SkydProdDependencies = new(SkynetDependencies)
    23  )
    24  
    25  type (
    26  	// SkydDependencies defines dependencies used by all of Skyd's modules.
    27  	// Custom dependencies can be created to inject certain behavior during
    28  	// testing.
    29  	SkydDependencies interface {
    30  		modules.Dependencies
    31  
    32  		// SkynetAddress returns an address to be used to send SC to Skynet Labs
    33  		SkynetAddress() types.UnlockHash
    34  	}
    35  
    36  	// SkynetDependencies are the dependencies used in a Release or Debug
    37  	// production build.
    38  	SkynetDependencies struct {
    39  	}
    40  )
    41  
    42  // Satisfy the Skynet specific interface
    43  
    44  // SkynetAddress returns an address owned by Skynet Labs
    45  func (*SkynetDependencies) SkynetAddress() types.UnlockHash {
    46  	return skynetAddress
    47  }
    48  
    49  // Satisfy the modules.Dependencies interface
    50  
    51  // AtLeastOne will return a value that is equal to 1 if debugging is disabled.
    52  // If debugging is enabled, a higher value may be returned.
    53  func (*SkynetDependencies) AtLeastOne() uint64 {
    54  	return modules.ProdDependencies.AtLeastOne()
    55  }
    56  
    57  // CreateFile gives the host the ability to create files on the operating
    58  // system.
    59  func (sd *SkynetDependencies) CreateFile(s string) (modules.File, error) {
    60  	return modules.ProdDependencies.CreateFile(s)
    61  }
    62  
    63  // Destruct checks that all resources have been cleaned up correctly.
    64  func (sd *SkynetDependencies) Destruct() {
    65  	modules.ProdDependencies.Destruct()
    66  }
    67  
    68  // DialTimeout creates a tcp connection to a certain address with the specified
    69  // timeout.
    70  func (*SkynetDependencies) DialTimeout(addr modules.NetAddress, timeout time.Duration) (net.Conn, error) {
    71  	return net.DialTimeout("tcp", string(addr), timeout)
    72  }
    73  
    74  // Disrupt can be used to inject specific behavior into a module by overwriting
    75  // it using a custom dependency.
    76  func (*SkynetDependencies) Disrupt(string) bool {
    77  	return false
    78  }
    79  
    80  // Listen gives the host the ability to receive incoming connections.
    81  func (*SkynetDependencies) Listen(s1, s2 string) (net.Listener, error) {
    82  	return net.Listen(s1, s2)
    83  }
    84  
    85  // LoadFile loads JSON encoded data from a file.
    86  func (*SkynetDependencies) LoadFile(meta persist.Metadata, data interface{}, filename string) error {
    87  	return persist.LoadJSON(meta, data, filename)
    88  }
    89  
    90  // LookupIP resolves a hostname to a number of IP addresses. If an IP address
    91  // is provided as an argument it will just return that IP.
    92  func (*SkynetDependencies) LookupIP(host string) ([]net.IP, error) {
    93  	return (modules.ProductionResolver{}).LookupIP(host)
    94  }
    95  
    96  // SaveFileSync writes JSON encoded data to a file and syncs the file to disk
    97  // afterwards.
    98  func (*SkynetDependencies) SaveFileSync(meta persist.Metadata, data interface{}, filename string) error {
    99  	return persist.SaveJSON(meta, data, filename)
   100  }
   101  
   102  // MkdirAll gives the host the ability to create chains of folders within the
   103  // filesystem.
   104  func (*SkynetDependencies) MkdirAll(s string, fm os.FileMode) error {
   105  	return os.MkdirAll(s, fm)
   106  }
   107  
   108  // NewLogger creates a logger that the host can use to log messages and write
   109  // critical statements.
   110  func (*SkynetDependencies) NewLogger(s string) (*persist.Logger, error) {
   111  	return persist.NewFileLogger(s)
   112  }
   113  
   114  // OpenDatabase creates a database that the host can use to interact with large
   115  // volumes of persistent data.
   116  func (*SkynetDependencies) OpenDatabase(m persist.Metadata, s string) (*persist.BoltDatabase, error) {
   117  	return persist.OpenDatabase(m, s)
   118  }
   119  
   120  // Open opens a file readonly.
   121  func (sd *SkynetDependencies) Open(s string) (modules.File, error) {
   122  	return sd.OpenFile(s, os.O_RDONLY, 0)
   123  }
   124  
   125  // OpenFile opens a file with the specified mode and permissions.
   126  func (sd *SkynetDependencies) OpenFile(s string, i int, fm os.FileMode) (modules.File, error) {
   127  	return modules.ProdDependencies.OpenFile(s, i, fm)
   128  }
   129  
   130  // RandRead fills the input bytes with random data.
   131  func (*SkynetDependencies) RandRead(b []byte) (int, error) {
   132  	return fastrand.Reader.Read(b)
   133  }
   134  
   135  // ReadFile reads a file from the filesystem.
   136  func (*SkynetDependencies) ReadFile(s string) ([]byte, error) {
   137  	return ioutil.ReadFile(s)
   138  }
   139  
   140  // RemoveFile will remove a file from disk.
   141  func (sd *SkynetDependencies) RemoveFile(s string) error {
   142  	return modules.ProdDependencies.RemoveFile(s)
   143  }
   144  
   145  // RenameFile renames a file on disk.
   146  func (sd *SkynetDependencies) RenameFile(s1 string, s2 string) error {
   147  	return modules.ProdDependencies.RenameFile(s1, s2)
   148  }
   149  
   150  // Sleep blocks the calling thread for a certain duration.
   151  func (*SkynetDependencies) Sleep(d time.Duration) {
   152  	time.Sleep(d)
   153  }
   154  
   155  // Symlink creates a symlink between a source and a destination file.
   156  func (*SkynetDependencies) Symlink(s1, s2 string) error {
   157  	return os.Symlink(s1, s2)
   158  }
   159  
   160  // WriteFile writes a file to the filesystem.
   161  func (*SkynetDependencies) WriteFile(s string, b []byte, fm os.FileMode) error {
   162  	return ioutil.WriteFile(s, b, fm)
   163  }
   164  
   165  // Resolver returns the ProductionResolver.
   166  func (*SkynetDependencies) Resolver() modules.Resolver {
   167  	return modules.ProductionResolver{}
   168  }