Skip to content

Commit

Permalink
added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddy J committed Nov 20, 2023
1 parent 2fe072b commit 295c83b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ require (
github.com/goccy/go-yaml v1.9.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
Expand All @@ -53,6 +55,7 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand All @@ -73,6 +76,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
73 changes: 37 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"os"

"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// this needs to be hand crafted. If you want the root of the header at the slot x,
// then look for entry in (x)%SLOTS_PER_HISTORICAL_ROOT in the block_roots.

func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

// Defining flags for all the parameters
command := flag.String("command", "", "The command to execute")

Expand All @@ -38,7 +41,7 @@ func main() {

// Check if the required 'command' flag is provided
if *command == "" {
fmt.Println("Error: command flag is required")
log.Debug().Msg("Error: command flag is required")
return
}

Expand All @@ -54,7 +57,7 @@ func main() {
GenerateBalanceUpdateProof(*oracleBlockHeaderFile, *stateFile, *validatorIndex, *chainID, *outputFile)

default:
fmt.Println("Unknown command:", *command)
log.Debug().Str("Unknown command:", *command)
}
}

Expand All @@ -64,30 +67,31 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string
var oracleBeaconBlockHeader phase0.BeaconBlockHeader
stateJSON, err := parseStateJSONFile(stateFile)
if err != nil {
fmt.Println("error with JSON parsing")
log.Debug().Msg("error with JSON parsing")
}
ParseCapellaBeaconStateFromJSON(*stateJSON, &state)

oracleBeaconBlockHeader, err = ExtractBlockHeader(oracleBlockHeaderFile)
if err != nil {
fmt.Println("read error with header file")
log.Debug().AnErr("Error with parsing header file", err)

}

beaconStateRoot, err := state.HashTreeRoot()

if err != nil {
fmt.Println("Error with HashTreeRoot of state", err)
log.Debug().AnErr("Error with HashTreeRoot of state", err)
}

epp, err := NewEigenPodProofs(chainID, 1000)
if err != nil {
fmt.Println("Error creating EPP object", err)
log.Debug().AnErr("Error creating EPP object", err)

}

stateRootProof, validatorFieldsProof, err := epp.ProveValidatorFields(&oracleBeaconBlockHeader, &state, uint64(validatorIndex))
if err != nil {
fmt.Println("Error with ProveValidatorFields", err)
log.Debug().AnErr("Error with ProveValidatorFields", err)
}

proofs := WithdrawalCredentialProofs{
Expand All @@ -100,7 +104,7 @@ func GenerateValidatorFieldsProof(oracleBlockHeaderFile string, stateFile string

proofData, err := json.Marshal(proofs)
if err != nil {
fmt.Println("error")
log.Debug().AnErr("JSON marshal error: ", err)
}

_ = os.WriteFile(output, proofData, 0644)
Expand All @@ -113,39 +117,39 @@ func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string,
var oracleBeaconBlockHeader phase0.BeaconBlockHeader
stateJSON, err := parseStateJSONFile(stateFile)
if err != nil {
fmt.Println("error with JSON parsing")
log.Debug().AnErr("error with JSON parsing", err)
}
ParseCapellaBeaconStateFromJSON(*stateJSON, &state)

oracleBeaconBlockHeader, err = ExtractBlockHeader(oracleBlockHeaderFile)
if err != nil {
fmt.Println("read error with header file")
log.Debug().AnErr("Error with parsing header file", err)
}

beaconStateRoot, err := state.HashTreeRoot()

if err != nil {
fmt.Println("Error with HashTreeRoot of state", err)
log.Debug().AnErr("Error with HashTreeRoot of state", err)
}

epp, err := NewEigenPodProofs(chainID, 1000)
if err != nil {
fmt.Println("Error creating EPP object", err)
log.Debug().AnErr("Error creating EPP object", err)
}

balanceRootList, err := GetBalanceRoots(state.Balances)
if err != nil {
fmt.Println("Error with GetBalanceRoots", err)
log.Debug().AnErr("Error with GetBalanceRoots", err)
}
balanceRoot := balanceRootList[validatorIndex/4]
balanceProof, err := epp.ProveValidatorBalance(&oracleBeaconBlockHeader, &state, uint64(validatorIndex))
if err != nil {
fmt.Println("Error with ProveValidatorBalance", err)
log.Debug().AnErr("Error with ProveValidatorBalance", err)
}

stateRootProof, validatorFieldsProof, err := epp.ProveValidatorFields(&oracleBeaconBlockHeader, &state, uint64(validatorIndex))
if err != nil {
fmt.Println("Error with ProveValidatorFields", err)
log.Debug().AnErr("Error with ProveValidatorFields", err)
}
proofs := BalanceUpdateProofs{
ValidatorIndex: uint64(validatorIndex),
Expand All @@ -159,7 +163,7 @@ func GenerateBalanceUpdateProof(oracleBlockHeaderFile string, stateFile string,

proofData, err := json.Marshal(proofs)
if err != nil {
fmt.Println("this error")
log.Debug().AnErr("JSON marshal error: ", err)
}

_ = os.WriteFile(output, proofData, 0644)
Expand Down Expand Up @@ -190,29 +194,29 @@ func GenerateWithdrawalFieldsProof(

oracleBeaconBlockHeader, err := ExtractBlockHeader(oracleBlockHeaderFile)
if err != nil {
fmt.Println("read error with header file")
log.Debug().AnErr("Error with parsing header file", err)
}

stateJSON, err := parseStateJSONFile(stateFile)
if err != nil {
fmt.Println("error with JSON parsing state file")
log.Debug().AnErr("error with JSON parsing state file", err)
}
ParseCapellaBeaconStateFromJSON(*stateJSON, &state)

historicalSummaryJSON, err := parseStateJSONFile(historicalSummaryStateFile)
if err != nil {
fmt.Println("error with JSON parsing historical summary state file")
log.Debug().AnErr("error with JSON parsing historical summary state file", err)
}
ParseCapellaBeaconStateFromJSON(*historicalSummaryJSON, &historicalSummaryState)

withdrawalBlockHeader, err = ExtractBlockHeader(blockHeaderFile)
if err != nil {
fmt.Println("read error with header file")
log.Debug().AnErr("Error with parsing header file", err)
}

withdrawalBlock, err = ExtractBlock(blockBodyFile)
if err != nil {
fmt.Println("read error with body file")
log.Debug().AnErr("Error with parsing body file", err)
}

hh := ssz.NewHasher()
Expand All @@ -222,48 +226,45 @@ func GenerateWithdrawalFieldsProof(
// validatorIndex := phase0.ValidatorIndex(index)
beaconStateRoot, err := state.HashTreeRoot()
if err != nil {
fmt.Println("Error with HashTreeRoot of state", err)
log.Debug().AnErr("Error with HashTreeRoot of state", err)
}

slot := withdrawalBlockHeader.Slot
hh.PutUint64(uint64(slot))
slotRoot := ConvertTo32ByteArray(hh.Hash())

if err != nil {
fmt.Println("Error with HashTreeRoot of latestBlockHeader", err)
}

timestamp := withdrawalBlock.Body.ExecutionPayload.Timestamp
hh.PutUint64(uint64(timestamp))
timestampRoot := ConvertTo32ByteArray(hh.Hash())

blockHeaderRoot, err := withdrawalBlockHeader.HashTreeRoot()
if err != nil {
fmt.Println("Error with HashTreeRoot of latestBlockHeader", err)
log.Debug().AnErr("Error with HashTreeRoot of blockHeader", err)
}
executionPayloadRoot, err := withdrawalBlock.Body.ExecutionPayload.HashTreeRoot()
if err != nil {
fmt.Println("Error with HashTreeRoot of executionPayload", err)
log.Debug().AnErr("Error with HashTreeRoot of executionPayload", err)
}

epp, err := NewEigenPodProofs(chainID, 1000)
if err != nil {
fmt.Println("Error creating EPP object", err)
log.Debug().AnErr("Error creating EPP object", err)
}
oracleBeaconStateTopLevelRoots, err := epp.ComputeBeaconStateTopLevelRoots(&state)
//blockHeaderProof, slotProof, withdrawalProof, validatorProof, timestampProof, executionPayloadProof, stateRootAgainstLatestBlockHeaderProof, historicalSummaryProof, err :=
// withdrawalProof, stateRootProof, validatorProof, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &oracleState, historicalSummaryState.BlockRoots, &withdrawalBlock, validatorIndex)
if err != nil {
log.Debug().AnErr("Error with ComputeBeaconStateTopLevelRoots", err)
}
withdrawalProof, err := epp.ProveWithdrawal(&oracleBeaconBlockHeader, &state, oracleBeaconStateTopLevelRoots, historicalSummaryState.BlockRoots, &withdrawalBlock, uint64(validatorIndex), FIRST_CAPELLA_SLOT_GOERLI)
if err != nil {
fmt.Println("ProveWithdrawal error", err)
log.Debug().AnErr("Error with ProveWithdrawal", err)
}
stateRootProof, err := ProveStateRootAgainstBlockHeader(&oracleBeaconBlockHeader)
if err != nil {
fmt.Println("ProveStateRootAgainstBlockHeader error", err)
log.Debug().AnErr("Error with ProveStateRootAgainstBlockHeader", err)
}
validatorProof, err := epp.ProveValidatorAgainstBeaconState(&state, oracleBeaconStateTopLevelRoots, uint64(validatorIndex))
if err != nil {
fmt.Println("ProveValidatorAgainstBeaconState error", err)
log.Debug().AnErr("Error with ProveValidatorAgainstBeaconState", err)
}
proofs := WithdrawalProofs{
StateRootAgainstLatestBlockHeaderProof: ConvertBytesToStrings(stateRootProof),
Expand All @@ -287,7 +288,7 @@ func GenerateWithdrawalFieldsProof(

proofData, err := json.Marshal(proofs)
if err != nil {
fmt.Println("error")
log.Debug().AnErr("JSON marshal error: ", err)
}

_ = os.WriteFile(outputFile, proofData, 0644)
Expand Down

0 comments on commit 295c83b

Please sign in to comment.