Skip to content

Commit

Permalink
feat: optimize miner target state CBOR marshaling (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney authored Jul 22, 2024
1 parent 61091f0 commit 73d8ca3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
29 changes: 18 additions & 11 deletions internal/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Miner struct {
}

type TargetState interface {
ToBytes() ([]byte, error)
MarshalCBOR() ([]byte, error)
SetNonce([16]byte)
GetNonce() [16]byte
}
Expand Down Expand Up @@ -81,10 +81,6 @@ func (state *TargetStateV1) MarshalCBOR() ([]byte, error) {
return cbor.Encode(&tmp)
}

func (state *TargetStateV1) ToBytes() ([]byte, error) {
return cbor.Encode(state)
}

type TargetStateV2 struct {
Nonce [16]byte
MinerCredHash []byte
Expand All @@ -93,6 +89,7 @@ type TargetStateV2 struct {
CurrentHash []byte
LeadingZeros int64
DifficultyNumber int64
cachedCbor []byte
}

func (t *TargetStateV2) SetNonce(nonce [16]byte) {
Expand All @@ -104,6 +101,14 @@ func (t *TargetStateV2) GetNonce() [16]byte {
}

func (state *TargetStateV2) MarshalCBOR() ([]byte, error) {
// Use cached CBOR to generate new CBOR more quickly
if state.cachedCbor != nil {
// Replace nonce value in cached CBOR with current nonce
for i := 0; i < 16; i++ {
state.cachedCbor[4+i] = state.Nonce[i]
}
return state.cachedCbor, nil
}
tmp := cbor.NewConstructor(
0,
cbor.IndefLengthList{
Expand All @@ -116,11 +121,13 @@ func (state *TargetStateV2) MarshalCBOR() ([]byte, error) {
state.DifficultyNumber,
},
)
return cbor.Encode(&tmp)
}

func (state *TargetStateV2) ToBytes() ([]byte, error) {
return cbor.Encode(state)
cborData, err := cbor.Encode(&tmp)
if err != nil {
return nil, err
}
state.cachedCbor = make([]byte, len(cborData))
copy(state.cachedCbor, cborData)
return cborData, nil
}

type DifficultyMetrics struct {
Expand Down Expand Up @@ -347,7 +354,7 @@ func (m *Miner) calculateHash() []byte {
default:
break
}
stateBytes, err := m.state.ToBytes()
stateBytes, err := m.state.MarshalCBOR()
if err != nil {
logging.GetLogger().Error(err)
return nil
Expand Down
36 changes: 36 additions & 0 deletions internal/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,39 @@ func BenchmarkSha256Simd(b *testing.B) {
hasher.Sum(nil)
}
}

// BenchmarkTargetStateCborMarshal tests marshaling TargetStateV2 to CBOR
func BenchmarkTargetStateCborMarshal(b *testing.B) {
tmpHash := [32]byte{}
tmpState := TargetStateV2{
Nonce: randomNonce(),
MinerCredHash: tmpHash[:],
EpochTime: 999999,
BlockNumber: 999999,
CurrentHash: tmpHash[:],
LeadingZeros: 4,
DifficultyNumber: 65535,
}
for i := 0; i < b.N; i++ {
_, _ = tmpState.MarshalCBOR()
}
}

// BenchmarkTargetStateCborMarshalNoCache tests marshaling TargetStateV2 to CBOR without caching
func BenchmarkTargetStateCborMarshalNoCache(b *testing.B) {
tmpHash := [32]byte{}
tmpState := TargetStateV2{
Nonce: randomNonce(),
MinerCredHash: tmpHash[:],
EpochTime: 999999,
BlockNumber: 999999,
CurrentHash: tmpHash[:],
LeadingZeros: 4,
DifficultyNumber: 65535,
}
for i := 0; i < b.N; i++ {
_, _ = tmpState.MarshalCBOR()
// Wipe out CBOR cache
tmpState.cachedCbor = nil
}
}

0 comments on commit 73d8ca3

Please sign in to comment.