Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 50 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Computantis is not keeping track of all transactions in a single blockchain but
0. Run database `docker compose up`.
1. Create `server_settings.yaml` according to `server_settings_example.yaml` in the repo root folder.
2. Run `make run` or `go run cmd/central/main.go`.

## Stress test

Directory `./stress/` contains REST API performance tests.
For example the test on MacBook with M2 arm64 chip, 24GB RAM, mongodb run in docker container with 1CPU and 1GB RAM,
for full cycle of creating 1000 transactions took 3.75 sec.
<!-- Code generated by gomarkdoc. DO NOT EDIT -->

# block
Expand Down Expand Up @@ -230,15 +236,15 @@ var (
)
```

## type [AddressChecker](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L58-L60>)
## type [AddressChecker](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L59-L61>)

```go
type AddressChecker interface {
CheckAddressExists(ctx context.Context, address string) (bool, error)
}
```

## type [BlockFinder](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L66-L69>)
## type [BlockFinder](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L67-L70>)

```go
type BlockFinder interface {
Expand All @@ -247,7 +253,7 @@ type BlockFinder interface {
}
```

## type [BlockReadWriter](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L53-L56>)
## type [BlockReadWriter](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L54-L57>)

```go
type BlockReadWriter interface {
Expand All @@ -256,23 +262,23 @@ type BlockReadWriter interface {
}
```

## type [BlockReader](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L45-L47>)
## type [BlockReader](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L46-L48>)

```go
type BlockReader interface {
LastBlockHashIndex() ([32]byte, uint64)
}
```

## type [BlockWriter](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L49-L51>)
## type [BlockWriter](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L50-L52>)

```go
type BlockWriter interface {
WriteBlock(ctx context.Context, block block.Block) error
}
```

## type [Config](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L71-L75>)
## type [Config](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L72-L76>)

```go
type Config struct {
Expand All @@ -282,13 +288,13 @@ type Config struct {
}
```

### func \(Config\) [Validate](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L77>)
### func \(Config\) [Validate](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L78>)

```go
func (c Config) Validate() error
```

## type [Ledger](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L95-L105>)
## type [Ledger](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L96-L106>)

Ledger is a collection of ledger functionality to perform bookkeeping.

Expand All @@ -298,53 +304,53 @@ type Ledger struct {
}
```

### func [NewLedger](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L108-L116>)
### func [NewLedger](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L109-L117>)

```go
func NewLedger(config Config, bc BlockReadWriter, tx TrxWriteReadMover, ac AddressChecker, vr SignatureVerifier, tf BlockFinder, log logger.Logger) (*Ledger, error)
```

NewLedger creates new Ledger if config is valid or returns error otherwise.

### func \(\*Ledger\) [Run](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L135>)
### func \(\*Ledger\) [Run](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L136>)

```go
func (l *Ledger) Run(ctx context.Context)
```

Run runs the Ladger engine that writes blocks to the blockchain repository. Run starts a goroutine and can be stopped by cancelling the context.

### func \(\*Ledger\) [VerifySignature](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L197>)
### func \(\*Ledger\) [VerifySignature](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L201>)

```go
func (l *Ledger) VerifySignature(message, signature []byte, hash [32]byte, address string) error
```

### func \(\*Ledger\) [WriteCandidateTransaction](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L180>)
### func \(\*Ledger\) [WriteCandidateTransaction](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L184>)

```go
func (l *Ledger) WriteCandidateTransaction(ctx context.Context, trx *transaction.Transaction) error
```

WriteCandidateTransaction validates and writes a transaction to the repository. Transaction is not yet a part of the blockchain.

### func \(\*Ledger\) [WriteIssuerSignedTransactionForReceiver](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L162-L166>)
### func \(\*Ledger\) [WriteIssuerSignedTransactionForReceiver](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L166-L170>)

```go
func (l *Ledger) WriteIssuerSignedTransactionForReceiver(ctx context.Context, receiverAddr string, trx *transaction.Transaction) error
```

WriteIssuerSignedTransactionForReceiver validates issuer signature and writes a transaction to the repository for receiver.

## type [SignatureVerifier](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L62-L64>)
## type [SignatureVerifier](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L63-L65>)

```go
type SignatureVerifier interface {
Verify(message, signature []byte, hash [32]byte, address string) error
}
```

## type [TrxWriteReadMover](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L36-L43>)
## type [TrxWriteReadMover](<https://github.com/bartossh/Computantis/blob/main/bookkeeping/bookkeeping.go#L36-L44>)

```go
type TrxWriteReadMover interface {
Expand All @@ -354,6 +360,7 @@ type TrxWriteReadMover interface {
RemoveAwaitingTransaction(ctx context.Context, trxHash [32]byte) error
ReadAwaitingTransactionsByReceiver(ctx context.Context, address string) ([]transaction.Transaction, error)
ReadAwaitingTransactionsByIssuer(ctx context.Context, address string) ([]transaction.Transaction, error)
ReadTemporaryTransactions(ctx context.Context) ([]transaction.Transaction, error)
}
```

Expand Down Expand Up @@ -693,15 +700,15 @@ import "github.com/bartossh/Computantis/logging"
## Index

- [type Helper](<#type-helper>)
- [func New(callOnErr func(error), writers ...io.Writer) Helper](<#func-new>)
- [func New(callOnWriteLogErr, callOnFatal func(error), writers ...io.Writer) Helper](<#func-new>)
- [func (h Helper) Debug(msg string)](<#func-helper-debug>)
- [func (h Helper) Error(msg string)](<#func-helper-error>)
- [func (h Helper) Fatal(msg string)](<#func-helper-fatal>)
- [func (h Helper) Info(msg string)](<#func-helper-info>)
- [func (h Helper) Warn(msg string)](<#func-helper-warn>)


## type [Helper](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L15-L18>)
## type [Helper](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L16-L20>)

Helper helps with writing logs to io.Writers. Helper implements logger.Logger interface. Writing is done concurrently with out blocking the current thread.

Expand All @@ -711,47 +718,47 @@ type Helper struct {
}
```

### func [New](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L21>)
### func [New](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L23>)

```go
func New(callOnErr func(error), writers ...io.Writer) Helper
func New(callOnWriteLogErr, callOnFatal func(error), writers ...io.Writer) Helper
```

New creates new Helper.

### func \(Helper\) [Debug](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L26>)
### func \(Helper\) [Debug](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L28>)

```go
func (h Helper) Debug(msg string)
```

Debug writes debug log.

### func \(Helper\) [Error](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L59>)
### func \(Helper\) [Error](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L61>)

```go
func (h Helper) Error(msg string)
```

Error writes error log.

### func \(Helper\) [Fatal](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L70>)
### func \(Helper\) [Fatal](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L72>)

```go
func (h Helper) Fatal(msg string)
```

Fatal writes fatal log.

### func \(Helper\) [Info](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L37>)
### func \(Helper\) [Info](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L39>)

```go
func (h Helper) Info(msg string)
```

Info writes info log.

### func \(Helper\) [Warn](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L48>)
### func \(Helper\) [Warn](<https://github.com/bartossh/Computantis/blob/main/logging/logging.go#L50>)

```go
func (h Helper) Warn(msg string)
Expand Down Expand Up @@ -782,6 +789,7 @@ import "github.com/bartossh/Computantis/repo"
- [func (db DataBase) ReadAwaitingTransactionsByIssuer(ctx context.Context, address string) ([]transaction.Transaction, error)](<#func-database-readawaitingtransactionsbyissuer>)
- [func (db DataBase) ReadAwaitingTransactionsByReceiver(ctx context.Context, address string) ([]transaction.Transaction, error)](<#func-database-readawaitingtransactionsbyreceiver>)
- [func (db DataBase) ReadBlockByHash(ctx context.Context, hash [32]byte) (block.Block, error)](<#func-database-readblockbyhash>)
- [func (db DataBase) ReadTemporaryTransactions(ctx context.Context) ([]transaction.Transaction, error)](<#func-database-readtemporarytransactions>)
- [func (db DataBase) RemoveAwaitingTransaction(ctx context.Context, trxHash [32]byte) error](<#func-database-removeawaitingtransaction>)
- [func (c DataBase) RunMigration(ctx context.Context) error](<#func-database-runmigration>)
- [func (db DataBase) Write(p []byte) (n int, err error)](<#func-database-write>)
Expand Down Expand Up @@ -927,6 +935,14 @@ func (db DataBase) ReadBlockByHash(ctx context.Context, hash [32]byte) (block.Bl

ReadBlockByHash returns block with given hash.

### func \(DataBase\) [ReadTemporaryTransactions](<https://github.com/bartossh/Computantis/blob/main/repo/transaction.go#L122>)

```go
func (db DataBase) ReadTemporaryTransactions(ctx context.Context) ([]transaction.Transaction, error)
```

ReadTemporaryTransactions reads transactions from the temporary storage.

### func \(DataBase\) [RemoveAwaitingTransaction](<https://github.com/bartossh/Computantis/blob/main/repo/transaction.go#L30>)

```go
Expand Down Expand Up @@ -1381,6 +1397,16 @@ type UpgradeConnectionRequest struct {
}
```

# stress

```go
import "github.com/bartossh/Computantis/stress"
```

## Index



# transaction

```go
Expand Down
16 changes: 16 additions & 0 deletions bookkeeping/bookkeeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type TrxWriteReadMover interface {
RemoveAwaitingTransaction(ctx context.Context, trxHash [32]byte) error
ReadAwaitingTransactionsByReceiver(ctx context.Context, address string) ([]transaction.Transaction, error)
ReadAwaitingTransactionsByIssuer(ctx context.Context, address string) ([]transaction.Transaction, error)
ReadTemporaryTransactions(ctx context.Context) ([]transaction.Transaction, error)
}

type BlockReader interface {
Expand Down Expand Up @@ -133,6 +134,9 @@ func NewLedger(
// Run runs the Ladger engine that writes blocks to the blockchain repository.
// Run starts a goroutine and can be stopped by cancelling the context.
func (l *Ledger) Run(ctx context.Context) {
if err := l.forgeTemporary(ctx); err != nil {
l.log.Fatal(fmt.Sprintf("forging temporary failed: %s", err.Error()))
}
go func(ctx context.Context) {
ticker := time.NewTicker(time.Duration(l.config.BlockWriteTimestamp) * time.Second)
outer:
Expand Down Expand Up @@ -198,6 +202,18 @@ func (l *Ledger) VerifySignature(message, signature []byte, hash [32]byte, addre
return l.vr.Verify(message, signature, hash, address)
}

func (l *Ledger) forgeTemporary(ctx context.Context) error {
trxs, err := l.tx.ReadTemporaryTransactions(ctx)
if err != nil {
return err
}
for _, trx := range trxs {
l.hashes = append(l.hashes, trx.Hash)
}
l.forge(ctx)
return nil
}

func (l *Ledger) forge(ctx context.Context) {
defer l.cleanHashes()
blcHash, err := l.saveBlock(ctx)
Expand Down
6 changes: 5 additions & 1 deletion cmd/central/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func main() {
fmt.Println("error with logger: ", err)
}

log := logging.New(callbackOnErr, db)
callbackOnFatal := func(err error) {
panic(fmt.Sprintf("error with logger: %s", err))
}

log := logging.New(callbackOnErr, callbackOnFatal, db)

if err := blockchain.GenesisBlock(ctx, db); err != nil {
fmt.Println(err)
Expand Down
Loading