Skip to content

Commit

Permalink
Restore simulator recordings [#3593]
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Apr 18, 2024
1 parent 3a6fe26 commit daec34b
Show file tree
Hide file tree
Showing 24 changed files with 3,542 additions and 270 deletions.
14 changes: 6 additions & 8 deletions pkg/url/txid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"sync/atomic"
)

// TxID is a transaction identifier.
Expand All @@ -18,7 +19,7 @@ type TxID struct {
hash [32]byte

memoize struct {
str string
str atomic.Pointer[string]
}
}

Expand Down Expand Up @@ -98,12 +99,9 @@ func (x *TxID) Compare(y *TxID) int {
// String reassembles the transaction ID into a valid URL string. See
// net/url.URL.String().
func (x *TxID) String() string {
if x.memoize.str != "" {
return x.memoize.str
}

x.memoize.str = x.url.format(x.hash[:], true)
return x.memoize.str
return getOrMakeAtomic(&x.memoize.str, func() string {
return x.url.format(x.hash[:], true)
})
}

// RawString reassembles the URL into a valid URL string without encoding any
Expand Down Expand Up @@ -135,6 +133,6 @@ func (x *TxID) UnmarshalJSON(data []byte) error {
return err
}

*x = *v
*x = *v //nolint:govet
return nil
}
10 changes: 8 additions & 2 deletions test/harness/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -43,6 +44,7 @@ func NewSim(tb testing.TB, opts ...simulator.Option) *Sim {

func Recordings(tb testing.TB) simulator.RecordingFunc {
onFail := os.Getenv("RECORD_FAILURE")
always := strings.EqualFold(os.Getenv("RECORD_ALWAYS"), "yes")
if onFail == "" {
return nil
}
Expand All @@ -60,12 +62,16 @@ func Recordings(tb testing.TB) simulator.RecordingFunc {
return nil, err
}
tb.Cleanup(func() {
if !tb.Failed() {
if !always && !tb.Failed() {
assert.NoError(tb, f.Close())
return
}
if !didAnnounce {
tb.Logf("Failure recordings for %s are prefixed with %x", tb.Name(), prefix)
msg := "Failure recordings"
if !tb.Failed() {
msg = "Recordings"
}
tb.Logf("%s for %s are prefixed with %x", msg, tb.Name(), prefix)
didAnnounce = true
}
dst := filepath.Join(onFail, fmt.Sprintf("%x-%s-%d.record", prefix, partition, node))
Expand Down
4 changes: 2 additions & 2 deletions test/simulator/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (p *Partition) Submit(envelope *messaging.Envelope, pretend bool) ([]*proto
return st, nil
}

var resp consensus.Capture[*consensus.SubmissionResponse]
err = p.sim.hub.With(&resp).Send(&consensus.Submission{
var resp consensus.Capture[*consensus.EnvelopeSubmitted]
err = p.sim.hub.With(&resp).Send(&consensus.SubmitEnvelope{
Network: p.ID,
Envelope: envelope,
Pretend: pretend,
Expand Down
12 changes: 12 additions & 0 deletions test/simulator/consensus/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ type ExecutorApp struct {
Executor execute.Executor
Restore RestoreFunc
EventBus *events.Bus
Record Recorder
}

type RestoreFunc func(ioutil.SectionReader) error

func (a *ExecutorApp) SetRecorder(rec Recorder) {
a.Record = rec
}

func (a *ExecutorApp) Info(*InfoRequest) (*InfoResponse, error) {
last, hash, err := a.Executor.LastBlock()
if err != nil {
Expand Down Expand Up @@ -191,6 +196,13 @@ func (a *ExecutorApp) Commit(req *CommitRequest) (*CommitResponse, error) {
return nil, errors.UnknownError.Wrap(err)
}

if a.Record != nil {
err = a.Record.DidCommitBlock(s)
if err != nil {
return nil, errors.UnknownError.Wrap(err)
}
}

return &CommitResponse{
Hash: hash,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion test/simulator/consensus/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (d *Dispatcher) Submit(ctx context.Context, dest *url.URL, envelope *messag

d.mu.Lock()
defer d.mu.Unlock()
d.queue = append(d.queue, &Submission{
d.queue = append(d.queue, &SubmitEnvelope{
Network: partition,
Envelope: envelope,
})
Expand Down
22 changes: 22 additions & 0 deletions test/simulator/consensus/enums.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
messageType:
StartBlock:
value: 0x10
ProposeLeader:
value: 0x11
ProposeBlock:
value: 0x12
AcceptBlockProposal:
value: 0x13
FinalizedBlock:
value: 0x14
CommittedBlock:
value: 0x15
ExecutedBlock:
value: 0x16

SubmitEnvelope:
value: 0x20
AcceptedSubmission:
value: 0x21
EnvelopeSubmitted:
value: 0x22
134 changes: 134 additions & 0 deletions test/simulator/consensus/enums_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package consensus

// GENERATED BY go run ./tools/cmd/gen-enum. DO NOT EDIT.

import (
"encoding/json"
"fmt"
"strings"
)

// messageTypeStartBlock .
const messageTypeStartBlock messageType = 16

// messageTypeProposeLeader .
const messageTypeProposeLeader messageType = 17

// messageTypeProposeBlock .
const messageTypeProposeBlock messageType = 18

// messageTypeAcceptBlockProposal .
const messageTypeAcceptBlockProposal messageType = 19

// messageTypeFinalizedBlock .
const messageTypeFinalizedBlock messageType = 20

// messageTypeCommittedBlock .
const messageTypeCommittedBlock messageType = 21

// messageTypeExecutedBlock .
const messageTypeExecutedBlock messageType = 22

// messageTypeSubmitEnvelope .
const messageTypeSubmitEnvelope messageType = 32

// messageTypeAcceptedSubmission .
const messageTypeAcceptedSubmission messageType = 33

// messageTypeEnvelopeSubmitted .
const messageTypeEnvelopeSubmitted messageType = 34

// GetEnumValue returns the value of the message Type
func (v messageType) GetEnumValue() uint64 { return uint64(v) }

// SetEnumValue sets the value. SetEnumValue returns false if the value is invalid.
func (v *messageType) SetEnumValue(id uint64) bool {
u := messageType(id)
switch u {
case messageTypeStartBlock, messageTypeProposeLeader, messageTypeProposeBlock, messageTypeAcceptBlockProposal, messageTypeFinalizedBlock, messageTypeCommittedBlock, messageTypeExecutedBlock, messageTypeSubmitEnvelope, messageTypeAcceptedSubmission, messageTypeEnvelopeSubmitted:
*v = u
return true
}
return false
}

// String returns the name of the message Type.
func (v messageType) String() string {
switch v {
case messageTypeStartBlock:
return "startBlock"
case messageTypeProposeLeader:
return "proposeLeader"
case messageTypeProposeBlock:
return "proposeBlock"
case messageTypeAcceptBlockProposal:
return "acceptBlockProposal"
case messageTypeFinalizedBlock:
return "finalizedBlock"
case messageTypeCommittedBlock:
return "committedBlock"
case messageTypeExecutedBlock:
return "executedBlock"
case messageTypeSubmitEnvelope:
return "submitEnvelope"
case messageTypeAcceptedSubmission:
return "acceptedSubmission"
case messageTypeEnvelopeSubmitted:
return "envelopeSubmitted"
}
return fmt.Sprintf("messageType:%d", v)
}

// messageTypeByName returns the named message Type.
func messageTypeByName(name string) (messageType, bool) {
switch strings.ToLower(name) {
case "startblock":
return messageTypeStartBlock, true
case "proposeleader":
return messageTypeProposeLeader, true
case "proposeblock":
return messageTypeProposeBlock, true
case "acceptblockproposal":
return messageTypeAcceptBlockProposal, true
case "finalizedblock":
return messageTypeFinalizedBlock, true
case "committedblock":
return messageTypeCommittedBlock, true
case "executedblock":
return messageTypeExecutedBlock, true
case "submitenvelope":
return messageTypeSubmitEnvelope, true
case "acceptedsubmission":
return messageTypeAcceptedSubmission, true
case "envelopesubmitted":
return messageTypeEnvelopeSubmitted, true
}
return 0, false
}

// MarshalJSON marshals the message Type to JSON as a string.
func (v messageType) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}

// UnmarshalJSON unmarshals the message Type from JSON as a string.
func (v *messageType) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}

var ok bool
*v, ok = messageTypeByName(s)
if !ok || strings.ContainsRune(v.String(), ':') {
return fmt.Errorf("invalid message Type %q", s)
}
return nil
}
51 changes: 51 additions & 0 deletions test/simulator/consensus/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package consensus

import "gitlab.com/accumulatenetwork/accumulate/pkg/types/encoding"

//go:generate go run gitlab.com/accumulatenetwork/accumulate/tools/cmd/gen-enum --package consensus --out enums_gen.go enums.yml
//go:generate go run gitlab.com/accumulatenetwork/accumulate/tools/cmd/gen-types --package consensus messages.yml
//go:generate go run gitlab.com/accumulatenetwork/accumulate/tools/cmd/gen-types --package consensus --language go-union --out unions_gen.go messages.yml

type messageType int

// A Message is a message sent between modules.
type Message interface {
encoding.UnionValue
isMsg()
Type() messageType
}

// A NetworkMessage is a message that is specific to a network.
type NetworkMessage interface {
Message
PartitionID() string
}

// nodeMessages are passed between nodes.
type NodeMessage interface {
NetworkMessage
SenderID() [32]byte
}

func (*baseNodeMessage) isMsg() {}
func (*SubmitEnvelope) isMsg() {}
func (*EnvelopeSubmitted) isMsg() {}
func (*StartBlock) isMsg() {}
func (*ExecutedBlock) isMsg() {}

func (m *baseNodeMessage) PartitionID() string { return m.Network }
func (s *SubmitEnvelope) PartitionID() string { return s.Network }

func (m *baseNodeMessage) SenderID() [32]byte { return m.PubKeyHash }

var _ NodeMessage = (*proposeLeader)(nil)
var _ NodeMessage = (*proposeBlock)(nil)
var _ NodeMessage = (*acceptBlockProposal)(nil)
var _ NodeMessage = (*finalizedBlock)(nil)
var _ NodeMessage = (*committedBlock)(nil)
var _ NetworkMessage = (*SubmitEnvelope)(nil)
Loading

0 comments on commit daec34b

Please sign in to comment.