-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Rocket-Rescue-Node/jms/update-guards
Pull out consensus cache into file, add withdrawal creds to cached info
- Loading branch information
Showing
4 changed files
with
148 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package consensuslayer | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/allegro/bigcache/v3" | ||
"github.com/ethereum/go-ethereum/common" | ||
rptypes "github.com/rocket-pool/rocketpool-go/types" | ||
) | ||
|
||
const pubkeyLength = 48 | ||
const withdrawalLength = 20 | ||
|
||
type validatorCache struct { | ||
*bigcache.BigCache | ||
} | ||
|
||
// The cache value will be a byte slice of 68 length | ||
// First 48 bytes for the publick key | ||
// Last 20 bytes for the address of the 0x01 credential, or a guardian address if a BLS key. | ||
|
||
func newValidatorCache(ctx context.Context, config bigcache.Config) (*validatorCache, error) { | ||
bc, err := bigcache.New(ctx, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &validatorCache{bc}, nil | ||
} | ||
|
||
func (c *validatorCache) Get(index string) *ValidatorInfo { | ||
blob, err := c.BigCache.Get(index) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
if len(blob) != pubkeyLength+withdrawalLength { | ||
return nil | ||
} | ||
|
||
out := ValidatorInfo{} | ||
out.Pubkey = rptypes.BytesToValidatorPubkey(blob[:pubkeyLength]) | ||
out.WithdrawalAddress = common.BytesToAddress(blob[pubkeyLength:]) | ||
return &out | ||
} | ||
|
||
func (c *validatorCache) Set(index string, v *ValidatorInfo) { | ||
var blob [pubkeyLength + withdrawalLength]byte | ||
|
||
copy(blob[:], v.Pubkey[:]) | ||
copy(blob[pubkeyLength:], v.WithdrawalAddress[:]) | ||
|
||
_ = c.BigCache.Set(index, blob[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package consensuslayer | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/hex" | ||
"testing" | ||
"time" | ||
|
||
"github.com/allegro/bigcache/v3" | ||
"github.com/ethereum/go-ethereum/common" | ||
rptypes "github.com/rocket-pool/rocketpool-go/types" | ||
) | ||
|
||
func TestCacheRoundTrip(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
config := bigcache.DefaultConfig(10 * time.Hour) | ||
|
||
cache, err := newValidatorCache(ctx, config) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
expectedKey, _ := hex.DecodeString("b20fb4a9340f8b23197b8449db7a5d3d8d068570a2b61a8d78817537aac4fd5645434d3e89a918a3ba9d0b7707cbeae0") | ||
expectedAddr, _ := hex.DecodeString("6a6d731664115Ff3C823807442a4dC94999b0923") | ||
|
||
cache.Set("test", &ValidatorInfo{ | ||
Pubkey: rptypes.BytesToValidatorPubkey(expectedKey), | ||
WithdrawalAddress: common.BytesToAddress(expectedAddr), | ||
}) | ||
|
||
vInfo := cache.Get("test") | ||
if vInfo == nil { | ||
t.Fail() | ||
return | ||
} | ||
|
||
if !bytes.EqualFold(vInfo.Pubkey[:], expectedKey) { | ||
t.Fail() | ||
} | ||
|
||
if !bytes.EqualFold(vInfo.WithdrawalAddress[:], expectedAddr) { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters